from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate, make_msgid
from functools import cache
import html
import os
from pathlib import Path
import smtplib
import sys
import tempfile
import pandas as pd
from bokeh.models import NumberFormatter, BooleanFormatter, HTMLTemplateFormatter
from bokeh.resources import INLINE
import gradio as gr
import pytz
import panel as pn
import seaborn as sns
from markdown import markdown
from rdkit import Chem, RDConfig
from rdkit.Chem import Crippen, Descriptors, rdMolDescriptors, Lipinski, rdmolops, Draw
import requests
from app import static
sys.path.append(str(Path(RDConfig.RDContribDir) / 'SA_Score'))
import sascorer
def lipinski(mol):
"""
Lipinski's rules:
Hydrogen bond donors <= 5
Hydrogen bond acceptors <= 10
Molecular weight <= 500 daltons
logP <= 5
"""
return (
Lipinski.NumHDonors(mol) <= 5 and
Lipinski.NumHAcceptors(mol) <= 10 and
Descriptors.MolWt(mol) <= 500 and
Crippen.MolLogP(mol) <= 5
)
def reos(mol):
"""
Rapid Elimination Of Swill filter:
Molecular weight between 200 and 500
LogP between -5.0 and +5.0
H-bond donor count between 0 and 5
H-bond acceptor count between 0 and 10
Formal charge between -2 and +2
Rotatable bond count between 0 and 8
Heavy atom count between 15 and 50
"""
return (
200 <= Descriptors.MolWt(mol) <= 500 and
-5.0 <= Crippen.MolLogP(mol) <= 5.0 and
0 <= Lipinski.NumHDonors(mol) <= 5 and
0 <= Lipinski.NumHAcceptors(mol) <= 10 and
-2 <= rdmolops.GetFormalCharge(mol) <= 2 and
0 <= rdMolDescriptors.CalcNumRotatableBonds(mol) <= 8 and
15 <= rdMolDescriptors.CalcNumHeavyAtoms(mol) <= 50
)
def ghose(mol):
"""
Ghose drug like filter:
Molecular weight between 160 and 480
LogP between -0.4 and +5.6
Atom count between 20 and 70
Molar refractivity between 40 and 130
"""
return (
160 <= Descriptors.MolWt(mol) <= 480 and
-0.4 <= Crippen.MolLogP(mol) <= 5.6 and
20 <= rdMolDescriptors.CalcNumAtoms(mol) <= 70 and
40 <= Crippen.MolMR(mol) <= 130
)
def veber(mol):
"""
The Veber filter is a rule of thumb filter for orally active drugs described in
Veber et al., J Med Chem. 2002; 45(12): 2615-23.:
Rotatable bonds <= 10
Topological polar surface area <= 140
"""
return (
rdMolDescriptors.CalcNumRotatableBonds(mol) <= 10 and
rdMolDescriptors.CalcTPSA(mol) <= 140
)
def rule_of_three(mol):
"""
Rule of Three filter (Congreve et al., Drug Discov. Today. 8 (19): 876–7, (2003).):
Molecular weight <= 300
LogP <= 3
H-bond donor <= 3
H-bond acceptor count <= 3
Rotatable bond count <= 3
"""
return (
Descriptors.MolWt(mol) <= 300 and
Crippen.MolLogP(mol) <= 3 and
Lipinski.NumHDonors(mol) <= 3 and
Lipinski.NumHAcceptors(mol) <= 3 and
rdMolDescriptors.CalcNumRotatableBonds(mol) <= 3
)
@cache
def load_smarts_patterns(smarts_path):
# Load the CSV file containing SMARTS patterns
smarts_df = pd.read_csv(Path(smarts_path))
# Convert all SMARTS patterns to molecules
smarts_mols = [Chem.MolFromSmarts(smarts) for smarts in smarts_df['smarts']]
return smarts_mols
def smarts_filter(mol, smarts_mols):
for smarts_mol in smarts_mols:
if smarts_mol is not None and mol.HasSubstructMatch(smarts_mol):
return False
return True
def pains(mol):
smarts_mols = load_smarts_patterns("data/filters/pains.csv")
return smarts_filter(mol, smarts_mols)
def mlsmr(mol):
smarts_mols = load_smarts_patterns("data/filters/mlsmr.csv")
return smarts_filter(mol, smarts_mols)
def dundee(mol):
smarts_mols = load_smarts_patterns("data/filters/dundee.csv")
return smarts_filter(mol, smarts_mols)
def glaxo(mol):
smarts_mols = load_smarts_patterns("data/filters/glaxo.csv")
return smarts_filter(mol, smarts_mols)
def bms(mol):
smarts_mols = load_smarts_patterns("data/filters/bms.csv")
return smarts_filter(mol, smarts_mols)
SCORE_MAP = {
'Synthetic Accessibility': sascorer.calculateScore,
'LogP': Crippen.MolLogP,
'Molecular Weight': Descriptors.MolWt,
'Number of Atoms': rdMolDescriptors.CalcNumAtoms,
'Number of Heavy Atoms': rdMolDescriptors.CalcNumHeavyAtoms,
'Molar Refractivity': Crippen.MolMR,
'H-Bond Donor Count': Lipinski.NumHDonors,
'H-Bond Acceptor Count': Lipinski.NumHAcceptors,
'Rotatable Bond Count': rdMolDescriptors.CalcNumRotatableBonds,
'Topological Polar Surface Area': rdMolDescriptors.CalcTPSA,
}
FILTER_MAP = {
# TODO support number_of_violations
'REOS': reos,
"Lipinski's Rule of Five": lipinski,
'Ghose': ghose,
'Rule of Three': rule_of_three,
'Veber': veber,
'PAINS': pains,
'MLSMR': mlsmr,
'Dundee': dundee,
'Glaxo': glaxo,
'BMS': bms,
}
def get_timezone_by_ip(ip):
try:
data = requests.get(f'https://worldtimeapi.org/api/ip/{ip}').json()
return data['timezone']
except Exception:
return 'UTC'
def ts_to_str(timestamp, timezone):
# Create a timezone-aware datetime object from the UNIX timestamp
dt = datetime.fromtimestamp(timestamp, pytz.utc)
# Convert the timezone-aware datetime object to the target timezone
target_timezone = pytz.timezone(timezone)
localized_dt = dt.astimezone(target_timezone)
# Format the datetime object to the specified string format
return localized_dt.strftime('%Y-%m-%d %H:%M:%S (%Z%z)')
def send_email(job_info):
if job_info.get('email'):
try:
email_info = job_info.copy()
email_serv = os.getenv('EMAIL_SERV')
email_port = os.getenv('EMAIL_PORT')
email_addr = os.getenv('EMAIL_ADDR')
email_pass = os.getenv('EMAIL_PASS')
email_form = os.getenv('EMAIL_FORM')
email_subj = os.getenv('EMAIL_SUBJ')
for key, value in email_info.items():
if key.endswith("time") and value:
email_info[key] = ts_to_str(value, get_timezone_by_ip(email_info['ip']))
server = smtplib.SMTP(email_serv, int(email_port))
# server.starttls()
server.login(email_addr, email_pass)
msg = MIMEMultipart("alternative")
msg["From"] = email_addr
msg["To"] = email_info['email']
msg["Subject"] = email_subj.format(**email_info)
msg["Date"] = formatdate(localtime=True)
msg["Message-ID"] = make_msgid()
msg.attach(MIMEText(markdown(email_form.format(**email_info)), 'html'))
msg.attach(MIMEText(email_form.format(**email_info), 'plain'))
server.sendmail(email_addr, email_info['email'], msg.as_string())
server.quit()
gr.Info('Email notification sent.')
except Exception as e:
gr.Warning('Failed to send email notification due to error: ' + str(e))
def read_molecule(path):
if path.endswith('.pdb'):
return Chem.MolFromPDBFile(path, sanitize=False, removeHs=True)
if path.endswith('.pdr'):
return open(path, 'r').read()
elif path.endswith('.mol'):
return Chem.MolFromMolFile(path, sanitize=False, removeHs=True)
elif path.endswith('.mol2'):
return Chem.MolFromMol2File(path, sanitize=False, removeHs=True)
elif path.endswith('.sdf'):
return Chem.SDMolSupplier(path, sanitize=False, removeHs=True)[0]
raise Exception('Unknown file extension')
def read_molecule_file(in_file, allowed_extentions):
if isinstance(in_file, str):
path = in_file
else:
path = in_file.name
extension = path.split('.')[-1]
if extension not in allowed_extentions:
msg = static.INVALID_FORMAT_MSG.format(extension=extension)
return None, None, msg
try:
mol = read_molecule(path)
except Exception as e:
e = str(e).replace('\'', '')
msg = static.ERROR_FORMAT_MSG.format(message=e)
return None, None, msg
if extension in 'pdb':
content = Chem.MolToPDBBlock(mol)
elif extension in ['mol', 'mol2', 'sdf']:
content = Chem.MolToMolBlock(mol, kekulize=False)
extension = 'mol'
else:
raise NotImplementedError
return content, extension, None
# def create_complex_view_html(
# complex_path, pocket_path_dict=None,
# interactive_ligands=True, interactive_pockets=True
# ):
# """Generates HTML for complex visualization."""
# model_i = -1
# viewer_models = ""
# if complex_path:
# complex_data, extension, html = read_molecule_file(complex_path, allowed_extentions=['pdb'])
# viewer_models += f'viewer.addModel(`{complex_data}`, "pdb");'
# model_i += 1
# viewer_models += f"viewer.getModel({model_i}).setStyle({{ hetflag: false }}, proteinStyle);"
# viewer_models += f"viewer.getModel({model_i}).setStyle({{ hetflag: true }}, ligandStyle);"
# if interactive_ligands:
# # return ligand residue info when the ligand is clicked
# viewer_models += f"""
# let selectedLigand = null;
# viewer.getModel({model_i}).setClickable(
# {{ hetflag: true, byres: true }},
# true,
# function (_atom, _viewer, _event, _container) {{
# let currentLigand = {{ resn: _atom.resn, chain: _atom.chain, resi: _atom.resi }};
#
# if (selectedLigand === currentLigand) {{
# // Deselect ligand
# selectedLigand = null;
# _viewer.setStyle(
# {{ resn: _atom.resn, chain: _atom.chain, resi: _atom.resi }},
# ligandStyle
# );
# console.log("Deselected Residue:", currentLigand);
# window.parent.postMessage({{
# name: "ligand_selection",
# data: {{ residue: currentLigand, add: false }}
# }}, "*");
# }} else {{
# // Select ligand and deselect previous
# if (selectedLigand) {{
# _viewer.setStyle(
# {{
# resn: selectedLigand.resn,
# chain: selectedLigand.chain,
# resi: selectedLigand.resi
# }},
# ligandStyle
# );
# }}
# selectedLigand = currentLigand;
# _viewer.setStyle(
# {{ resn: _atom.resn, chain: _atom.chain, resi: _atom.resi }},
# {{ stick: {{ color: "red", radius: 0.4}} }}
# );
# console.log("Selected Residue:", currentLigand);
# window.parent.postMessage({{
# name: "ligand_selection",
# data: {{ residue: currentLigand, add: true }}
# }}, "*");
# }}
# _viewer.render();
# }}
# );
# """
# if pocket_path_dict:
# pocket_data_dict = {k: open(v, 'r').read() for k, v in pocket_path_dict.items()}
# for pocket_name, pocket_data in pocket_data_dict.items():
# viewer_models += f'viewer.addModel(`{pocket_data}`, "pqr");'
# model_i += 1
# viewer_models += f'viewer.getModel({model_i}).setStyle(pocketStyle);'
# if interactive_pockets:
# # return the pocket name when the pocket is clicked
# viewer_models += f"""
# let selectedPocket = null;
# viewer.getModel({model_i}).setClickable(
# {{ byres: true }},
# true,
# function (_atom, _viewer, _event, _container) {{
# let currentPocket = "{pocket_name}";
#
# if (selectedPocket == currentPocket) {{
# // Deselect pocket
# selectedPocket = null;
# _viewer.getModel({model_i}).setStyle( pocketStyle );
# console.log("Deselected Pocket:", currentPocket);
# window.parent.postMessage({{
# name: "pocket_selection",
# data: {{ pocket: currentPocket, add: false }}
# }}, "*");
# }} else {{
# // Select pocket and deselect previous
# if (selectedPocket) {{
# _viewer.getModel(selectedPocket).setStyle( pocketStyle );
# }}
# selectedPocket = currentPocket;
# _viewer.getModel({model_i}).setStyle(
# {{ sphere: {{ color: "red", opacity: 0.9}} }}
# );
# console.log("Selected Pocket:", currentPocket);
# window.parent.postMessage({{
# name: "pocket_selection",
# data: {{ pocket: currentPocket, add: true }}
# }}, "*");
# }}
# _viewer.render();
# }}
# );
# """
#
# html = static.COMPLEX_RENDERING_TEMPLATE.format(viewer_models=viewer_models)
# return static.IFRAME_TEMPLATE.format(html=html)
def create_result_table_html(summary_df, result_info, opts=(), progress=gr.Progress(track_tqdm=True)):
html_df = summary_df.copy().drop(columns=['mol'])
column_aliases = {
'out_path': 'Pose',
'ligand_conf_path': 'Pose',
'ID1': 'Compound ID',
'ID2': 'Target ID',
'X1': 'Fragment SMILES',
'X1^': 'Compound SMILES',
'name': 'Complex Name',
}
output_dir = Path(result_info['output_dir'])
job_type = result_info['type']
html_df.rename(columns=column_aliases, inplace=True)
html_df['Pose'] = html_df['Pose'].apply(lambda x: str(output_dir / job_type / x))
# drop remaining columns ending with '_path'
hidden_cols = [col for col in html_df.columns if col.endswith('_path')]
html_df.index.name = 'Index'
# if 'Scaffold' in html_df.columns and 'Exclude Scaffold Graph' not in opts:
# html_df['Scaffold'] = html_df['Scaffold'].parallel_apply(
# lambda x: PandasTools.PrintAsImageString(x) if not pd.isna(x) else x)
# else:
# html_df.drop(['Scaffold'], axis=1, inplace=True)
num_cols = html_df.select_dtypes('number').columns
num_col_colors = sns.color_palette('husl', len(num_cols))
bool_cols = html_df.select_dtypes(bool).columns
image_zoom_formatter = HTMLTemplateFormatter(
template=''
)
uniprot_id_formatter = HTMLTemplateFormatter(
template='<% if (value == value) { ' # Check if value is not NaN
'if (/^[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}$/.test(value)) '
# Check if value is a valid UniProt ID
'{ %><%= value %><% '
# Else treat it as a sequence or other plain-text string, line-warping every 60 characters
'} else { %>