File size: 16,282 Bytes
66686a6
 
562106f
 
 
66686a6
562106f
66686a6
 
 
562106f
 
66686a6
 
 
 
 
 
 
 
 
 
 
 
d83b59e
66686a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d83b59e
66686a6
 
d83b59e
 
 
 
 
 
66686a6
 
562106f
 
 
 
 
 
 
 
66686a6
 
 
 
 
562106f
 
 
 
 
 
 
d83b59e
562106f
 
 
 
 
 
 
 
66686a6
d83b59e
 
 
66686a6
 
 
 
 
 
 
 
562106f
 
 
 
66686a6
 
 
d83b59e
66686a6
 
d83b59e
66686a6
 
 
d83b59e
 
 
66686a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d83b59e
66686a6
 
 
d83b59e
66686a6
 
 
562106f
66686a6
 
562106f
66686a6
562106f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66686a6
562106f
 
 
 
 
 
 
 
 
 
66686a6
 
 
 
d83b59e
66686a6
 
 
 
 
 
 
d83b59e
66686a6
 
 
d83b59e
66686a6
 
 
 
 
 
 
d83b59e
66686a6
 
 
 
 
 
d83b59e
66686a6
 
 
 
562106f
66686a6
 
 
562106f
66686a6
562106f
66686a6
562106f
 
 
66686a6
 
 
d83b59e
 
 
 
 
 
66686a6
 
562106f
66686a6
d83b59e
66686a6
 
 
d83b59e
66686a6
 
 
 
 
562106f
 
 
 
d83b59e
562106f
 
d83b59e
66686a6
d83b59e
562106f
 
 
66686a6
 
 
 
 
 
 
 
 
 
 
 
d83b59e
66686a6
 
 
 
 
 
 
 
 
 
562106f
66686a6
 
 
 
 
 
d83b59e
66686a6
 
 
 
 
 
d83b59e
66686a6
 
 
 
 
 
 
d83b59e
66686a6
 
 
d83b59e
66686a6
d83b59e
66686a6
d83b59e
 
66686a6
 
 
 
 
 
 
562106f
 
 
 
 
 
66686a6
562106f
66686a6
 
 
 
562106f
d83b59e
 
66686a6
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
Chromatographic Retention Time Predictor
Laboratory-conditioned retention-time prediction using archived fingerprint
neural-network fold models trained on 3,776 structure--laboratory observations
from 23 represented laboratories.

Reference: Accompanying retention-time modelling study.
"""

import os
import json
import hashlib
import warnings
warnings.filterwarnings("ignore")

import numpy as np
import torch
import torch.nn as nn
import gradio as gr
from rdkit import Chem
from rdkit.Chem import Draw, Descriptors, Crippen, rdFingerprintGenerator, DataStructs
from PIL import Image

# ─────────────────────────────────────────────────────────────────────────────
# Model architecture (FingerprintNN) β€” must match training exactly
# ─────────────────────────────────────────────────────────────────────────────

class FingerprintNN(nn.Module):
    """Feed-forward network for Morgan fingerprints with laboratory embedding."""

    def __init__(
        self,
        input_dim: int = 2048,
        hidden_dims=(768, 384, 192, 96),
        dropout: float = 0.15,
        use_batch_norm: bool = True,
        num_labs: int = 23,
        lab_embed_dim: int = 64,
        input_dropout: float = 0.1,
    ):
        super().__init__()
        self.lab_embedding = nn.Embedding(num_labs, lab_embed_dim)
        initial_dim = input_dim + lab_embed_dim
        self.input_dropout = nn.Dropout(input_dropout)

        layers = []
        prev_dim = initial_dim
        for idx, hidden_dim in enumerate(hidden_dims):
            layers.append(nn.Linear(prev_dim, hidden_dim))
            if use_batch_norm:
                layers.append(nn.BatchNorm1d(hidden_dim))
            layers.append(nn.GELU())
            layers.append(nn.Dropout(dropout if idx < len(hidden_dims) - 1 else dropout * 0.5))
            prev_dim = hidden_dim
        layers.append(nn.Linear(prev_dim, 1))
        self.network = nn.Sequential(*layers)

        self.target_mean: float = 0.0
        self.target_std: float = 1.0

    def forward(self, x: torch.Tensor, lab_indices: torch.Tensor) -> torch.Tensor:
        x = self.input_dropout(x)
        if lab_indices.dim() > 1:
            lab_indices = lab_indices.squeeze(-1)
        lab_emb = self.lab_embedding(lab_indices.long())
        x = torch.cat([x, lab_emb], dim=-1)
        return self.network(x).squeeze(-1)


# ─────────────────────────────────────────────────────────────────────────────
# Laboratory mapping (alphabetical LabelEncoder order, matching training)
# ─────────────────────────────────────────────────────────────────────────────

LAB_NAMES = [
    "Aarhus",
    "Academy of Forensic Science",
    "Adelaide",
    "Australian Racing Forensic Laboratory",
    "CFSRE",
    "ChemCentre",
    "Copenhagen",
    "Estonian Forensic Science Institute",
    "Finnish Customs Laboratory",
    "Ghent University",
    "IUPA, UJI I (E)",
    "King's College Hospital",
    "LADR",
    "Labor Krone",
    "Mainz",
    "Odense",
    "San Francisco OCME",
    "The University of Queensland",
    "Trondheim",
    "University Hospital of Northern Norway",
    "University of Athens",
    "Victorian Institute of Forensic Medicine",
    "Zurich Institute of Forensic Medicine",
]
LAB_TO_IDX = {name: idx for idx, name in enumerate(LAB_NAMES)}

# ─────────────────────────────────────────────────────────────────────────────
# Model loading (lazy, on first prediction)
# ─────────────────────────────────────────────────────────────────────────────

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_DIR = os.path.join(BASE_DIR, "models")
DATA_DIR = os.path.join(BASE_DIR, "data")

_models = []
_train_fps = None


def _sha256(path: str) -> str:
    digest = hashlib.sha256()
    with open(path, "rb") as stream:
        for block in iter(lambda: stream.read(1024 * 1024), b""):
            digest.update(block)
    return digest.hexdigest()


def _load_models():
    global _models, _train_fps
    if _models:
        return

    bundle_path = os.path.join(BASE_DIR, "model_bundle.json")
    if not os.path.isfile(bundle_path):
        raise FileNotFoundError("The versioned FPNN model bundle is not installed.")
    with open(bundle_path, "r", encoding="utf-8") as stream:
        bundle = json.load(stream)
    if bundle.get("component") != "FPNN only":
        raise ValueError("The installed bundle is not the scoped FPNN component.")
    fold_files = sorted(f for f in os.listdir(MODEL_DIR) if f.endswith(".pt"))
    if len(fold_files) != int(bundle["fold_models"]):
        raise FileNotFoundError(
            f"Expected {bundle['fold_models']} FPNN fold models; found {len(fold_files)}."
        )
    for item in bundle["files"]:
        path = os.path.join(BASE_DIR, *item["relative_path"].split("/"))
        if not os.path.isfile(path) or _sha256(path) != item["sha256"]:
            raise ValueError(f"Missing or hash-mismatched bundle file: {item['relative_path']}")
    for fname in fold_files:
        ckpt = torch.load(
            os.path.join(MODEL_DIR, fname), map_location="cpu", weights_only=False
        )
        model = FingerprintNN()
        model.load_state_dict(ckpt["model_state"])
        model.target_mean = float(ckpt["target_mean"])
        model.target_std = float(ckpt["target_std"])
        model.eval()
        _models.append(model)

    fps_path = os.path.join(DATA_DIR, "training_fps.npz")
    if not os.path.exists(fps_path):
        raise FileNotFoundError("Development-set fingerprints are missing from the bundle.")
    with np.load(fps_path) as fingerprint_bundle:
        _train_fps = fingerprint_bundle["fps"].astype(np.float32)


# ─────────────────────────────────────────────────────────────────────────────
# Molecular feature helpers
# ─────────────────────────────────────────────────────────────────────────────

def _smiles_to_fp(smiles: str):
    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return None
    gen = rdFingerprintGenerator.GetMorganGenerator(
        radius=2, fpSize=2048, includeChirality=True
    )
    fp = gen.GetFingerprint(mol)
    arr = np.zeros(2048, dtype=np.float32)
    DataStructs.ConvertToNumpyArray(fp, arr)
    return arr


def _mol_descriptors(smiles: str) -> dict:
    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return {}
    return {
        "MW": Descriptors.MolWt(mol),
        "LogP": Crippen.MolLogP(mol),
        "TPSA": Descriptors.TPSA(mol),
        "HBD": Descriptors.NumHDonors(mol),
        "HBA": Descriptors.NumHAcceptors(mol),
        "RotBonds": Descriptors.NumRotatableBonds(mol),
        "AromaticRings": Descriptors.NumAromaticRings(mol),
        "HeavyAtoms": mol.GetNumHeavyAtoms(),
    }


def _smiles_to_image(smiles: str):
    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return None
    return Draw.MolToImage(mol, size=(300, 220))


# ─────────────────────────────────────────────────────────────────────────────
# Prospective structural-similarity context
# ─────────────────────────────────────────────────────────────────────────────

def _check_similarity_context(fp: np.ndarray, threshold: float = 0.4):
    if _train_fps is None:
        return float("nan"), False, "Development-set fingerprints are unavailable."
    # Binary logical reductions compute the exact Morgan-fingerprint Tanimoto
    # counts without dispatching to a BLAS matrix multiplication.  Avoiding
    # BLAS here also prevents a Windows OpenMP-runtime conflict between the
    # packaged RDKit and PyTorch wheels during first prediction.
    development = np.asarray(_train_fps) > 0
    query = np.asarray(fp) > 0
    intersections = np.logical_and(development, query).sum(axis=1, dtype=np.int32)
    unions = np.logical_or(development, query).sum(axis=1, dtype=np.int32)
    similarities = np.divide(
        intersections,
        unions,
        out=np.zeros(intersections.shape, dtype=np.float32),
        where=unions > 0,
    )
    maximum_similarity = float(np.max(similarities))
    represented = maximum_similarity >= threshold
    status = (
        f"Maximum development-set Tanimoto similarity: {maximum_similarity:.3f} "
        f"(reference threshold {threshold:.2f})."
    )
    return maximum_similarity, represented, status


def _format_prediction_summary(prediction_mean: float, prediction_sd: float) -> str:
    return (
        f"### Predicted RT: {prediction_mean:.2f} min\n\n"
        f"Uncalibrated fold-model disagreement (SD): {prediction_sd:.2f} min"
    )


# ─────────────────────────────────────────────────────────────────────────────
# Main prediction function
# ─────────────────────────────────────────────────────────────────────────────

def predict(smiles: str, lab_name: str):
    _load_models()

    smiles = smiles.strip()
    if not smiles:
        return None, "⚠️ Please enter a SMILES string.", "", "", ""

    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return None, "❌ Invalid SMILES. Please check the input.", "", "", ""

    if lab_name not in LAB_TO_IDX:
        return None, f"Unknown laboratory: {lab_name}", "", "", ""

    fp = _smiles_to_fp(smiles)
    lab_idx = LAB_TO_IDX[lab_name]

    fp_tensor = torch.tensor(fp, dtype=torch.float32).unsqueeze(0)
    lab_tensor = torch.tensor([lab_idx], dtype=torch.long)

    fold_preds = []
    with torch.no_grad():
        for model in _models:
            raw = model(fp_tensor, lab_tensor).item()
            fold_preds.append(max(0.0, raw * model.target_std + model.target_mean))

    pred_mean = float(np.mean(fold_preds))
    pred_std = float(np.std(fold_preds))

    _, represented, similarity_status = _check_similarity_context(fp)
    desc = _mol_descriptors(smiles)
    mol_img = _smiles_to_image(smiles)

    rt_text = _format_prediction_summary(pred_mean, pred_std)

    ad_icon = "βœ…" if represented else "⚠️"
    ad_text = (
        f"{ad_icon} {similarity_status}\n\n"
        "This prospective similarity score provides structural context only; "
        "it is not a reliability guarantee or a predictive interval."
    )

    desc_text = (
        f"**MW:** {desc.get('MW', 0):.1f} Da  |  "
        f"**LogP:** {desc.get('LogP', 0):.2f}  |  "
        f"**TPSA:** {desc.get('TPSA', 0):.1f} Γ…Β²  |  "
        f"**HBD/HBA:** {int(desc.get('HBD', 0))}/{int(desc.get('HBA', 0))}  |  "
        f"**RotBonds:** {int(desc.get('RotBonds', 0))}  |  "
        f"**HeavyAtoms:** {int(desc.get('HeavyAtoms', 0))}"
    )

    fold_text = "Fold-model predictions: " + "  |  ".join(f"{p:.2f}" for p in fold_preds)

    return mol_img, rt_text, ad_text, desc_text, fold_text


# ─────────────────────────────────────────────────────────────────────────────
# Gradio interface (compatible with gradio 5.x)
# ─────────────────────────────────────────────────────────────────────────────

DESCRIPTION = """
# Chromatographic Retention Time Predictor

This interface exposes the archived **fingerprint neural-network (FPNN) fold models**, not the
GAT/GCN/ExtraTrees stack. The model requires one of the 23 laboratory labels represented during
training. The modelling table contains 3,776 structure--laboratory observations corresponding to
1,357 InChIKey connectivity groups.

> The fold-model standard deviation is uncalibrated model disagreement, not a confidence interval.
> The Tanimoto value is a prospective structural-similarity diagnostic, not a reliability guarantee.
"""

PERF_TABLE = """
The study evaluation uses molecular-identity-grouped and scaffold-aware outer holdouts over
three prespecified split repetitions. Results for the FPNN component and the full stack are reported separately
in the accompanying manuscript because they estimate performance under different validation tasks.
"""

EXAMPLES = [
    ["c1ccc2c(c1)cc1ccc3cccc4ccc2c1c34", "Aarhus"],
    ["CC(=O)Oc1ccccc1C(=O)O", "Copenhagen"],
    ["CN1CCC23c4c(ccc(O)c4OC2(CCN(C)CC3=O)C1)O", "Ghent University"],
    ["c1ccc(cc1)C(c1ccccc1)N1CCCC1", "Mainz"],
    ["CC12CCC3C(C1CCC2O)CCC4=CC(=O)CCC34C", "CFSRE"],
]

with gr.Blocks(
    title="RT Predictor β€” Multi-Lab Chromatography",
    theme=gr.themes.Soft(primary_hue="blue"),
) as demo:

    gr.Markdown(DESCRIPTION)
    gr.Markdown("---")

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### Input")
            smiles_input = gr.Textbox(
                label="SMILES String",
                value="CC(=O)Oc1ccccc1C(=O)O",
                placeholder="e.g., c1ccc2c(c1)cc1ccc3cccc4ccc2c1c34",
                lines=2,
            )
            lab_input = gr.Dropdown(
                choices=LAB_NAMES,
                value="Aarhus",
                label="Target Laboratory",
            )
            predict_btn = gr.Button("Predict Retention Time", variant="primary")

            gr.Examples(
                examples=EXAMPLES,
                inputs=[smiles_input, lab_input],
                label="Example compounds (click to load)",
            )

        with gr.Column(scale=1):
            gr.Markdown("### Molecular Structure")
            mol_image = gr.Image(label="2D Structure", height=240)

    gr.Markdown("---")
    gr.Markdown("### Results")

    with gr.Row():
        with gr.Column(scale=1):
            rt_output = gr.Markdown()
        with gr.Column(scale=2):
            ad_output = gr.Markdown()

    desc_output = gr.Markdown()
    fold_output = gr.Markdown()

    predict_btn.click(
        fn=predict,
        inputs=[smiles_input, lab_input],
        outputs=[mol_image, rt_output, ad_output, desc_output, fold_output],
    )

    demo.load(
        fn=predict,
        inputs=[smiles_input, lab_input],
        outputs=[mol_image, rt_output, ad_output, desc_output, fold_output],
    )

    gr.Markdown("---")
    gr.Markdown("### Evaluation Scope")
    gr.Markdown(PERF_TABLE)

    gr.Markdown("""
---
**Dataset:** HighResNPS-derived forensic toxicology data; 23 represented laboratory labels
**Model repo:** [AI4deeperScience/chromatography-rt-prediction](https://huggingface.co/AI4deeperScience/chromatography-rt-prediction)
**Citation:** Manuscript under review
""")

if __name__ == "__main__":
    demo.launch()