"""Cancer Cell 2022, doi 10.1016/j.ccell.2022.03.006 (GSE198265, GSM5942456): melanoma patient X205, BD Rhapsody, CD4 memory cells
(Cell_Type_Experimental = T_CD4_memory). Beta chain only: no alpha, no V or J genes. The deposited beta CDR3 lacks the C and the last F/W
(cdr3b_core is set). Cells without a beta CDR3 are dropped.
CD4_neo = 1: neoantigen-specific (antigen in CD4_neo_antigen; 'unknown' = neoantigen not named in the table). CD4_self = 1: per the paper
(Results, Figure 2D-F: "specific for self antigens") cells of clonotypes found after CD40L sorting of TIL stimulated with a pool of
tumour-associated non-mutated antigens (Tyrosinase, Mart1, TRP2, GP100, SSX2, NY-ESO, MAGE A1, MAGE A3); which peptide is not in the table,
so the antigen is the pool. Both are antigen level (CD40L sort / IFN-gamma screening); the raw flag is kept in context. The other cells have no annotation.
Restriction HLA not in the table. Other patients of the paper are not in the downloaded files."""
import pandas as pd
from atlas import SRC


def parse():
    v = pd.read_csv(SRC / "Veatch2022/GSM5942456_X205_annotated_VDJ_update.csv.gz")
    v = v[v.Cell_Type_Experimental.eq("T_CD4_memory")].rename(columns={"TCR_Beta_Delta_CDR3_Translation_Dominant": "CDR3b"})
    v = v[v.CDR3b.str.fullmatch(r"[A-Z]{4,40}", na=False)].copy()
    neo, own = v.CD4_neo.eq(1), v.CD4_self.eq(1)
    v["antigen"] = v.CD4_neo_antigen.where(neo).map(lambda a: None if pd.isna(a) else "neoantigen " + a if a != "unknown" else "neoantigen (not named in the table)")
    v["antigen"] = v.antigen.mask(own, "tumour-associated self antigen pool (peptide not in the table)")
    v["context"] = "melanoma TIL, CD4 memory cells, CD4_neo=" + v.CD4_neo.astype(int).astype(str) + " CD4_self=" + v.CD4_self.astype(int).astype(str)
    v["donor"] = "X205"
    d = v.fillna({"antigen": ""}).groupby(["donor", "context", "antigen", "CDR3b"]).size().rename("cells").reset_index().replace("", None)
    d["cdr3b_core"] = d.CDR3b
    ann = d.antigen.notna()
    d["annotation_level"] = ann.map({True: "antigen", False: "none"})
    d["specificity_evidence"] = ann.map({True: "peptide screening of expanded PD-1high TIL (IFN-gamma, CD40L sort), TCRVb sequencing, matched to single cells", False: "none"})
    return d.assign(source="Veatch 2022 (Cancer Cell)", study="doi:10.1016/j.ccell.2022.03.006", species="human", cd4_evidence="CD4 memory cluster (single cell), sorted CD4+ TIL")
