Spaces:
Running
Running
File size: 12,830 Bytes
8034147 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# importing Liberaries
import streamlit as st
from streamlit_ketcher import st_ketcher
from rdkit import Chem
from rdkit.Chem import Draw, AllChem, rdDetermineBonds, Descriptors
from PIL import Image
import py3Dmol
from stmol import showmol
from stmol import speck_plot
from io import StringIO
import sys
import base64
import subprocess
import time
import datamol as dm
import medchem as mc
import streamlit_shadcn_ui as ui
from molvs import standardize_smiles
from molvs import Standardizer
import pandas as pd
import streamlit_shadcn_ui as ui
try:
# from openbabel import OBMol, OBConversion
import openbabel
except ModuleNotFoundError as e:
subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/usr/include/openbabel3" --global-option="-L/usr/lib/openbabel" openbabel==2.4.1'], shell=True)
subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/home/appuser/include/openbabel3" --global-option="-L/home/appuser/lib/openbabel" openbabel==2.4.1'], shell=True)
subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/home/appuser/usr/include/openbabel3" --global-option="-L/home/appuser/usr/lib/openbabel" openbabel==2.4.1'], shell=True)
# wait for subprocess to install package before running your actual code below
#print('openbabel python not importing')
time.sleep(90)
from openbabel import pybel
#from chemicalconverters import NamesConverter
st.set_page_config(page_title="CHEMSTUDIO24", page_icon="data/favicon.png", layout="centered")
st.logo("data/banner24.png")
tab_titles = ['Home', 'Drawing Arena', 'Rendering and Visualization', 'Representation and Format Conversion', 'Descriptors and Rules',]
tab1, tab2, tab3, tab4, tab5,= st.tabs(tab_titles)
#st.sidebar.link_button("Contact me", "mailto:[email protected]")
Titlelogo = Image.open('data/banner24.png')
#st.sidebar.image(Titlelogo)
Titlelogo = Image.open('data/banner24.png')
with tab1:
# banner and header
st.image(Titlelogo)
st.link_button("**:gray[@IN SILICO] :violet[CHEMISTRY]**", "https://www.insilicochemistry.io")
st.link_button(":rainbow[@mohamed elsoudy]", "mailto:[email protected]")
# st.markdown('#') #insert space
# show gif
file_ = open("data/baa.gif", "rb")
contents = file_.read()
data_url = base64.b64encode(contents).decode("utf-8")
file_.close()
st.markdown(
f'<center><img src="data:image/gif;base64,{data_url}" class="centerImage" alt="cat gif"></center>',
unsafe_allow_html=True,
)
st.divider()
def support():
link = "https://www.paypal.com/paypalme/insilicochemistry"
file_ = open("data/support.png", "rb")
contents = file_.read()
data_url = base64.b64encode(contents).decode("utf-8")
file_.close()
st.markdown(
f'<center><a href="{link}"><img src="data:image/png;base64,{data_url}" class="centerImage" alt="cat gif"></center>',
unsafe_allow_html=True, )
def support_button():
url = "https://www.paypal.com/paypalme/insilicochemistry"
st.markdown(f'''
<a href={url}><button style="background-color:#a870dd;">Support us</button></a>
''',
unsafe_allow_html=True)
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
pass
with col2:
pass
with col4:
pass
with col5:
pass
with col3:
st.link_button("**:white[Support us]**", "https://www.paypal.com/paypalme/insilicochemistry", type="primary")
with tab2:
# KETCHER
st.subheader("Drawing Arena")
molecule = st.text_input("Enter SMILES or Draw your Molecule :art:", "C1=C[SH]=CCO1")
smiles = st_ketcher(molecule, height=800)
stan_smiles = standardize_smiles(smiles)
#converter = NamesConverter(model_name="knowledgator/SMILES2IUPAC-canonical-base")
#st.code(converter.smiles_to_iupac(stan_smiles), "html")
st.code(stan_smiles, "html")
mol = Chem.MolFromSmiles(smiles)
s = Standardizer()
smol = s.standardize(mol)
# RENDERING
def render_2d(mol):
mol = Chem.MolFromSmiles(mol)
s = Standardizer()
smol = s.standardize(mol)
image = Draw.MolToImage(mol, size = (400, 400))
return image
def render_3d(smi):
mol = Chem.MolFromSmiles(smi)
if Hydrogen:
mol = Chem.AddHs(mol)
else:
pass
AllChem.EmbedMolecule(mol)
mblock = Chem.MolToMolBlock(mol)
xyzview = py3Dmol.view( width=400, height=400)
xyzview.addModel(mblock, 'mol')
xyzview.setStyle(stylemd, viewer=(0,0))
xyzview.setBackgroundColor(bcolor)
if box:
xyzview.addBox({'center': {'x': 0,'y': 0,'z': 0},
# 'dimensions': {'w': 1, 'h': 1, 'd': 1}, # scalars
'dimensions': {'w': {'x': 8, 'y': 8, 'z': 0}, #[1, 1, 0], #np.array([1,1,0]),
'h': {'x': 8, 'y': -8, 'z': 0}, # [1, -1, 0], #np.array([1,-1,0]),
'd': {'x': 0, 'y': 0, 'z': 8}, # [0, 0, 1], #np.array([0,0,1]),
},
'color': '#C791FB',
'alpha': 0.5,
})
else:
pass
xyzview.rotate(23, {'x': 1, 'y': 1, 'z': 1})
xyzview.zoomTo()
if spin:
xyzview.spin(True)
else:
xyzview.spin(False)
xyzview.setViewStyle({"style": "outline", "color": "#CA86FF", "width": 0.05})
#xyzview.addLabel(stan_smiles, {'position': {'x': -6.89, 'y': 0.75, 'z': 0.35},
# 'backgroundColor': '#515151', 'backgroundOpacity': 0.5,'fontSize':18,'fontColor':'#F9F7F7 ',
# 'fontOpacity':1,'borderThickness':0.0,'inFront':'true','showBackground':'true'})
showmol(xyzview,height=400,width=400)
blk2 = render_2d(smiles)
with tab3:
#st.code(converter.smiles_to_iupac(stan_smiles)[0], "html")
st.code(stan_smiles, "html")
col1, col2= st.columns((4,4))
with col1:
col1.subheader("2D STRUCTURE")
st.image(blk2)
with col2:
col2.subheader("3D STRUCTURE")
st.markdown("Select your Model!")
styles3d = {"Line Model": {"line": {}},
"Cross Model": {"cross":{}},
"Stick Model": {"stick": {}, "sphere": {"scale": 0}},
"Ball and Stick Model": {"stick": {}, "sphere": {"scale": 0.3}},
"CPK Model": {"sphere": {}}
}
list_style = ['Line', 'Cross', 'Stick', 'Ball and Stick', 'CPK']
value = ui.tabs(options=list_style, default_value='Line', key="kanaries")
def stylem(value):
if value == list_style[0]:
value2 = styles3d["Line Model"]
elif value == list_style[1]:
value2 = styles3d["Cross Model"]
elif value == list_style[2]:
value2 = styles3d["Stick Model"]
elif value == list_style[3]:
value2 = styles3d["Ball and Stick Model"]
elif value == list_style[4]:
value2 = styles3d["CPK Model"]
return value2
stylemd = stylem(value)
Hydrogen = st.toggle('Add Hydrogens', value=False, key=1)
spin = st.toggle('Animate', value=False, key=2)
box = st.toggle('Show Box', value=False, key=3)
bcolor = st.color_picker('Pick Background Color', '#323232', key=4)
render_3d(smiles)
with tab4:
st.subheader("**:gray[Representation and Format] :gray[Conversion]**")
datamol_mol = dm.to_mol(smiles)
#st.markdown(datamol_mol)
def to_smarts(datamol_mol):
smarts = dm.smiles_as_smarts(datamol_mol)
st.code(smarts, "html")
def to__inchi(datamol_mol):
inchi = dm.to_inchi(datamol_mol)
st.code(inchi, "html")
def to__inchikey(datamol_mol):
inchikey = dm.to_inchikey(datamol_mol)
st.code(inchikey, "html")
def to_xyz(smiles, unsafe_allow_html=True):
mol = Chem.MolFromSmiles(smiles)
mol_h = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol_h)
xyz = Chem.MolToXYZBlock(mol_h)
mol2 = Chem.MolFromXYZBlock(xyz)
rdDetermineBonds.DetermineBonds(mol2)
xyz = Chem.MolToXYZBlock(mol2)
st.code(xyz,"html")
st.download_button(label="Download .xyz File", data=xyz,
file_name=f'{stan_smiles}' + "." + "xyz", mime='text/csv', )
def to_rdkit3d(smiles):
mol = Chem.MolFromSmiles(smiles)
mol_h = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol_h)
xyz = Chem.MolToXYZBlock(mol_h)
mol2 = Chem.MolFromXYZBlock(xyz)
rdDetermineBonds.DetermineBonds(mol2)
xyz = Chem.MolToXYZBlock(mol2)
input_geom_str = xyz
mol2 = pybel.readstring("xyz", input_geom_str)
# mol.make3D()
output_geom_str = mol2.write("mol")
st.code(output_geom_str,"html")
st.download_button(label="Download .mol File", data=output_geom_str,
file_name=f'{stan_smiles}' + "." + "mol", mime='text/csv', )
def to_sdf(smiles):
mol = Chem.MolFromSmiles(smiles)
mol_h = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol_h)
xyz = Chem.MolToXYZBlock(mol_h)
mol2 = Chem.MolFromXYZBlock(xyz)
rdDetermineBonds.DetermineBonds(mol2)
xyz = Chem.MolToXYZBlock(mol2)
input_geom_str = xyz
mol2 = pybel.readstring("xyz", input_geom_str)
# mol.make3D()
output_geom_str = mol2.write("sdf")
st.code(output_geom_str,"html")
st.download_button(label="Download .sdf File", data=output_geom_str,
file_name=f'{stan_smiles}' + "." + "sdf", mime='text/csv', )
def to_cif(smiles):
mol = Chem.MolFromSmiles(smiles)
mol_h = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol_h)
xyz = Chem.MolToXYZBlock(mol_h)
mol2 = Chem.MolFromXYZBlock(xyz)
rdDetermineBonds.DetermineBonds(mol2)
xyz = Chem.MolToXYZBlock(mol2)
input_geom_str = xyz
mol2 = pybel.readstring("xyz", input_geom_str)
# mol.make3D()
output_geom_str = mol2.write("cif")
st.code(output_geom_str,"html")
st.download_button(label="Download .cif File", data=output_geom_str,
file_name=f'{stan_smiles}' + "." + "cif", mime='text/csv', )
def to_pdb(smiles):
mol = Chem.MolFromSmiles(smiles)
mol_h = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol_h)
xyz = Chem.MolToXYZBlock(mol_h)
mol2 = Chem.MolFromXYZBlock(xyz)
rdDetermineBonds.DetermineBonds(mol2)
xyz = Chem.MolToXYZBlock(mol2)
input_geom_str = xyz
mol2 = pybel.readstring("xyz", input_geom_str)
# mol.make3D()
output_geom_str = mol2.write("pdb")
st.code(output_geom_str,"html")
st.download_button(label="Download .pdb File", data=output_geom_str,
file_name=f'{stan_smiles}' + "." + "pdb", mime='text/csv', )
SMILES_OUT = st.selectbox("Select Output!",["SMARTS", "InChI", "InChIKey", "mol", "sdf", "xyz", "cif", "pdb"])
if SMILES_OUT == "SMARTS":
to_smarts(datamol_mol)
elif SMILES_OUT == "InChI":
to__inchi(datamol_mol)
elif SMILES_OUT == "InChIKey":
to__inchikey(datamol_mol)
elif SMILES_OUT == "xyz":
to_xyz(smiles, unsafe_allow_html=True)
elif SMILES_OUT == "mol":
to_rdkit3d(smiles)
elif SMILES_OUT == "sdf":
to_sdf(smiles)
elif SMILES_OUT == "cif":
to_cif(smiles)
elif SMILES_OUT == "pdb":
to_pdb(smiles)
with tab5:
my_dict = {'SMILES': stan_smiles,
'Molecular Weight': Descriptors.MolWt(smol),
'Number of H Acceptors': Descriptors.NumHAcceptors(smol),
'Number of H Donors': Descriptors.NumHDonors(smol),
'Octanol/Water Partition Coefficient LogP': Descriptors.MolLogP(smol),
"Topological Polar Surface Area (TPSA)": Descriptors.TPSA(smol),
"Lipinski's Rule of 5": mc.rules.basic_rules.rule_of_five(datamol_mol),
"Pfizer Rule 3/75": mc.rules.basic_rules.rule_of_pfizer_3_75(datamol_mol)
}
new = pd.DataFrame.from_dict([my_dict],)
new = new.rename(index={0: 'Descriptors and Rules'})
NEWT = new.T
st.dataframe(NEWT)
NEWT = NEWT.to_csv(index=True).encode('utf-8')
st.download_button(
"Download data!",
NEWT,
f"{stan_smiles}"+" Descriptors_Rules"+".csv",
"text/csv",
key='download-csv'
)
|