"""Compare the AlphaFold Database model of calmodulin with every full-length calmodulin in the PDB.

Run: python calmodulin_compare.py   (needs numpy, pandas, biopython; downloads about 400 small C-alpha-only files)
1. Download AF-P0DP23-F1-model_v6.pdb (AlphaFold Database, human calmodulin-1).
2. RCSB sequence search (>= 95% identity) for polymer entities of the AlphaFold sequence. Keep 140 to 151 residues
   and the methods X-ray, electron microscopy and solution NMR.
3. For each entity fetch the C-alpha atoms of its first chain from the RCSB model server, map residues to the AlphaFold
   sequence by global alignment, and keep chains with at least 48 N-lobe and 50 C-lobe C-alpha atoms.
4. Lobes are residues 7 to 66 and 86 to 148 of the 149-residue UniProt sequence (6 to 65 and 85 to 147 in the
   conventional numbering that omits Met1).
5. Per model (up to 20 NMR models): lobe-by-lobe RMSD to AlphaFold, the rotation between the two lobes after fitting each
   lobe separately, the distance between lobe C-alpha centroids, and the RMSD of both lobes fitted together.
6. First model of each entity: all-pairs RMSD over the common lobe residues, and RMSD to AlphaFold.
"""
import collections
import json
import os
import urllib.request

import numpy as np
import pandas as pd
from Bio import Align
from Bio.PDB import PDBParser, PPBuilder
from Bio.PDB.MMCIF2Dict import MMCIF2Dict

AF_URL = "https://alphafold.ebi.ac.uk/files/AF-P0DP23-F1-model_v6.pdb"
if not os.path.exists("AF-P0DP23-F1-model_v6.pdb"):
    req = urllib.request.Request(AF_URL, headers={"User-Agent": "Mozilla/5.0"})
    open("AF-P0DP23-F1-model_v6.pdb", "wb").write(urllib.request.urlopen(req, timeout=60).read())
af_structure = PDBParser(QUIET=1).get_structure("af", "AF-P0DP23-F1-model_v6.pdb")
REF = str(PPBuilder().build_peptides(af_structure)[0].get_sequence())
AF = {r.id[1]: r["CA"].coord for r in af_structure[0]["A"] if "CA" in r}
NL, CL = range(7, 67), range(86, 149)
METHODS = ("X-RAY DIFFRACTION", "ELECTRON MICROSCOPY", "SOLUTION NMR")


def post(url, body):
    req = urllib.request.Request(url, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"})
    return json.loads(urllib.request.urlopen(req, timeout=120).read())


def entities():
    q = {"query": {"type": "terminal", "service": "sequence",
                   "parameters": {"evalue_cutoff": 1e-10, "identity_cutoff": 0.95, "sequence_type": "protein", "value": REF}},
         "return_type": "polymer_entity", "request_options": {"paginate": {"start": 0, "rows": 1000}}}
    ids = [r["identifier"] for r in post("https://search.rcsb.org/rcsbsearch/v2/query", q)["result_set"]]
    gql = """query($ids:[String!]!){polymer_entities(entity_ids:$ids){rcsb_id entity_poly{pdbx_seq_one_letter_code_can rcsb_sample_sequence_length}
      rcsb_polymer_entity_container_identifiers{asym_ids} entry{rcsb_id exptl{method} rcsb_entry_info{resolution_combined} struct{title}
      rcsb_accession_info{initial_release_date}}}}"""
    rows = []
    for i in range(0, len(ids), 100):
        rows += post("https://data.rcsb.org/graphql", {"query": gql, "variables": {"ids": ids[i:i + 100]}})["data"]["polymer_entities"]
    return [r for r in rows if 140 <= r["entity_poly"]["rcsb_sample_sequence_length"] <= 151 and r["entry"]["exptl"][0]["method"] in METHODS]


def fetch(row):
    os.makedirs("cam_cache", exist_ok=True)
    fn = f"cam_cache/{row['rcsb_id']}.cif"
    if not os.path.exists(fn):
        entry, asym = row["rcsb_id"].split("_")[0], row["rcsb_polymer_entity_container_identifiers"]["asym_ids"][0]
        url = f"https://models.rcsb.org/v1/{entry}/atoms?label_asym_id={asym}&label_atom_id=CA&encoding=cif&copy_all_categories=false"
        open(fn, "wb").write(urllib.request.urlopen(url, timeout=90).read())
    return fn


def fit(P, Q):
    """Best rotation and RMSD of P onto Q (Kabsch)."""
    pc, qc = P.mean(0), Q.mean(0)
    U, _, Vt = np.linalg.svd((P - pc).T @ (Q - qc))
    R = Vt.T @ np.diag([1, 1, np.sign(np.linalg.det(Vt.T @ U.T))]) @ U.T
    return R, np.sqrt((((P - pc) @ R.T - (Q - qc)) ** 2).sum(1).mean())


def angle(R):
    return np.degrees(np.arccos(np.clip((np.trace(R) - 1) / 2, -1, 1)))


def metrics(xyz):
    n, c = [i for i in NL if i in xyz], [i for i in CL if i in xyz]
    if len(n) < 48 or len(c) < 50:
        return None
    PN, QN = np.array([xyz[i] for i in n]), np.array([AF[i] for i in n])
    PC, QC = np.array([xyz[i] for i in c]), np.array([AF[i] for i in c])
    RN, rn = fit(PN, QN)
    RC, rc = fit(PC, QC)
    both = n + c
    _, rall = fit(np.array([xyz[i] for i in both]), np.array([AF[i] for i in both]))
    return dict(rmsd_Nlobe=rn, rmsd_Clobe=rc, rmsd_both=rall, interlobe_angle=angle(RC @ RN.T),
                centroid_dist=float(np.linalg.norm(PN.mean(0) - PC.mean(0))))


aligner = Align.PairwiseAligner(mode="global", match_score=2, mismatch_score=-1, open_gap_score=-3, extend_gap_score=-0.5)
results, coords = [], {}
for row in entities():
    D = MMCIF2Dict(fetch(row))
    if "_atom_site.Cartn_x" not in D:
        continue
    seq = row["entity_poly"]["pdbx_seq_one_letter_code_can"].replace("\n", "")
    mapping = {}
    for (a0, a1), (b0, b1) in zip(*aligner.align(REF, seq)[0].aligned):
        for k in range(a1 - a0):
            mapping[b0 + k + 1] = a0 + k + 1
    models = collections.defaultdict(dict)
    for i, s in enumerate(D["_atom_site.label_seq_id"]):
        if s in (".", "?") or D["_atom_site.label_alt_id"][i] not in (".", "A", "?") or int(s) not in mapping:
            continue
        models[int(D["_atom_site.pdbx_PDB_model_num"][i])][mapping[int(s)]] = np.array(
            [float(D[f"_atom_site.Cartn_{a}"][i]) for a in "xyz"])
    e = row["entry"]
    for mn in sorted(models)[:20]:
        m = metrics(models[mn])
        if m:
            coords[(row["rcsb_id"], mn)] = models[mn]
            results.append(dict(id=row["rcsb_id"], entry=e["rcsb_id"], method=e["exptl"][0]["method"], model=mn, title=e["struct"]["title"],
                                res=(e["rcsb_entry_info"]["resolution_combined"] or [None])[0],
                                year=e["rcsb_accession_info"]["initial_release_date"][:4], **m))
df = pd.DataFrame(results)
df.to_csv("calmodulin-models.csv", index=False)
print(df.groupby("method").agg(entities=("id", "nunique"), models=("id", "size"), median_centroid=("centroid_dist", "median")))

keys = sorted({k[0] for k in coords})
first = {k: coords[(k, min(m for (e, m) in coords if e == k))] for k in keys}
IDX = list(NL) + list(CL)


def rms(a, b):
    common = [i for i in IDX if i in a and i in b]
    return fit(np.array([a[i] for i in common]), np.array([b[i] for i in common]))[1] if len(common) >= 100 else np.nan


pairs = [rms(first[a], first[b]) for i, a in enumerate(keys) for b in keys[i + 1:]]
to_af = [rms(first[k], AF) for k in keys]
print("experiment vs experiment, median / 10th / 90th percentile:", np.nanpercentile(pairs, [50, 10, 90]).round(1))
print("AlphaFold vs experiment, median / 10th / 90th percentile:", np.nanpercentile(to_af, [50, 10, 90]).round(1), "nearest", np.nanmin(to_af).round(1))
