BILBY-PRO / scripts /prepare_motifbench.py
EdeBruyn's picture
Initial upload for camera-ready version of NeurIPS paper. (part 5)
88152cd verified
Raw History Blame Contribute Delete
3.43 kB
#!/usr/bin/env python3
# Copyright 2026 Emile de Bruyn
# SPDX-License-Identifier: Apache-2.0
"""Make an isolated MotifBench-compatible working copy."""
import argparse
import csv
import shutil
import subprocess
import tempfile
from pathlib import Path
from validate_release import ROOT, residues, validate
UPSTREAM_COMMIT = "d5a714c6a376c701e29dfa66d42a1b13c5e339e0"
def adapt_pdb(source, destination, redesign):
with source.open() as inp, destination.open("w") as out:
for line in inp:
if line.startswith("ATOM "):
key = (line[21], int(line[22:26]))
if key in redesign:
if line[12:16].strip() not in {"N", "CA", "C", "O"}:
continue
line = line[:17] + "UNK" + line[20:]
out.write(line)
def prepare(upstream, dest):
upstream = upstream.resolve(strict=True)
dest = dest.resolve()
if dest.exists() or dest == upstream or upstream in dest.parents:
raise ValueError("destination must be a new directory outside the upstream checkout")
if not dest.parent.is_dir():
raise ValueError("destination parent directory must already exist")
commit = subprocess.check_output(
["git", "-C", str(upstream), "rev-parse", "HEAD"], text=True
).strip()
if commit != UPSTREAM_COMMIT:
raise ValueError(f"expected MotifBench commit {UPSTREAM_COMMIT}, found {commit}")
status = subprocess.check_output(
["git", "-C", str(upstream), "status", "--porcelain"], text=True
)
if status:
raise ValueError("MotifBench checkout must be clean")
validate()
with (ROOT / "test_cases.csv").open(newline="") as handle:
cases = list(csv.DictReader(handle))
with (ROOT / "contig_specifications.csv").open(newline="") as handle:
reader = csv.DictReader(handle)
specs = list(reader)
names = {row["pdb_id"]: f"{i:02d}_{row['pdb_id']}" for i, row in enumerate(cases, 1)}
with tempfile.TemporaryDirectory(prefix=".bilby-motifbench-", dir=dest.parent) as temp:
work = Path(temp) / "work"
shutil.copytree(upstream, work, ignore=shutil.ignore_patterns(".git", "motif_pdbs"))
(work / "motif_pdbs").mkdir()
shutil.copy2(ROOT / "test_cases.csv", work / "test_cases.csv")
for row in cases:
case = row["pdb_id"]
adapt_pdb(ROOT / "motif_pdbs" / f"{case}.pdb",
work / "motif_pdbs" / f"{names[case]}.pdb",
residues(row["redesign_idcs"]))
with (work / "example" / "contig_specifications.csv").open("w", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=["problem", "length", "contig"],
extrasaction="ignore")
writer.writeheader()
for row in specs:
row["problem"] = names[row["problem"]]
writer.writerow(row)
work.rename(dest)
print(f"Created {dest} with {len(cases)} adapted cases; original checkout unchanged.")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("upstream", type=Path, help="clean pinned MotifBench checkout")
parser.add_argument("destination", type=Path, help="new working directory")
args = parser.parse_args()
prepare(args.upstream, args.destination)
if __name__ == "__main__":
main()