"""Nat Med 2023, doi 10.1038/s41591-023-02556-5 (GSE227638): CD4+ T cells reactive to Candida albicans, C. tropicalis or Saccharomyces cerevisiae (activation-marker enrichment) from blood of
3 Crohn's disease patients (CD1-3) and 3 healthy donors (HD1-3), 10x 5' TCR libraries (filtered contig annotations, one file per donor and yeast species, 18 files; the 18 GEX libraries have no file here).
GSM -> donor, disease and yeast species come from the series matrix (sample title, "disease state" and "treatment" fields); they agree with the GSM map in the manifest.
Filter is_cell, high_confidence, productive; top-UMI TRA and TRB per cell. Every cell of a library comes from the enriched fraction, there is no per-cell flag; the antigen is the whole microbe, no epitope.
Same clonotype in the libraries of two yeast species stays two records (one per species). Which markers were used for enrichment is not stated in the series matrix: only "activation-marker enrichment" is claimed."""
import gzip
import pandas as pd
from atlas import SRC, clones, pair_contigs

SPECIES = {"C. albicans": "Candida albicans", "C.tropicalis": "Candida tropicalis", "S.cerevisiae": "Saccharomyces cerevisiae"}


def parse():
    rows = [l.rstrip("\n").split("\t") for l in gzip.open(SRC / "Martini2023/GSE227638_series_matrix.txt.gz", "rt")]
    row = lambda k: [[x.strip('"') for x in r[1:]] for r in rows if r[0] == k]
    title, gsm = row("!Sample_title")[0], row("!Sample_geo_accession")[0]
    chars = row("!Sample_characteristics_ch1")
    treat = next(c for c in chars if c[0].startswith("treatment:"))
    dis = next(c for c in chars if c[0].startswith("disease state:"))
    out = []
    for t, g, tr, di in zip(title, gsm, treat, dis):
        if not t.endswith(" TCR"): continue
        f = next((SRC / "Martini2023").glob(f"{g}_*_filtered_contig_annotations.csv.gz"))
        c = pd.read_csv(f)
        c = c[c.is_cell & c.high_confidence & c.productive]
        cells = pair_contigs(c, ["barcode"])
        cells["donor"] = t.split(", ")[-1].split()[0]
        cells["antigen"] = SPECIES[tr.split(": ")[1]]
        cells["context"] = "Crohn's disease patient" if "Crohn" in di else "healthy donor"
        out.append(cells)
    d = clones(pd.concat(out), ["donor", "antigen", "context"])
    d["context"] = d.context + ", yeast-reactive CD4+ T cells (" + d.antigen + ")"
    return d.assign(source="Martini 2023 (Nat Med)", study="doi:10.1038/s41591-023-02556-5", species="human", cd4_evidence="sorted CD4+", organism="whole microbe, no epitope",
                    specificity_evidence="activation-marker enrichment after yeast stimulation, no epitope mapped", annotation_level="antigen")
