"""Cell 2020, doi 10.1016/j.cell.2020.10.001 (GSE152522): sorted memory CD4+ T cells, COVID-19 patients and healthy donors, 10x paired CDR3 amino acids
(no V/J genes, no cell barcode in the deposited tables; the manifest mentions a barcode column that is not there).
Loaded: `cd4t0n6` (0 h and 6 h cells) and `cd4t24` (24 h cells plus 6,735 cells with orig.stim_time 6); `cd4t6` is a subset of `cd4t0n6` and is not loaded.
The two files share no library. When a cell carries several chains (separated by ';') only the first chain of each type is kept.
Antigen: peptide codes M (membrane pool), S (spike pool), MS (both) are taken from the manifest. Code `A` (healthy HT donors) is UNVERIFIED: the raw code is kept in
`context` and no antigen is claimed. 0 h cells are ex vivo activation-marker+ cells, not antigen-sorted: annotation "none".
Cells without a hashtag donor call (orig.donor empty) get the donor "unassigned" plus their library; they are not merged across libraries.
cd4t24 contains library CoVi17_4_MS_24_9P with orig.stim_time 6 (library name says 24): the metadata value is used as is."""
import pandas as pd
from atlas import SRC, clones


def parse():
    d = pd.concat([pd.read_csv(SRC / f"Meckiff2020/GSE152522_{n}_annotation.txt.gz", sep="\t", low_memory=False) for n in ("cd4t0n6", "cd4t24")], ignore_index=True)
    d["CDR3a"], d["CDR3b"] = (d[f"{c}.aa.chains.tag"].str.split(";").str[0] for c in ("TRA", "TRB"))
    d = d[d.CDR3a.notna() | d.CDR3b.notna()].copy()
    pep, h = d["orig.peptide"].astype(str), d["orig.stim_time"].astype(int)
    d["donor"] = d["orig.donor"].fillna("unassigned " + d.origlib)
    d["context"] = ("SARS-CoV-2 study, " + d["orig.severity"].fillna("healthy or unrecorded severity") + ", " + h.astype(str) + " h, peptide code " + pep
                    + ", " + d["orig.project_id"].astype(str))
    d["pool"] = pep.map({"M": "SARS-CoV-2 membrane peptide pool", "S": "SARS-CoV-2 spike peptide pool", "MS": "SARS-CoV-2 spike and membrane peptide pools"})
    d["kind"] = pep.map(lambda p: "ex vivo" if p == "0" else "unverified" if p == "A" else "pool")
    d["h"] = h
    r = clones(d, ["donor", "context", "pool", "kind", "h"])
    r["antigen"] = r.pool
    r["organism"] = r.pool.map(lambda x: "SARS-CoV-2" if isinstance(x, str) else None)
    gate = r.h.map({6: "CD154+ CD69+ at 6 h", 24: "CD137+ CD69+ at 24 h"})
    r["specificity_evidence"] = r.kind.map({"pool": "activation-marker sort after peptide pool stimulation", "unverified": "activation-marker sort after peptide pool code A (pool unverified)",
                                            "ex vivo": "none (0 h ex vivo activation-marker+ cells, not antigen-sorted)"})
    r.loc[r.kind.eq("pool"), "specificity_evidence"] += " (" + gate + ")"
    r["annotation_level"] = r.kind.map({"pool": "antigen", "unverified": "none", "ex vivo": "none"})
    return r.drop(columns=["pool", "kind", "h"]).assign(source="Meckiff 2020 (Cell)", study="doi:10.1016/j.cell.2020.10.001", species="human", cd4_evidence="sorted memory CD4+")
