BenchRep-T / immuneML /README.md
lordim's picture
Add immuneML-ready AIRR export (immuneML/) and document it in the cards
432c0d7 verified
|
Raw History Blame Contribute Delete
15.5 kB
# BenchRep-T: immuneML-ready AIRR export
This folder contains every BenchRep-T repertoire in AIRR Rearrangement format. It also has metadata files, the benchmark's fixed fold splits, and runnable specifications for [immuneML](https://github.com/uio-bmi/immuneML).
The specimens and rows are the same as in the native files under `Mal-ID/` and `immunoSEQ/`. Only the columns differ: they are mapped onto one shared AIRR schema, and junctions are rebuilt where they can be checked.
- **Source:** built from dataset revision `165c55149366eadfef1507240aa8c0c60d391fbb` with `utils/immuneml_export.py` and `utils/immuneml_tasks.py` from [github.com/boyd-lab/BenchRep-T](https://github.com/boyd-lab/BenchRep-T).
- **Tested with:** immuneML 3.0.27 (with pandas 2.3.3) and the `airr` 2.0.0 schema validator. See [Verification](#verification).
## Quick start
```bash
pip install "immuneML==3.0.27" "pandas<3"
```
```python
# Download one cohort with its task files and spec (here: rheumatoid arthritis)
from huggingface_hub import snapshot_download
snapshot_download(
"lordim/BenchRep-T", repo_type="dataset", local_dir="BenchRep-T",
allow_patterns=["immuneML/immunoSEQ/Savola_RA/*", "immuneML/tasks/ra/*",
"immuneML/specs/ra.yaml"],
)
```
From the BenchRep-T code repository, the same download is `python -m utils.huggingface_data --layout immuneml --cohort ra --output-dir BenchRep-T`.
```bash
cd BenchRep-T # specs use paths relative to this directory
immune-ml immuneML/specs/ra.yaml results/ra
```
> **Use pandas 2 with immuneML 3.0.27.** immuneML declares `pandas>=2.1.0`, so a plain `pip install immuneML` currently installs pandas 3. With pandas 3, immuneML 3.0.27 fails to import AIRR files whose text columns are only partly filled, such as Mal-ID's `d_call`. immuneML's own lock file pins pandas 2.1.1.
## Layout
```
immuneML/
├── README.md # this file
├── MANIFEST.tsv # every file: sha256, size; repertoires also: source file, source sha256, rows
├── export_report.json # per-cohort row counts and junction statistics
├── Mal-ID/ metadata.csv + repertoires/ 550 specimens
├── immunoSEQ/
│ ├── Savola_RA/ metadata.csv + repertoires/ 91
│ ├── Musvosvi_TB/ metadata.csv + repertoires/ 140
│ ├── Rawat_T1D/ metadata.csv + repertoires/ 614
│ ├── Mitchell_T1D/ metadata.csv + repertoires/ 196
│ └── Emerson_CMV/ metadata.csv + repertoires/ 761
├── tasks/<task>/ metadata.csv, fold{0,1,2}_train.csv, fold{0,1,2}_test.csv
└── specs/<task>.yaml one immuneML specification per task
```
Repertoire file names are the same as in the native folders. `MANIFEST.tsv` maps every exported repertoire to its native source file and that file's checksum.
## Repertoire files
Each repertoire file is a gzipped, tab-separated AIRR Rearrangement file. It has one row for each row of the native file, in the same order: no rows are added, removed or merged. Every file has the same 20 columns, including all 14 fields that AIRR requires:
| Column | Mal-ID | immunoSEQ cohorts (Savola, Musvosvi, Rawat, Mitchell, Emerson) |
|---|---|---|
| `sequence_id` | native `sequence_id` | generated as `<specimen_label>\|<row number>`, with rows numbered from 0 |
| `sequence` | native read, uppercased | native rearrangement `sequence` (identical to Adaptive's `nucleotide`) |
| `rev_comp` | `F` (every native `strand` is `+`) | `F` |
| `productive`, `vj_in_frame` | native `t`/`f` written as `T`/`F` | `T`/`T` (native files keep only Adaptive `sequenceStatus == "In"`) |
| `stop_codon` | native `t`/`f` written as `T`/`F` | empty: the Adaptive exports carry no stop-codon flag |
| `locus` | `TRB` (native `TCRB`) | `TRB` |
| `v_call`, `j_call` | native, with alleles (e.g. `TRBV12-3*01`) | native, gene level (e.g. `TRBV12-3`) |
| `d_call` | native | empty (BenchRep-T doesn't harmonize Adaptive D calls) |
| `sequence_alignment`, `germline_alignment`, `v_cigar`, `d_cigar`, `j_cigar` | empty (full IgBLAST fields stay in `Mal-ID/repertoires`) | empty |
| `junction`, `junction_aa` | rebuilt; see below | rebuilt; see below |
| `cdr3_aa` | native `cdr3_aa`, unchanged | native `cdr3_aa`, unchanged |
| `duplicate_count` | empty: Mal-ID files have no count column | native `num_reads` (= Adaptive `count (templates/reads)`); empty where Adaptive gives none or `-1` |
| `repertoire_id` | specimen label | specimen label |
### `cdr3_aa` and `junction_aa`
These follow IMGT definitions:
- The **junction** runs from the conserved cysteine (2nd-CYS, position 104) to the conserved phenylalanine or tryptophan (J-PHE/J-TRP, position 118), both included.
- The **CDR3** is the junction without those two anchor residues (positions 105–117).
**`cdr3_aa` is the BenchRep-T benchmark sequence.** It is copied from the native files unchanged, and every method in the paper uses it. It is also immuneML's default analysis region (`region_type: IMGT_CDR3`), so the specs analyse exactly the sequences the benchmark used.
**`junction` and `junction_aa` are new.** The native files don't have them; Mal-ID's columns exist but are empty. They are rebuilt from nucleotide data. A candidate junction is kept only if it passes every check:
1. its length is 3 × (length of `cdr3_aa` + 2);
2. it contains only A, C, G and T;
3. it translates without a stop codon;
4. its translation without the first and last residue equals `cdr3_aa`;
5. it occurs in `sequence`.
If no candidate passes, both columns are left empty and the row is still exported. Anchor residues are not required to be C and F/W; the table below reports how often they are.
How candidates are found:
- **Mal-ID:** take the last codon of IgBLAST's `fwr3`, then `cdr3`, then the first codon of `fwr4`, after removing alignment gaps (`-`) and uppercasing.
- **Savola, Musvosvi, Mitchell:** take Adaptive's annotated window, `nucleotide[vIndex : vIndex + cdr3Length]`, which Adaptive defines to span the junction. If the window is unusable, fall back to the search below.
- **Rawat, Emerson** (no position columns): translate `sequence` in all three reading frames, find `cdr3_aa`, and take one codon on each side. If more than one valid candidate is found, keep the one with C…F/W anchors. If that still leaves more than one, the junction is left empty (`ambiguous`).
| Cohort | Rows | Junction rebuilt | Anchors C…F/W¹ | Not rebuilt (reason) |
|---|---:|---:|---:|---|
| Mal-ID | 71,302,390 | 99.778% | 99.60% | 146,897 nucleotide CDR3 doesn't translate to `cdr3_aa`; 11,577 length; 134 stop codon |
| Savola_RA | 1,079,493 | 99.9997% | 99.28% | 3 length |
| Musvosvi_TB | 13,963,315 | 99.9998% | 99.34% | 21 length |
| Rawat_T1D | 55,538,654 | 99.9994% | 99.50% | 341 ambiguous |
| Mitchell_T1D | 26,298,037 | 99.9998% | 99.56% | 42 length |
| Emerson_CMV | 119,423,488 | 99.998% | 99.45% | 2,534 ambiguous; 15 stop codon |
¹ Share of rebuilt junctions that start with C and end with F or W.
A "length" rejection means the annotated window's length doesn't match `cdr3_aa`, and the frame search found no valid alternative. Per-method and per-reason counts are in `export_report.json`.
Using junctions in immuneML:
- With `region_type: IMGT_JUNCTION`, immuneML analyses `junction_aa` and drops the rows without a junction (0.22% of Mal-ID rows, a handful elsewhere). Every other row is kept.
- immuneML 3.0.27 stores empty `junction`/`junction_aa` cells internally as the text `NAN`. Under IMGT_JUNCTION, only its default illegal-character check removes those rows. Don't combine `region_type: IMGT_JUNCTION` with `import_illegal_characters: true`.
**There is no nucleotide `cdr3` column, on purpose.**
- It would only repeat `junction[3:-3]`.
- immuneML 3.0.27 turns empty cells of a partly filled `cdr3` column into `NAN`, then silently drops those rows as containing illegal characters, even under the default IMGT_CDR3 analysis.
- Without the column, immuneML derives `cdr3` from `junction` itself, and keeps every row.
### Other conversion notes
- **Counts.** BenchRep-T methods weight every row equally and never read counts; the example specs do the same (`reads: unique`). `duplicate_count` is provided for other analyses.
- immuneML reads an empty `duplicate_count` as `-1`.
- It is empty for all Mal-ID rows, and for 8,313,647 Emerson rows (7.0%) in 77 of the 761 Emerson files, where Adaptive reported no template count.
- Mal-ID rows are kept as published, so several rows can share a clone or CDR3.
- **V/J gene names.** Mal-ID calls keep alleles; the immunoSEQ calls are gene level, with V genes that BenchRep-T treats as indistinguishable already collapsed. Before comparing genes across cohorts, strip alleles (`TRBV7-8*01` → `TRBV7-8`) and collapse IMGT singleton names as `utils/gene_harmonization.py::canonicalize_gene` does. The example specs use `cdr3_aa` only.
- **Rows that immuneML would filter.**
- The native files are already filtered to productive rearrangements with standard-amino-acid `cdr3_aa`.
- With the default import settings (`import_productive: true`, `import_illegal_characters: false`), immuneML 3.0.27 keeps every row; this was checked on all cohorts (see Verification).
## Metadata files
`<cohort>/metadata.csv` is comma-separated (immuneML doesn't read tab-separated metadata) and has one row per specimen:
| Column | Content |
|---|---|
| `filename` | repertoire file name, resolved against `<cohort>/repertoires/` |
| `subject_id` | specimen label. immuneML's manual splits match on `subject_id` and require it to be unique, so it identifies the specimen, not the person |
| `identifier` | `<cohort key>__<specimen label>`, unique across all cohorts |
| `participant_label` | the person: specimens of one participant share it |
| `disease` | the native label, unchanged (e.g. `Healthy/Background`, `Healthy`, `Controller`) |
| `fold` | BenchRep-T's fixed outer fold, 0–2 |
| `age`, `sex`, `ancestry` | where the native metadata has them: Mal-ID and Emerson have all three; Rawat and Mitchell have age and sex |
immuneML treats every column except `filename`, `subject_id` and `identifier` as a label.
These files keep only the fields above. The full native metadata, including HLA, autoantibody and sequencing statistics, stays in `<cohort>/metadata.tsv` outside this folder. Two fields are deliberately left out here:
- Rawat's exact specimen dates;
- Mitchell's `ancestry`, a cohort-level constant set during conversion (see DATASHEET.md).
## Tasks and folds
Each task folder lists only the task's two classes. Filenames are relative to `immuneML/`, so one dataset can span cohorts.
For each fold k:
- `fold{k}_test.csv` holds that fold's specimens;
- `fold{k}_train.csv` holds the other two folds.
This is exactly how the BenchRep-T evaluators split, and their per-fold predictions are pooled for the reported metrics. immuneML's `k_fold` and `stratified_k_fold` splits don't reproduce these folds: they ignore participants and draw new folds.
| Task | Cohort(s) | Positive class | Negative class | Specimens | Test fold 0 / 1 / 2 |
|---|---|---|---|---:|---|
| `malid-hiv` | Mal-ID | `HIV` | `Healthy/Background` | 295 | 99 / 99 / 97 |
| `malid-t1d` | Mal-ID | `T1D` | `Healthy/Background` | 293 | 98 / 100 / 95 |
| `malid-lupus` | Mal-ID | `Lupus` | `Healthy/Background` | 261 | 88 / 87 / 86 |
| `malid-covid19` | Mal-ID | `Covid19` | `Healthy/Background` | 255 | 85 / 86 / 84 |
| `malid-influenza` | Mal-ID | `Influenza` | `Healthy/Background` | 234 | 78 / 78 / 78 |
| `mitchell-t1d` | Mitchell_T1D | `T1D` | `Healthy/Background` | 196 | 65 / 66 / 65 |
| `t1d-pooled` | Mal-ID + Mitchell_T1D | `T1D` | `Healthy/Background` | 489 | 163 / 166 / 160 |
| `rawat-t1d` | Rawat_T1D | `T1D` | `Healthy/Background` | 614 | 205 / 205 / 204 |
| `tb-progression` | Musvosvi_TB | `Progressor` | `Controller` | 140 | 47 / 48 / 45 |
| `ra` | Savola_RA | `Rheumatoid Arthritis` | `Healthy` | 91 | 30 / 33 / 28 |
| `cmv` | Emerson_CMV | `CMV` | `Healthy/Background` | 761 | 321 / 320 / 120 |
Notes on the folds:
- **Shared controls.** The five Mal-ID tasks share the same 197 healthy controls.
- **Pooled T1D.** `t1d-pooled` adds a `source_cohort` column. It keeps only the demographic columns both cohorts have (age, sex).
- **Emerson.** Fold 2 is the independent validation cohort (cohort 2, 120 specimens); folds 0 and 1 split the discovery cohort. The paper's CMV results are fold 2.
- **Rawat.** The paper's Rawat results are fold 2 (204 specimens).
- **RA.** All 91 specimens are included. The paper's 90-specimen RA comparison is a bootstrap subset that leaves out `RA6-SF`; that is not an exclusion from the dataset. Participant RA6 has three specimens, all in fold 1.
- **TB folds are not fully participant-grouped.** Three controllers have specimens in more than one fold: `07-0177` (folds 0, 1, 2), `07-0202` (0, 2) and `04-1104` (0, 2). The export keeps the folds exactly as the benchmark used them.
## Specifications
`specs/<task>.yaml` imports the task as an AIRR repertoire dataset with `region_type: IMGT_CDR3`. It encodes repertoires as 3-mer frequencies of `cdr3_aa` (`KmerFrequency`, `reads: unique`) and trains a class-balanced `LogisticRegression`. It contains one `TrainMLModel` instruction per fold, each using a manual split on that fold's train and test CSVs.
The specs are runnable examples of the data contract, not the benchmark's methods. Change the encodings and models as you like, but keep the dataset and `manual_config` blocks to stay on the benchmark folds.
Run the specs from the directory that contains `immuneML/`. Mal-ID (71 M rows) and Emerson (119 M rows) are the largest cohorts, so start with a smaller one.
## Verification
Before publication, the export was checked as follows:
- **Against the native files:** every row of every file was re-read and compared with its native source. Checks: row counts; `cdr3_aa`, `v_call`, `j_call` and counts unchanged; `sequence` changed only by uppercasing; `sequence_id` unique per file; every rebuilt junction re-checked against the rules above.
- **AIRR schema:** all 287,605,377 rows in all 2,352 files pass the `airr` 2.0.0 Rearrangement validator.
- **immuneML import** (3.0.27, pandas 2.3.3), on 10 specimens × 2,000 rows from every cohort and on the full RA cohort:
- the default import keeps every row;
- `cdr3_aa`, `junction_aa`, V/J calls, `sequence_id` and `duplicate_count` survive the round trip;
- immuneML's derived `cdr3` equals `junction[3:-3]`;
- `IMGT_JUNCTION` drops exactly the rows without a junction.
- **TrainMLModel runs:**
- all three folds of `ra.yaml` on the full RA cohort;
- reduced copies of `mitchell-t1d` (numeric specimen IDs) and `t1d-pooled` (two cohorts, `/` in class names);
- in every run, each fold's train/test sizes match the CSVs.
## Provenance and terms
- **Regenerating:** from the code repository, run `python -m utils.immuneml_export --snapshot <native download> --output-root <dir>`, then `python -m utils.immuneml_tasks --output-root <dir>`. The gzip output has no timestamps or file names in it, so rerunning with the same software reproduces the checksums in `MANIFEST.tsv`.
- **Terms:** this folder is a reformatting of the native BenchRep-T files and carries the same terms and source attributions. See the dataset card and `DATASHEET.md`.