"""Cell Rep 2024, doi 10.1016/j.celrep.2024.114704 (Observed TCR Space): healthy, unannotated, paired alpha/beta 10x CD4 T cells of three source studies, read from the slim tables in
`OTS2024/derived/` (see MANIFEST_repertoires.md). `source` = "OTS: <first author year>", `study` = PMID of the original study:
Coz 2023 (Sci Immunol, PMID 37027481, GSE214572): child tonsil sorted CD4 subsets treg, tfr, tfh, cd25hi tfh (names from GEO/OTS, sort gates not read), donors TC174 and TC341 (read from SRA).
Ren 2022 (Blood Adv, PMID 35947128, GSE197619): healthy-control blood CD4, donors N1 to N3; CD4 isolation method unverified.
Collora 2022 (Immunity, PMID 35320704, GSE187515): uninfected-control blood CD4, unstimulated, negative selection. Donor labels 2004 and 2023 come from the sample titles
(2004scTCR, 2023scTCR 1 and 2); whether the two 2023 samples are one donor is UNVERIFIED (could be 3 donors) and is said in `context`.
Cells: rows are deduplicated on (gsm, barcode) first (Coz: each GSM was sequenced in two SRA runs; where the two runs disagree on the contig call for a barcode the lowest SRR is kept),
then `cells` counts barcodes per donor, subset and TCR. `junction_aa` (with C and F/W anchors) is used for both chains. Alleles are stripped from V and J before counting, so cells that differ only in the allele call join one record. Subset in `context` ("none" in the slim table is left out).
OTS has no donor field: these labels are not shared with the same donors in other datasets."""
import pandas as pd
from atlas import SRC, clones

STUDIES = (("Coz2023", "OTS: Coz 2023", "PMID:37027481", "healthy child tonsil, sorted CD4 subset ", "sorted CD4+ (OTS cd4+ label)"),
           ("Ren2022", "OTS: Ren 2022", "PMID:35947128", "healthy control blood, CD4 T cells", "CD4+ T cells (OTS cd4+ label; isolation method not verified)"),
           ("Collora2022", "OTS: Collora 2022", "PMID:35320704", "healthy control blood, unstimulated CD4 T cells, negative selection; donor label from sample titles, 2023 may be one or two donors", "CD4+ T cells, negative selection (OTS cd4+ label)"))


def parse():
    out = []
    for name, source, study, ctx, ev in STUDIES:
        t = pd.read_csv(SRC / f"OTS2024/derived/{name}_cd4_paired_slim.csv.gz", dtype=str).sort_values("run", kind="stable").drop_duplicates(["gsm", "barcode"])
        g = lambda c: t[c].str.split("*").str[0]
        d = pd.DataFrame({"TRAV": g("v_call_alpha"), "TRAJ": g("j_call_alpha"), "CDR3a": t.junction_aa_alpha, "TRBV": g("v_call_beta"), "TRBJ": g("j_call_beta"), "CDR3b": t.junction_aa_beta, "donor": t.donor})
        d["context"] = ctx + (t.subset.str.replace("_cells", "").replace({"cd25hitfh": "CD25hi Tfh"}) if name == "Coz2023" else "")
        out.append(clones(d, ["donor", "context"], source=source, study=study, species="human", cd4_evidence=ev, specificity_evidence="none", annotation_level="none"))
    return pd.concat(out, ignore_index=True)
