File size: 2,442 Bytes
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
# βœ… 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()