"""Draw each testable claim's controls (plan section 6): protein-disease pairs nobody in the census claimed.

    python code/controls.py <mapped claims csv> <proteins dir> <out csv>

For each testable claim, three UKB-PPP assays drawn at random, with a generator seeded from 20261027
and the claim's PMID, among assays with a cis credible set whose strongest lead's |z| lies in the same
fifth as the claim's assay's, whose gene isn't the claim's, and whose gene no paper in the census
claimed for the claim's disease (any of the terms it stands for). A control is tested against the claim's outcome study. Only protein
data (the leads' z and allele frequencies) are read. These are controls 1 to 3 (`draw` "fifth"). A well-powered
claim also gets controls 4 to 6 (`draw` "well powered"), the primary comparison's: drawn the same way, with a
generator seeded from its PMID and "well powered", from the assays whose strongest lead the claim's outcome study
could see at the claim's effect (well_powered.py). Every control carries the outcome's expected |z| per unit of log
odds ratio at its assay's strongest lead. <proteins dir> holds pqtl_sets.parquet, pqtl_marginal.parquet, and
pqtl_lead_af.parquet.
"""

import hashlib
import sys
from pathlib import Path

import numpy as np
import pandas as pd

sys.path.insert(0, str(Path(__file__).parent))
from well_powered import WELL_POWERED_Z, lead_power, rule_log_or, strongest_leads  # noqa: E402

SEED = 20261027
PER_CLAIM = 3


def claim_rng(key: str):
    h = int.from_bytes(hashlib.sha256(f"control|{key}".encode()).digest()[:8], "big")
    return np.random.default_rng(np.random.SeedSequence([SEED, h]))


def main(mapped: Path, proteins: Path, out: Path) -> None:
    claims = pd.read_csv(mapped, dtype=str)
    pq = pd.read_parquet(proteins / "pqtl_sets.parquet", columns=["studyLocusId", "studyId", "gene_id"])
    marg = pd.read_parquet(proteins / "pqtl_marginal.parquet", columns=["studyLocusId", "lead_z"])
    leads = strongest_leads(proteins)
    pq = pq.merge(marg, on="studyLocusId")
    strength = pq.assign(z=pq.lead_z.abs()).groupby(["studyId", "gene_id"]).z.max().reset_index()
    strength["fifth"] = pd.qcut(strength.z.rank(method="first"), 5, labels=False)
    fifth_of = dict(zip(strength.studyId, strength.fifth))
    elig = claims[claims.eligibility == "eligible"]
    # The genes the census claimed for each disease term, by every term a claim's disease stands for.
    claimed = {}
    for terms, genes in zip(elig.disease_terms.fillna(""), elig.ensembl.fillna("")):
        for t in filter(None, terms.split(";")):
            claimed.setdefault(t, set()).update(filter(None, genes.split(";")))
    rows = []
    for c in elig[elig.testable == "True"].to_dict("records"):
        fifth = max(fifth_of[a] for a in c["assays"].split(";"))
        taken = set().union(*(claimed.get(t, set()) for t in str(c["disease_terms"]).split(";")))
        pool = strength[(strength.fifth == fifth) & (strength.gene_id != c["ensembl"]) & ~strength.gene_id.isin(taken)].sort_values("studyId")
        # One assay per gene, so a gene with several assays isn't drawn more often.
        pool = pool.drop_duplicates("gene_id")
        power = lambda s: lead_power(leads.loc[s], c["outcome_source"], float(c["outcome_cases"]), float(c["outcome_controls"]))  # noqa: E731
        draws = [("fifth", claim_rng(c["pmid"]), pool)]
        if c["well_powered"] == "True":
            effect = rule_log_or(c["or"], c["or_unit"])
            draws.append(("well powered", claim_rng(f"{c['pmid']}|well powered"), pool[[power(s) * effect >= WELL_POWERED_Z for s in pool.studyId]]))
        number = 0
        for draw, rng, candidates in draws:
            if len(candidates) < PER_CLAIM:
                raise SystemExit(f"{c['pmid']}: only {len(candidates)} assays to draw its {draw} controls from")
            for i in sorted(rng.choice(len(candidates), PER_CLAIM, replace=False)):
                x = candidates.iloc[i]
                number += 1
                rows.append({"pmid": c["pmid"], "control": number, "gene_id": x.gene_id, "assay": x.studyId, "fifth": int(fifth),
                             "outcome_source": c["outcome_source"], "outcome_study": c["outcome_study"], "z_per_log_or": round(power(x.studyId), 4),
                             "draw": draw})
    t = pd.DataFrame(rows)
    t.to_csv(out, index=False)
    print(f"{len(t)} controls for {t.pmid.nunique()} claims:", t.draw.value_counts().to_dict())


if __name__ == "__main__":
    main(Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3]))
