"""eLife 2023, doi 10.7554/eLife.79016 (GSE196301): juvenile idiopathic arthritis, 9 patients, bulk TCR beta (no alpha) of sorted CD3+CD4+CD25highCD127low Treg and CD25low/int CD127high/int
non-Treg from blood (PB) and synovial fluid (SF, left or right knee), 48 samples read from GSE196301_RAW.tar. Patient, visit, tissue and cell type come from the series matrix (sample GSM id
joins to the file name). `Read.count` is a read count, not a cell count, so `cells` is left empty. 12 of the 48 files are tab separated, the others comma separated (sniffed). A row is one clonotype (CDR3, V, J) of one sample; the same CDR3 in two samples
(Treg and non-Treg, two visits, two tissues) stays two records because the context differs. No antigen."""
import gzip
import tarfile
import pandas as pd
from atlas import SRC


def parse():
    rows = [l.rstrip("\n").split("\t") for l in gzip.open(SRC / "Mijnheer2023/GSE196301_series_matrix.txt.gz", "rt")]
    row = lambda k: [[x.strip('"') for x in r[1:]] for r in rows if r[0] == k]
    gsm, chars = row("!Sample_geo_accession")[0], row("!Sample_characteristics_ch1")
    val = lambda i: [x.split(": ", 1)[1] for x in chars[i]]
    meta = pd.DataFrame({"tissue": val(0), "celltype": val(1), "subject": val(2), "visit": val(3)}, index=gsm)
    out = []
    with tarfile.open(SRC / "Mijnheer2023/GSE196301_RAW.tar") as tf:
        for m in tf.getmembers():
            s = meta.loc[m.name.split("_")[0]]
            d = pd.read_csv(tf.extractfile(m), compression="gzip", sep=None, engine="python").rename(columns={"CDR3.amino.acid.sequence": "CDR3b", "V.gene": "TRBV", "J.gene": "TRBJ"})
            d["donor"] = s.subject
            d["context"] = f"juvenile idiopathic arthritis, {s.tissue.lower()}, {s.celltype}, {s.visit}"
            out.append(d[["CDR3b", "TRBV", "TRBJ", "donor", "context"]].drop_duplicates())
    return pd.concat(out).assign(source="Mijnheer 2023 (eLife)", study="GEO:GSE196301", species="human", cd4_evidence="sorted CD4+", specificity_evidence="none", annotation_level="none")
