File size: 4,894 Bytes
9a21c1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""DPP-4 Kd Predictor — DPP-4 Kd LTN.

Transfer-learned LTN for DPP-4 binding affinity (Kd): stage 1 pre-trained
on the all-target Kd corpus, stage 2 fine-tuned on DPP-4 Ki. On the fully
held-out external set of 15 DPP-4 Kd compounds (never trained on):
RMSE 1.604 · MAE 1.276 · class accuracy 0.867 (scores in pKd).

Weights (ltn_stage2.pt) and the DPP-4 ESM-2 protein vector are bundled in
this Space; the standalone model repo is Delower/dpp4-kd-ltn.
"""
import os

import gradio as gr
import pandas as pd
import spaces

from kd_inference import load_esm_vector, load_model, predict_smiles_list

NET = load_model()
ESM_VEC = load_esm_vector()


@spaces.GPU
def predict_frame(df, smiles_col):
    smiles = df[smiles_col].astype(str).tolist()
    rows = predict_smiles_list(NET, ESM_VEC, smiles)
    out = df.copy()
    out["predicted_pKd"] = [r["predicted_pKd"] for r in rows]
    out["predicted_Kd_nM"] = [r["predicted_Kd_nM"] for r in rows]
    out["predicted_deltaG_kcal_mol"] = [r["predicted_deltaG_kcal_mol"] for r in rows]
    out["predicted_class"] = [r["predicted_class"] for r in rows]
    out["status"] = [r["status"] for r in rows]
    return out


def predict_single(smiles):
    smiles = (smiles or "").strip()
    if not smiles:
        return "—", "—", "—", "—"
    df = pd.DataFrame({"smiles": [smiles]})
    out = predict_frame(df, "smiles")
    r = out.iloc[0]
    if r["status"] != "ok":
        raise gr.Error("Invalid SMILES — RDKit could not parse that structure. "
                       "Please check the string and try again.")
    return (f"{r['predicted_pKd']:.3f}",
            f"{r['predicted_Kd_nM']:.2f}",
            f"{r['predicted_deltaG_kcal_mol']:.2f}",
            f"{r['predicted_class']}")


def predict_batch(file):
    if file is None:
        return None, None
    df = pd.read_csv(file.name)
    col = next((c for c in df.columns
                if c.lower() in ("smiles", "canonical_smiles", "smile")), None)
    if col is None:
        raise gr.Error("CSV must contain a 'smiles' column.")
    out = predict_frame(df, col)
    path = "/tmp/kd_batch_predictions.csv"
    out.to_csv(path, index=False)
    return out, path


ABOUT = """
## Model
**DPP-4 Kd LTN** — a tuned Logic Tensor Network (MLP trunk 768-384-192 with
regression + band-classification heads trained under LTN axioms), the best
model of the DPP-4 Kd transfer suite.

- Target: human DPP-4 (UniProt **P27487** / ChEMBL **CHEMBL284**)
- Features: Morgan fingerprint (r=2, 2048 bits) + ESM-2 650M embedding of
  DPP-4 (1280) = 3,328
- Transfer learning: **stage 1** pre-train on the all-target Kd corpus
  (86,569 compound-target rows) → **stage 2** fine-tune on DPP-4 Ki
  (555 compounds; Ki used as the pKd-scale fine-tune label)
- Outputs: predicted **pKd**, **Kd (nM)** = 10^(9 − pKd),
  **ΔG (kcal/mol)** = −1.3642 × pKd, and potency class
  (potent pKd ≥ 7 · moderate 6–7 · weak < 6)

## Performance — external test, 15 DPP-4 Kd compounds (never trained on)
| Metric | DPP-4 Kd LTN |
|---|---|
| RMSE (pKd) | 1.604 |
| MAE (pKd) | 1.276 |
| Class accuracy | 0.867 |

Honest caveat: this is a Ki→Kd transfer model evaluated on only 15
compounds; an RMSE of ~1.6 log units means predictions are a ranking /
triage signal, not a substitute for a measured Kd. The model systematically
under-predicts the very tightest binders (e.g. linagliptin, experimental
pKd 11.18, is predicted at 8.26).

Part of an open UAB SPARC series: DPP-4 pIC50 predictor
(Delower/dpp4-pic50-webapp), EGFR pIC50 predictor
(Delower/egfr-pic50-webapp), MolGemma DPP-4 generator
(Delower/dpp4-molgemma-generator).
"""

with gr.Blocks(title="DPP-4 Kd Predictor") as demo:
    gr.Markdown("# DPP-4 Kd Predictor — DPP-4 Kd LTN\n"
                "Stage-1 all-target Kd pre-train → stage-2 DPP-4 Ki fine-tune · "
                "external-15 RMSE 1.604 (pKd)")
    with gr.Tab("Single molecule"):
        smi = gr.Textbox(label="SMILES", placeholder="e.g. sitagliptin: N[C@@H](CC(=O)N1CCn2c(nnc2C(F)(F)F)C1)Cc1cc(F)c(F)cc1F")
        btn = gr.Button("Predict Kd", variant="primary")
        o1 = gr.Textbox(label="Predicted pKd")
        o2 = gr.Textbox(label="Predicted Kd (nM)")
        o3 = gr.Textbox(label="Predicted ΔG (kcal/mol)")
        o4 = gr.Textbox(label="Potency class")
        btn.click(predict_single, inputs=smi, outputs=[o1, o2, o3, o4])
    with gr.Tab("Batch (CSV)"):
        f = gr.File(label="CSV with a 'smiles' column", file_types=[".csv"])
        bbtn = gr.Button("Run batch prediction", variant="primary")
        table = gr.Dataframe(label="Predictions")
        dl = gr.File(label="Download predictions CSV")
        bbtn.click(predict_batch, inputs=f, outputs=[table, dl])
    with gr.Tab("About"):
        gr.Markdown(ABOUT)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))