"""Rheumatology (Oxford) 2023, doi 10.1093/rheumatology/kead220 (GSE216245): rheumatoid arthritis, 4 patients, synovium-infiltrating helper T cells ("cell type: helper T cell" in GEO; the sort
scheme is not stated beyond that), 10x VDJ consensus annotations (one row per clonotype and chain, no cell barcodes, no cell counts). Patient N = file DK21229_0N = "RA_Patient_N" TCR library of the
series matrix. Alpha and beta of one `clonotype_id` are paired; a clonotype with two alpha or two beta gives one record per combination; only productive consensus rows with an amino acid CDR3.
`cells` is left empty (the table has reads and UMIs, not cells). No antigen."""
import pandas as pd
from atlas import SRC


def parse():
    out = []
    for f in sorted((SRC / "Etori2023").glob("GSE216245_DK21229_0?_consensus_annotations.csv.gz")):
        c = pd.read_csv(f)
        c = c[c.productive.astype(str).eq("True") & c.cdr3.notna()]
        ch = lambda k, r: c[c.chain == k].rename(columns=r)[["clonotype_id"] + list(r.values())]
        d = ch("TRA", {"v_gene": "TRAV", "j_gene": "TRAJ", "cdr3": "CDR3a"}).merge(ch("TRB", {"v_gene": "TRBV", "j_gene": "TRBJ", "cdr3": "CDR3b"}), on="clonotype_id", how="outer")
        d["donor"] = "RA patient " + f.name.split("_0")[1][0]
        out.append(d.drop(columns="clonotype_id"))
    return pd.concat(out).drop_duplicates().assign(
        source="Etori 2023 (Rheumatology)", study="GEO:GSE216245", species="human", cd4_evidence="helper T cells of synovium (GEO, paper title: CD4+); sort scheme not verified",
        context="rheumatoid arthritis, synovium-infiltrating helper T cell", specificity_evidence="none", annotation_level="none")
