MirrorPeptidizer / scripts /plot_bo_synthesis_distribution.py
dahuilangda's picture
Add MirrorPeptidizer benchmark (D/L comparison, new targets, ablation, figure data, SI)
0613483 verified
Raw History Blame Contribute Delete
4.1 kB
"""Plot synthesis-feasibility score distributions: pre-BO vs post-BO.
Mirrors the D/L distribution figures but compares two ablation workflows
instead of D vs L:
* pre-BO = Chroma+ProteinMPNN, no Bayesian optimization
(ablation/{target}/chroma_proteinmpnn/all_results.csv)
* post-BO = full Chroma+ProteinMPNN+BO workflow
(ablation/{target}/chroma_proteinmpnn_bo/all_bo_results.csv)
x-axis = synthesis feasibility score (bo.scoring.synthesis_metrics, same as
the ablation study). Targets PDL1/MDM2/IL23R. Same buried-pose filter and
Nature style as the other benchmark figures. Per-target sample sizes differ
(process-level), so histograms are density-normalised and both n are shown.
"""
from pathlib import Path
import sys
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
ROOT = Path(__file__).resolve().parents[2]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
SCRIPT_DIR = Path(__file__).resolve().parent
if str(SCRIPT_DIR) not in sys.path:
sys.path.insert(0, str(SCRIPT_DIR))
from plot_ablation_dl_figures import ( # noqa: E402
BENCH,
PALETTE,
TARGETS,
_load_bad_pose_keys,
apply_nature_style,
save_pub_fig,
)
PRE_FLOW, PRE_FNAME = "chroma_proteinmpnn", "all_results.csv"
POST_FLOW, POST_FNAME = "chroma_proteinmpnn_bo", "all_bo_results.csv"
def _load_ablation(target, flow, fname, filter_poses=True):
path = BENCH / "ablation" / target / flow / fname
df = pd.read_csv(path)
if filter_poses:
# Only meaningful for the full Chroma+ProteinMPNN pool (pose ids 1-50).
# The BO result table uses its own selected-pose ids (1-7) that do not
# share the buried-pose inventory's indexing, so it must NOT be filtered.
bad = _load_bad_pose_keys("D_peptide")
if bad:
keys = list(zip(
[target] * len(df),
df["length"].astype(int),
df["pose"].astype(int),
))
df = df.loc[[k not in bad for k in keys]].copy()
return df.dropna(subset=["synthesis_feasibility_score"])
def plot_bo_synthesis_distribution():
apply_nature_style(font_size=8)
from scipy import stats as sp_stats
fig, axes = plt.subplots(1, len(TARGETS), figsize=(9.2, 2.8))
for idx, target in enumerate(TARGETS):
ax = axes[idx]
pre = _load_ablation(target, PRE_FLOW, PRE_FNAME, filter_poses=True)
post = _load_ablation(target, POST_FLOW, POST_FNAME, filter_poses=False)
ps = pre["synthesis_feasibility_score"].to_numpy()
qs = post["synthesis_feasibility_score"].to_numpy()
bins = np.linspace(
min(ps.min(), qs.min()),
max(np.percentile(ps, 99), np.percentile(qs, 99)),
50,
)
ax.hist(
ps, bins=bins, density=True, alpha=0.55,
color=PALETTE["orange"], label="Pre-BO (Chroma+ProteinMPNN)",
)
ax.hist(
qs, bins=bins, density=True, alpha=0.55,
color=PALETTE["blue_main"], label="Post-BO (+BO)",
)
ks_stat, ks_p = sp_stats.ks_2samp(ps, qs)
ax.axvline(np.median(ps), color=PALETTE["orange"], ls="--", lw=0.8)
ax.axvline(np.median(qs), color=PALETTE["blue_main"], ls="--", lw=0.8)
ax.set_xlabel(f"{target} — Synthesis feasibility score")
if idx == 0:
ax.set_ylabel("Density")
ptxt = f"p < {ks_p:.1e}" if ks_p < 0.001 else f"p = {ks_p:.3f}"
ax.text(
0.03, 0.95,
f"pre $n$ = {len(ps)}\npost $n$ = {len(qs)}\n"
f"KS = {ks_stat:.3f}\n{ptxt}",
transform=ax.transAxes, ha="left", va="top",
fontsize=7, color=PALETTE["neutral_dark"],
bbox=dict(boxstyle="round,pad=0.3", fc="white",
ec=PALETTE["neutral_light"], lw=0.5),
)
if idx == 0:
ax.legend(loc="upper right", fontsize=7)
save_pub_fig(fig, "fig_bo_synthesis_distribution")
def main():
plot_bo_synthesis_distribution()
if __name__ == "__main__":
main()