"""Rebuild TAPE's CASP12 secondary-structure labels from the PDB with several tools and compare residue by residue. usage: python -I scripts/compare_ss_tools.py [mkdssp-binary ...] Reference: data/original/tape/secondary_structure/secondary_structure/secondary_structure_casp12.json (TAPE's download; ss8 ints decoded as G H I B E S T C). Compared on residues TAPE marks as resolved (disorder == 1). """ import sys, json, collections sys.path.insert(0, "scripts") import ss_assign as A from Bio import Align S8, S3 = "GHIBESTC", "HEC" tape = json.load(open("data/original/tape/secondary_structure/secondary_structure/secondary_structure_casp12.json")) binaries = sys.argv[1:] aligner = Align.PairwiseAligner(mode="global", match_score=2, mismatch_score=-1, open_gap_score=-5, extend_gap_score=-0.5) def project(ref_seq, seqres, labels): """Map labels on `seqres` onto positions of `ref_seq` (identity if equal, else via global alignment).""" if ref_seq == seqres: return labels, "identical" aln = aligner.align(ref_seq, seqres)[0] out = [None] * len(ref_seq) for (a0, a1), (b0, b1) in zip(*aln.aligned): for k in range(a1 - a0): out[a0 + k] = labels[b0 + k] return out, f"aligned ({sum(a1 - a0 for (a0, a1), _ in zip(*aln.aligned))}/{len(ref_seq)} positions)" tools = {"mdtraj (chain)": lambda st, ch: A.mdtraj_assign(st, ch, "chain"), "mdtraj (whole ASU)": lambda st, ch: A.mdtraj_assign(st, ch, "asu")} try: import pydssp # noqa: F401 tools["PyDSSP (chain, 3-state)"] = lambda st, ch: A.pydssp_assign(st, ch) except ImportError: print("PyDSSP not installed; skipping it") for b in binaries: tools[f"{b} (chain)"] = lambda st, ch, b=b: A.mkdssp_assign(st, ch, b, "chain") tools[f"{b} (whole ASU)"] = lambda st, ch, b=b: A.mkdssp_assign(st, ch, b, "asu") tot = {t: collections.Counter() for t in tools} per_protein = collections.defaultdict(dict) seq_notes, mask_notes = {}, {} for rec in tape: pdb, ch = rec["id"].split("-") st = A.load(f"data/original/casp12_pdb/{pdb}.cif.gz") t8 = [S8[x] for x in rec["ss8"]] t3 = [S3[x] for x in rec["ss3"]] resolved = [d == 1 for d in rec["disorder"]] for name, fn in tools.items(): seqres, labels = fn(st, ch) lab, how = project(rec["primary"], seqres, labels) seq_notes[rec["id"]] = how c = tot[name] p = collections.Counter() for i, ok in enumerate(resolved): have = lab[i] is not None if ok and not have: c["tape_resolved_tool_missing"] += 1 if have and not ok: c["tool_has_tape_unresolved"] += 1 if ok and have: if lab[i] == "P": # DSSP 4 polyproline; older DSSP (and TAPE) call these residues coil c["P_as_C"] += 1 lab[i] = "C" c["n"] += 1; p["n"] += 1 l3 = A.SS8_TO_SS3.get(lab[i], lab[i]) c["ss3"] += l3 == t3[i]; p["ss3"] += l3 == t3[i] if "PyDSSP" not in name: c["ss8"] += lab[i] == t8[i]; p["ss8"] += lab[i] == t8[i] if lab[i] != t8[i]: c[f"conf {t8[i]}->{lab[i]}"] += 1 per_protein[rec["id"]][name] = p print(rec["id"], seq_notes[rec["id"]], flush=True) print("\n=== Agreement with TAPE CASP12 on TAPE-resolved residues") for name, c in tot.items(): n = c["n"] s8 = f"{100 * c['ss8'] / n:6.2f}%" if "PyDSSP" not in name else " n/a " exact = sum(1 for pid in per_protein if per_protein[pid][name]["ss3"] == per_protein[pid][name]["n"]) exact8 = sum(1 for pid in per_protein if per_protein[pid][name]["ss8"] == per_protein[pid][name]["n"]) if "PyDSSP" not in name else "-" print(f"{name:26s} residues={n:5d} ss8={s8} ss3={100 * c['ss3'] / n:6.2f}% proteins identical ss3={exact}/21 ss8={exact8}/21" f" | resolved-but-missing={c['tape_resolved_tool_missing']} tool-labels-on-unresolved={c['tool_has_tape_unresolved']}") conf = sorted(((k, v) for k, v in c.items() if k.startswith("conf ")), key=lambda kv: -kv[1])[:8] if conf: print(" top ss8 disagreements (TAPE->tool):", ", ".join(f"{k[5:]}:{v}" for k, v in conf)) print("\nPer protein ss8 agreement:", " / ".join(n for n in tools if "PyDSSP" not in n)) for pid, d in per_protein.items(): print(f" {pid:8s} n={d['mdtraj (chain)']['n']:4d} " + " ".join(f"{100 * d[n]['ss8'] / max(d[n]['n'], 1):6.2f}%" for n in tools if "PyDSSP" not in n))