File size: 5,835 Bytes
d1988f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Validate the conformer-based cCPP surrogate against the MD reference data.

The surrogate is only ever used as an *input descriptor*; this script documents
how far it can be trusted.  Three checks are reported:

1. Direction of the protonation shift.  LNPDB Supplementary Fig. 8 reports that
   protonated ionisable lipids have a systematically lower CPP than their
   neutral counterparts.  We measure the fraction of unique lipids for which the
   single-molecule conformer surrogate reproduces that sign.
2. Absolute scale on the one system with shipped MD values (``result_LM_1``).
3. Functional relevance.  LNPDB Fig. 4e-f reports a positive association between
   the neutral-lipid MD CPP and transfection for the N = 34 stable LM_2019
   bilayers (Pearson r = 0.53-0.72 depending on the amine subgroup).  We
   recompute that association with cCPP on exactly the same 34 formulations.

Every statistic is guarded against degenerate subgroups so the script runs on
any future release of the database.
"""

from __future__ import annotations

import json

import numpy as np
import pandas as pd
from scipy import stats

from lnpbench import paths
from lnpbench.store import FeatureStore

MD_CPPV_NEUTRAL = 1.5421388969374041   # results/CPP_V_results_protonated.csv, result_LM_1
MD_CPPV_PROTONATED = 1.3789960987540757


def safe_pearson(x: np.ndarray, y: np.ndarray) -> dict:
    x = np.asarray(x, dtype=float)
    y = np.asarray(y, dtype=float)
    ok = np.isfinite(x) & np.isfinite(y)
    n = int(ok.sum())
    if n < 3 or np.std(x[ok]) < 1e-12 or np.std(y[ok]) < 1e-12:
        return {"n": n, "pearson": float("nan"), "p": float("nan"), "spearman": float("nan")}
    r, p = stats.pearsonr(x[ok], y[ok])
    return {"n": n, "pearson": float(r), "p": float(p), "spearman": float(stats.spearmanr(x[ok], y[ok]).statistic)}


def main() -> None:
    store = FeatureStore()
    out: dict = {}

    # ---- 1. protonation direction over the whole library -------------------
    cpp = store.il_cpp
    neutral_v, prot_v = cpp[:, 0], cpp[:, 2]
    valid = (neutral_v > 0) & (prot_v > 0)
    out["protonation_direction"] = {
        "n_lipids": int(valid.sum()),
        "frac_protonated_lower_cppv": float(np.mean(prot_v[valid] < neutral_v[valid])),
        "median_neutral": float(np.median(neutral_v[valid])),
        "median_protonated": float(np.median(prot_v[valid])),
        "wilcoxon_p": float(stats.wilcoxon(neutral_v[valid], prot_v[valid]).pvalue),
        "md_expectation": "protonated lower than neutral",
    }

    # ---- 2. absolute scale on the single system with MD values -------------
    ids = list(store.samples["LNP_ID"])
    if "LNP_0003871" in ids:
        il = int(store.il_id[ids.index("LNP_0003871")])
        out["lm1_absolute"] = {
            "surrogate_cppv_neutral": float(cpp[il, 0]),
            "md_cppv_neutral": MD_CPPV_NEUTRAL,
            "surrogate_cppv_protonated": float(cpp[il, 2]),
            "md_cppv_protonated": MD_CPPV_PROTONATED,
            "surrogate_cpprg_neutral": float(cpp[il, 1]),
        }

    # ---- 3. functional relevance on the 34 stable LM_2019 bilayers ---------
    md = pd.read_csv(paths.MD_STABILITY_CSV)
    id_to_row = {v: i for i, v in enumerate(store.samples["LNP_ID"])}
    rows, labels, groups = [], [], []
    for _, r in md.iterrows():
        if r.LNP_ID in id_to_row and float(r["stable"]) == 1.0:
            i = id_to_row[r.LNP_ID]
            rows.append(i)
            labels.append(float(store.y[i]))
            groups.append(str(r.IL_head_name))
    if len(rows) < 3:
        out["stable_lm2019"] = {"n": len(rows), "note": "too few stable formulations"}
    else:
        rows_a = np.asarray(rows)
        labels_a = np.asarray(labels)
        groups_a = np.asarray(groups)
        il_ids = store.il_id[rows_a]
        block = {
            "n": int(len(rows_a)),
            "md_reported_pearson_range": [0.53, 0.723],
            "ccppv": safe_pearson(cpp[il_ids, 0], labels_a),
            "ccpprg": safe_pearson(cpp[il_ids, 1], labels_a),
            "ccppv_protonated": safe_pearson(cpp[il_ids, 2], labels_a),
        }
        per_group = {}
        for g in np.unique(groups_a):
            m = groups_a == g
            if m.sum() >= 5:
                stat = safe_pearson(cpp[il_ids[m], 0], labels_a[m])
                stat["n"] = int(m.sum())
                per_group[str(g)] = stat
        block["per_amine"] = per_group

        # high-CPP subgroup (LNPDB Supplementary Fig. 10: CPP > median)
        hi = cpp[il_ids, 0] > np.median(cpp[il_ids, 0])
        block["above_median_cpp"] = safe_pearson(cpp[il_ids[hi], 0], labels_a[hi])

        # bootstrap CI on the headline correlation
        rng = np.random.default_rng(0)
        x = cpp[il_ids, 0]
        y = labels_a
        boot = []
        for _ in range(2000):
            k = rng.integers(0, len(x), len(x))
            if np.std(x[k]) > 1e-12 and np.std(y[k]) > 1e-12:
                boot.append(stats.pearsonr(x[k], y[k])[0])
        if boot:
            block["pearson_ci95"] = [float(np.percentile(boot, 2.5)), float(np.percentile(boot, 97.5))]
        out["stable_lm2019"] = block

    out["verdict"] = (
        "The single-molecule conformer surrogate reproduces the absolute neutral-state "
        "CPP but not the pH-induced protonation shift, and it does not reproduce the "
        "MD-derived positive CPP-transfection association. It is therefore used only "
        "as an input descriptor, and its usefulness is established empirically by the "
        "w/o PhysBias ablation rather than by physical equivalence to MD."
    )

    dest = paths.RESULTS_DIR / "ccpp_validation.json"
    dest.parent.mkdir(parents=True, exist_ok=True)
    dest.write_text(json.dumps(out, indent=2))
    print(json.dumps(out, indent=2))


if __name__ == "__main__":
    main()