File size: 9,959 Bytes
b1ec78c |
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 |
import streamlit as st
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from rdkit import Chem
from streamlit_ketcher import st_ketcher
from io import StringIO
from openadmet_models.models.gradient_boosting.lgbm import LGBMRegressorModel
from openadmet_models.features.combine import FeatureConcatenator
from openadmet_models.features.molfeat_properties import DescriptorFeaturizer
from openadmet_models.features.molfeat_fingerprint import FingerprintFeaturizer
def _is_valid_smiles(smi):
if smi is None or smi == "":
return False
try:
m = Chem.MolFromSmiles(smi)
if m is None:
return False
else:
return True
except:
return False
def sdf_str_to_rdkit_mol(sdf):
from io import BytesIO
bio = BytesIO(sdf.encode())
suppl = Chem.ForwardSDMolSupplier(bio, removeHs=False)
mols = [mol for mol in suppl if mol is not None]
return mols
@st.cache_data
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode("utf-8")
def get_model(path, target, model_type):
model_path = os.path.join(path, f"{model_type}/{target.lower()}_model.json")
model_file = os.path.join(path, f"{model_type}/{target.lower()}_model.pkl")
print(model_path, model_file)
if not os.path.exists(model_path) or not os.path.exists(model_file):
return None
model = LGBMRegressorModel.deserialize(model_path, model_file)
featurizer = FeatureConcatenator(featurizers=[FingerprintFeaturizer(fp_type="ecfp:4"), DescriptorFeaturizer(descr_type="mordred")])
return model, featurizer
# Set the title of the Streamlit app
st.title("OpenADMET Streamlit DEMO")
# Set the title of the Streamlit app
st.title("OpenADMET Streamlit DEMO")
st.markdown("## Background")
st.markdown(
"**The [OpenADMET](https://openadmet.org) initiative provides a suite of open-source machine learning models to predict ADMET (Absorption, Distribution, Metabolism, Excretion, and Toxicity) properties, facilitating drug discovery and development.**"
)
st.markdown(
"This web app enables researchers and scientists to leverage OpenADMET’s models without needing to write or run code, making predictive analytics more accessible."
)
st.markdown("---")
st.markdown("## Input :clipboard:")
input = st.selectbox(
"How would you like to enter your input?",
["Upload a CSV file", "Draw a molecule", "Enter SMILES", "Upload an SDF file"],
key="input",
)
multismiles = False
if input == "Draw a molecule":
smiles = st_ketcher(None)
if _is_valid_smiles(smiles):
st.success("Valid molecule", icon="✅")
else:
st.error("Invalid molecule", icon="🚨")
st.stop()
smiles = [smiles]
queried_df = pd.DataFrame(smiles, columns=["SMILES"])
smiles_column_name = "SMILES"
smiles_column = queried_df[smiles_column_name]
elif input == "Enter SMILES":
smiles = st.text_input("Enter a SMILES string", key="smiles_user_input")
if _is_valid_smiles(smiles):
st.success("Valid SMILES string", icon="✅")
else:
st.error("Invalid SMILES string", icon="🚨")
st.stop()
smiles = [smiles]
queried_df = pd.DataFrame(smiles, columns=["SMILES"])
smiles_column_name = "SMILES"
smiles_column = queried_df[smiles_column_name]
elif input == "Upload a CSV file":
# Create a file uploader for CSV files
uploaded_file = st.file_uploader(
"Choose a CSV file to upload your predictions to", type="csv", key="csv_file"
)
# If a file is uploaded, parse it into a DataFrame
if uploaded_file is not None:
queried_df = pd.read_csv(uploaded_file)
else:
st.stop()
# Select a column from the DataFrame
smiles_column_name = st.selectbox("Select a SMILES column", queried_df.columns, key="df_smiles_column")
multismiles = True
smiles_column = queried_df[smiles_column_name]
# check if the smiles are valid
valid_smiles = [_is_valid_smiles(smi) for smi in smiles_column]
if not all(valid_smiles):
st.error(
"Some of the SMILES strings are invalid, please check the input", icon="🚨"
)
st.stop()
st.success(
f"All SMILES strings are valid (n={len(valid_smiles)}), proceeding with prediction",
icon="✅",
)
elif input == "Upload an SDF file":
# Create a file uploader for SDF files
uploaded_file = st.file_uploader(
"Choose a SDF file to upload your predictions to", type="sdf"
)
# read with rdkit
if uploaded_file is not None:
# To convert to a string based IO:
try:
stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
# To read file as string:
string_data = stringio.read()
mols = sdf_str_to_rdkit_mol(string_data)
smiles = [Chem.MolToSmiles(m) for m in mols]
queried_df = pd.DataFrame(smiles, columns=["SMILES"])
except:
st.error("Error reading the SDF file, please check the input", icon="🚨")
st.stop()
else:
st.error("No file uploaded", icon="🚨")
st.stop()
st.success(
f"All molecule entries are valid (n={len(queried_df)}), proceeding with prediction",
icon="✅",
)
smiles_column_name = "SMILES"
smiles_column = queried_df[smiles_column_name]
multismiles = True
st.markdown("## Model parameters :nut_and_bolt:")
targets = ['CYP3A4', 'CYP2D6', 'CYP2C9']
models = {"ecfp:4 Mordred LGBM":"ecfp4_mordred_lgbm", "ChemProp":"chemprop"}
models_inversed = {v: k for k, v in models.items()}
model_names = list(models.keys())
endpoints = ["pIC50"]
# Select a target value from the preset list
target_value = st.selectbox("Select a biological target ", targets, key="target")
# endpoints
# Select a target value from the preset list
endpoint_value = st.selectbox("Select a property ", endpoints, key="endpoint")
model_value = st.selectbox("Select a model type ", model_names, key="model")
if target_value != "CYP3A4":
st.write("Only CYP3A4 is currently supported")
st.stop()
if endpoint_value != "pIC50":
st.write("Only pIC50 is currently supported")
st.stop()
if model_value != "ecfp:4 Mordred LGBM":
st.write("Only ecfp:4 Mordred LGBM is currently supported")
st.stop()
model, featurizer = get_model("./models", target_value, models[model_value])
if model is None:
st.write(f"No model found for {target_value} {endpoint_value}")
st.stop()
# retry with a different target or endpoint
st.markdown("## Prediction 🚀")
st.write(
f"Predicting **{target_value} {endpoint_value}** using model:\n\n `{model_value}`"
)
# featurize the smiles
X, _ = featurizer.featurize(smiles_column)
# predict the properties
preds = model.predict(X)
# not implemented yet
err = None
pred_column_name = f"{target_value}_computed-{endpoint_value}"
unc_column_name = f"{target_value}_computed-{endpoint_value}_uncertainty"
queried_df[pred_column_name] = preds
queried_df[unc_column_name] = err
st.markdown("---")
if multismiles:
# plot the predictions and errors
# Histogram first
fig, ax = plt.subplots()
sorted_df = queried_df.sort_values(by=pred_column_name)
n_bins = int(len(sorted_df[pred_column_name]) / 10)
if n_bins < 5: # makes the histogram slightly more interpretable with low data
n_bins = 5
ax.hist(sorted_df[pred_column_name], bins=n_bins)
ax.set_ylabel("Count")
ax.set_xlabel(f"Computed {endpoint_value}")
ax.set_title(f"Histogram of computed {endpoint_value} for target: {target_value}")
st.pyplot(fig)
# then a barplot
fig, ax = plt.subplots()
ax.bar(range(len(sorted_df)), sorted_df[pred_column_name])
ax.set_xticks([])
ax.set_xlabel(f"Query compounds")
ax.set_ylabel(f"Computed {endpoint_value}")
ax.set_title(f"Barplot of computed {endpoint_value} for target: {target_value}")
st.pyplot(fig)
# if endpoint_value == "pIC50":
# from rdkit.Chem.Descriptors import MolWt
# import seaborn as sns
# # then a scatterplot of uncertainty vs MW
# queried_df["MW"] = [
# MolWt(Chem.MolFromSmiles(smi)) for smi in sorted_df[smiles_column_name]
# ]
# fig, ax = plt.subplots()
# ax = sns.scatterplot(
# x="MW",
# y=pred_column_name,
# hue=unc_column_name,
# palette="coolwarm",
# data=queried_df,
# )
# norm = plt.Normalize(
# queried_df[unc_column_name].min(), queried_df[unc_column_name].max()
# )
# sm = plt.cm.ScalarMappable(cmap="coolwarm", norm=norm)
# sm.set_array([])
# # Remove the legend and add a colorbar
# cbar = ax.figure.colorbar(sm, ax=ax)
# ax.annotate(
# f"Computed {endpoint_value} uncertainty",
# xy=(1.2, 0.3),
# xycoords="axes fraction",
# rotation=270,
# )
# ax.set_title(
# f"Scatterplot of predicted {endpoint_value} versus MW\ntarget: {target_value}"
# )
# ax.set_xlabel(f"Molecular weight (Da)")
# ax.set_ylabel(f"Computed {endpoint_value}")
# st.pyplot(fig)
else:
# just print the prediction
preds = queried_df[pred_column_name].values[0]
smiles = queried_df["SMILES"].values[0]
if err:
err = queried_df[unc_column_name].values[0]
errstr = f"± {err:.2f}"
else:
errstr = ""
st.markdown(
f"Predicted {target_value} {endpoint_value} for {smiles} is {preds:.2f} {errstr}."
)
# allow the user to download the predictions
csv = convert_df(queried_df)
st.download_button(
label="Download data as CSV",
data=csv,
file_name=f"predictions_{model_value}.csv",
mime="text/csv",
) |