"""Front Immunol 2020, doi 10.3389/fimmu.2020.00611 (GSE147794): healthy donors, sorted CD4+CD25+CD127lo Tregs from umbilical cord blood (CB) and adult peripheral blood (APB),
before (Pre) and after (Post) in vitro expansion, 10x all_contig_annotations (4 libraries). Tissue and expansion come from the file name and agree with the series matrix. Filter is_cell,
high_confidence, productive (read as text: the column is mixed); top-UMI TRA and TRB per cell. The number of donors per library is not in the files, so `donor` is left empty and the clone
counts are per library: a clonotype seen Pre and Post, or in CB and APB, is one record per library. Post-expansion cells are cultured Tregs. No antigen.
The CD4 Treg sort scheme comes from the paper title / GEO design; FOXP3 was not checked in the files."""
import pandas as pd
from atlas import SRC, clones, pair_contigs


def parse():
    out = []
    for f in sorted((SRC / "Motwani2020").glob("GSM*_Treg_*_all_contig_annotation*.csv.gz")):
        _, tissue, _, exp, _ = f.name.split("_", 4)
        c = pd.read_csv(f, low_memory=False)
        c = c[c.is_cell & c.high_confidence & c.productive.astype(str).eq("True")]
        d = pair_contigs(c, ["barcode"])
        d["context"] = (f"healthy {'umbilical cord blood' if tissue == 'CB' else 'adult peripheral blood'} Treg, "
                        + ("before" if exp == "Pre" else "after") + " in vitro expansion")
        out.append(d)
    return clones(pd.concat(out), ["context"]).assign(
        source="Motwani 2020 (Front Immunol)", study="GEO:GSE147794", species="human", cd4_evidence="sorted CD4+ Treg", specificity_evidence="none", annotation_level="none")
