"""How TAPE writes modified residues: compare TAPE's sequences with the PDB entries' canonical sequences. For each TAPE chain whose PDB entry is available locally and whose canonical sequence has the same length, list every position where TAPE's letter differs from the PDB's canonical letter, with the PDB residue name at that position (e.g. MSE = selenomethionine). Read-only. usage: python -I scripts/check_modified_residues.py CASP12: TAPE's 21 chains, entries in data/original/rcsb (downloaded by build_ssp.py) train: TAPE training chains whose entries are in data/original/netsurfp_train_pdb (200 random entries, seed 0, downloaded for the NetSurfP label check) """ import os, sys, glob, json, collections sys.path.insert(0, "scripts") import gemmi import ssp_lib as L TAPE_DIR = "data/original/tape/secondary_structure/secondary_structure" def compare(rows, cif_dir): n = same = 0 diff, skipped = collections.Counter(), [] for r in rows: code, chain = r["id"].split("-", 1) try: st, can, _ = L.rcsb_chain(f"{cif_dir}/{code}.cif.gz", chain) except Exception as e: # chain renamed or entity layout changed in today's entry skipped.append((r["id"], f"{type(e).__name__}")); continue mon = [gemmi.Entity.first_mon(m) for m in st.get_entity_of(st[0][chain].get_polymer()).full_sequence] s = r["primary"] if len(s) != len(can): skipped.append((r["id"], f"length {len(s)} vs {len(can)}")); continue n += 1; same += s == can for a, b, m in zip(s, can, mon): if a != b: diff[(a, b, m)] += 1 t = gemmi.find_tabulated_residue(m) if t is None or not t.is_standard(): # every modified residue, whatever TAPE wrote there modified[(m, "TAPE X" if a == "X" else f"TAPE {a}")] += 1 return n, same, diff, skipped modified = collections.Counter() for name, fn, cif_dir in [("CASP12", "secondary_structure_casp12.json", "data/original/rcsb"), ("train", "secondary_structure_train.json", "data/original/netsurfp_train_pdb")]: have = {os.path.basename(f)[:4] for f in glob.glob(f"{cif_dir}/*.cif.gz")} rows = [r for r in json.load(open(f"{TAPE_DIR}/{fn}")) if r["id"][:4] in have] modified.clear() n, same, diff, skipped = compare(rows, cif_dir) print(f"== {name}: {n} chains compared ({len(skipped)} skipped: {skipped}); identical to the PDB canonical sequence: {same}") print(f" positions where TAPE differs: {sum(diff.values())}") for (a, b, m), c in diff.most_common(): print(f" TAPE {a} PDB canonical {b} PDB residue {m}: {c}") print(f" modified (non-standard) residues in these chains: {sum(modified.values())}; what TAPE wrote there: " f"{dict(collections.Counter(k[1] for k in modified.elements()))}") print(f" by residue: {dict(modified.most_common())}")