BILBY-PRO / tests /test_release.py
EdeBruyn's picture
Initial upload for camera-ready version of NeurIPS paper. (part 5)
88152cd verified
Raw History Blame Contribute Delete
7.24 kB
# Copyright 2026 Emile de Bruyn
# SPDX-License-Identifier: Apache-2.0
import csv
import importlib.util
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "scripts"))
from prepare_motifbench import adapt_pdb
from validate_release import residues, validate
class ReleaseTests(unittest.TestCase):
def test_reference(self):
validate()
def test_corrected_contig_preserves_motif_and_target(self):
with (ROOT / "contig_specifications.csv").open(newline="") as handle:
row = next(r for r in csv.DictReader(handle) if r["problem"] == "1A22_PS01352_c1")
self.assertEqual(row["length"], "75")
self.assertEqual(row["contig"], "0-0;A1-5;16-20;B1-3;21-25;C1-30;0-0")
self.assertEqual(row["contig_original"], "0-0;B353-357;16-20;B374-376;21-25;B402-431;0-0")
def test_adapter_preserves_backbone_and_masks_redesign(self):
case = "1AHD_PS00027_c1"
with (ROOT / "test_cases.csv").open(newline="") as handle:
row = next(r for r in csv.DictReader(handle) if r["pdb_id"] == case)
redesign = residues(row["redesign_idcs"])
with tempfile.TemporaryDirectory() as temp:
path = Path(temp) / "adapted.pdb"
adapt_pdb(ROOT / "motif_pdbs" / f"{case}.pdb", path, redesign)
for line in path.read_text().splitlines():
if line.startswith("ATOM "):
key = (line[21], int(line[22:26]))
if key in redesign:
self.assertEqual(line[17:20], "UNK")
self.assertIn(line[12:16].strip(), {"N", "CA", "C", "O"})
else:
self.assertNotEqual(line[17:20], "UNK")
def test_reconstruction_rejects_unpinned_prosite(self):
with tempfile.TemporaryDirectory() as temp:
temp = Path(temp)
fake = temp / "prosite.dat"
fake.write_text("not the pinned PROSITE release\n")
config = temp / "config.yaml"
config.write_text(f"input:\n prosite_dat: {fake}\n")
result = subprocess.run(
[sys.executable, str(ROOT / "scripts/reconstruct.py"), "--config", str(config)],
text=True, capture_output=True,
)
self.assertNotEqual(result.returncode, 0)
self.assertIn("wrong PROSITE input", result.stderr)
@unittest.skipUnless(os.environ.get("MOTIFBENCH_WORK"), "set MOTIFBENCH_WORK for upstream smoke test")
def test_upstream_motif_info_and_segment_checker(self):
work = Path(os.environ["MOTIFBENCH_WORK"])
with (ROOT / "test_cases.csv").open(newline="") as handle:
cases = list(csv.DictReader(handle))
module_spec = importlib.util.spec_from_file_location(
"upstream_motif_info", work / "scripts/write_motifInfo_from_scaffoldInfo.py"
)
upstream = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(upstream)
for index, row in enumerate(cases, 1):
name = f"{index:02d}_{row['pdb_id']}"
chains = upstream.read_pdb_residues(work / "motif_pdbs" / f"{name}.pdb")
observed = {(chain, number) for chain, entries in chains.items()
for number, name in entries.items() if name == "UNK"}
self.assertEqual(observed, residues(row["redesign_idcs"]), row["pdb_id"])
with tempfile.TemporaryDirectory() as temp:
info = Path(temp) / "scaffold_info.csv"
output = Path(temp) / "motif_info.csv"
indices = (
0,
next(i for i, r in enumerate(cases) if ";" in r["motif_residues"]),
next(i for i, r in enumerate(cases) if r["pdb_id"] == "1A22_PS01352_c1"),
)
for index in indices:
row = cases[index]
name = f"{index + 1:02d}_{row['pdb_id']}"
chains = [segment[0] for segment in row["motif_residues"].split(";")]
with info.open("w", newline="") as handle:
writer = csv.writer(handle)
writer.writerow(["sample_num", "motif_placements"])
placement = "0/A/16/B/21/C/0" if row["pdb_id"] == "1A22_PS01352_c1" else "/".join(["0"] + chains + ["0"])
writer.writerow([0, placement])
subprocess.run([sys.executable, str(work / "scripts/write_motifInfo_from_scaffoldInfo.py"),
str(info), str(work / "motif_pdbs" / f"{name}.pdb"),
str(output)], check=True)
with output.open(newline="") as handle:
result = next(csv.DictReader(handle))
self.assertEqual(result["pdb_name"], name)
self.assertEqual(result["segment_order"], ";".join(chains))
self.assertEqual(residues(result["redesign_positions"]), residues(row["redesign_idcs"]))
subprocess.run([sys.executable, str(work / "scripts/check_segment_validity.py"),
"-i", str(work / "test_cases.csv"), "-t", str(output)], check=True)
if row["pdb_id"] == "1A22_PS01352_c1":
with (work / "example/contig_specifications.csv").open(newline="") as handle:
spec = next(r for r in csv.DictReader(handle) if r["problem"] == name)
self.assertEqual(spec["contig"], "0-0;A1-5;16-20;B1-3;21-25;C1-30;0-0")
@unittest.skipUnless(os.environ.get("MOTIFBENCH_WORK"), "set MOTIFBENCH_WORK for upstream smoke test")
def test_upstream_summary_is_ordered_before_scoring(self):
work = Path(os.environ["MOTIFBENCH_WORK"])
with (ROOT / "test_cases.csv").open(newline="") as handle:
cases = list(csv.DictReader(handle))
first = f"01_{cases[0]['pdb_id']}"
last = f"100_{cases[99]['pdb_id']}"
with tempfile.TemporaryDirectory() as temp:
result = Path(temp)
cluster = result / first / "esm_successful_backbones" / "diversity_cluster.tsv"
cluster.parent.mkdir(parents=True)
cluster.write_text(f"{first}_0.pdb {first}_0.pdb\n")
with (result / "summary_by_problem.csv").open("w", newline="") as handle:
writer = csv.writer(handle)
writer.writerow(["Problem", "Num_Solutions", "Novelty", "Success_Rate", "Num_Evaluated"])
writer.writerow([last, 0, 0, 0, 100])
writer.writerow([first, 1, 0, 1, 100])
subprocess.run([sys.executable, str(ROOT / "scripts/summarize_motifbench.py"),
str(work), str(result)], check=True)
with (result / "summary_by_problem_ordered.csv").open(newline="") as handle:
scored = list(csv.DictReader(handle))
self.assertEqual([r["Problem"] for r in scored], [first, last])
self.assertGreater(float(scored[0]["overall_score"]), 0)
self.assertEqual(float(scored[1]["overall_score"]), 0)
if __name__ == "__main__":
unittest.main()