"""Diabetes 2021, doi 10.2337/db20-1072 (GSE159001), mouse NOD and C57BL/6 (B6), 8-week-old females, thymus: bulk MiXCR TCR-seq of CD4+CD8-Thy1.1+
cells sorted by Foxp3-GFP: GFP+ = thymic Treg (files 1-12), GFP- = conventional CD4 (files 13-24). Strain, chain and gate read from the file names and
checked on the GEO sample records (sorting, strain and title fields). Alpha and beta are in separate files and are NOT paired: one record per sample,
chain and clonotype (V, J, CDR3aa; rows that differ only in CDR3 nucleotides are merged), the other chain empty. `cells` is empty: the `count` column
is a UMI count, not a cell number. Donor = GEO sample accession (replicates are pools of unknown mouse number; whether alpha and beta replicate n come
from the same mice is not stated, so no shared donor is assumed). No antigen specificity, level "none"."""
import pandas as pd
from atlas import SRC


def parse():
    out = []
    for f in sorted((SRC / "Galindo2021").glob("*.txt.gz")):
        gsm, _, chain, strain, *rest = f.name.removesuffix(".txt.gz").split("_")
        gate = "GFP-negative conventional CD4" if "GFPneg" in rest else "GFP+ Treg"
        t = pd.read_csv(f, sep="\t").drop_duplicates(["v", "j", "cdr3aa"])
        a = chain == "TCRalpha"
        out.append(pd.DataFrame({"TRAV": t.v if a else None, "TRAJ": t.j if a else None, "CDR3a": t.cdr3aa if a else None,
                                 "TRBV": None if a else t.v, "TRBJ": None if a else t.j, "CDR3b": None if a else t.cdr3aa,
                                 "donor": gsm, "context": f"{strain}, thymus, {gate}, {chain[3:]} chain, {rest[-1]}"}))
    return pd.concat(out).assign(source="Galindo 2021 (Diabetes)", study="doi:10.2337/db20-1072", species="mouse", cd4_evidence="sorted CD4+CD8- thymocytes",
                                 specificity_evidence="none", annotation_level="none", cells=None)
