"""Nat Biotechnol 2020, doi 10.1038/s41587-020-0505-4 (GLIPH2): Supplementary Table 1, sheet 'single cell TCR': one row per sorted
activated CD4+ T cell after Mtb lysate stimulation, latent TB infection (South African adolescents, 24 individuals), paired alpha/beta.
Only the primary alpha chain (Valpha / Jalpha / CDR3alpha) is kept; the second alpha (1,077 cells) is not loaded. Reads and confidence columns are not used for filtering.
Sheet 'bulk TCR' (10,501 beta rows, 12 individuals) is NOT loaded: the supplement describes it only as 'Sorted_Bulk' with the stimulation 'Mtb lysate / peptide pool'
and the selection 'activation marker / cytokine capture', with no CD4 statement for those samples, so CD4 cannot be told from the file.
Jalpha sometimes carries trailing numbers ('TRAJ45 61 0'): only the gene name is kept. Valpha names starting with 'm' (mTRDV2-2, mTRAV14D-1; 522 cells) are mouse-style
names that cannot be read as human genes: TRAV is left empty for them, the CDR3 is kept.
No epitope per TCR (GLIPH2 groups are in Supplementary Table 3, not used here)."""
import pandas as pd
from atlas import SRC, clones


def parse():
    s = pd.read_excel(SRC / "Huang2020/41587_2020_505_MOESM3_ESM.xlsx", sheet_name="single cell TCR")
    d = s.rename(columns={"Individual": "donor", "Vbeta": "TRBV", "Jbeta": "TRBJ", "CDR3beta": "CDR3b", "Valpha": "TRAV", "Jalpha": "TRAJ", "CDR3alpha": "CDR3a"})
    d["TRAJ"], d["TRAV"] = d.TRAJ.str.split().str[0], d.TRAV.where(~d.TRAV.astype(str).str.startswith("m"))
    d = clones(d[d.CDR3a.notna() | d.CDR3b.notna()].copy(), ["donor"])
    return d.assign(source="Huang 2020 (Nat Biotechnol)", study="doi:10.1038/s41587-020-0505-4", species="human", cd4_evidence="sorted CD4+",
                    antigen="Mycobacterium tuberculosis lysate", organism="Mycobacterium tuberculosis", context="latent TB infection, South African adolescent",
                    specificity_evidence="activation-marker sort (CD154+ CD69+) after Mtb lysate stimulation", annotation_level="antigen")
