Spaces:
Sleeping
Sleeping
# β dependencies: gradio, rdkit-pypi, pandas, matplotlib, openpyxl | |
import gradio as gr | |
import pandas as pd | |
from rdkit import Chem | |
from rdkit.Chem import Descriptors | |
import matplotlib.pyplot as plt | |
import io | |
import base64 | |
def get_admet(smiles): | |
mol = Chem.MolFromSmiles(smiles) | |
if not mol: | |
return pd.Series([None]*7, index=["MW", "LogP", "TPSA", "HBA", "HBD", "RotB", "Lipinski"]) | |
mw = Descriptors.MolWt(mol) | |
logp = Descriptors.MolLogP(mol) | |
tpsa = Descriptors.TPSA(mol) | |
hba = Descriptors.NumHAcceptors(mol) | |
hbd = Descriptors.NumHDonors(mol) | |
rotb = Descriptors.NumRotatableBonds(mol) | |
lipinski = "β " if (mw <= 500 and logp <= 5 and hba <= 10 and hbd <= 5) else "β" | |
return pd.Series([mw, logp, tpsa, hba, hbd, rotb, lipinski], | |
index=["MW", "LogP", "TPSA", "HBA", "HBD", "RotB", "Lipinski"]) | |
def analyze_admet(file): | |
smiles_list = [line.strip() for line in file.read().decode().splitlines() if line.strip()] | |
df = pd.DataFrame({"SMILES": smiles_list}) | |
admet_df = df["SMILES"].apply(get_admet) | |
final_df = pd.concat([df, admet_df], axis=1) | |
# Save Excel to buffer | |
excel_buffer = io.BytesIO() | |
final_df.to_excel(excel_buffer, index=False) | |
excel_buffer.seek(0) | |
excel_data = base64.b64encode(excel_buffer.read()).decode() | |
# Create and save plot to buffer | |
plt.figure(figsize=(12, 6)) | |
plot_df = final_df.dropna(subset=["LogP"]) | |
plt.barh(plot_df["SMILES"], plot_df["LogP"], color='skyblue') | |
plt.xlabel("LogP") | |
plt.title("LogP Values per SMILES") | |
plt.gca().invert_yaxis() | |
plt.tight_layout() | |
img_buffer = io.BytesIO() | |
plt.savefig(img_buffer, format='png') | |
img_buffer.seek(0) | |
return ( | |
final_df.to_markdown(index=False), | |
f"data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{excel_data}", | |
img_buffer | |
) | |
# Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("## π§ͺ ADMET Analysis from SMILES File") | |
with gr.Row(): | |
smiles_file = gr.File(label="π Upload SMILES .txt File") | |
run_btn = gr.Button("π Analyze") | |
result = gr.Textbox(label="π Results Table (Markdown)", lines=20) | |
download_excel = gr.File(label="β¬οΈ Download Excel") | |
image_output = gr.Image(label="π LogP Plot") | |
run_btn.click(fn=analyze_admet, inputs=smiles_file, outputs=[result, download_excel, image_output]) | |
demo.launch() | |