File size: 19,850 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
"""Plot Ramachandran-style backbone mirror QC for paired L/D peptide poses."""
from __future__ import annotations

from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from Bio.PDB import PDBParser
from Bio.PDB.vectors import calc_dihedral
from scipy.ndimage import gaussian_filter


ROOT = Path(__file__).resolve().parents[2]
BENCH = ROOT / "benchmark"
FIGDIR = BENCH / "figures"
FIGDATA = BENCH / "figure_data"

TARGET_DIRS = {
    "PDL1": BENCH / "PDL1" / "D_peptide",
    "MDM2": BENCH / "MDM2" / "D_peptide",
    "IL23R": BENCH / "IL23R" / "D_peptide",
    "TNFalpha": BENCH / "new_targets" / "designs" / "TNFalpha" / "D_peptide",
    "CXCR2": BENCH / "new_targets" / "designs" / "CXCR2" / "D_peptide",
    "CXCR4": BENCH / "new_targets" / "designs" / "CXCR4" / "D_peptide",
}

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

PALETTE = {
    "blue_main": "#0F4D92",
    "orange": "#E28E2C",
    "neutral_light": "#CFCECE",
    "neutral_mid": "#767676",
    "neutral_dark": "#4D4D4D",
}


def apply_nature_style(font_size: int = 7) -> None:
    plt.rcParams["font.family"] = "sans-serif"
    plt.rcParams["font.sans-serif"] = ["Arial", "DejaVu Sans", "Liberation Sans"]
    plt.rcParams["svg.fonttype"] = "none"
    plt.rcParams["pdf.fonttype"] = 42
    plt.rcParams["axes.spines.right"] = False
    plt.rcParams["axes.spines.top"] = False
    plt.rcParams["axes.linewidth"] = 0.7
    plt.rcParams["legend.frameon"] = False
    plt.rcParams["font.size"] = font_size
    plt.rcParams["axes.titlesize"] = font_size + 1
    plt.rcParams["axes.labelsize"] = font_size + 1
    plt.rcParams["xtick.labelsize"] = font_size
    plt.rcParams["ytick.labelsize"] = font_size
    plt.rcParams["legend.fontsize"] = font_size
    plt.rcParams["xtick.direction"] = "out"
    plt.rcParams["ytick.direction"] = "out"
    plt.rcParams["xtick.major.width"] = 0.7
    plt.rcParams["ytick.major.width"] = 0.7
    plt.rcParams["xtick.major.size"] = 2.5
    plt.rcParams["ytick.major.size"] = 2.5


def add_panel_label(ax, label: str) -> None:
    ax.text(
        -0.20,
        1.10,
        label,
        transform=ax.transAxes,
        fontsize=8,
        fontweight="bold",
        ha="left",
        va="bottom",
    )


def _binder_chain(structure):
    chains = list(structure.get_chains())
    if not chains:
        raise ValueError("No chains found")
    return sorted(chains, key=lambda chain: chain.id)[-1]


def _chain_phi_psi(path: Path) -> list[tuple[float | None, float | None]]:
    parser = PDBParser(QUIET=True)
    structure = parser.get_structure(path.stem, path)
    chain = _binder_chain(structure)
    residues = [
        residue
        for residue in chain
        if residue.id[0] == " " and all(atom in residue for atom in ["N", "CA", "C"])
    ]
    angles: list[tuple[float | None, float | None]] = []
    for idx, residue in enumerate(residues):
        phi = None
        psi = None
        if idx > 0:
            prev = residues[idx - 1]
            phi = calc_dihedral(
                prev["C"].get_vector(),
                residue["N"].get_vector(),
                residue["CA"].get_vector(),
                residue["C"].get_vector(),
            )
        if idx < len(residues) - 1:
            nxt = residues[idx + 1]
            psi = calc_dihedral(
                residue["N"].get_vector(),
                residue["CA"].get_vector(),
                residue["C"].get_vector(),
                nxt["N"].get_vector(),
            )
        angles.append((phi, psi))
    return angles


def _paired_pose_files(base: Path) -> list[tuple[str, Path, Path]]:
    pairs = []
    for l_path in sorted(base.glob("len_*/Poses/Binder_L_pose_*.pdb")):
        d_path = l_path.with_name(l_path.name.replace("Binder_L_pose_", "Binder_D_pose_"))
        if d_path.exists():
            pose_key = f"{l_path.parents[1].name}:{l_path.stem.split('_')[-1]}"
            pairs.append((pose_key, l_path, d_path))
    return pairs


def collect_phi_psi() -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
    rama_rows = []
    angle_rows = []
    summary_rows = []
    for target, base in TARGET_DIRS.items():
        pairs = _paired_pose_files(base)
        if not pairs:
            raise FileNotFoundError(f"No paired L/D pose files found under {base}")

        target_phi_errors: list[float] = []
        target_psi_errors: list[float] = []
        target_rama_rows = 0
        for pose_key, l_path, d_path in pairs:
            l_angles = _chain_phi_psi(l_path)
            d_angles = _chain_phi_psi(d_path)
            if len(l_angles) != len(d_angles):
                raise ValueError(f"Residue count mismatch: {l_path} vs {d_path}")

            for residue_idx, ((phi_l, psi_l), (phi_d, psi_d)) in enumerate(
                zip(l_angles, d_angles),
                start=1,
            ):
                phi_l_deg = float(np.degrees(phi_l)) if phi_l is not None else None
                psi_l_deg = float(np.degrees(psi_l)) if psi_l is not None else None
                phi_d_deg = float(np.degrees(phi_d)) if phi_d is not None else None
                psi_d_deg = float(np.degrees(psi_d)) if psi_d is not None else None

                phi_err = None
                psi_err = None
                if phi_l_deg is not None and phi_d_deg is not None:
                    phi_err = abs(phi_l_deg + phi_d_deg)
                    target_phi_errors.append(phi_err)
                    angle_rows.append(
                        {
                            "target": target,
                            "pose_key": pose_key,
                            "residue_index": residue_idx,
                            "angle": "phi",
                            "L_angle": phi_l_deg,
                            "D_angle": phi_d_deg,
                            "abs_L_plus_D": phi_err,
                        }
                    )
                if psi_l_deg is not None and psi_d_deg is not None:
                    psi_err = abs(psi_l_deg + psi_d_deg)
                    target_psi_errors.append(psi_err)
                    angle_rows.append(
                        {
                            "target": target,
                            "pose_key": pose_key,
                            "residue_index": residue_idx,
                            "angle": "psi",
                            "L_angle": psi_l_deg,
                            "D_angle": psi_d_deg,
                            "abs_L_plus_D": psi_err,
                        }
                    )

                if (
                    phi_l_deg is not None
                    and psi_l_deg is not None
                    and phi_d_deg is not None
                    and psi_d_deg is not None
                ):
                    target_rama_rows += 1
                    rama_rows.append(
                        {
                            "target": target,
                            "pose_key": pose_key,
                            "residue_index": residue_idx,
                            "phi_L": phi_l_deg,
                            "psi_L": psi_l_deg,
                            "phi_D": phi_d_deg,
                            "psi_D": psi_d_deg,
                            "phi_D_sign_inverted": -phi_d_deg,
                            "psi_D_sign_inverted": -psi_d_deg,
                            "abs_phi_L_plus_phi_D": phi_err,
                            "abs_psi_L_plus_psi_D": psi_err,
                        }
                    )

        summary_rows.append(
            {
                "target": target,
                "n_pose_pairs": len(pairs),
                "n_paired_phi": len(target_phi_errors),
                "n_paired_psi": len(target_psi_errors),
                "n_rama_coordinates": target_rama_rows,
                "max_abs_phi_L_plus_phi_D": max(target_phi_errors),
                "max_abs_psi_L_plus_psi_D": max(target_psi_errors),
            }
        )

    return pd.DataFrame(rama_rows), pd.DataFrame(angle_rows), pd.DataFrame(summary_rows)


def _mode_pose_files(target: str, mode: str) -> list[Path]:
    if mode == "L":
        return sorted((BENCH / target / "L_peptide").glob("len_*/Poses/Binder_L_pose_*.pdb"))
    if mode == "D":
        return sorted((BENCH / target / "D_peptide").glob("len_*/Poses/Binder_D_pose_*.pdb"))
    raise ValueError(f"Unknown mode: {mode}")


def collect_benchmark_ld_phi_psi() -> tuple[pd.DataFrame, pd.DataFrame]:
    """Collect raw phi/psi distributions from benchmark direct-L and final-D designs."""
    rows = []
    summary_rows = []
    for target in BENCHMARK_TARGETS:
        for mode in ["L", "D"]:
            files = _mode_pose_files(target, mode)
            n_rama = 0
            for path in files:
                angles = _chain_phi_psi(path)
                pose_key = f"{path.parents[1].name}:{path.stem.split('_')[-1]}"
                for residue_idx, (phi, psi) in enumerate(angles, start=1):
                    if phi is None or psi is None:
                        continue
                    n_rama += 1
                    rows.append(
                        {
                            "target": target,
                            "mode": mode,
                            "pose_key": pose_key,
                            "residue_index": residue_idx,
                            "phi": float(np.degrees(phi)),
                            "psi": float(np.degrees(psi)),
                        }
                    )
            summary_rows.append(
                {
                    "target": target,
                    "mode": mode,
                    "n_pose_backbones": len(files),
                    "n_rama_coordinates": n_rama,
                }
            )
    return pd.DataFrame(rows), pd.DataFrame(summary_rows)


def _draw_reference_axes(ax) -> None:
    ax.axhline(0, color=PALETTE["neutral_light"], lw=0.6, zorder=0)
    ax.axvline(0, color=PALETTE["neutral_light"], lw=0.6, zorder=0)
    ax.set_xlim(-180, 180)
    ax.set_ylim(-180, 180)
    ax.set_aspect("equal", adjustable="box")
    ax.set_xticks([-180, 0, 180])
    ax.set_yticks([-180, 0, 180])


def _density_contour(ax, x, y, color, label, linestyle="-", linewidth=1.3):
    hist, xedges, yedges = np.histogram2d(
        x,
        y,
        bins=90,
        range=[[-180, 180], [-180, 180]],
    )
    z = gaussian_filter(hist.T, sigma=1.2)
    positive = z[z > 0]
    if len(positive) == 0:
        return None
    levels = np.quantile(positive, [0.70, 0.85, 0.94])
    levels = np.unique(levels)
    xc = (xedges[:-1] + xedges[1:]) / 2
    yc = (yedges[:-1] + yedges[1:]) / 2
    return ax.contour(
        xc,
        yc,
        z,
        levels=levels,
        colors=[color],
        linewidths=linewidth,
        linestyles=linestyle,
    )


def _summary_table(ax, summary: pd.DataFrame) -> None:
    ax.axis("off")
    display = summary.copy()
    display["target"] = display["target"].replace({"TNFalpha": "TNF-alpha"})
    rows = []
    for _, row in display.iterrows():
        rows.append(
            [
                row["target"],
                f"{int(row['n_pose_pairs'])}",
                f"{int(row['n_paired_phi'])}",
                f"{int(row['n_paired_psi'])}",
                f"{row['max_abs_phi_L_plus_phi_D']:.3f}",
                f"{row['max_abs_psi_L_plus_psi_D']:.3f}",
            ]
        )
    rows.append(
        [
            "All",
            f"{int(summary['n_pose_pairs'].sum())}",
            f"{int(summary['n_paired_phi'].sum())}",
            f"{int(summary['n_paired_psi'].sum())}",
            f"{summary['max_abs_phi_L_plus_phi_D'].max():.3f}",
            f"{summary['max_abs_psi_L_plus_psi_D'].max():.3f}",
        ]
    )
    table = ax.table(
        cellText=rows,
        colLabels=[
            "Target",
            "Pose\npairs",
            "Paired\nphi",
            "Paired\npsi",
            "Max phi\nerror (deg)",
            "Max psi\nerror (deg)",
        ],
        loc="center",
        cellLoc="center",
        colLoc="center",
        colWidths=[0.17, 0.14, 0.15, 0.15, 0.19, 0.19],
    )
    table.auto_set_font_size(False)
    table.set_fontsize(6.8)
    table.scale(1.0, 1.42)
    for (row_idx, col_idx), cell in table.get_celld().items():
        cell.set_edgecolor("#D7D7D7")
        cell.set_linewidth(0.55)
        if row_idx == 0:
            cell.set_facecolor(PALETTE["blue_main"])
            cell.set_text_props(color="white", weight="bold")
        elif row_idx == len(rows):
            cell.set_facecolor("#EEF2F6")
            cell.set_text_props(weight="bold")
        elif row_idx % 2 == 0:
            cell.set_facecolor("#F7F7F7")
        else:
            cell.set_facecolor("white")
    add_panel_label(ax, "a")


def _scatter_cloud(ax, x, y, color, label, marker="o", face=True, alpha=0.20, size=5):
    facecolor = color if face else "none"
    edgecolor = "none" if face else color
    linewidth = 0.35 if not face else 0.0
    ax.scatter(
        x,
        y,
        s=size,
        marker=marker,
        alpha=alpha,
        linewidths=linewidth,
        facecolors=facecolor,
        edgecolors=edgecolor,
        color=color if face else None,
        rasterized=True,
        label=label,
    )


def _sample_points(df: pd.DataFrame, n: int = 4500) -> pd.DataFrame:
    if len(df) <= n:
        return df
    return df.sample(n=n, random_state=7)


def plot(rows: pd.DataFrame, summary: pd.DataFrame) -> None:
    apply_nature_style(font_size=7)
    fig, axes = plt.subplots(1, 2, figsize=(6.5, 2.9), sharex=True, sharey=True)
    plot_rows = _sample_points(rows, n=4500)

    ax_raw = axes[0]
    _draw_reference_axes(ax_raw)
    _scatter_cloud(
        ax_raw,
        plot_rows["phi_L"],
        plot_rows["psi_L"],
        PALETTE["blue_main"],
        "L backbone",
        alpha=0.26,
        size=5,
    )
    _scatter_cloud(
        ax_raw,
        plot_rows["phi_D"],
        plot_rows["psi_D"],
        PALETTE["orange"],
        "Final D backbone",
        alpha=0.24,
        size=5,
    )
    _density_contour(
        ax_raw,
        rows["phi_L"],
        rows["psi_L"],
        PALETTE["blue_main"],
        "L backbone",
        linewidth=1.2,
    )
    _density_contour(
        ax_raw,
        rows["phi_D"],
        rows["psi_D"],
        PALETTE["orange"],
        "Final D backbone",
        linewidth=1.2,
    )
    ax_raw.set_xlabel("φ (°)")
    ax_raw.set_ylabel("ψ (°)")
    add_panel_label(ax_raw, "a")

    ax_inv = axes[1]
    _draw_reference_axes(ax_inv)
    _scatter_cloud(
        ax_inv,
        plot_rows["phi_L"],
        plot_rows["psi_L"],
        PALETTE["blue_main"],
        "L backbone",
        alpha=0.25,
        size=5,
    )
    _scatter_cloud(
        ax_inv,
        plot_rows["phi_D_sign_inverted"],
        plot_rows["psi_D_sign_inverted"],
        PALETTE["orange"],
        "Sign-inverted D backbone",
        face=False,
        alpha=0.28,
        size=8,
    )
    _density_contour(
        ax_inv,
        rows["phi_L"],
        rows["psi_L"],
        PALETTE["blue_main"],
        "L backbone",
        linewidth=1.2,
    )
    _density_contour(
        ax_inv,
        rows["phi_D_sign_inverted"],
        rows["psi_D_sign_inverted"],
        PALETTE["neutral_dark"],
        "Sign-inverted D",
        linestyle="--",
        linewidth=1.1,
    )
    ax_inv.set_xlabel("φ (°)")
    ax_inv.set_ylabel("ψ (°)")
    add_panel_label(ax_inv, "b")

    handles = [
        plt.Line2D(
            [0],
            [0],
            marker="o",
            color="w",
            markerfacecolor=PALETTE["blue_main"],
            markersize=5,
            label="L backbone",
        ),
        plt.Line2D(
            [0],
            [0],
            marker="o",
            color="w",
            markerfacecolor=PALETTE["orange"],
            markersize=5,
            label="Final D backbone",
        ),
        plt.Line2D(
            [0],
            [0],
            color=PALETTE["neutral_dark"],
            lw=1.2,
            ls="--",
            label="Sign-inverted D backbone",
        ),
    ]
    fig.legend(handles=handles, loc="upper center", ncol=3, bbox_to_anchor=(0.5, 1.05))
    FIGDIR.mkdir(parents=True, exist_ok=True)
    fig.tight_layout(rect=[0.02, 0.03, 1.0, 0.92], w_pad=1.0)
    for ext in ["svg", "pdf"]:
        fig.savefig(FIGDIR / f"fig_phi_psi_mirror_qc.{ext}", bbox_inches="tight")
    fig.savefig(FIGDIR / "fig_phi_psi_mirror_qc.png", dpi=300, bbox_inches="tight")
    plt.close(fig)


def plot_benchmark_ld(rows: pd.DataFrame) -> None:
    apply_nature_style(font_size=7)
    fig, axes = plt.subplots(1, 3, figsize=(7.0, 2.55), sharex=True, sharey=True)
    target_labels = {"PDL1": "PD-L1", "MDM2": "MDM2", "IL23R": "IL-23R"}

    for idx, target in enumerate(BENCHMARK_TARGETS):
        ax = axes[idx]
        _draw_reference_axes(ax)
        tdf = rows[rows["target"] == target]
        l_all = tdf[tdf["mode"] == "L"]
        d_all = tdf[tdf["mode"] == "D"]
        l_plot = _sample_points(l_all, n=1800)
        d_plot = _sample_points(d_all, n=1800)

        _scatter_cloud(
            ax,
            l_plot["phi"],
            l_plot["psi"],
            PALETTE["blue_main"],
            "Direct L-peptide",
            alpha=0.26,
            size=5,
        )
        _scatter_cloud(
            ax,
            d_plot["phi"],
            d_plot["psi"],
            PALETTE["orange"],
            "Mirror-derived D-peptide",
            alpha=0.24,
            size=5,
        )
        _density_contour(
            ax,
            l_all["phi"],
            l_all["psi"],
            PALETTE["blue_main"],
            "Direct L-peptide",
            linewidth=1.2,
        )
        _density_contour(
            ax,
            d_all["phi"],
            d_all["psi"],
            PALETTE["orange"],
            "Mirror-derived D-peptide",
            linewidth=1.2,
        )
        ax.set_xlabel(f"{target_labels[target]}  φ (°)")
        if idx == 0:
            ax.set_ylabel("ψ (°)")
        add_panel_label(ax, "abc"[idx])

    handles = [
        plt.Line2D(
            [0],
            [0],
            marker="o",
            color="w",
            markerfacecolor=PALETTE["blue_main"],
            markersize=5,
            label="Direct L-peptide design",
        ),
        plt.Line2D(
            [0],
            [0],
            marker="o",
            color="w",
            markerfacecolor=PALETTE["orange"],
            markersize=5,
            label="Mirror-derived D-peptide design",
        ),
    ]
    fig.legend(handles=handles, loc="upper center", ncol=2, bbox_to_anchor=(0.5, 1.04))
    FIGDIR.mkdir(parents=True, exist_ok=True)
    fig.tight_layout(rect=[0.02, 0.02, 1.0, 0.90], w_pad=0.8)
    for ext in ["svg", "pdf"]:
        fig.savefig(FIGDIR / f"fig_phi_psi_mirror_qc.{ext}", bbox_inches="tight")
    fig.savefig(FIGDIR / "fig_phi_psi_mirror_qc.png", dpi=300, bbox_inches="tight")
    plt.close(fig)


def main() -> None:
    FIGDATA.mkdir(parents=True, exist_ok=True)
    rows, angle_rows, summary = collect_phi_psi()
    rows.to_csv(FIGDATA / "fig_phi_psi_mirror_qc_rama_coordinates.csv", index=False)
    angle_rows.to_csv(FIGDATA / "fig_phi_psi_mirror_qc_angles.csv", index=False)
    summary.to_csv(FIGDATA / "fig_phi_psi_mirror_qc_summary.csv", index=False)
    benchmark_rows, benchmark_summary = collect_benchmark_ld_phi_psi()
    benchmark_rows.to_csv(
        FIGDATA / "fig_phi_psi_benchmark_ld_rama_coordinates.csv", index=False
    )
    benchmark_summary.to_csv(
        FIGDATA / "fig_phi_psi_benchmark_ld_summary.csv", index=False
    )
    plot_benchmark_ld(benchmark_rows)
    print(summary.to_string(index=False))
    print(benchmark_summary.to_string(index=False))


if __name__ == "__main__":
    main()