"""Nat Commun 2019, doi 10.1038/s41467-019-13234-x (GSE135852): Supplementary Table 2, single-cell sorted hypocretin (HCRT) peptide:HLA-DQ0602 tetramer+ CD4+ T cells
from narcolepsy type 1 patients (P) and DQ6 matched controls (C), one row per well, beta and dominant alpha chain. Donor, tetramer peptide and group are parsed from
`Sample name` (for example `P10-HCRT(1-13)`: patient 10, tetramer loaded with HCRT(1-13)); four peptides occur (1-13, 25-37, 56-69, 87-100). The peptide is given only as a residue
range, the sequence is not in the table. Restriction as the manifest states (GEO summary: DQ6). Only the dominant alpha chain is used (the secondary alpha is left out).
One stray row (sample name "ffff", no TCR) is dropped. Wells with one chain give single-chain records. No functional assay: specificity is the tetramer sort."""
import pandas as pd
from atlas import SRC, clones


def parse():
    t = pd.read_excel(SRC / "Jiang2019/GSE135852_S2_All_wells_containing_single_cells_NC.xlsx", header=None).iloc[4:]
    t = t[t[0].astype(str).str.match(r"^[PC]\d+-HCRT\(\d+-\d+\)$")]
    s = t[0].str.extract(r"^([PC]\d+)-(.*)$")
    d = pd.DataFrame({"donor": s[0], "epitope": s[1], "TRBV": t[3], "TRBJ": t[5], "CDR3b": t[6], "TRAV": t[9], "TRAJ": t[10], "CDR3a": t[11]})
    d["context"] = "narcolepsy type 1 " + d.donor.str[0].map({"P": "patient", "C": "DQ6 matched control"}) + ", HCRT peptide:DQ0602 tetramer+ CD4+ T cell"
    return clones(d, ["donor", "epitope", "context"]).assign(
        source="Jiang 2019 (Nat Commun)", study="doi:10.1038/s41467-019-13234-x", species="human", cd4_evidence="sorted CD4+", restriction="HLA-DQ6 (DQB1*06:02)",
        antigen="hypocretin", organism="Homo sapiens (autoantigen)", specificity_evidence="peptide:MHC tetramer", annotation_level="epitope")
