{ "cells": [ { "cell_type": "markdown", "id": "a76f5068", "metadata": {}, "source": [ "# Cross integration: Multiple RNA + ADT\n", "\n", "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.\n", "\n", "StabMap and sciPENN run on Linux, Windows and macOS (Apple silicon). Or [open in Colab](https://colab.research.google.com/github/DSichang/scMultiBench/blob/main/notebooks/tutorial_cross_rna_adt.ipynb): sections 1 to 4 take about 6 minutes there." ] }, { "cell_type": "markdown", "id": "8ed4f740", "metadata": {}, "source": [ "## 1. Install\n", "\n", "pip may then print a note about restarting the kernel: no restart is needed, because the pins keep the numpy and pandas already installed." ] }, { "cell_type": "code", "execution_count": null, "id": "259ab03d", "metadata": {}, "outputs": [], "source": [ "import numpy, pandas\n", "pins = f\"numpy=={numpy.__version__} pandas=={pandas.__version__}\"\n", "%pip install -q \"multibench-sc>=0.7.4\" {pins}" ] }, { "cell_type": "markdown", "id": "30586d2d", "metadata": {}, "source": [ "## 2. Download the data and the environments\n", "\n", "`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." ] }, { "cell_type": "code", "execution_count": null, "id": "d7b44783", "metadata": {}, "outputs": [], "source": [ "import multibench as mtb\n", "import pandas as pd\n", "\n", "pd.set_option(\"display.max_columns\", None) # show every column of the tables\n", "\n", "mtb.data.fetch(\"D52mini\")\n", "METHODS = [\"StabMap\", \"sciPENN\"]\n", "envs = mtb.env.install(METHODS)" ] }, { "cell_type": "markdown", "id": "668ed360", "metadata": {}, "source": [ "## 3. Run the methods\n", "\n", "`run` runs each method on `D52mini` and [scores its output](https://dsichang.github.io/scMultiBench/tutorials/evaluate/#what-each-metric-measures). 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.\n", "\n", "
\n", "Details\n", "\n", "`params={\"Method\": {\"key\": value}}` sets a method's parameters, and `mtb.params_for` lists them.\n", "\n", "`mtb.load_run(\"out/D52mini\")` reloads these results later.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "3f81b659", "metadata": {}, "outputs": [], "source": [ "res = mtb.run(\"D52mini\", \"cross\", methods=METHODS, out_dir=\"out/D52mini\")\n", "res.summary" ] }, { "cell_type": "markdown", "id": "4abb5d57", "metadata": {}, "source": [ "## 4. Plot\n", "\n", "`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.\n", "\n", "
\n", "Details\n", "\n", "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.\n", "\n", "A column with fewer than two different values is drawn grey. A figure of one method is grey everywhere.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "7b7d06f7", "metadata": {}, "outputs": [], "source": [ "res.plot()" ] }, { "cell_type": "markdown", "id": "82571c3a", "metadata": {}, "source": [ "## 5. Your own data\n", "\n", "Your data needs raw RNA and ADT counts in one `.h5ad` file, with a cell type and a batch for each cell.\n", "\n", "The methods of this task read three batches.\n", "\n", "Recipes cover [a 10x `.h5` file](https://dsichang.github.io/scMultiBench/tutorials/run/#recipe-10x-h5), [labels in a CSV file](https://dsichang.github.io/scMultiBench/tutorials/run/#recipe-labels-csv), [no labels](https://dsichang.github.io/scMultiBench/tutorials/run/#recipe-no-labels) and [another number of donors](https://dsichang.github.io/scMultiBench/tutorials/run/#recipe-donors).\n", "\n", "
\n", "Details: export\n", "\n", "The example keeps the protein names in `adata.uns[\"protein_names\"]`. A DataFrame in `adata.obsm[\"protein\"]` brings its own names in its columns.\n", "\n", "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`.\n", "\n", "For one AnnData per batch, call `export_dataset` once per batch with `batch_index=N` instead of `batch=`.\n", "\n", "
" ] }, { "cell_type": "markdown", "id": "129ddca0", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "id": "d3f413b7", "metadata": {}, "outputs": [], "source": [ "mtb.data.write_example(\"D52mini\")" ] }, { "cell_type": "markdown", "id": "19d24b8f", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "id": "4a9bbacd", "metadata": {}, "outputs": [], "source": [ "import anndata as ad\n", "\n", "adata = ad.read_h5ad(\"my_file.h5ad\") # <- your file, every batch in it\n", "adata" ] }, { "cell_type": "markdown", "id": "b6077ba5", "metadata": {}, "source": [ "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\"`." ] }, { "cell_type": "code", "execution_count": null, "id": "b1d1b396", "metadata": {}, "outputs": [], "source": [ "NAME = \"MYCROSS_RNA_ADT\" # <- a new name for your own data\n", "mtb.io.export_dataset(adata, \"mydata/\" + NAME,\n", " rna=\"X\", # <- where the RNA counts are:\n", " # \"X\" or \"layer:counts\"\n", " adt=\"obsm:protein\", # <- where the proteins (ADT) are\n", " labels=\"obs:celltype\", # <- the cell-type column\n", " batch=\"obs:batch\", # <- the batch or donor column\n", " category=\"cross\", overwrite=True)\n", "mtb.scan(\"mydata/\" + NAME, \"cross\", methods=METHODS)" ] }, { "cell_type": "code", "execution_count": null, "id": "9a061045", "metadata": {}, "outputs": [], "source": [ "mine = mtb.run(\"mydata/\" + NAME, \"cross\", methods=METHODS,\n", " out_dir=\"out/\" + NAME)\n", "mine.summary" ] }, { "cell_type": "code", "execution_count": null, "id": "81cd1c33", "metadata": {}, "outputs": [], "source": [ "mine.plot()" ] }, { "cell_type": "markdown", "id": "4134b132", "metadata": {}, "source": [ "## 6. Stored results\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "id": "14848c31", "metadata": {}, "outputs": [], "source": [ "stored = mtb.load_results(\"cross\", dataset=\"D52\")\n", "mtb.plot.bubble(stored)" ] }, { "cell_type": "markdown", "id": "6aa5b87f", "metadata": {}, "source": [ "## 7. Every method of this task\n", "\n", "10 methods run this task. [This notebook](https://colab.research.google.com/github/DSichang/scMultiBench/blob/main/notebooks/tutorial_cross_rna_adt_all.ipynb) 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." ] }, { "cell_type": "markdown", "id": "bdccb043", "metadata": {}, "source": [ "## Troubleshooting\n", "\n", "`res.summary[res.summary.status != \"scored\"]` lists each method that failed or was skipped. Its `reason` column ends with the method's error output.\n", "\n", "
\n", "Details\n", "\n", "| symptom | fix |\n", "|---|---|\n", "| a method fails on a small dataset | `mtb.scan` names what the method needs from the data's size in its `caveat` column |\n", "| batch metrics use the wrong batches | `res.rescore(batch=...)`, with one batch per cell, scores again without running the methods |\n", "\n", "
" ] }, { "cell_type": "markdown", "id": "c79e3fef", "metadata": {}, "source": [ "## Next steps\n", "\n", "- [Run a method](https://dsichang.github.io/scMultiBench/tutorials/run/), [Score an embedding](https://dsichang.github.io/scMultiBench/tutorials/evaluate/), [Load and plot results](https://dsichang.github.io/scMultiBench/tutorials/plot/) and [Discover methods](https://dsichang.github.io/scMultiBench/tutorials/discover/).\n", "- [Look at an embedding](https://dsichang.github.io/scMultiBench/tutorials/evaluate/#look-at-an-embedding) draws a method's embedding as a UMAP.\n", "- The [interactive explorer](https://shiny.maths.usyd.edu.au/scMultiBench/) has the full benchmark's rankings, with no install needed." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.10" } }, "nbformat": 4, "nbformat_minor": 5 }