{ "cells": [ { "cell_type": "markdown", "id": "0d13fd75", "metadata": {}, "source": [ "# Mosaic integration: [RNA, RNA + ATAC, ATAC]\n", "\n", "Mosaic integration combines batches that share only some modalities, and every mosaic method reads ATAC as peaks. This tutorial is for an RNA batch, an RNA + ATAC batch and an ATAC batch. It runs StabMap and scMoMaT on `D45mini`, 6,000 cells of the benchmark dataset `D45`, and then on data in your own format.\n", "\n", "StabMap and scMoMaT run on Linux, Windows and macOS (Apple silicon). Or [open in Colab](https://colab.research.google.com/github/DSichang/scMultiBench/blob/main/notebooks/tutorial_mosaic_rna_atac.ipynb): sections 1 to 4 take about 12 minutes there." ] }, { "cell_type": "markdown", "id": "844a87ad", "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": "5c0673ed", "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": "bd218e20", "metadata": {}, "source": [ "## 2. Download the data and the environments\n", "\n", "`mtb.data.fetch` downloads `D45mini` (11 MB) once. `mtb.env.install` downloads the environments for StabMap and scMoMaT. On Linux the download is 1.8 GB on a computer without a GPU and 5.4 GB on one with a GPU. On Windows it is 0.6 GB and 2.5 GB. On macOS it is 1.0 GB." ] }, { "cell_type": "code", "execution_count": null, "id": "a17bbe95", "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(\"D45mini\")\n", "METHODS = [\"StabMap\", \"scMoMaT\"]\n", "envs = mtb.env.install(METHODS)" ] }, { "cell_type": "markdown", "id": "fb37972b", "metadata": {}, "source": [ "## 3. Run the methods\n", "\n", "`run` runs each method on `D45mini` 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. scMoMaT writes a graph: `run` scores that graph, which has no ASW, iASW or ASW_batch and no classification scores. 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/D45mini\")` reloads these results later.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "04446fa1", "metadata": {}, "outputs": [], "source": [ "res = mtb.run(\"D45mini\", \"mosaic\", methods=METHODS, out_dir=\"out/D45mini\")\n", "res.summary" ] }, { "cell_type": "markdown", "id": "34201066", "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": "cb2ebd44", "metadata": {}, "outputs": [], "source": [ "res.plot()" ] }, { "cell_type": "markdown", "id": "5fac850e", "metadata": {}, "source": [ "## 5. Your own data\n", "\n", "Your data needs raw counts, with one `.h5ad` file per batch and modality and a cell type for each cell. Number the batches as below: RNA, then RNA + ATAC, then ATAC. Give ATAC as peaks.\n", "\n", "Every RNA file needs the same gene names and every ATAC file the same peaks, in the same order. scMoMaT matches the features of two batches by position, and the check does not compare their names across 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) and [no labels](https://dsichang.github.io/scMultiBench/tutorials/run/#recipe-no-labels).\n", "\n", "
\n", "Details: export\n", "\n", "[The folder layout](https://dsichang.github.io/scMultiBench/tutorials/run/#folder-layout) lists the batch patterns and the methods that accept each one.\n", "\n", "
" ] }, { "cell_type": "markdown", "id": "c80b9746", "metadata": {}, "source": [ "The next cell writes four small example files, one per batch and modality, from the first 1,500 cells of each batch of `D45mini`. Skip it for your own data." ] }, { "cell_type": "code", "execution_count": null, "id": "98a6d147", "metadata": {}, "outputs": [], "source": [ "mtb.data.write_example(\"D45mini\")" ] }, { "cell_type": "markdown", "id": "396dc50f", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "id": "47b5dae7", "metadata": {}, "outputs": [], "source": [ "import anndata as ad\n", "\n", "rna1 = ad.read_h5ad(\"my_rna1.h5ad\") # <- batch 1: your RNA file\n", "rna2 = ad.read_h5ad(\"my_rna2.h5ad\") # <- batch 2: your RNA file\n", "atac2 = ad.read_h5ad(\"my_atac2.h5ad\") # <- batch 2: peaks of the same cells\n", "atac3 = ad.read_h5ad(\"my_atac3.h5ad\") # <- batch 3: your peak file\n", "print(rna2)\n", "print(atac2)" ] }, { "cell_type": "markdown", "id": "4da0fb95", "metadata": {}, "source": [ "Write the dataset folder. Change each name in quotes marked `# <-` to one the printout above lists for two of your files, with its prefix, as `\"obs:celltype\"`." ] }, { "cell_type": "code", "execution_count": null, "id": "1a2dc946", "metadata": {}, "outputs": [], "source": [ "NAME = \"MYMOSAIC_RNA_ATAC\" # <- a new name for your own data\n", "folder = \"mydata/\" + NAME\n", "celltype = \"obs:celltype\" # <- the cell-type column of every file\n", "# batch 1: RNA only\n", "mtb.io.export_dataset(rna1, folder, rna=\"X\", labels=celltype,\n", " batch_index=1, category=\"mosaic\", overwrite=True)\n", "# batch 2: RNA + ATAC peaks\n", "mtb.io.export_dataset(rna2, folder, rna=\"X\", atac=atac2, atac_kind=\"peak\",\n", " labels=celltype,\n", " batch_index=2, category=\"mosaic\", overwrite=True)\n", "# batch 3: ATAC peaks only\n", "mtb.io.export_dataset(atac3, folder, rna=None, atac=\"X\", atac_kind=\"peak\",\n", " labels=celltype,\n", " batch_index=3, category=\"mosaic\", overwrite=True)\n", "mtb.scan(\"mydata/\" + NAME, \"mosaic\", methods=METHODS)" ] }, { "cell_type": "code", "execution_count": null, "id": "42b3b730", "metadata": {}, "outputs": [], "source": [ "mine = mtb.run(\"mydata/\" + NAME, \"mosaic\", methods=METHODS,\n", " out_dir=\"out/\" + NAME)\n", "mine.summary" ] }, { "cell_type": "code", "execution_count": null, "id": "bb582567", "metadata": {}, "outputs": [], "source": [ "mine.plot()" ] }, { "cell_type": "markdown", "id": "3ebb6794", "metadata": {}, "source": [ "## 6. Stored results\n", "\n", "The package ships stored results for 4 methods on the full `D45`. `load_results` reads them and `mtb.plot.bubble` draws them, without running anything." ] }, { "cell_type": "code", "execution_count": null, "id": "f8b7389f", "metadata": {}, "outputs": [], "source": [ "stored = mtb.load_results(\"mosaic\", dataset=\"D45\")\n", "mtb.plot.bubble(stored)" ] }, { "cell_type": "markdown", "id": "3c7c09cb", "metadata": {}, "source": [ "## 7. Every method of this task\n", "\n", "6 methods run this task. [This notebook](https://colab.research.google.com/github/DSichang/scMultiBench/blob/main/notebooks/tutorial_mosaic_rna_atac_all.ipynb) runs them all. Its 6 environments download 7.8 GB on a computer without a GPU and 8.9 GB on one with a GPU." ] }, { "cell_type": "markdown", "id": "97850f59", "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": "7c188431", "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 }