"""Write the dataset card for hazemessam/SSP from a built and validated SSP. Numbers about the data are computed here from the files and logs; check results come from logs/validation.json (written by validate_ssp.py). Numbers from the earlier analyses are listed in ANALYSES with the script that computed them. usage: python -I scripts/make_ssp_readme.py [build/ssp] -> {out}/README.md, and copies the scripts to {out}/scripts/ """ import os, sys, glob, json, shutil, hashlib, statistics, collections import pyarrow as pa, pyarrow.parquet as pq, pyarrow.compute as pc OUT = sys.argv[1] if len(sys.argv) > 1 else "build/ssp" TAPE_SPLITS = ["train", "validation", "cb513", "ts115", "casp12"] ROUNDS = ["casp12", "casp13", "casp14", "casp15", "casp16"] SCRIPTS = ["build_ssp.py", "ssp_lib.py", "validate_ssp.py", "make_ssp_readme.py", "compare_ss_tools_helpers.py", "check_modified_residues.py", "check_netsurfp_train.py", "compare_ss_tools.py", "ss_assign.py", "compare_casp12_archive.py", "casp12_superpose.py"] # Results of the analyses run before the build (outputs recorded in reports/SSP_report.md) ANALYSES = { "train_check": dict(script="check_netsurfp_train.py", chains=200, residues=48_889, ss8=99.95, ss3=99.96, identical=191), "tools": dict(script="compare_ss_tools.py", residues=6_417, chain=99.94, entry=99.77), "casp_files": dict(script="compare_casp12_archive.py, casp12_superpose.py", in_archive=16, residues=4_121, ss8=91.92, ss3=94.69, same=9, same_rmsd="0.00-0.06", slight=2, slight_rmsd="0.35-0.55", far=5, far_rmsd="1.5-5.6"), "modified": dict(script="check_modified_residues.py", casp12_chains=20, casp12_mod=44, train_chains=238, train_mod=308, train_res="MSE 296, FME 2, ACE 2, CSO 2, 6V1 2, CSD 1, CME 1, PCA 1, TOX 1"), "tape_code": "tape/datasets.py, class SecondaryStructureDataset, commit 6d345c2 (github.com/songlab-cal/tape)", } A = ANALYSES V = json.load(open(f"{OUT}/logs/validation.json")) assert V["passed"], "validation did not pass; run validate_ssp.py first" def n(x): return f"{x:,}" def table(headers, rows): out = ["| " + " | ".join(headers) + " |", "| " + " | ".join("---" for _ in headers) + " |"] return "\n".join(out + ["| " + " | ".join(str(c) for c in r) + " |" for r in rows]) def sha256(path): h = hashlib.sha256() with open(path, "rb") as f: for b in iter(lambda: f.read(1 << 20), b""): h.update(b) return h.hexdigest() def flat(col): return pc.list_flatten(col) # ------------------------------------------------------------------------------------------------ statistics stats = {} for path in sorted(glob.glob(f"{OUT}/*/*.parquet")): key = os.path.relpath(path, OUT) t = pq.read_table(path, columns=["id", "sequence", "ss3", "ss8", "resolved", "valid_mask", "casp_targets", "target_match", "target_identity", "in_tape"]) seq, res, vm = t["sequence"].to_pylist(), t["resolved"].to_pylist(), t["valid_mask"].to_pylist() lens = [len(s) for s in seq] c3, c8 = collections.Counter(), collections.Counter() for a, b, r in zip(t["ss3"].to_pylist(), t["ss8"].to_pylist(), res): c3.update(x for x, m in zip(a, r) if m); c8.update(x for x, m in zip(b, r) if m) pairs = [(m, i) for ms, ids in zip(t["target_match"].to_pylist(), t["target_identity"].to_pylist()) if ms for m, i in zip(ms, ids)] stats[key] = dict(rows=len(seq), ids=len(set(t["id"].to_pylist())), residues=sum(lens), resolved=sum(sum(r) for r in res), masked=sum(v.count(0) for v in vm if v), min=min(lens), max=max(lens), median=int(statistics.median(lens)), c3=c3, c8=c8, match=collections.Counter(m for m, _ in pairs), differs=[i for m, i in pairs if m == "differs"], targets=len(pairs), in_tape=sum(1 for x in t["in_tape"].to_pylist() if x), sha=sha256(path), mb=os.path.getsize(path) / 2 ** 20) if key == "tape/cb513.parquet": def runs(m): return sum(1 for a, b in zip([0] + m, m) if b == 1 and a == 0) stats[key].update(one_run=sum(1 for m in vm if runs(m) == 1), no_run=sum(1 for m in vm if 1 not in m), masked_resolved=sum(1 for v, r in zip(vm, res) for a, b in zip(v, r) if a == 0 and b == 1)) S = stats.get # rcsb/casp: residues with a complete backbone (N, CA, C, O) that mkdssp left without a label full_bb = full_bb_unlabelled = 0 for v in ("rcsb", "casp"): for r in ROUNDS: for row in pq.read_table(f"{OUT}/{v}/{r}.parquet", columns=["resolved", "n_xyz", "ca_xyz", "c_xyz", "o_xyz"]).to_pylist(): for k, m in enumerate(row["resolved"]): if all(row[a][k] is not None for a in ("n_xyz", "ca_xyz", "c_xyz", "o_xyz")): full_bb += 1; full_bb_unlabelled += m == 0 assert full_bb_unlabelled == 1, "the card describes exactly one such residue (DHI in 7utd-A); update the text" _t12 = pq.read_table(f"{OUT}/rcsb/casp12.parquet", columns=["casp_targets", "in_tape"]).to_pylist() tape12_targets = sum(len(r["casp_targets"]) for r in _t12 if r["in_tape"]) # TAPE's extra fields, measured over all five TAPE splits fields = collections.Counter() rng = {} for s in TAPE_SPLITS: t = pq.read_table(f"{OUT}/tape/{s}.parquet", columns=["sequence", "resolved", "rsa", "asa_max", "phi", "psi", "interface", "evolutionary"]) R = flat(t["resolved"]) res_mask, unres_mask = pc.equal(R, 1), pc.equal(R, 0) for f in ("rsa", "asa_max", "phi", "psi"): v = pc.filter(flat(t[f]), res_mask) if f in ("phi", "psi"): v = pc.filter(v, pc.not_equal(v, 360)) if f == "asa_max": v = pc.filter(v, pc.not_equal(v, -1)) mm = pc.min_max(v).as_py() lo, hi = rng.get(f, (float("inf"), float("-inf"))) rng[f] = (min(lo, mm["min"]), max(hi, mm["max"])) u = pc.filter(flat(t[f]), unres_mask) fields[f"{f}_unresolved_nonzero"] += len(u) - pc.sum(pc.equal(u, 0)).as_py() if f != "asa_max" else 0 e = pc.list_flatten(flat(t["evolutionary"])) mm = pc.min_max(e).as_py() lo, hi = rng.get("evolutionary", (float("inf"), float("-inf"))) rng["evolutionary"] = (min(lo, mm["min"]), max(hi, mm["max"])) fields["evo_len_not_30"] += pc.sum(pc.not_equal(pc.list_value_length(flat(t["evolutionary"])), 30)).as_py() fields["interface_1"] += pc.sum(flat(t["interface"])).as_py() fields["interface_unresolved_1"] += pc.sum(pc.filter(flat(t["interface"]), unres_mask)).as_py() seqs = t["sequence"].to_pylist() for sq, r, phi, psi, am in zip(seqs, t["resolved"].to_pylist(), t["phi"].to_pylist(), t["psi"].to_pylist(), t["asa_max"].to_pylist()): L_ = len(r) for k in range(L_): if not r[k]: continue if phi[k] == 360: fields["phi360"] += 1; fields["phi360_end"] += k == 0 or not r[k - 1] if psi[k] == 360: fields["psi360"] += 1; fields["psi360_end"] += k == L_ - 1 or not r[k + 1] if am[k] == -1: fields["asa_max_-1"] += 1; fields["asa_max_-1_X"] += sq[k] == "X" assert all(fields[f"{f}_unresolved_nonzero"] == 0 for f in ("rsa", "phi", "psi")) and fields["interface_unresolved_1"] == 0 assert fields["evo_len_not_30"] == 0 and fields["asa_max_-1"] == fields["asa_max_-1_X"] logs = {} for f in sorted(glob.glob(f"{OUT}/logs/*_*.json")): l = json.load(open(f)) if "split" in l: logs[f"{l['source']}/{l['split']}.parquet"] = l pp = collections.Counter() for l in logs.values(): pp.update(l.get("polyproline") or {}) dssp_version = next(l["dssp"] for l in logs.values() if l.get("dssp")) tape_ok = all(c["ok"] for c in V["checks"] if "identical to TAPE's" in c["check"]) r12 = V["rcsb_casp12_vs_tape"] flag = r12["disorder_flag_vs_pdb"] tape12_resolved = sum(v for k, v in flag.items() if k.startswith("tape_disorder_1")) tape12_missing = sum(v for k, v in flag.items() if k.startswith("tape_disorder_0")) cb = S("tape/cb513.parquet") tr = S("tape/train.parquet") listed = {"casp12": 45, "casp13": 41, "casp14": 26, "casp15": 65, "casp16": 44} def lenstr(s): return f"{n(s['min'])} to {n(s['max'])} (median {n(s['median'])})" # ------------------------------------------------------------------------------------------------ front matter configs = { "rcsb": [(s, f"tape/{s}.parquet") for s in TAPE_SPLITS[:4]] + [(r, f"rcsb/{r}.parquet") for r in ROUNDS], "casp": [(s, f"tape/{s}.parquet") for s in TAPE_SPLITS[:4]] + [(r, f"casp/{r}.parquet") for r in ROUNDS], "tape": [(s, f"tape/{s}.parquet") for s in TAPE_SPLITS], } fm = ["---", "pretty_name: SSP (protein secondary structure)", "license: other", "license_name: mixed", "tags:", "- biology", "- proteins", "- protein-language-models", "- secondary-structure", "- token-classification", "- dssp", "- casp", "- tape", "size_categories:", "- 10K; TAPE's repository is BSD-3-Clause. - `rcsb`: mmCIF files from the RCSB PDB (); PDB data are CC0 1.0. - `casp`: target structures and sequences from the Protein Structure Prediction Center (). - Labels: DSSP ({dssp_version}, ). Please cite TAPE and NetSurfP-2.0 for the training data and the classic test sets, and CASP, the PDB and DSSP for the CASP test sets: ```bibtex @inproceedings{{rao2019tape, title = {{Evaluating Protein Transfer Learning with TAPE}}, author = {{Rao, Roshan and Bhattacharya, Nicholas and Thomas, Neil and Duan, Yan and Chen, Xi and Canny, John and Abbeel, Pieter and Song, Yun S.}}, booktitle = {{Advances in Neural Information Processing Systems}}, year = {{2019}} }} @article{{klausen2019netsurfp, title = {{NetSurfP-2.0: Improved prediction of protein structural features by integrated deep learning}}, author = {{Klausen, Michael Schantz and Jespersen, Martin Closter and Nielsen, Henrik and Jensen, Kamilla Kjaergaard and Jurtz, Vanessa Isabell and Soenderby, Casper Kaae and Sommer, Morten Otto Alexander and Winther, Ole and Nielsen, Morten and Petersen, Bent and others}}, journal = {{Proteins: Structure, Function, and Bioinformatics}}, year = {{2019}} }} @article{{kabsch1983dssp, title = {{Dictionary of protein secondary structure: pattern recognition of hydrogen-bonded and geometrical features}}, author = {{Kabsch, Wolfgang and Sander, Christian}}, journal = {{Biopolymers}}, volume = {{22}}, number = {{12}}, pages = {{2577--2637}}, year = {{1983}} }} @article{{berman2000pdb, title = {{The Protein Data Bank}}, author = {{Berman, Helen M. and Westbrook, John and Feng, Zukang and Gilliland, Gary and Bhat, T. N. and Weissig, Helge and Shindyalov, Ilya N. and Bourne, Philip E.}}, journal = {{Nucleic Acids Research}}, volume = {{28}}, number = {{1}}, pages = {{235--242}}, year = {{2000}} }} ``` For CASP, cite the CASP assessment paper of the round you use. ## Changes Created on {V['date']}. """ os.makedirs(f"{OUT}/scripts", exist_ok=True) for s in SCRIPTS: shutil.copy(f"scripts/{s}", f"{OUT}/scripts/{s}") open(f"{OUT}/README.md", "w").write("\n".join(fm) + "\n" + body) print(f"wrote {OUT}/README.md and {len(SCRIPTS)} scripts")