Spaces:
Sleeping
Sleeping
Trương Gia Bảo
commited on
Commit
·
f9355e9
1
Parent(s):
69a5b2c
Update UI
Browse files- about.png +0 -0
- app.py +125 -60
- fast_jtnn/mol_tree.py +39 -39
- requirements.txt +3 -2
- style.css +58 -0
about.png
ADDED
![]() |
app.py
CHANGED
@@ -1,5 +1,9 @@
|
|
|
|
1 |
import torch
|
|
|
2 |
import streamlit as st
|
|
|
|
|
3 |
import sys, os
|
4 |
import rdkit
|
5 |
import rdkit.Chem as Chem
|
@@ -7,6 +11,8 @@ from rdkit.Chem.Draw import MolToImage
|
|
7 |
from rdkit.Chem import Descriptors
|
8 |
import sascorer
|
9 |
import networkx as nx
|
|
|
|
|
10 |
|
11 |
os.environ['KMP_DUPLICATE_LIB_OK']='True'
|
12 |
|
@@ -15,21 +21,34 @@ from mol_tree import Vocab, MolTree
|
|
15 |
from jtprop_vae import JTPropVAE
|
16 |
from molbloom import buy
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def penalized_logp_standard(mol):
|
35 |
|
@@ -61,51 +80,97 @@ def penalized_logp_standard(mol):
|
|
61 |
standardized_cycle = (cycle_score - cycle_mean) / cycle_std
|
62 |
return standardized_log_p + standardized_SA + standardized_cycle
|
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 |
-
else:
|
92 |
-
st.write('New SMILES:')
|
93 |
-
st.code(new_smiles)
|
94 |
-
new_mol = Chem.MolFromSmiles(new_smiles)
|
95 |
-
if new_mol is None:
|
96 |
-
st.write('New SMILES is invalid.')
|
97 |
-
else:
|
98 |
-
st.write('New SMILES molecule:')
|
99 |
-
st.image(MolToImage(new_mol,size=(300,300)))
|
100 |
-
new_score = penalized_logp_standard(new_mol)
|
101 |
-
st.write('New penalized logP score: %.5f' % (new_score))
|
102 |
-
st.write('Caching ZINC20 if necessary...')
|
103 |
-
if buy(new_smiles, catalog='zinc20',canonicalize=True):
|
104 |
-
st.write('This molecule exists.')
|
105 |
-
st.caption('Checked by molbloom.')
|
106 |
else:
|
107 |
-
st.
|
108 |
-
st.
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
import torch
|
3 |
+
from st_on_hover_tabs import on_hover_tabs
|
4 |
import streamlit as st
|
5 |
+
st.set_page_config(layout="wide")
|
6 |
+
|
7 |
import sys, os
|
8 |
import rdkit
|
9 |
import rdkit.Chem as Chem
|
|
|
11 |
from rdkit.Chem import Descriptors
|
12 |
import sascorer
|
13 |
import networkx as nx
|
14 |
+
from stqdm import stqdm
|
15 |
+
import base64, io
|
16 |
|
17 |
os.environ['KMP_DUPLICATE_LIB_OK']='True'
|
18 |
|
|
|
21 |
from jtprop_vae import JTPropVAE
|
22 |
from molbloom import buy
|
23 |
|
24 |
+
css='''
|
25 |
+
[data-testid="metric-container"] {
|
26 |
+
width: fit-content;
|
27 |
+
margin: auto;
|
28 |
+
}
|
29 |
+
|
30 |
+
[data-testid="metric-container"] > div {
|
31 |
+
width: fit-content;
|
32 |
+
margin: auto;
|
33 |
+
}
|
34 |
+
|
35 |
+
[data-testid="metric-container"] label {
|
36 |
+
width: fit-content;
|
37 |
+
margin: auto;
|
38 |
+
}
|
39 |
+
'''
|
40 |
+
|
41 |
+
st.markdown(f'<style>{css}</style>',unsafe_allow_html=True)
|
42 |
+
|
43 |
+
def img_to_bytes(img_path):
|
44 |
+
img_bytes = Path(img_path).read_bytes()
|
45 |
+
encoded = base64.b64encode(img_bytes).decode()
|
46 |
+
return encoded
|
47 |
+
def img_to_html(img_path):
|
48 |
+
img_html = "<img src='data:image/png;base64,{}' class='img-fluid' style='max-width: 500px;'>".format(
|
49 |
+
img_to_bytes(img_path)
|
50 |
+
)
|
51 |
+
return img_html
|
52 |
|
53 |
def penalized_logp_standard(mol):
|
54 |
|
|
|
80 |
standardized_cycle = (cycle_score - cycle_mean) / cycle_std
|
81 |
return standardized_log_p + standardized_SA + standardized_cycle
|
82 |
|
83 |
+
lg = rdkit.RDLogger.logger()
|
84 |
+
lg.setLevel(rdkit.RDLogger.CRITICAL)
|
85 |
+
|
86 |
+
st.markdown("<h1 style='text-align: center;'>Junction Tree Variational Autoencoder for Molecular Graph Generation (JTVAE)</h1>",unsafe_allow_html=True)
|
87 |
+
st.markdown("<h3 style='text-align: center;'>Wengong Jin, Regina Barzilay, Tommi Jaakkola</h3>",unsafe_allow_html=True)
|
88 |
+
st.markdown('<style>' + open('./style.css').read() + '</style>', unsafe_allow_html=True)
|
89 |
+
|
90 |
+
with st.sidebar:
|
91 |
+
# st.header('+')
|
92 |
+
st.markdown("<h5 style='text-align: center; color:grey;'>Explore</h5>",unsafe_allow_html=True)
|
93 |
+
tabs = on_hover_tabs(tabName=['Optimize a molecule', 'Optimize batch', 'About'],
|
94 |
+
iconName=['science', 'batch_prediction', 'info'], default_choice=0)
|
95 |
+
|
96 |
+
if tabs == 'About':
|
97 |
+
descrip = '''
|
98 |
+
We seek to automate the design of molecules based on specific chemical properties. In computational terms, this task involves continuous embedding and generation of molecular graphs. Our primary contribution is the direct realization of molecular graphs, a task previously approached by generating linear SMILES strings instead of graphs. Our junction tree variational autoencoder generates molecular graphs in two phases, by first generating a tree-structured scaffold over chemical substructures, and then combining them into a molecule with a graph message passing network. This approach allows us to incrementally expand molecules while maintaining chemical validity at every step. We evaluate our model on multiple tasks ranging from molecular generation to optimization. Across these tasks, our model outperforms previous state-of-the-art baselines by a significant margin.
|
99 |
+
|
100 |
+
[https://arxiv.org/abs/1802.04364](https://arxiv.org/abs/1802.04364)'''
|
101 |
+
st.markdown(descrip)
|
102 |
+
st.markdown("<p style='text-align: center;'>"+
|
103 |
+
img_to_html('about.png')+
|
104 |
+
"</p>", unsafe_allow_html=True)
|
105 |
+
elif tabs == 'Optimize a molecule':
|
106 |
+
st.markdown("<h2 style='text-align: center;'>Optimize a molecule</h2>",unsafe_allow_html=True)
|
107 |
+
st.text_input('Enter a SMILES string:','CNC(=O)C1=NC=CC(=C1)OC2=CC=C(C=C2)NC(=O)NC3=CC(=C(C=C3)Cl)C(F)(F)F',key='smiles')
|
108 |
+
|
109 |
+
mol = Chem.MolFromSmiles(st.session_state.smiles)
|
110 |
+
if mol is None:
|
111 |
+
st.markdown("<p style='text-align: center; color: red;'>SMILES is invalid. Please enter a valid SMILES.</p>",unsafe_allow_html=True)
|
112 |
+
else:
|
113 |
+
score = penalized_logp_standard(mol)
|
114 |
+
# with st.columns(3)[1]:
|
115 |
+
# st.markdown("<style>{text-align: center;}</style>",unsafe_allow_html=True)
|
116 |
+
imgByteArr = io.BytesIO()
|
117 |
+
MolToImage(mol,size=(400,400)).save(imgByteArr,format='PNG')
|
118 |
+
st.markdown("<p style='text-align: center;'>"+
|
119 |
+
f"<img src='data:image/png;base64,{base64.b64encode(imgByteArr.getvalue()).decode()}' class='img-fluid'>"+
|
120 |
+
"</p>", unsafe_allow_html=True)
|
121 |
+
# st.image(MolToImage(mol,size=(300,300)))
|
122 |
+
st.metric('Penalized logP score', '%.5f' % (score))
|
123 |
+
|
124 |
+
if mol is not None:
|
125 |
+
# col1, col2, col3 = st.columns(3)
|
126 |
+
st.slider('Choose learning rate: ',0.0,10.0,0.4,key='lr')
|
127 |
+
st.slider('Choose similarity cutoff: ',0.0,3.0,0.4,key='sim_cutoff')
|
128 |
+
st.slider('Choose number of iterations: ',1,100,80,key='n_iter')
|
129 |
+
vocab = [x.strip("\r\n ") for x in open('./vocab.txt')]
|
130 |
+
vocab = Vocab(vocab)
|
131 |
+
if st.button('Optimize'):
|
132 |
+
# st.write('Testing')
|
133 |
+
|
134 |
+
# with st.columns(3)[1]:
|
135 |
+
with st.spinner("Operation in progress. Please wait."):
|
136 |
+
|
137 |
+
model = JTPropVAE(vocab, 450, 56, 20, 3)
|
138 |
+
|
139 |
+
model.load_state_dict(torch.load('./model.iter-685000',map_location=torch.device('cpu')))
|
140 |
|
141 |
+
new_smiles,sim = model.optimize(st.session_state.smiles, sim_cutoff=st.session_state.sim_cutoff, lr=st.session_state.lr, num_iter=st.session_state.n_iter)
|
142 |
|
143 |
+
del model
|
144 |
+
if new_smiles is None:
|
145 |
+
st.markdown("<p style='text-align: center; color: red;'>Cannot optimize! Please choose another setting.</p>",unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
else:
|
147 |
+
st.markdown("<b style='text-align: center;'>New SMILES</b>",unsafe_allow_html=True)
|
148 |
+
st.code(new_smiles)
|
149 |
+
new_mol = Chem.MolFromSmiles(new_smiles)
|
150 |
+
if new_mol is None:
|
151 |
+
st.markdown("<p style='text-align: center; color: red;'>New SMILES is invalid! Please choose another setting.</p>",unsafe_allow_html=True)
|
152 |
+
# st.write('New SMILES is invalid.')
|
153 |
+
else:
|
154 |
+
# st.write('New SMILES molecule:')
|
155 |
+
imgByteArr = io.BytesIO()
|
156 |
+
MolToImage(new_mol,size=(400,400)).save(imgByteArr,format='PNG')
|
157 |
+
st.markdown("<p style='text-align: center;'>"+
|
158 |
+
f"<img src='data:image/png;base64,{base64.b64encode(imgByteArr.getvalue()).decode()}' class='img-fluid'>"+
|
159 |
+
"</p>", unsafe_allow_html=True)
|
160 |
+
new_score = penalized_logp_standard(new_mol)
|
161 |
+
# st.write('New penalized logP score: %.5f' % (new_score))
|
162 |
+
st.metric('New penalized logP score','%.5f' % (new_score), '%.5f'%(new_score-score))
|
163 |
+
st.metric('Similarity','%.5f' % (sim))
|
164 |
+
# st.write('Caching ZINC20 if necessary...')
|
165 |
+
with st.spinner("Caching ZINC20 if necessary..."):
|
166 |
+
if buy(new_smiles, catalog='zinc20',canonicalize=True):
|
167 |
+
st.write('This molecule exists.')
|
168 |
+
st.markdown("<h3 style='text-align: center; color: cyan;'><b>This molecule exists.</h3>",unsafe_allow_html=True)
|
169 |
+
else:
|
170 |
+
# st.write('THIS MOLECULE DOES NOT EXIST!')
|
171 |
+
st.markdown("<h3 style='text-align: center; color: lightgreen;'>THIS MOLECULE DOES NOT EXIST!</h3>",unsafe_allow_html=True)
|
172 |
+
st.markdown("<p style='text-align: center; color: grey;'>Checked using molbloom</p>",unsafe_allow_html=True)
|
173 |
+
elif tabs == 'Optimize batch':
|
174 |
+
st.write('Incoming...')
|
175 |
+
|
176 |
+
|
fast_jtnn/mol_tree.py
CHANGED
@@ -2,7 +2,7 @@ import rdkit
|
|
2 |
import rdkit.Chem as Chem
|
3 |
from chemutils import get_clique_mol, tree_decomp, get_mol, get_smiles, set_atommap, enum_assemble, decode_stereo
|
4 |
from vocab import *
|
5 |
-
import argparse
|
6 |
|
7 |
class MolTreeNode(object):
|
8 |
|
@@ -124,45 +124,45 @@ def data_process_chunk(smiles_list):
|
|
124 |
# print(i, ' / 1584663')
|
125 |
return list(cset)
|
126 |
|
127 |
-
if __name__ == "__main__":
|
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 |
|
|
|
2 |
import rdkit.Chem as Chem
|
3 |
from chemutils import get_clique_mol, tree_decomp, get_mol, get_smiles, set_atommap, enum_assemble, decode_stereo
|
4 |
from vocab import *
|
5 |
+
# import argparse
|
6 |
|
7 |
class MolTreeNode(object):
|
8 |
|
|
|
124 |
# print(i, ' / 1584663')
|
125 |
return list(cset)
|
126 |
|
127 |
+
# if __name__ == "__main__":
|
128 |
+
# import sys
|
129 |
+
# lg = rdkit.RDLogger.logger()
|
130 |
+
# lg.setLevel(rdkit.RDLogger.CRITICAL)
|
131 |
|
132 |
+
# i = 0
|
133 |
|
134 |
+
# import os
|
135 |
+
# from joblib import Parallel,delayed
|
136 |
+
# from tqdm import tqdm
|
137 |
+
# parser = argparse.ArgumentParser()
|
138 |
+
# parser.add_argument('--smiles_path', type=str,required=True)
|
139 |
+
# parser.add_argument('--vocab_path', type=str,required=True)
|
140 |
+
# parser.add_argument('--prop', type=bool,default=False)
|
141 |
+
# parser.add_argument('--ncpu', default=8,type=int)
|
142 |
+
# args = parser.parse_args()
|
143 |
+
|
144 |
+
# if args.prop:
|
145 |
+
# import pandas as pd
|
146 |
+
# smiles_list = pd.read_csv(args.smiles_path,usecols=['SMILES'])
|
147 |
+
# smiles_list = list(smiles_list.SMILES)
|
148 |
+
# else:
|
149 |
+
# with open(args.smiles_path,'r') as f:
|
150 |
+
# smiles_list = [line.split()[0] for line in f]
|
151 |
+
# print('Total smiles = ',len(smiles_list))
|
152 |
+
|
153 |
+
# # moses: 1584663
|
154 |
|
155 |
+
# chunk_size = 10000
|
156 |
+
# vocab_set_list = Parallel(n_jobs=args.ncpu)(
|
157 |
+
# delayed(data_process_chunk)(smiles_list[start: start + chunk_size])
|
158 |
+
# for start in tqdm(range(0, len(smiles_list), chunk_size))
|
159 |
+
# )
|
160 |
+
# vocab_list =[]
|
161 |
+
# for set_list in vocab_set_list:
|
162 |
+
# vocab_list.extend(set_list)
|
163 |
+
|
164 |
+
# cset = set(vocab_list)
|
165 |
+
# with open(args.vocab_path,'w') as f:
|
166 |
+
# for x in cset:
|
167 |
+
# f.write(''.join([x,'\n']))
|
168 |
|
requirements.txt
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
rdkit
|
2 |
numpy
|
3 |
torch
|
4 |
-
argparse
|
5 |
tqdm
|
6 |
networkx
|
7 |
scipy
|
8 |
-
molbloom
|
|
|
|
|
|
1 |
rdkit
|
2 |
numpy
|
3 |
torch
|
|
|
4 |
tqdm
|
5 |
networkx
|
6 |
scipy
|
7 |
+
molbloom
|
8 |
+
st_on_hover_tabs
|
9 |
+
stqdm
|
style.css
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
section[data-testid='stSidebar'] {
|
2 |
+
background-color: #111;
|
3 |
+
min-width:unset !important;
|
4 |
+
width: unset !important;
|
5 |
+
flex-shrink: unset !important;
|
6 |
+
|
7 |
+
}
|
8 |
+
|
9 |
+
button[kind="header"] {
|
10 |
+
background-color: transparent;
|
11 |
+
color:rgb(180, 167, 141)
|
12 |
+
}
|
13 |
+
|
14 |
+
@media(hover){
|
15 |
+
/* header element to be removed */
|
16 |
+
header[data-testid="stHeader"] {
|
17 |
+
display:none;
|
18 |
+
}
|
19 |
+
|
20 |
+
/* The navigation menu specs and size */
|
21 |
+
section[data-testid='stSidebar'] > div {
|
22 |
+
height: 100%;
|
23 |
+
width: 95px;
|
24 |
+
position: relative;
|
25 |
+
z-index: 1;
|
26 |
+
top: 0;
|
27 |
+
left: 0;
|
28 |
+
background-color: #111;
|
29 |
+
overflow-x: hidden;
|
30 |
+
transition: 0.5s ease;
|
31 |
+
padding-top: 60px;
|
32 |
+
white-space: nowrap;
|
33 |
+
}
|
34 |
+
|
35 |
+
/* The navigation menu open and close on hover and size */
|
36 |
+
/* section[data-testid='stSidebar'] > div {
|
37 |
+
height: 100%;
|
38 |
+
width: 75px; /* Put some width to hover on. */
|
39 |
+
/* }
|
40 |
+
|
41 |
+
/* ON HOVER */
|
42 |
+
section[data-testid='stSidebar'] > div:hover{
|
43 |
+
width: 300px;
|
44 |
+
}
|
45 |
+
|
46 |
+
/* The button on the streamlit navigation menu - hidden */
|
47 |
+
/* button[kind="header"] {
|
48 |
+
display: none;
|
49 |
+
} */
|
50 |
+
}
|
51 |
+
|
52 |
+
@media(max-width: 272px){
|
53 |
+
|
54 |
+
section[data-testid='stSidebar'] > div {
|
55 |
+
width:15rem;
|
56 |
+
}
|
57 |
+
}
|
58 |
+
|