from pathlib import Path
import torch
from st_on_hover_tabs import on_hover_tabs
import streamlit as st
st.set_page_config(layout="wide")
import sys, os
import rdkit
import rdkit.Chem as Chem
from rdkit.Chem.Draw import MolToImage
from rdkit.Chem import Descriptors
import sascorer
import networkx as nx
from stqdm import stqdm
import base64, io
os.environ['KMP_DUPLICATE_LIB_OK']='True'
sys.path.append('%s/fast_jtnn/' % os.path.dirname(os.path.realpath(__file__)))
from mol_tree import Vocab, MolTree
from jtprop_vae import JTPropVAE
from molbloom import buy
css='''
[data-testid="metric-container"] {
width: fit-content;
margin: auto;
}
[data-testid="metric-container"] > div {
width: fit-content;
margin: auto;
}
[data-testid="metric-container"] label {
width: fit-content;
margin: auto;
}
'''
st.markdown(f'',unsafe_allow_html=True)
def img_to_bytes(img_path):
img_bytes = Path(img_path).read_bytes()
encoded = base64.b64encode(img_bytes).decode()
return encoded
def img_to_html(img_path):
img_html = "".format(
img_to_bytes(img_path)
)
return img_html
def penalized_logp_standard(mol):
logP_mean = 2.4399606244103639873799239
logP_std = 0.9293197802518905481505840
SA_mean = -2.4485512208785431553792478
SA_std = 0.4603110476923852334429910
cycle_mean = -0.0307270378623088931402396
cycle_std = 0.2163675785228087178335699
log_p = Descriptors.MolLogP(mol)
SA = -sascorer.calculateScore(mol)
# cycle score
cycle_list = nx.cycle_basis(nx.Graph(Chem.rdmolops.GetAdjacencyMatrix(mol)))
if len(cycle_list) == 0:
cycle_length = 0
else:
cycle_length = max([len(j) for j in cycle_list])
if cycle_length <= 6:
cycle_length = 0
else:
cycle_length = cycle_length - 6
cycle_score = -cycle_length
# print(logP_mean)
standardized_log_p = (log_p - logP_mean) / logP_std
standardized_SA = (SA - SA_mean) / SA_std
standardized_cycle = (cycle_score - cycle_mean) / cycle_std
return standardized_log_p + standardized_SA + standardized_cycle
lg = rdkit.RDLogger.logger()
lg.setLevel(rdkit.RDLogger.CRITICAL)
st.markdown("
"+ img_to_html('about.png')+ "
", unsafe_allow_html=True) elif tabs == 'Optimize a molecule': st.markdown("SMILES is invalid. Please enter a valid SMILES.
",unsafe_allow_html=True) else: score = penalized_logp_standard(mol) # with st.columns(3)[1]: # st.markdown("",unsafe_allow_html=True) imgByteArr = io.BytesIO() MolToImage(mol,size=(400,400)).save(imgByteArr,format='PNG') st.markdown(""+
f""+
"
Cannot optimize! Please choose another setting.
",unsafe_allow_html=True) else: st.markdown("New SMILES",unsafe_allow_html=True) st.code(new_smiles) new_mol = Chem.MolFromSmiles(new_smiles) if new_mol is None: st.markdown("New SMILES is invalid! Please choose another setting.
",unsafe_allow_html=True) # st.write('New SMILES is invalid.') else: # st.write('New SMILES molecule:') imgByteArr = io.BytesIO() MolToImage(new_mol,size=(400,400)).save(imgByteArr,format='PNG') st.markdown(""+
f""+
"
Checked using molbloom
",unsafe_allow_html=True) elif tabs == 'Optimize batch': st.write('Incoming...')