Cross integration: Multiple RNA + ADT¶
Cross integration combines batches that all measure the same modalities. The task is to remove batch effects and keep the biological structure. This tutorial is for batches that each hold RNA and ADT. It runs StabMap and sciPENN on D52mini, 3,000 cells in three batches of the benchmark dataset D52, and then on data in your own format.
StabMap and sciPENN run on Linux, Windows and macOS (Apple silicon). Or open in Colab: sections 1 to 4 take about 6 minutes there.
1. Install¶
pip may then print a note about restarting the kernel: no restart is needed, because the pins keep the numpy and pandas already installed.
import numpy, pandas
pins = f"numpy=={numpy.__version__} pandas=={pandas.__version__}"
%pip install -q "multibench-sc>=0.7.4" {pins}
Note: you may need to restart the kernel to use updated packages.
2. Download the data and the environments¶
mtb.data.fetch downloads D52mini (2 MB) once. mtb.env.install downloads the environments for StabMap and sciPENN. On Linux the download is 1.4 GB on a computer without a GPU and 3.0 GB on one with a GPU. On Windows it is 0.5 GB and 1.6 GB. On macOS it is 1.1 GB.
import multibench as mtb
import pandas as pd
pd.set_option("display.max_columns", None) # show every column of the tables
mtb.data.fetch("D52mini")
METHODS = ["StabMap", "sciPENN"]
envs = mtb.env.install(METHODS)
downloading D52mini (2 MB) ...
3. Run the methods¶
run runs each method on D52mini and scores its output. The clustering metrics score how well the embedding's clusters and neighbourhoods match the cell types, and how separate the clusters are. The batch metrics measure how well the batches mix. The classification scores measure how well the cell types learned on one batch are predicted in the other batches. Higher is better for every metric.
Details
params={"Method": {"key": value}} sets a method's parameters, and mtb.params_for lists them.
mtb.load_run("out/D52mini") reloads these results later.
res = mtb.run("D52mini", "cross", methods=METHODS, out_dir="out/D52mini")
res.summary
[run] StabMap (cross/D52mini) ... [run] -> scored (35.8s) ARI 0.392 [run] sciPENN (cross/D52mini) ... [run] -> scored (15.4s) ARI 0.572
| method | status | ARI | NMI | ASW | iASW | iF1 | cLISI | ASW_batch | ARI_batch | NMI_batch | GC | iLISI | OCA | ACA | Spec | Sens | F1 | run_sec | caveat | reason | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | StabMap | scored | 0.3915 | 0.5835 | 0.5557 | 0.5688 | 0.4070 | 0.9605 | 0.8511 | 0.9915 | 0.9893 | 0.8610 | 0.6834 | 0.7919 | 0.6505 | 0.9911 | 0.6505 | 0.6276 | 35.8 | ||
| 1 | sciPENN | scored | 0.5718 | 0.7651 | 0.6236 | 0.6436 | 0.5586 | 0.9913 | 0.8305 | 0.9938 | 0.9881 | 0.9012 | 0.6394 | 0.8183 | 0.6827 | 0.9922 | 0.6827 | 0.6565 | 15.4 |
4. Plot¶
res.plot() draws the scores as a bubble table. Circle size ranks the rows within each column and colour shows the value scaled per column, so a small gap can look large. res.summary has the values.
Details
Metrics are grouped by family: blue for clustering, green for batch correction, pink for classification. Each family starts with an Overall bar: each row's mean rank over the family's metrics, scaled so that the lowest gets the shortest bar.
A column with fewer than two different values is drawn grey. A figure of one method is grey everywhere.
res.plot()
5. Your own data¶
Your data needs raw RNA and ADT counts in one .h5ad file, with a cell type and a batch for each cell.
The methods of this task read three batches.
Recipes cover a 10x .h5 file, labels in a CSV file, no labels and another number of donors.
Details: export
The example keeps the protein names in adata.uns["protein_names"]. A DataFrame in adata.obsm["protein"] brings its own names in its columns.
The batches are numbered in the sorted order of their names, read as text: donors A, B and C become batches 1, 2 and 3, and 10 comes before 2.
For one AnnData per batch, call export_dataset once per batch with batch_index=N instead of batch=.
The next cell writes a small example file, my_file.h5ad, from 60% of the cells of D52mini, with a batch column. Skip it for your own data.
mtb.data.write_example("D52mini")
wrote my_file.h5ad: 1,800 cells. RNA counts in X, protein counts in obsm["protein"], cell types in obs["celltype"], batches in obs["batch"].
Read your file. For your own data, change the file name marked # <-. On Colab, upload the file first: click the folder icon on the left, then the upload icon.
import anndata as ad
adata = ad.read_h5ad("my_file.h5ad") # <- your file, every batch in it
adata
AnnData object with n_obs × n_vars = 1800 × 1000
obs: 'celltype', 'batch'
uns: 'protein_names'
obsm: 'protein'
layers: None (.X)
Write the dataset folder. Change each name in quotes marked # <- to one the printout above lists for your file, with its prefix, as "obs:celltype" or "obsm:protein".
NAME = "MYCROSS_RNA_ADT" # <- a new name for your own data
mtb.io.export_dataset(adata, "mydata/" + NAME,
rna="X", # <- where the RNA counts are:
# "X" or "layer:counts"
adt="obsm:protein", # <- where the proteins (ADT) are
labels="obs:celltype", # <- the cell-type column
batch="obs:batch", # <- the batch or donor column
category="cross", overwrite=True)
mtb.scan("mydata/" + NAME, "cross", methods=METHODS)
[scan] 2 of 2 methods have their input files in MYCROSS_RNA_ADT. 2 of 2 can run on this computer. MYCROSS_RNA_ADT holds RNA and ADT, in batches 1-3. [scan] Cells per file: rna1.h5 609, adt1.h5 609, rna2.h5 594, adt2.h5 594, rna3.h5 597, adt3.h5 597.
| method | modalities | runnable | reason | env | caveat | |
|---|---|---|---|---|---|---|
| 0 | StabMap | rna1+rna2+rna3+adt1+adt2+adt3 | True | scmb_r | ||
| 1 | sciPENN | rna1+rna2+rna3+adt1+adt2+adt3 | True | env_sciPENN |
mine = mtb.run("mydata/" + NAME, "cross", methods=METHODS,
out_dir="out/" + NAME)
mine.summary
[run] StabMap (cross/MYCROSS_RNA_ADT) ... [run] -> scored (29.4s) ARI 0.413 [run] sciPENN (cross/MYCROSS_RNA_ADT) ... [run] -> scored (13.4s) ARI 0.534
| method | status | ARI | NMI | ASW | iASW | iF1 | cLISI | ASW_batch | ARI_batch | NMI_batch | GC | iLISI | OCA | ACA | Spec | Sens | F1 | run_sec | caveat | reason | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | StabMap | scored | 0.4126 | 0.6030 | 0.5598 | 0.5712 | 0.3881 | 0.9522 | 0.8476 | 0.9939 | 0.9889 | 0.8261 | 0.7298 | 0.7634 | 0.6114 | 0.9896 | 0.6114 | 0.6034 | 29.4 | ||
| 1 | sciPENN | scored | 0.5341 | 0.7521 | 0.5892 | 0.6061 | 0.5431 | 0.9864 | 0.8397 | 0.9922 | 0.9896 | 0.8844 | 0.6590 | 0.8230 | 0.6737 | 0.9923 | 0.6737 | 0.6483 | 13.4 |
mine.plot()
6. Stored results¶
The package ships stored results for 7 methods on the full D52. load_results reads them and mtb.plot.bubble draws them, without running anything.
stored = mtb.load_results("cross", dataset="D52")
mtb.plot.bubble(stored)
7. Every method of this task¶
10 methods run this task. This notebook runs them all. Its 8 environments download 17.1 GB on a computer without a GPU and 22.1 GB on one with a GPU.
Troubleshooting¶
res.summary[res.summary.status != "scored"] lists each method that failed or was skipped. Its reason column ends with the method's error output.
Details
| symptom | fix |
|---|---|
| a method fails on a small dataset | mtb.scan names what the method needs from the data's size in its caveat column |
| batch metrics use the wrong batches | res.rescore(batch=...), with one batch per cell, scores again without running the methods |
Next steps¶
- Run a method, Score an embedding, Load and plot results and Discover methods.
- Look at an embedding draws a method's embedding as a UMAP.
- The interactive explorer has the full benchmark's rankings, with no install needed.