Cross integration: Multiple ADT + ATAC¶
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 ADT and ATAC peaks, with no RNA. It runs StabMap and UINMF on D58mini, 3,000 cells in two batches of the benchmark dataset D58, and then on data in your own format.
StabMap and UINMF run on Linux and macOS (Apple silicon), and UINMF has no Windows environment. On Windows, open this notebook 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 D58mini (7 MB) once. mtb.env.install downloads the environments for StabMap and UINMF. On Linux the download is 0.9 GB. On macOS it is 0.7 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("D58mini")
METHODS = ["StabMap", "UINMF"]
envs = mtb.env.install(METHODS)
downloading D58mini (7 MB) ... [env] downloading scmb_r 917 MB ... [env] 917 MB of 917 MB (29 MB/s) [env] unpacking scmb_r ...
3. Run the methods¶
run runs each method on D58mini 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/D58mini") reloads these results later.
res = mtb.run("D58mini", "cross", methods=METHODS, out_dir="out/D58mini")
res.summary
[run] StabMap (cross/D58mini) ... [run] -> scored (45.4s) ARI 0.916 [run] UINMF (cross/D58mini) ... [run] -> scored (55.6s) ARI 0.682
| 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.9163 | 0.8661 | 0.5649 | 0.5770 | 0.9510 | 0.9985 | 0.8736 | 0.9984 | 0.9949 | 0.9973 | 0.7995 | 0.9750 | 0.9550 | 0.9823 | 0.9550 | 0.9556 | 45.4 | ||
| 1 | UINMF | scored | 0.6824 | 0.6476 | 0.5221 | 0.5906 | 0.8419 | 0.9988 | 0.7890 | 0.9997 | 0.9993 | 0.9851 | 0.8852 | 0.9547 | 0.8857 | 0.9679 | 0.8857 | 0.9043 | 55.6 |
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 ADT counts in one .h5ad file and an ATAC peak matrix of the same cells in another, with a cell type and a batch for each cell.
The methods of this task read two batches.
Recipes cover a 10x .h5 file, labels in a CSV file, no labels and another number of donors.
Details: export
When the ADT file also holds a matrix in obsm under a name that does not start with X_, export_dataset asks which one is the protein matrix. X_umap and X_pca are left alone.
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 two small example files, my_adt.h5ad and my_atac.h5ad, from 60% of the cells of D58mini, with a batch column. Skip it for your own data.
mtb.data.write_example("D58mini")
wrote my_adt.h5ad: 1,800 cells. protein counts in X, cell types in obs["celltype"], batches in obs["batch"]. wrote my_atac.h5ad: 1,800 cells. ATAC peak counts in X, cell types in obs["celltype"], batches in obs["batch"].
Read your files. For your own data, change every file name marked # <-. On Colab, upload the files first: click the folder icon on the left, then the upload icon.
import anndata as ad
adt = ad.read_h5ad("my_adt.h5ad") # <- your protein (ADT) file, every batch
atac = ad.read_h5ad("my_atac.h5ad") # <- your peak file, the same cells
print(adt)
print(atac)
AnnData object with n_obs × n_vars = 1800 × 227
obs: 'celltype', 'batch'
layers: None (.X)
AnnData object with n_obs × n_vars = 1800 × 5000
obs: 'celltype', 'batch'
layers: None (.X)
Write the dataset folder. Change each name in quotes marked # <- to one the printout above lists for your files, with its prefix, as "obs:celltype".
NAME = "MYCROSS_ADT_ATAC" # <- a new name for your own data
mtb.io.export_dataset(adt, "mydata/" + NAME,
rna=None, # this data has no RNA
adt="X", # <- where the proteins (ADT) are
atac=atac, # the ATAC file, peak counts in X
atac_kind="peak", # the methods here read peaks
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_ADT_ATAC. 2 of 2 can run on this computer. MYCROSS_ADT_ATAC holds ADT and ATAC, in batches 1-2. [scan] Cells per file: adt1.h5 913, atac_peak1.h5 913, adt2.h5 887, atac_peak2.h5 887.
| method | modalities | runnable | reason | env | caveat | |
|---|---|---|---|---|---|---|
| 0 | StabMap | adt1+adt2+atac1+atac2 | True | scmb_r | ||
| 1 | UINMF | adt1+adt2+atac1+atac2 | True | scmb_r |
mine = mtb.run("mydata/" + NAME, "cross", methods=METHODS,
out_dir="out/" + NAME)
mine.summary
[run] StabMap (cross/MYCROSS_ADT_ATAC) ... [run] -> scored (36.3s) ARI 0.451 [run] UINMF (cross/MYCROSS_ADT_ATAC) ... [run] -> scored (42.9s) ARI 0.531
| 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.4507 | 0.5775 | 0.5434 | 0.5427 | 0.7854 | 0.9962 | 0.8787 | 0.9998 | 1.0000 | 0.9998 | 0.8199 | 0.9833 | 0.9732 | 0.9884 | 0.9732 | 0.9739 | 36.3 | ||
| 1 | UINMF | scored | 0.5313 | 0.5891 | 0.6197 | 0.6061 | 0.7879 | 0.9982 | 0.8903 | 0.9965 | 0.9927 | 0.9932 | 0.8392 | 0.9367 | 0.8318 | 0.9508 | 0.8318 | 0.8583 | 42.9 |
mine.plot()
6. Every method of this task¶
5 methods run this task. This notebook runs them all. Its 4 environments download 12.1 GB on a computer without a GPU and 15.5 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.