File size: 4,581 Bytes
0613483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Export all plotting CSVs for each figure into benchmark/figure_data/."""
from pathlib import Path
import pandas as pd
import numpy as np
import shutil

BENCH = Path(__file__).resolve().parents[1]
FIGDATA = BENCH / "figure_data"
FIGDATA.mkdir(exist_ok=True)

TARGETS = ["PDL1", "MDM2", "IL23R"]


def main():
    # ── fig_ablation_bo_tradeoff ──
    ablation = pd.read_csv(BENCH / "ablation" / "ablation_summary_pose_filtered.csv")
    ablation.to_csv(FIGDATA / "fig_ablation_bo_tradeoff_ablation_summary.csv", index=False)
    tradeoff = pd.read_csv(BENCH / "ablation" / "ablation_tradeoff_pose_filtered.csv")
    tradeoff.to_csv(FIGDATA / "fig_ablation_bo_tradeoff_tradeoff.csv", index=False)

    bad = pd.read_csv(BENCH / "buried_pose_inventory.csv")
    # Ablation Tier-1 reuses the dl D-peptide designs, so the ablation bad-pose
    # counts are the D_peptide buried poses per core target.
    bad_ab = bad[bad["peptide_mode"] == "D_peptide"]
    bad_counts = (
        bad_ab.groupby("target")
        .size()
        .reindex(TARGETS, fill_value=0)
        .rename("n_bad_poses")
        .reset_index()
    )
    bad_counts.to_csv(FIGDATA / "fig_ablation_bo_tradeoff_bad_pose_counts.csv", index=False)
    print(
        f"fig_ablation: {len(ablation)} summary rows + "
        f"{len(tradeoff)} trade-off rows + {len(bad_counts)} pose counts"
    )

    # ── fig_dl_score_distribution ──
    for t in TARGETS:
        for m in ["D_peptide", "L_peptide"]:
            src = BENCH / "dl_downsampled" / f"{t}_{m}_filtered.csv"
            dst = FIGDATA / f"fig_dl_score_dist_{t}_{m}.csv"
            shutil.copy2(src, dst)

    from scipy import stats as sp_stats

    rows = []
    for t in TARGETS:
        d = pd.read_csv(FIGDATA / f"fig_dl_score_dist_{t}_D_peptide.csv")
        l = pd.read_csv(FIGDATA / f"fig_dl_score_dist_{t}_L_peptide.csv")
        ks, p = sp_stats.ks_2samp(d["score"], l["score"])
        rows.append(
            {
                "target": t,
                "n": len(d),
                "D_median": d["score"].median(),
                "L_median": l["score"].median(),
                "D_mean": d["score"].mean(),
                "L_mean": l["score"].mean(),
                "KS_stat": ks,
                "KS_p": p,
            }
        )
    pd.DataFrame(rows).to_csv(FIGDATA / "fig_dl_score_dist_summary.csv", index=False)
    print("fig_dl_score_dist: 6 CSVs + summary")

    # ── fig_dl_sequence_comparison (t-SNE top-100) ──
    from sklearn.decomposition import PCA
    from sklearn.manifold import TSNE

    AAS = list("ACDEFGHIKLMNPQRSTVWY")
    TOP_N = 100
    aa2i = {a: i for i, a in enumerate(AAS)}

    def onehot(seqs, max_len):
        vecs = []
        for s in seqs:
            v = np.zeros(max_len * len(AAS))
            for pos, aa in enumerate(str(s)):
                if aa in aa2i:
                    v[pos * len(AAS) + aa2i[aa]] = 1.0
            vecs.append(v)
        return np.array(vecs)

    for t in TARGETS:
        d_df = pd.read_csv(FIGDATA / f"fig_dl_score_dist_{t}_D_peptide.csv")
        l_df = pd.read_csv(FIGDATA / f"fig_dl_score_dist_{t}_L_peptide.csv")
        d_top = d_df.nsmallest(TOP_N, "score").drop_duplicates("sequence").copy()
        l_top = l_df.nsmallest(TOP_N, "score").drop_duplicates("sequence").copy()

        ml = max(d_top["sequence"].str.len().max(), l_top["sequence"].str.len().max())
        dv = onehot(d_top["sequence"], ml)
        lv = onehot(l_top["sequence"], ml)
        X = np.vstack([dv, lv])
        npc = min(50, X.shape[1], X.shape[0] - 1)
        Xp = PCA(n_components=npc, random_state=42).fit_transform(X)
        Xt = TSNE(
            n_components=2, perplexity=30, random_state=42,
            learning_rate="auto", init="pca",
        ).fit_transform(Xp)

        nd = len(dv)
        d_out = d_top[["sequence", "score", "length", "pose"]].copy()
        d_out["tsne_1"] = Xt[:nd, 0]
        d_out["tsne_2"] = Xt[:nd, 1]
        d_out["type"] = "D"

        l_out = l_top[["sequence", "score", "length", "pose"]].copy()
        l_out["tsne_1"] = Xt[nd:, 0]
        l_out["tsne_2"] = Xt[nd:, 1]
        l_out["type"] = "L"

        combined = pd.concat([d_out, l_out], ignore_index=True)
        combined.to_csv(FIGDATA / f"fig_dl_tsne_{t}.csv", index=False)
        print(f"fig_dl_tsne_{t}: {nd} D + {len(l_out)} L = {len(combined)} rows")

    print(f"\nAll figure data saved to {FIGDATA}/")
    for f in sorted(FIGDATA.iterdir()):
        print(f"  {f.name}  ({f.stat().st_size/1024:.1f} KB)")


if __name__ == "__main__":
    main()