{ "cells": [ { "cell_type": "markdown", "id": "173554ed", "metadata": {}, "source": [ "# Diagonal integration: [multiple RNA, multiple ATAC]\n", "\n", "Diagonal integration combines RNA and ATAC measured in different cells, with no pairing between them. This tutorial is for data in several batches: three RNA batches and three ATAC batches. It runs iNMF and online_iNMF on `D37mini`, the RNA and ATAC of 3,000 cells of the benchmark dataset `D37` with the pairing left out, then on data in your own format.\n", "\n", "iNMF and online_iNMF read ATAC as gene-activity scores, made beforehand with a tool such as Signac or ArchR.\n", "\n", "iNMF and online_iNMF run on Linux, Windows and macOS (Apple silicon). Or [open in Colab](https://colab.research.google.com/github/DSichang/scMultiBench/blob/main/notebooks/tutorial_diagonal_multi.ipynb): sections 1 to 4 take about 6 minutes there." ] }, { "cell_type": "markdown", "id": "f13e016b", "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": "819f87a8", "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": "f49a0f68", "metadata": {}, "source": [ "## 2. Download the data and the environments\n", "\n", "`mtb.data.fetch` downloads `D37mini` (12 MB) once. `mtb.env.install` downloads the environments for iNMF and online_iNMF. On Linux the download is 0.9 GB. On Windows it is 0.3 GB. On macOS it is 0.7 GB." ] }, { "cell_type": "code", "execution_count": null, "id": "9e5ef3ba", "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(\"D37mini\")\n", "METHODS = [\"iNMF\", \"online_iNMF\"]\n", "envs = mtb.env.install(METHODS)" ] }, { "cell_type": "markdown", "id": "21fd08af", "metadata": {}, "source": [ "## 3. Run the methods\n", "\n", "`run` runs each method on `D37mini` 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/D37mini\")` reloads these results later.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "b455dd45", "metadata": {}, "outputs": [], "source": [ "res = mtb.run(\"D37mini\", \"diagonal\", methods=METHODS, out_dir=\"out/D37mini\")\n", "res.summary" ] }, { "cell_type": "markdown", "id": "83b58986", "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": "27372b09", "metadata": {}, "outputs": [], "source": [ "res.plot()" ] }, { "cell_type": "markdown", "id": "4742d3c2", "metadata": {}, "source": [ "## 5. Your own data\n", "\n", "Your data needs raw RNA counts and ATAC as one `.h5ad` file per batch, with a cell type for each cell. online_iNMF needs about 5,000 cells in total.\n", "\n", "Recipes cover [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 RNA batches and the ATAC batches are numbered separately. RNA batch 1 and ATAC batch 1 need not come from the same sample. scJoint, GLUE, iNMF and online_iNMF read exactly three RNA and three ATAC batches. For another number of batches, see [another number of donors](https://dsichang.github.io/scMultiBench/tutorials/run/#recipe-donors).\n", "\n", "For ATAC as a peak matrix, pass `atac_kind=\"peak\"`. GLUE reads the peak matrix.\n", "\n", "
" ] }, { "cell_type": "markdown", "id": "e31e3546", "metadata": {}, "source": [ "The next cell writes six small example files, `my_rna1.h5ad` to `my_rna3.h5ad` and `my_atac1.h5ad` to `my_atac3.h5ad`, from the first 900 RNA profiles and the first 900 ATAC profiles of each batch of `D37mini`. Skip it for your own data." ] }, { "cell_type": "code", "execution_count": null, "id": "4554f920", "metadata": {}, "outputs": [], "source": [ "mtb.data.write_example(\"D37mini\")" ] }, { "cell_type": "markdown", "id": "8fdd3cb1", "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": "fbeb9bb8", "metadata": {}, "outputs": [], "source": [ "import anndata as ad\n", "\n", "rna1 = ad.read_h5ad(\"my_rna1.h5ad\") # <- RNA batch 1: your file\n", "rna2 = ad.read_h5ad(\"my_rna2.h5ad\") # <- RNA batch 2: your file\n", "rna3 = ad.read_h5ad(\"my_rna3.h5ad\") # <- RNA batch 3: your file\n", "atac1 = ad.read_h5ad(\"my_atac1.h5ad\") # <- ATAC batch 1: your file\n", "atac2 = ad.read_h5ad(\"my_atac2.h5ad\") # <- ATAC batch 2: your file\n", "atac3 = ad.read_h5ad(\"my_atac3.h5ad\") # <- ATAC batch 3: your file\n", "print(rna1)\n", "print(atac1)" ] }, { "cell_type": "markdown", "id": "696f2fcc", "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": "7d80ac95", "metadata": {}, "outputs": [], "source": [ "NAME = \"MYDIAG_BATCHES\" # <- a new name for your own data\n", "rna = [rna1, rna2, rna3] # <- your RNA batches\n", "atac = [atac1, atac2, atac3] # <- your ATAC batches\n", "for b in range(len(rna)): # one call per batch\n", " mtb.io.export_dataset(rna[b], \"mydata/\" + NAME,\n", " rna=\"X\", # <- the RNA counts:\n", " # \"X\" or \"layer:counts\"\n", " atac=atac[b], # the ATAC batch\n", " atac_kind=\"gene_activity\", # <- or \"peak\"\n", " labels=\"obs:celltype\", # <- the cell-type column\n", " category=\"diagonal\", batch_index=b + 1,\n", " overwrite=True)\n", "mtb.scan(\"mydata/\" + NAME, \"diagonal\", methods=METHODS)" ] }, { "cell_type": "code", "execution_count": null, "id": "e697ed41", "metadata": {}, "outputs": [], "source": [ "mine = mtb.run(\"mydata/\" + NAME, \"diagonal\", methods=METHODS,\n", " out_dir=\"out/\" + NAME)\n", "mine.summary" ] }, { "cell_type": "code", "execution_count": null, "id": "b42483af", "metadata": {}, "outputs": [], "source": [ "mine.plot()" ] }, { "cell_type": "markdown", "id": "88bfce90", "metadata": {}, "source": [ "## 6. Every method of this task\n", "\n", "4 methods run this task. [This notebook](https://colab.research.google.com/github/DSichang/scMultiBench/blob/main/notebooks/tutorial_diagonal_multi_all.ipynb) runs them all. Its 3 environments download 4.8 GB on a computer without a GPU and 8.3 GB on one with a GPU." ] }, { "cell_type": "markdown", "id": "c6280b68", "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": "3a7cd7ce", "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 }