"""eLife 2022, doi 10.7554/eLife.76339 (GSE182320), mouse C57BL/6 wild type: spleen GP66:I-Ab+ CD4+ T cells (tetramer-sorted), LCMV Armstrong or
clone 13, day 7 / 21 / 80, 7 `all_contig_annotations` files. Infection and day are taken from the GEO sample titles (the GEO `treatment` field says
"Clone 13" on every sample and is wrong, per MANIFEST_mouse.md). Filters: is_cell, high_confidence, productive; TRA and TRB only.
Two files pool two samples (SCAF963 = d7-Arm-3 + d21-Arm-1; SCAF964 = d7-Cl13-1 + d7-Cl13-2). The split needs hashtags from the GEX files (not
downloaded), so these keep the pooled label in `donor` and `context`; cells of the two samples cannot be told apart, and identical barcodes from the two
samples (all end in -1) cannot be excluded, so a few cells of these files may be merged by pair_contigs. Level "antigen" (tetramer gate, no peptide per clonotype)."""
import pandas as pd
from atlas import SRC, pair_contigs, clones

TRUE = lambda s: s.astype(str).str.lower().eq("true")
SAMPLES = {"GSM5526724": ("d7-Arm-1", "Armstrong", "day 7"), "GSM5526725": ("d7-Arm-2", "Armstrong", "day 7"),
           "GSM5526726": ("d7-Arm-3 + d21-Arm-1 (pooled)", "Armstrong", "day 7 and day 21, two samples pooled"),
           "GSM5526727": ("d80-Arm-1", "Armstrong", "day 80"), "GSM5526728": ("d80-Arm-2", "Armstrong", "day 80"),
           "GSM5526729": ("d7-Cl13-1 + d7-Cl13-2 (pooled)", "clone 13", "day 7, two samples pooled"), "GSM5526730": ("d21-Cl13-2", "clone 13", "day 21")}


def parse():
    out = []
    for f in sorted((SRC / "Andreatta2022").glob("*_all_contig_annotations.csv.gz")):
        donor, virus, day = SAMPLES[f.name.split("_")[0]]
        c = pd.read_csv(f, low_memory=False)
        c = c[TRUE(c.is_cell) & TRUE(c.high_confidence) & TRUE(c.productive)]
        d = pair_contigs(c, ["barcode"]).assign(donor=donor, context=f"C57BL/6, LCMV {virus}, {day}, spleen", organism=f"LCMV {virus}")
        out.append(clones(d, ["donor", "context", "organism"]))
    return pd.concat(out).assign(source="Andreatta 2022 (eLife)", study="doi:10.7554/eLife.76339", species="mouse", cd4_evidence="sorted CD4+ (GP66:I-Ab tetramer+)",
                                 restriction="H2-IAb", antigen="LCMV glycoprotein GP66-77", specificity_evidence="peptide:MHC tetramer", annotation_level="antigen")
