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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from rdkit import Chem
|
| 3 |
+
from rdkit.Chem import Descriptors, Draw, AllChem
|
| 4 |
+
import cirpy
|
| 5 |
+
import py3Dmol
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# RDKit API with multiple endpoints
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def _mol_from_smiles(smiles: str):
|
| 12 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 13 |
+
if mol is None:
|
| 14 |
+
raise gr.Error("Invalid SMILES string.")
|
| 15 |
+
return mol
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def smiles_to_canonical(smiles: str) -> str:
|
| 19 |
+
mol = _mol_from_smiles(smiles)
|
| 20 |
+
return Chem.MolToSmiles(mol)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def molecular_weight(smiles: str) -> float:
|
| 24 |
+
mol = _mol_from_smiles(smiles)
|
| 25 |
+
return float(Descriptors.MolWt(mol))
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def logp(smiles: str) -> float:
|
| 29 |
+
mol = _mol_from_smiles(smiles)
|
| 30 |
+
return float(Descriptors.MolLogP(mol))
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def tpsa(smiles: str) -> float:
|
| 34 |
+
mol = _mol_from_smiles(smiles)
|
| 35 |
+
return float(Descriptors.TPSA(mol))
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def mol_image(smiles: str):
|
| 39 |
+
mol = _mol_from_smiles(smiles)
|
| 40 |
+
return Draw.MolToImage(mol)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def name_to_smiles(name: str) -> str:
|
| 44 |
+
"""Convert chemical name to SMILES using Chemical Identifier Resolver (CIR)"""
|
| 45 |
+
try:
|
| 46 |
+
smiles = cirpy.resolve(name, 'smiles')
|
| 47 |
+
if smiles is None:
|
| 48 |
+
raise gr.Error(f"Could not find SMILES for chemical name: {name}")
|
| 49 |
+
return smiles
|
| 50 |
+
except Exception as e:
|
| 51 |
+
raise gr.Error(f"Error converting name to SMILES: {str(e)}")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def name_to_3d_molecule(name: str) -> str:
|
| 55 |
+
"""Convert chemical name to 3D molecule visualization"""
|
| 56 |
+
try:
|
| 57 |
+
# Convert name to SMILES
|
| 58 |
+
smiles = cirpy.resolve(name, 'smiles')
|
| 59 |
+
if smiles is None:
|
| 60 |
+
raise gr.Error(f"Could not find SMILES for chemical name: {name}")
|
| 61 |
+
|
| 62 |
+
# Create molecule from SMILES
|
| 63 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 64 |
+
if mol is None:
|
| 65 |
+
raise gr.Error(f"Could not create molecule from SMILES: {smiles}")
|
| 66 |
+
|
| 67 |
+
# Add hydrogens for better 3D structure
|
| 68 |
+
mol = Chem.AddHs(mol)
|
| 69 |
+
|
| 70 |
+
# Generate 3D coordinates
|
| 71 |
+
success = AllChem.EmbedMolecule(mol, AllChem.ETKDG())
|
| 72 |
+
if success == -1:
|
| 73 |
+
raise gr.Error(f"Could not generate 3D coordinates for: {name}")
|
| 74 |
+
|
| 75 |
+
# Optimize geometry
|
| 76 |
+
AllChem.MMFFOptimizeMolecule(mol)
|
| 77 |
+
|
| 78 |
+
# Convert to PDB format for py3Dmol
|
| 79 |
+
pdb_block = Chem.MolToPDBBlock(mol)
|
| 80 |
+
|
| 81 |
+
# Create 3D viewer
|
| 82 |
+
viewer = py3Dmol.view(width=400, height=400)
|
| 83 |
+
viewer.addModel(pdb_block, 'pdb')
|
| 84 |
+
viewer.setStyle({'stick': {}})
|
| 85 |
+
viewer.zoomTo()
|
| 86 |
+
|
| 87 |
+
# Return HTML representation
|
| 88 |
+
return viewer._repr_html_()
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
raise gr.Error(f"Error creating 3D molecule: {str(e)}")
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
smiles_interface = gr.Interface(
|
| 95 |
+
fn=smiles_to_canonical,
|
| 96 |
+
inputs=gr.Textbox(label="SMILES"),
|
| 97 |
+
outputs=gr.Textbox(label="Canonical SMILES"),
|
| 98 |
+
api_name="smiles_to_mol",
|
| 99 |
+
description="Convert an input SMILES string to its canonical form.",
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
name_interface = gr.Interface(
|
| 103 |
+
fn=name_to_smiles,
|
| 104 |
+
inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., aspirin, caffeine, benzene"),
|
| 105 |
+
outputs=gr.Textbox(label="SMILES"),
|
| 106 |
+
api_name="name_to_smiles",
|
| 107 |
+
description="Convert a chemical name to SMILES notation.",
|
| 108 |
+
examples=[["aspirin"], ["caffeine"], ["benzene"], ["ethanol"]],
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
mw_interface = gr.Interface(
|
| 112 |
+
fn=molecular_weight,
|
| 113 |
+
inputs=gr.Textbox(label="SMILES"),
|
| 114 |
+
outputs=gr.Number(label="Molecular Weight (g/mol)"),
|
| 115 |
+
api_name="molecular_weight",
|
| 116 |
+
description="Compute the molecular weight from a SMILES string.",
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
logp_interface = gr.Interface(
|
| 120 |
+
fn=logp,
|
| 121 |
+
inputs=gr.Textbox(label="SMILES"),
|
| 122 |
+
outputs=gr.Number(label="logP"),
|
| 123 |
+
api_name="logp",
|
| 124 |
+
description="Calculate the octanol/water partition coefficient (logP).",
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
tpsa_interface = gr.Interface(
|
| 128 |
+
fn=tpsa,
|
| 129 |
+
inputs=gr.Textbox(label="SMILES"),
|
| 130 |
+
outputs=gr.Number(label="TPSA"),
|
| 131 |
+
api_name="tpsa",
|
| 132 |
+
description="Calculate the topological polar surface area (TPSA).",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
molecule_3d_interface = gr.Interface(
|
| 136 |
+
fn=name_to_3d_molecule,
|
| 137 |
+
inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine"),
|
| 138 |
+
outputs=gr.HTML(label="3D Molecule Viewer"),
|
| 139 |
+
api_name="name_to_3d_molecule",
|
| 140 |
+
description="Convert a chemical name to an interactive 3D molecule visualization.",
|
| 141 |
+
examples=[["benzene"], ["aspirin"], ["caffeine"], ["ethanol"]],
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
demo = gr.TabbedInterface(
|
| 146 |
+
[name_interface, molecule_3d_interface, smiles_interface, mw_interface, logp_interface, tpsa_interface],
|
| 147 |
+
[
|
| 148 |
+
"Name to SMILES",
|
| 149 |
+
"3D Molecule Viewer",
|
| 150 |
+
"SMILES to Canonical",
|
| 151 |
+
"Molecular Weight",
|
| 152 |
+
"LogP",
|
| 153 |
+
"TPSA",
|
| 154 |
+
],
|
| 155 |
+
title="RDKit API",
|
| 156 |
+
css=".gradio-container {max-width: 800px; margin: auto;}",
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|