mohamed20003 commited on
Commit
0e3e8c8
Β·
verified Β·
1 Parent(s): 2749f4a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # βœ… dependencies: gradio, rdkit-pypi, pandas, matplotlib, openpyxl
2
+ import gradio as gr
3
+ import pandas as pd
4
+ from rdkit import Chem
5
+ from rdkit.Chem import Descriptors
6
+ import matplotlib.pyplot as plt
7
+ import io
8
+ import base64
9
+
10
+ def get_admet(smiles):
11
+ mol = Chem.MolFromSmiles(smiles)
12
+ if not mol:
13
+ return pd.Series([None]*7, index=["MW", "LogP", "TPSA", "HBA", "HBD", "RotB", "Lipinski"])
14
+ mw = Descriptors.MolWt(mol)
15
+ logp = Descriptors.MolLogP(mol)
16
+ tpsa = Descriptors.TPSA(mol)
17
+ hba = Descriptors.NumHAcceptors(mol)
18
+ hbd = Descriptors.NumHDonors(mol)
19
+ rotb = Descriptors.NumRotatableBonds(mol)
20
+ lipinski = "βœ…" if (mw <= 500 and logp <= 5 and hba <= 10 and hbd <= 5) else "❌"
21
+ return pd.Series([mw, logp, tpsa, hba, hbd, rotb, lipinski],
22
+ index=["MW", "LogP", "TPSA", "HBA", "HBD", "RotB", "Lipinski"])
23
+
24
+ def analyze_admet(file):
25
+ smiles_list = [line.strip() for line in file.read().decode().splitlines() if line.strip()]
26
+ df = pd.DataFrame({"SMILES": smiles_list})
27
+ admet_df = df["SMILES"].apply(get_admet)
28
+ final_df = pd.concat([df, admet_df], axis=1)
29
+
30
+ # Save Excel to buffer
31
+ excel_buffer = io.BytesIO()
32
+ final_df.to_excel(excel_buffer, index=False)
33
+ excel_buffer.seek(0)
34
+ excel_data = base64.b64encode(excel_buffer.read()).decode()
35
+
36
+ # Create and save plot to buffer
37
+ plt.figure(figsize=(12, 6))
38
+ plot_df = final_df.dropna(subset=["LogP"])
39
+ plt.barh(plot_df["SMILES"], plot_df["LogP"], color='skyblue')
40
+ plt.xlabel("LogP")
41
+ plt.title("LogP Values per SMILES")
42
+ plt.gca().invert_yaxis()
43
+ plt.tight_layout()
44
+ img_buffer = io.BytesIO()
45
+ plt.savefig(img_buffer, format='png')
46
+ img_buffer.seek(0)
47
+
48
+ return (
49
+ final_df.to_markdown(index=False),
50
+ f"data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{excel_data}",
51
+ img_buffer
52
+ )
53
+
54
+ # Gradio interface
55
+ with gr.Blocks() as demo:
56
+ gr.Markdown("## πŸ§ͺ ADMET Analysis from SMILES File")
57
+ with gr.Row():
58
+ smiles_file = gr.File(label="πŸ“„ Upload SMILES .txt File")
59
+ run_btn = gr.Button("πŸš€ Analyze")
60
+ result = gr.Textbox(label="πŸ“Š Results Table (Markdown)", lines=20)
61
+ download_excel = gr.File(label="⬇️ Download Excel")
62
+ image_output = gr.Image(label="πŸ“ˆ LogP Plot")
63
+
64
+ run_btn.click(fn=analyze_admet, inputs=smiles_file, outputs=[result, download_excel, image_output])
65
+
66
+ demo.launch()