admet / app.py
mohamed20003's picture
Update app.py
ebff2fb verified
raw
history blame
3.24 kB
import gradio as gr
from rdkit import Chem
from rdkit.Chem import Descriptors
import pandas as pd
import matplotlib.pyplot as plt
import io
import base64
import os
# دالة لحساب خصائص ADMET
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_smiles(file):
try:
# قراءة المحتوى باستخدام os
file_path = file.name if hasattr(file, "name") else file
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
smiles_list = [line.strip() for line in content.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)
# Excel file
excel_buffer = io.BytesIO()
final_df.to_excel(excel_buffer, index=False, engine="openpyxl")
excel_data = excel_buffer.getvalue()
excel_b64 = base64.b64encode(excel_data).decode()
# Plot image
plot_df = final_df.dropna(subset=["LogP"])
plt.figure(figsize=(10, 6))
plt.barh(plot_df["SMILES"], plot_df["LogP"], color="skyblue")
plt.xlabel("LogP (Lipophilicity)")
plt.title("LogP per SMILES")
plt.gca().invert_yaxis()
plt.tight_layout()
img_buffer = io.BytesIO()
plt.savefig(img_buffer, format="png")
img_buffer.seek(0)
img_b64 = base64.b64encode(img_buffer.read()).decode()
plt.close()
# Markdown table
table_md = final_df.to_markdown(index=False)
return (
"✅ التحليل ناجح!",
table_md,
f'<a download="ADMET_Analysis.xlsx" href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{excel_b64}">⬇️ تحميل ملف Excel</a>',
f'<img src="data:image/png;base64,{img_b64}" width="600"/>'
)
except Exception as e:
return f"❌ خطأ أثناء المعالجة: {str(e)}", "", "", ""
# واجهة Gradio
with gr.Blocks() as demo:
gr.Markdown("## 🧪 تحليل ADMET من ملف SMILES (.txt)")
with gr.Row():
smiles_file = gr.File(label="📄 ارفع ملف .txt يحتوي على SMILES", file_types=[".txt"])
run_btn = gr.Button("🚀 تحليل ADMET")
status = gr.Textbox(label="📢 الحالة")
table = gr.Textbox(label="📊 جدول النتائج (Markdown)", lines=12)
download = gr.HTML()
image = gr.HTML()
run_btn.click(fn=analyze_smiles, inputs=[smiles_file], outputs=[status, table, download, image])
demo.launch()