Spaces:
Runtime error
Runtime error
import streamlit as st | |
import ipywidgets | |
import py3Dmol | |
from rdkit import Chem | |
from rdkit.Chem import Draw | |
from PIL import Image | |
from rdkit import Chem | |
from rdkit.Chem import AllChem | |
from ipywidgets import interact,fixed,IntSlider | |
import streamlit as st | |
import streamlit.components.v1 as components | |
import py3Dmol | |
from rdkit import Chem | |
from rdkit.Chem import Draw | |
from rdkit.Chem import AllChem | |
def smi2conf(smiles): | |
'''Convert SMILES to rdkit.Mol with 3D coordinates''' | |
mol = Chem.MolFromSmiles(smiles) | |
if mol is not None: | |
mol = Chem.AddHs(mol) | |
AllChem.EmbedMolecule(mol) | |
AllChem.MMFFOptimizeMolecule(mol, maxIters=200) | |
return mol | |
else: | |
return None | |
def MolTo3DView(mol, size=(300, 300), style="stick", surface=False, opacity=0.5): | |
"""Draw molecule in 3D | |
Args: | |
---- | |
mol: rdMol, molecule to show | |
size: tuple(int, int), canvas size | |
style: str, type of drawing molecule | |
style can be 'line', 'stick', 'sphere', 'carton' | |
surface, bool, display SAS | |
opacity, float, opacity of surface, range 0.0-1.0 | |
Return: | |
---- | |
viewer: py3Dmol.view, a class for constructing embedded 3Dmol.js views in ipython notebooks. | |
""" | |
assert style in ('line', 'stick', 'sphere', 'carton') | |
mblock = Chem.MolToMolBlock(mol) | |
viewer = py3Dmol.view(width=size[0], height=size[1]) | |
viewer.addModel(mblock, 'mol') | |
viewer.setStyle({style:{}}) | |
if surface: | |
viewer.addSurface(py3Dmol.SAS, {'opacity': opacity}) | |
viewer.zoomTo() | |
return viewer | |
def MakeMolecule(name, ingredients): | |
st.write(name, ": ", ingredients) | |
m = Chem.MolFromSmiles(ingredients) | |
im=Draw.MolToImage(m) | |
st.image(im) | |
def conf_viewer(idx): | |
mol = confs[idx] | |
return MolTo3DView(mol).show() | |
def style_selector(idx, s): | |
conf = confs[idx] | |
return MolTo3DView(conf, style=s).show() | |
def smi2viewer(smi='CC=O'): | |
try: | |
conf = smi2conf(smi) | |
return MolTo3DView(conf).show() | |
except: | |
return None | |
smi = 'COc3nc(OCc2ccc(C#N)c(c1ccc(C(=O)O)cc1)c2P(=O)(O)O)ccc3C[NH2+]CC(I)NC(=O)C(F)(Cl)Br' | |
conf = smi2conf(smi) | |
viewer = MolTo3DView(conf, size=(600, 300), style='sphere') | |
viewer.show() | |
compound_smiles = 'c1cc(C(=O)O)c(OC(=O)C)cc1' | |
m = Chem.MolFromSmiles(compound_smiles) | |
im=Draw.MolToImage(m) | |
st.image(im) | |
viewer = MolTo3DView(conf, size=(600, 300), style='sphere') | |
viewer.show() | |
smis = [ 'COc3nc(OCc2ccc(C#N)c(c1ccc(C(=O)O)cc1)c2P(=O)(O)O)ccc3C[NH2+]CC(I)NC(=O)C(F)(Cl)Br', | |
'CC(NCCNCC1=CC=C(OCC2=C(C)C(C3=CC=CC=C3)=CC=C2)N=C1OC)=O', | |
'Cc1c(COc2cc(OCc3cccc(c3)C#N)c(CN3C[C@H](O)C[C@H]3C(O)=O)cc2Cl)cccc1-c1ccc2OCCOc2c1', | |
'CCCCC(=O)NCCCCC(=O)NCCCCCC(=O)[O-]', | |
"CC(NCCNCC1=CC=C(OCC2=C(C)C(C3=CC=CC=C3)=CC=C2)N=C1OC)=O"] | |
confs = [smi2conf(s) for s in smis] | |
st.title('⚛️🧬Molecule 3D Modeler🧬⚛️') | |
def show(smi, style='stick'): | |
mol = Chem.MolFromSmiles(smi) | |
mol = Chem.AddHs(mol) | |
AllChem.EmbedMolecule(mol) | |
AllChem.MMFFOptimizeMolecule(mol, maxIters=200) | |
mblock = Chem.MolToMolBlock(mol) | |
view = py3Dmol.view(width=400, height=400) | |
view.addModel(mblock, 'mol') | |
view.setStyle({style:{}}) | |
view.zoomTo() | |
view.show() | |
view.render() | |
t =view.js() | |
f = open('viz.html', 'w') | |
f.write(t.startjs) | |
f.write(t.endjs) | |
f.close() | |
compound_smiles=st.text_input('SMILES please','CCCCC(=O)NCCCCC(=O)NCCCCCC(=O)[O-]') | |
m = Chem.MolFromSmiles(compound_smiles) | |
#Draw.MolToFile(m,'mol.png') | |
show(compound_smiles) | |
HtmlFile = open("viz.html", 'r', encoding='utf-8') | |
source_code = HtmlFile.read() | |
c1,c2=st.columns(2) | |
with c1: | |
st.write('⚛️🧬Molecule🧬⚛️:') | |
with c2: | |
components.html(source_code, height = 400,width=400) | |
st.write('Info about SMILES: https://archive.epa.gov/med/med_archive_03/web/html/smiles.html') | |
MakeMolecule("Ethanol", "CCO") | |
MakeMolecule("Acetic acid", "CC(=O)O") | |
MakeMolecule("Cyclohexane", "C1CCCCC1") | |
MakeMolecule("Pyridine", "c1cnccc1") | |