Spaces:
Sleeping
Sleeping
Trương Gia Bảo
commited on
Commit
·
c274248
1
Parent(s):
fa1a9a6
Update UI
Browse files
app.py
CHANGED
@@ -345,31 +345,46 @@ def Optimize_a_molecule():
|
|
345 |
# oab_sel_container = st.empty()
|
346 |
if 'checked_single' not in st.session_state:
|
347 |
st.session_state.checked_single = 'NO'
|
348 |
-
if 'mode' not in st.session_state:
|
349 |
-
|
350 |
if 'single_optimized' not in st.session_state:
|
351 |
st.session_state.single_optimized = False
|
352 |
if 'smiles_checked' not in st.session_state:
|
353 |
st.session_state.smiles_checked = False
|
354 |
# with oab_sel_container.container():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
oam_sel_col = st.columns([3,7])
|
356 |
-
|
357 |
-
|
358 |
-
smiles = oam_sel_col[1].text_input('Enter a SMILES string (max 200 chars):',max_chars=200
|
359 |
-
|
360 |
-
|
361 |
-
smiles = '
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
373 |
|
374 |
if 'optim_single_butt' not in locals():
|
375 |
optim_single_butt = False
|
@@ -392,10 +407,16 @@ def Optimize_a_molecule():
|
|
392 |
set_step(0)
|
393 |
oam_sidebar(0)
|
394 |
elif st.session_state.checked_single == 'YES':
|
|
|
|
|
|
|
|
|
|
|
395 |
st.markdown("<b>Canonicalized SMILES</b>",unsafe_allow_html=True)
|
396 |
-
st.
|
|
|
397 |
st.markdown("<p style='text-align: center; color: mediumseagreen'>MOSES filters passed successfully.</p>",unsafe_allow_html=True)
|
398 |
-
mol = Chem.MolFromSmiles(st.session_state.
|
399 |
imgByteArr = io.BytesIO()
|
400 |
MolToImage(mol,size=(400,400)).save(imgByteArr,format='PNG')
|
401 |
st.markdown("<p style='text-align: center;'>"+
|
@@ -422,7 +443,7 @@ def Optimize_a_molecule():
|
|
422 |
optim_single_butt = st.form_submit_button("Optimize")
|
423 |
else:
|
424 |
st.markdown("<b>Canonicalized SMILES</b>",unsafe_allow_html=True)
|
425 |
-
st.code(st.session_state.
|
426 |
if st.session_state.checked_single == 'NoVocab':
|
427 |
st.markdown("<p style='text-align: center; color: red;'><b>The molecule contains unavailable vocab(s). Please use another molecule.</b></p>",unsafe_allow_html=True)
|
428 |
else:
|
@@ -446,7 +467,7 @@ def Optimize_a_molecule():
|
|
446 |
st.markdown('Operation in progress. Please wait...')
|
447 |
render_animation()
|
448 |
model = load_model()
|
449 |
-
st.session_state.new_smiles,st.session_state.sim = optim_single(st.session_state.
|
450 |
st.session_state.single_optimized = True
|
451 |
ani_con.empty()
|
452 |
# sidebar_con.empty()
|
@@ -498,10 +519,11 @@ def Optimize_a_molecule():
|
|
498 |
else: optim_single_con.empty()
|
499 |
else: optim_single_con.empty()
|
500 |
|
|
|
501 |
def check_single(smiles):
|
502 |
# render_view()
|
503 |
st.session_state.smiles_selected = True
|
504 |
-
st.session_state.smiles = smiles
|
505 |
# check_single_con = st.empty()
|
506 |
|
507 |
# optim = False
|
@@ -513,7 +535,7 @@ def check_single(smiles):
|
|
513 |
if mol is None:
|
514 |
st.session_state.checked_single = 'MolError'
|
515 |
else:
|
516 |
-
st.session_state.
|
517 |
st.session_state.logp,st.session_state.sa,st.session_state.cycle,st.session_state.pen_p = penalized_logp_standard(mol)
|
518 |
moses_passed = mol_passes_filters_custom(mol)
|
519 |
st.session_state.checked_single = moses_passed
|
@@ -610,7 +632,7 @@ def Optimize_a_batch():
|
|
610 |
with check_batch_con.container():
|
611 |
passed_num = st.session_state['df'][st.session_state['df'].checked != 'invalid'].shape[0]
|
612 |
st.markdown('Number of passed SMILES: '+str(passed_num))
|
613 |
-
st.dataframe(st.session_state['df'],use_container_width=True)
|
614 |
if passed_num == 0:
|
615 |
st.markdown("<p style='text-align: center; color: red;'><b>The uploaded file contains no suitable SMILES string.</b></p>",unsafe_allow_html=True)
|
616 |
st.session_state.batch_left_checked = False
|
@@ -697,9 +719,17 @@ def Optimize_a_batch():
|
|
697 |
optim_batch_con.empty()
|
698 |
|
699 |
|
700 |
-
|
|
|
|
|
|
|
|
|
|
|
701 |
def check_batch(smiles_list):
|
702 |
check = []
|
|
|
|
|
|
|
703 |
for smi in stqdm(smiles_list):
|
704 |
mol = Chem.MolFromSmiles(smi)
|
705 |
if (mol is not None) and (mol_passes_filters_custom(mol) == 'YES'):
|
@@ -713,6 +743,9 @@ def check_batch(smiles_list):
|
|
713 |
def calc_scores(smiles_list):
|
714 |
score_df = pd.concat([st.session_state.df[st.session_state.df.checked != 'invalid'].SMILES,pd.DataFrame({'Checked_SMILES':smiles_list})],axis=1)
|
715 |
scores =[]
|
|
|
|
|
|
|
716 |
for smi in stqdm(smiles_list):
|
717 |
logp,sa,cycle,pen_logp = penalized_logp_standard(Chem.MolFromSmiles(smi))
|
718 |
scores+=[(logp,sa,cycle,pen_logp)]
|
@@ -723,8 +756,22 @@ def calc_scores(smiles_list):
|
|
723 |
st.session_state.scores_calculated = True
|
724 |
return score_df
|
725 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
726 |
def calc_scores_new(result):
|
727 |
new_scores =[]
|
|
|
|
|
|
|
728 |
for new_smiles,sim in stqdm(result):
|
729 |
if new_smiles is None:
|
730 |
new_scores+=[('invalid',-100.0,-100.0,-100.0,-100.0,-100.0)]
|
|
|
345 |
# oab_sel_container = st.empty()
|
346 |
if 'checked_single' not in st.session_state:
|
347 |
st.session_state.checked_single = 'NO'
|
348 |
+
# if 'mode' not in st.session_state:
|
349 |
+
# st.session_state.mode = 0
|
350 |
if 'single_optimized' not in st.session_state:
|
351 |
st.session_state.single_optimized = False
|
352 |
if 'smiles_checked' not in st.session_state:
|
353 |
st.session_state.smiles_checked = False
|
354 |
# with oab_sel_container.container():
|
355 |
+
ls_opt = ['-','Sorafenib','Pazopanib','Sunitinib']
|
356 |
+
sample_mode = {
|
357 |
+
'-':'',
|
358 |
+
'Sorafenib':'CNC(=O)C1=NC=CC(=C1)OC2=CC=C(C=C2)NC(=O)NC3=CC(=C(C=C3)Cl)C(F)(F)F',
|
359 |
+
'Pazopanib':'CC1=C(C=C(C=C1)NC2=NC=CC(=N2)N(C)C3=CC4=NN(C(=C4C=C3)C)C)S(=O)(=O)N',
|
360 |
+
'Sunitinib':'CCN(CC)CCNC(=O)C1=C(NC(=C1C)C=C2C3=C(C=CC(=C3)F)NC2=O)C'
|
361 |
+
}
|
362 |
oam_sel_col = st.columns([3,7])
|
363 |
+
with st.form('sel_smiles'):
|
364 |
+
mode = oam_sel_col[0].selectbox("Select an example",options=ls_opt,on_change=reset_oam_state)
|
365 |
+
smiles = oam_sel_col[1].text_input('Enter a SMILES string (max 200 chars):',sample_mode[mode],max_chars=200,
|
366 |
+
disabled=(mode != '-'))
|
367 |
+
# if mode == '-':
|
368 |
+
# st.session_state.smiles = oam_sel_col[1].text_input('Enter a SMILES string (max 200 chars):',max_chars=200,key='opt_0')
|
369 |
+
# # st.session_state.mode = 0
|
370 |
+
# elif mode == 'Sorafenib':
|
371 |
+
# st.session_state.smiles = 'CNC(=O)C1=NC=CC(=C1)OC2=CC=C(C=C2)NC(=O)NC3=CC(=C(C=C3)Cl)C(F)(F)F'
|
372 |
+
# oam_sel_col[1].text_input('Enter a SMILES string (max 200 chars):','CNC(=O)C1=NC=CC(=C1)OC2=CC=C(C=C2)NC(=O)NC3=CC(=C(C=C3)Cl)C(F)(F)F',max_chars=200,disabled=True,key='opt_1')
|
373 |
+
# # st.session_state.mode = 1
|
374 |
+
# elif mode == 'Pazopanib':
|
375 |
+
# st.session_state.smiles = 'CC1=C(C=C(C=C1)NC2=NC=CC(=N2)N(C)C3=CC4=NN(C(=C4C=C3)C)C)S(=O)(=O)N'
|
376 |
+
# oam_sel_col[1].text_input('Enter a SMILES string (max 200 chars):','CC1=C(C=C(C=C1)NC2=NC=CC(=N2)N(C)C3=CC4=NN(C(=C4C=C3)C)C)S(=O)(=O)N',max_chars=200,disabled=True,key='opt_2')
|
377 |
+
# # st.session_state.mode = 2
|
378 |
+
# elif mode == 'Sunitinib':
|
379 |
+
# st.session_state.smiles = 'CCN(CC)CCNC(=O)C1=C(NC(=C1C)C=C2C3=C(C=CC(=C3)F)NC2=O)C'
|
380 |
+
# oam_sel_col[1].text_input('Enter a SMILES string (max 200 chars):','CCN(CC)CCNC(=O)C1=C(NC(=C1C)C=C2C3=C(C=CC(=C3)F)NC2=O)C',max_chars=200,disabled=True,key='opt_3')
|
381 |
+
# # st.session_state.mode = 3
|
382 |
+
check_single_butt = st.form_submit_button('Check SMILES',use_container_width=True)
|
383 |
+
# st.session_state.smiles = st.session_state['opt_'+str(ls_opt.index(st.session_state.mode))]
|
384 |
+
if check_single_butt:
|
385 |
+
st.session_state.mode = mode
|
386 |
+
st.session_state.smiles = smiles
|
387 |
+
check_single(st.session_state.smiles)
|
388 |
|
389 |
if 'optim_single_butt' not in locals():
|
390 |
optim_single_butt = False
|
|
|
407 |
set_step(0)
|
408 |
oam_sidebar(0)
|
409 |
elif st.session_state.checked_single == 'YES':
|
410 |
+
if st.session_state.mode != '-':
|
411 |
+
st.markdown(f"<h4 style='color:mediumseagreen;'>Using example: <b>{st.session_state.mode}</b></h4>",unsafe_allow_html=True)
|
412 |
+
else:
|
413 |
+
st.markdown(f"<h4>Selected SMILES</h4>",unsafe_allow_html=True)
|
414 |
+
st.code(st.session_state.smiles)
|
415 |
st.markdown("<b>Canonicalized SMILES</b>",unsafe_allow_html=True)
|
416 |
+
st.session_state.canon_smiles = Chem.CanonSmiles(st.session_state.smiles)
|
417 |
+
st.code(st.session_state.canon_smiles)
|
418 |
st.markdown("<p style='text-align: center; color: mediumseagreen'>MOSES filters passed successfully.</p>",unsafe_allow_html=True)
|
419 |
+
mol = Chem.MolFromSmiles(st.session_state.canon_smiles)
|
420 |
imgByteArr = io.BytesIO()
|
421 |
MolToImage(mol,size=(400,400)).save(imgByteArr,format='PNG')
|
422 |
st.markdown("<p style='text-align: center;'>"+
|
|
|
443 |
optim_single_butt = st.form_submit_button("Optimize")
|
444 |
else:
|
445 |
st.markdown("<b>Canonicalized SMILES</b>",unsafe_allow_html=True)
|
446 |
+
st.code(st.session_state.canon_smiles)
|
447 |
if st.session_state.checked_single == 'NoVocab':
|
448 |
st.markdown("<p style='text-align: center; color: red;'><b>The molecule contains unavailable vocab(s). Please use another molecule.</b></p>",unsafe_allow_html=True)
|
449 |
else:
|
|
|
467 |
st.markdown('Operation in progress. Please wait...')
|
468 |
render_animation()
|
469 |
model = load_model()
|
470 |
+
st.session_state.new_smiles,st.session_state.sim = optim_single(st.session_state.canon_smiles,model,st.session_state.lr_s,st.session_state.sim_cutoff_s,st.session_state.n_iter_s)
|
471 |
st.session_state.single_optimized = True
|
472 |
ani_con.empty()
|
473 |
# sidebar_con.empty()
|
|
|
519 |
else: optim_single_con.empty()
|
520 |
else: optim_single_con.empty()
|
521 |
|
522 |
+
|
523 |
def check_single(smiles):
|
524 |
# render_view()
|
525 |
st.session_state.smiles_selected = True
|
526 |
+
# st.session_state.smiles = smiles
|
527 |
# check_single_con = st.empty()
|
528 |
|
529 |
# optim = False
|
|
|
535 |
if mol is None:
|
536 |
st.session_state.checked_single = 'MolError'
|
537 |
else:
|
538 |
+
st.session_state.canon_smiles = Chem.MolToSmiles(mol)
|
539 |
st.session_state.logp,st.session_state.sa,st.session_state.cycle,st.session_state.pen_p = penalized_logp_standard(mol)
|
540 |
moses_passed = mol_passes_filters_custom(mol)
|
541 |
st.session_state.checked_single = moses_passed
|
|
|
632 |
with check_batch_con.container():
|
633 |
passed_num = st.session_state['df'][st.session_state['df'].checked != 'invalid'].shape[0]
|
634 |
st.markdown('Number of passed SMILES: '+str(passed_num))
|
635 |
+
st.dataframe(st.session_state['df'].style.applymap(highlight_result, subset=pd.IndexSlice[:, ['checked']]),use_container_width=True)
|
636 |
if passed_num == 0:
|
637 |
st.markdown("<p style='text-align: center; color: red;'><b>The uploaded file contains no suitable SMILES string.</b></p>",unsafe_allow_html=True)
|
638 |
st.session_state.batch_left_checked = False
|
|
|
719 |
optim_batch_con.empty()
|
720 |
|
721 |
|
722 |
+
def process_check_single(smiles):
|
723 |
+
mol = Chem.MolFromSmiles(smiles)
|
724 |
+
if (mol is not None) and (mol_passes_filters_custom(mol) == 'YES'):
|
725 |
+
return Chem.MolToSmiles(mol)
|
726 |
+
else:
|
727 |
+
return 'invalid'
|
728 |
def check_batch(smiles_list):
|
729 |
check = []
|
730 |
+
# check = Parallel(n_jobs=-1,backend='loky')(
|
731 |
+
# delayed(process_check_single)(smi) for smi in stqdm(smiles_list)
|
732 |
+
# )
|
733 |
for smi in stqdm(smiles_list):
|
734 |
mol = Chem.MolFromSmiles(smi)
|
735 |
if (mol is not None) and (mol_passes_filters_custom(mol) == 'YES'):
|
|
|
743 |
def calc_scores(smiles_list):
|
744 |
score_df = pd.concat([st.session_state.df[st.session_state.df.checked != 'invalid'].SMILES,pd.DataFrame({'Checked_SMILES':smiles_list})],axis=1)
|
745 |
scores =[]
|
746 |
+
# scores = Parallel(n_jobs=-1,backend='loky')(
|
747 |
+
# delayed(penalized_logp_standard)(Chem.MolFromSmiles(smi)) for smi in stqdm(smiles_list)
|
748 |
+
# )
|
749 |
for smi in stqdm(smiles_list):
|
750 |
logp,sa,cycle,pen_logp = penalized_logp_standard(Chem.MolFromSmiles(smi))
|
751 |
scores+=[(logp,sa,cycle,pen_logp)]
|
|
|
756 |
st.session_state.scores_calculated = True
|
757 |
return score_df
|
758 |
|
759 |
+
def process_calc_new_score(new_smiles,sim):
|
760 |
+
if new_smiles is None:
|
761 |
+
return ('invalid',-100.0,-100.0,-100.0,-100.0,-100.0)
|
762 |
+
else:
|
763 |
+
new_mol = Chem.MolFromSmiles(new_smiles)
|
764 |
+
if new_mol is None:
|
765 |
+
return ('invalid',-100.0,-100.0,-100.0,-100.0,-100.0)
|
766 |
+
else:
|
767 |
+
logp,sa,cycle,pen_logp = penalized_logp_standard(new_mol)
|
768 |
+
return (new_smiles,sim,logp,sa,cycle,pen_logp)
|
769 |
+
|
770 |
def calc_scores_new(result):
|
771 |
new_scores =[]
|
772 |
+
# new_scores = Parallel(n_jobs=-1,backend='loky')(
|
773 |
+
# delayed(process_calc_new_score)(new_smiles,sim) for new_smiles,sim in stqdm(result)
|
774 |
+
# )
|
775 |
for new_smiles,sim in stqdm(result):
|
776 |
if new_smiles is None:
|
777 |
new_scores+=[('invalid',-100.0,-100.0,-100.0,-100.0,-100.0)]
|