"""bioRxiv 2025 (preprint), doi 10.1101/2025.05.06.652535 (GEO GSE293807): single-cell TCR of ex vivo sorted CD4+CD8- T cells (HLA-DR+ and HLA-DR- sorts, GEO design statement) from
tuberculosis patients and Mtb-sensitised donors. Only the 12 filtered_contig_annotations files are loaded (10 `MTB20A` to `MTB20L`, 2 `HIPC_tube_3/4` = GEO libraries HIPC_Pilot_Hu_3/4, nature of the donors not stated);
the bulk TCRB files of GSE301489 are not loaded.
The contig files carry no condition and no per-cell antigen flag: the MTB300 / PT132 pool labels belong to the bulk PDI-TCR libraries, not to these ex vivo sorted cells, and the Mtb-specific call is the authors' statistic
on the bulk data. So every record is level "none". Each MTB20 library pools many patients and visits by hashtag (12 hashtags per library in the GEO record; the assignment is only in the expression matrices, not downloaded; HIPC tubes not stated), so no donor can be assigned:
`donor` is empty and the raw library label is kept in `context`; the same TCR in two libraries stays two records.
Contigs: is_cell, high_confidence and productive only; top-UMI TRA and TRB per barcode pair a cell; one record per library and TCR, `cells` = number of cells."""
import glob
import re
import pandas as pd
from atlas import SRC, pair_contigs, clones


def parse():
    out = []
    for f in sorted(glob.glob(str(SRC / "Tippalagama2025/GSE293807/*_filtered_contig_annotations.csv.gz"))):
        lib = re.search(r"GSM\d+_(.+)_filtered", f).group(1)
        c = pd.read_csv(f)
        c = c[c.is_cell.astype(str).eq("True") & c.high_confidence.astype(str).eq("True") & c.productive.astype(str).eq("True")].assign(lib=lib)
        out.append(pair_contigs(c, ["lib", "barcode"]))
    d = pd.concat(out, ignore_index=True)
    d["context"] = "single-cell library " + d.lib + ", ex vivo sorted CD4+CD8-; donor not given in the files"
    return clones(d, ["context"], source="Tippalagama 2025 (GSE293807)", study="GEO:GSE293807", species="human", cd4_evidence="sorted CD4+CD8-",
                  organism=None, specificity_evidence="none: ex vivo sorted, no antigen flag in the contig files", annotation_level="none")
