-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_utils.py
213 lines (197 loc) · 6.66 KB
/
streamlit_utils.py
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
import py3Dmol
import streamlit as st
from rdkit import Chem
from rdkit.Chem.AllChem import EmbedMolecule, MMFFOptimizeMolecule
from stmol import showmol
from rdkit_utils import (
run_reaction,
smi_to_canon_smi,
smi_to_svg,
smirks_to_svg,
)
def st_display_mol_with_smi(
smiles,
display_smiles=False,
substruct="",
use_smiles=False,
display_substruct=True,
img_size=(150, 100),
labels=[],
):
if substruct != "" and display_substruct:
if use_smiles:
st.markdown(f"substruct SMILES: `{substruct}`")
else:
st.markdown(f"substruct SMARTS: `{substruct}`")
n_cols = 4 if len(smiles) < 4 else len(smiles)
columns = [col for col in st.columns(n_cols)]
if len(labels) == 0:
labels = [""] * len(smiles)
for smi, label, col in zip(smiles, labels, columns):
col.image(
smi_to_svg(
smi,
img_size=img_size,
substruct=substruct,
use_smiles=use_smiles,
)
)
if label != "":
col.markdown(f" **{label}**")
if display_smiles:
col.markdown(f" `{smi}`")
if substruct != "":
mol = Chem.MolFromSmiles(smi)
if use_smiles:
substruct_mol = Chem.MolFromSmiles(substruct)
else:
substruct_mol = Chem.MolFromSmarts(substruct)
if mol.HasSubstructMatch(substruct_mol):
col.markdown("*Match!*")
else:
col.markdown("*No match!*")
def st_match_smi_to_mol(smiles, img_size=(150, 100), labels=[], idx_offset=0):
n_smi = len(smiles)
n_cols = 4 if len(smiles) < 4 else len(smiles)
columns = [col for col in st.columns(n_cols)]
key_idxs = [i + idx_offset for i in range(n_smi)]
if len(labels) == 0:
labels = [""] * len(smiles)
for smi, label, col, key_idx in zip(smiles, labels, columns, key_idxs):
smi_key = f"smi_{key_idx}"
button_key = f"button_match_smi_to_mol{key_idx}"
col.text_input("SMILES:", key=smi_key)
col.image(smi_to_svg(smi, img_size=img_size))
if label != "":
col.markdown(f" **{label}**")
try:
if smi_to_canon_smi(st.session_state[smi_key]) == smi_to_canon_smi(
smi
):
col.success("Correct!")
except ValueError:
pass
if col.button("Answer", key=button_key):
col.info(f"`{smi}`")
def st_check_smarts(
smiles_in, smiles_out, answer, prompt, img_size=(150, 100), idx_offset=0
):
st.write("")
st.markdown(
f"""
##### {prompt}
"""
)
smt_key = f"smt_{idx_offset}"
button_key = f"button_check_smarts_{idx_offset}"
smt = st.text_input("SMARTS:", key=smt_key)
substruct = Chem.MolFromSmarts(smt)
st.markdown("***Match these molecules:***")
st_display_mol_with_smi(
smiles_in, substruct=smt, display_substruct=False, img_size=img_size
)
st.markdown("""***Don't match these molecules:***""")
st_display_mol_with_smi(
smiles_out, substruct=smt, display_substruct=False, img_size=img_size
)
if (
sum(
[
Chem.MolFromSmiles(smi).HasSubstructMatch(substruct)
for smi in smiles_in
]
)
== len(smiles_in)
and sum(
[
Chem.MolFromSmiles(smi).HasSubstructMatch(substruct)
for smi in smiles_out
]
)
== 0
):
st.success("Correct!")
if st.button("Answer", key=button_key):
st.info(f"`{answer}`")
def st_display_reaction(
reactants=[],
reagents=[],
products=[],
reactants_are_smarts=False,
reagents_are_smarts=False,
products_are_smarts=False,
img_size=(400, 100),
):
def smi_to_smt(smi):
return Chem.MolToSmarts(Chem.MolFromSmiles(smi))
if not reactants_are_smarts:
reactants = [smi_to_smt(smi) for smi in reactants]
if not reagents_are_smarts:
reagents = [smi_to_smt(smi) for smi in reagents]
if not products_are_smarts:
products = [smi_to_smt(smi) for smi in products]
smirks = f'{".".join(reactants)}>{".".join(reagents)}>{".".join(products)}'
st.image(smirks_to_svg(smirks, img_size=img_size))
def st_run_reaction(smk, reactants, display_smk=True, img_size=(400, 100)):
if display_smk:
st.markdown(f"SMIRKS: `{smk}`")
products = run_reaction(smk, reactants)
if [Chem.MolToSmiles(product) for product in products] == [""]:
st_display_reaction(reactants=reactants, products=["*"])
st.error("no reaction")
else:
st_display_reaction(
reactants=reactants,
products=[Chem.MolToSmarts(product) for product in products],
products_are_smarts=True,
)
def st_check_smirks(
reactantss, productss, answer, prompt, img_size=(400, 100), idx_offset=0
):
st.write("")
st.markdown(
f"""
##### {prompt}
"""
)
n_schemes = len(reactantss)
smk_key = f"smk_{idx_offset}"
button_key = f"button_check_smirks_{idx_offset}"
smk = st.text_input("SMIRKS:", key=smk_key)
for i, reactants_products in enumerate(zip(reactantss, productss)):
reactants, products = reactants_products
if products == [""]:
st_display_reaction(
reactants=reactants, products="*", products_are_smarts=True
)
st.error("no reaction")
else:
st_display_reaction(reactants=reactants, products=products)
if smk != "":
st.markdown("***your reaction:***")
st_run_reaction(smk, reactants, display_smk=False)
products = [smi_to_canon_smi(product) for product in products]
output_products = run_reaction(smk, reactants)
if sorted(products) == sorted(
[
Chem.MolToSmiles(output_product)
for output_product in output_products
]
):
st.success("Correct!")
if i < n_schemes - 1:
st.markdown("---")
if st.button("Answer:", key=button_key):
st.info(answer)
def st_display_3dmol(mol):
mol = Chem.Mol(mol)
mol = Chem.AddHs(mol)
EmbedMolecule(mol)
MMFFOptimizeMolecule(mol)
view = py3Dmol.view(width=500, height=400)
view.addModel(Chem.MolToMolBlock(mol), "mol")
view.setBackgroundColor("black")
view.setStyle({"stick": {"colorscheme": "pinkCarbon", "radius": 0.25}})
view.setViewStyle({"style": "outline", "color": "hotpink", "width": 0.04})
view.zoomTo()
showmol(view)