"""Cell 2020, doi 10.1016/j.cell.2020.05.017 (GSE149652): bladder cancer, sorted CD4 T cells from tumour and adjacent normal tissue, 10x.
CD4 files only (the CD8 files are never read). TCR table joined to cell info on Sample_ID + first 14 characters of `index` (= cell.barcode);
TCR rows without cell info (the healthy-blood samples) are not loaded: blood is taken from Kwek 2025, which also contains the same
tumour and normal cells (Kwek 2025 loads blood only). Donor = `patient`; treatment Atezo = anti-PD-L1, empty = untreated (patients Untreated A and B).
No antigen specificity. Cell type labels (CD4CM, CD4GZMB ...) are not put in context so that clonotypes stay one record per patient and tissue."""
import pandas as pd
from atlas import SRC, clones


def parse():
    t = pd.read_csv(SRC / "Oh2020/GSE149652_CD4_TIL_TCR_paired_matrice.csv.gz")
    c = pd.read_csv(SRC / "Oh2020/GSE149652_CD4_TIL_droplet_cellinfo_matrice.csv.gz", usecols=["index", "patient", "tissue", "treatment", "Sample_ID"])
    c["cell.barcode"] = c["index"].str[:14]
    assert not c.duplicated(["Sample_ID", "cell.barcode"]).any()
    d = t.merge(c, on=["Sample_ID", "cell.barcode"])
    d = d.rename(columns={"alphaCDR3": "CDR3a", "alphaVGene": "TRAV", "alphaJGene": "TRAJ", "betaCDR3": "CDR3b", "betaVGene": "TRBV", "betaJGene": "TRBJ"})
    d["donor"] = d.patient
    d["context"] = "bladder cancer, " + d.tissue.str.lower() + " tissue, " + d.treatment.map({"Atezo": "anti-PD-L1 treated", "Chemo": "chemotherapy treated"}).fillna("untreated")
    d = clones(d, ["donor", "context"])
    return d.assign(source="Oh 2020 (Cell)", study="GEO:GSE149652", species="human", cd4_evidence="sorted CD4+", specificity_evidence="none", annotation_level="none")
