"""Nat Med 2023, doi 10.1038/s41591-022-02110-9: index-sorted single cells of adolescents with Mtb infection (ACS and GC6-74 cohorts), after Mtb lysate stimulation
(activation-marker gate CD69+ / CD154+ and/or CD137+). Source: raw_single_cell_tcr_data_acs_controllers_and_progressors.csv.zip (csv member opened by name).
Loaded: Cell.Type == CD4, flag == GOOD, MAIT.Classification != MAIT.CDR3a (619 cells dropped). Only the primary alpha chain (Va, Ja, CDR3a) is kept, not alt.Va / alt.CDR3a.
One record per donor, group and clonotype: the study visit (D0 to D720) is not kept in the record. Group = donorGrp (Progressor / Non Progressor, as in the file).
No epitope per TCR. Bulk immunoSEQ repertoires (immuneACCESS) and Figure_5a files (merge with Huang2020, would duplicate it) are not loaded."""
import zipfile

import pandas as pd
from atlas import SRC, clones


def parse():
    z = zipfile.ZipFile(SRC / "Musvosvi2023/raw_single_cell_tcr_data_acs_controllers_and_progressors.csv.zip")
    d = pd.read_csv(z.open("raw_single_cell_tcr_data_acs_controllers_and_progressors.csv"), low_memory=False)
    d = d[d["Cell.Type"].eq("CD4") & d.flag.eq("GOOD") & d["MAIT.Classification"].ne("MAIT.CDR3a")]
    d = d.rename(columns={"donorId": "donor", "Vb": "TRBV", "Jb": "TRBJ", "CDR3b": "CDR3b", "Va": "TRAV", "Ja": "TRAJ", "CDR3a": "CDR3a"})
    d = d[d.CDR3a.notna() | d.CDR3b.notna()].assign(context="Mtb-infected adolescent, group " + d.donorGrp.astype(str))
    return clones(d, ["donor", "context"]).assign(source="Musvosvi 2023 (Nat Med)", study="doi:10.1038/s41591-022-02110-9", species="human", cd4_evidence="sorted CD4+",
                                                  antigen="Mycobacterium tuberculosis lysate", organism="Mycobacterium tuberculosis",
                                                  specificity_evidence="activation-marker sort (CD69+ / CD154+ and/or CD137+) after Mtb lysate stimulation", annotation_level="antigen")
