File size: 3,240 Bytes
c32f5ba
0e3e8c8
 
6e7f1b9
0e3e8c8
 
c32f5ba
ebff2fb
0e3e8c8
5001317
0e3e8c8
 
 
 
 
 
 
 
 
 
 
 
 
 
ebff2fb
6501c66
6e7f1b9
ebff2fb
 
 
 
 
6e7f1b9
 
 
 
0e3e8c8
ebff2fb
c32f5ba
 
 
 
0e3e8c8
ebff2fb
6e7f1b9
c32f5ba
 
6e7f1b9
c32f5ba
6e7f1b9
 
c32f5ba
 
 
 
6e7f1b9
0e3e8c8
ebff2fb
c32f5ba
 
 
 
 
 
 
 
0e3e8c8
6e7f1b9
c32f5ba
6e7f1b9
c32f5ba
0e3e8c8
ebff2fb
 
0e3e8c8
5001317
c32f5ba
6e7f1b9
c32f5ba
 
 
 
0e3e8c8
ebff2fb
0e3e8c8
 
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
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()