"""Eur J Immunol 2021, doi 10.1002/eji.202048797 (GSE160097): juvenile idiopathic arthritis, 7 patients, synovial fluid (SF) and blood, sorted CD4+CD45RO+CD25- memory Tcon ("CD4" in the file name)
and CD4+CD45RO+CD127loCD25+ Treg, 10x contig annotations (27 CD4 files; the 14 CD8 files are not loaded). Patient = `pN` at the end of the file name (= "JIA patient N" of the series
matrix; `PMx` is only a sample code). Compartment and cell type from the file name. Filter is_cell, high_confidence, productive; top-UMI TRA and TRB per cell (Multi and IG contigs are dropped by
pair_contigs). Patient 3 has no blood Tcon file. No antigen."""
import pandas as pd
from atlas import SRC, clones, pair_contigs


def parse():
    out = []
    for f in sorted((SRC / "Maschmeyer2021").glob("*_PM*_*_filtered_contig_annotations.csv.gz")):
        _, _, ct, tissue, p, _ = f.name.split("_", 5)
        if ct not in ("CD4", "Treg"): continue
        c = pd.read_csv(f)
        c = c[c.is_cell & c.high_confidence & c.productive]
        d = pair_contigs(c, ["barcode"])
        d["donor"] = "JIA patient " + p[1:]
        d["context"] = ("juvenile idiopathic arthritis, " + {"SF": "synovial fluid", "blood": "blood"}[tissue] + ", "
                        + {"CD4": "CD4+CD45RO+CD25- memory Tcon", "Treg": "CD4+CD45RO+CD127loCD25+ Treg"}[ct])
        out.append(d)
    return clones(pd.concat(out), ["donor", "context"]).assign(
        source="Maschmeyer 2021 (Eur J Immunol)", study="GEO:GSE160097", species="human", cd4_evidence="sorted CD4+", specificity_evidence="none", annotation_level="none")
