Download code/17_experimental_structures.py from HarriziSaad/BULMA: direct link, hf CLI and curl.
- Browser
- Download file 3.14 kB
-
https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/17_experimental_structures.py
- Command line
-
hf download hf://datasets/HarriziSaad/BULMA/code/17_experimental_structures.py
-
curl -L -o 17_experimental_structures.py https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/17_experimental_structures.py
3.14 kB
| import json | |
| import urllib.request | |
| import numpy as np | |
| import pandas as pd | |
| from Bio.PDB import MMCIFParser, PDBParser, PPBuilder | |
| from common import RES, EXT, abc | |
| from structure_utils import correspondence, tm_score, alphafold_model | |
| PDB = EXT / "pdb" | |
| PDB.mkdir(parents=True, exist_ok=True) | |
| AF = EXT / "alphafold" | |
| A = abc().set_index("gene") | |
| def search(acc): | |
| q = {"query": {"type": "terminal", "service": "text", | |
| "parameters": {"attribute": "rcsb_polymer_entity_container_identifiers.reference_sequence_identifiers.database_accession", | |
| "operator": "exact_match", "value": acc}}, | |
| "return_type": "polymer_entity", "request_options": {"return_all_hits": True}} | |
| req = urllib.request.Request("https://search.rcsb.org/rcsbsearch/v2/query", data=json.dumps(q).encode(), | |
| headers={"Content-Type": "application/json"}) | |
| body = urllib.request.urlopen(req, timeout=60).read() | |
| return [h["identifier"] for h in json.loads(body)["result_set"]] if body else [] | |
| def ca(structure, chain=None): | |
| model = structure[0] | |
| chains = [model[chain]] if chain else list(model) | |
| res = [r for c in chains for r in c if "CA" in r and r.id[0] == " "] | |
| seq = "".join(str(pp.get_sequence()) for c in chains for pp in PPBuilder().build_peptides(c)) | |
| return seq, np.array([r["CA"].coord for r in res], dtype=float) | |
| rows = [] | |
| for gene, acc in A.uniprot.items(): | |
| for ent in search(acc): | |
| entry, eid = ent.split("_") | |
| meta = json.load(urllib.request.urlopen(f"https://data.rcsb.org/rest/v1/core/entry/{entry}", timeout=60)) | |
| pe = json.load(urllib.request.urlopen(f"https://data.rcsb.org/rest/v1/core/polymer_entity/{entry}/{eid}", timeout=60)) | |
| chain = pe["rcsb_polymer_entity_container_identifiers"]["auth_asym_ids"][0] | |
| f = PDB / f"{entry}.cif" | |
| if not f.exists(): | |
| urllib.request.urlretrieve(f"https://files.rcsb.org/download/{entry}.cif", f) | |
| s_exp, x_exp = ca(MMCIFParser(QUIET=True).get_structure(entry, f), chain) | |
| s_af, x_af = ca(PDBParser(QUIET=True).get_structure(gene, alphafold_model(acc, AF))) | |
| i_exp, i_af = correspondence(s_exp, s_af) | |
| tm, rmsd = tm_score(x_exp[i_exp], x_af[i_af], len(s_exp)) | |
| rows.append({"gene": gene, "pdb": entry, "chain": chain, "method": meta["exptl"][0]["method"], | |
| "resolution": (meta["rcsb_entry_info"].get("resolution_combined") or [np.nan])[0], | |
| "year": meta["rcsb_accession_info"]["initial_release_date"][:4], | |
| "doi": (meta.get("citation") or [{}])[0].get("pdbx_database_id_DOI"), | |
| "title": meta["struct"]["title"], "modelled_residues": len(s_exp), | |
| "uniprot_length": len(s_af), "coverage": len(s_exp) / len(s_af), | |
| "tm_score_af_vs_experimental": tm, "rmsd_core": rmsd}) | |
| out = pd.DataFrame(rows) | |
| out.to_csv(RES / "experimental_structures.csv", index=False) | |
| pd.set_option("display.width", 220) | |
| pd.set_option("display.max_colwidth", 70) | |