Adapt notebooks to other datasets
mainjupyter-book folder. Each notebook is provided with a minimal Conda environment to ensure reproducibility. Alternatively, notebooks can be downloaded directly from the rendered version of the book.repository·main·Indexed 22 days ago
https://github.com/theislab/single-cell-best-practicesA collection of tutorials and expert recommendations for single-cell analysis across various modalities. It provides reproducible workflows, including scATAC-seq quality control, doublet detection using scDblFinder and AMULET, and guidance on interoperability between the scverse (Python), Bioconductor, and Seurat (R) ecosystems using tools like zellkonverter, SeuratDisk, rpy2, and reticulate.
jupyter-book folder. Each notebook is provided with a minimal Conda environment to ensure reproducibility. Alternatively, notebooks can be downloaded directly from the rendered version of the book.The decision to sequence whole cells versus nuclei is largely driven by the type of tissue being studied and the preservation of cell types.
Key Consideration: Always discuss experimental design between wet lab and dry lab scientists, as dissociation ability significantly impacts which cell types are observable in the data.
The RAPIDS Memory Manager (RMM) controls how VRAM is allocated. You must pick exactly one strategy and configure it at the very top of your notebook before any CuPy arrays are created.
initial_pool_size close to your expected peak to avoid fragmentation.Warning: Do not enable both pool_allocator=True and managed_memory=True simultaneously; they conflict and degrade performance.
import cupy as cp
import rmm
from rmm.allocators.cupy import rmm_cupy_allocator
# Strategy 1: Pool Allocator (for datasets that fit in VRAM)
rmm.reinitialize(
pool_allocator=True,
managed_memory=False,
initial_pool_size="4GB",
maximum_pool_size="32GB",
)
cp.cuda.set_allocator(rmm_cupy_allocator)
# Strategy 2: Managed Memory (for datasets larger than VRAM)
rmm.reinitialize(
pool_allocator=False,
managed_memory=True,
)
cp.cuda.set_allocator(rmm_cupy_allocator)Next-Generation Sequencing (NGS) generally follows three main stages:
adapter molecules. These adapters facilitate hybridization to the sequencing matrix and provide priming sites.Single-cell sequencing protocols are categorized by how they isolate cells:
These methods encapsulate cells into tiny droplets (emulsions) using microfluidics, enabling massive parallelization and high throughput.
inDrop, Drop-seq, and 10x Genomics Chromium.These methods isolate cells into individual wells in a microwell plate.
RNA-Seq can be performed at two different resolutions:
Single-cell analysis is dominated by three main ecosystems: Bioconductor (R), Seurat (R), and scverse (Python). To be a competent analyst, you should be able to move between them to use the best-performing tools.
Interoperability is achieved through two main methods:
rpy2 in Python or reticulate in R) to access the same object or convert it in real-time. This is highly interactive but increases memory overhead and can be complex to set up.In RNA-seq, transcript amplification (via PCR) is necessary but can introduce amplification bias, where certain sequences are preferentially amplified, leading to artificially high counts.
Unique Molecular Identifiers (UMIs) are short, random nucleotide sequences (molecular barcodes) added to every molecule during library generation before the amplification step. Because each original molecule receives a unique UMI, researchers can distinguish between original molecules and their PCR duplicates. This allows for accurate quantification of the original number of molecules and helps normalize gene counts without losing accuracy.
To perform single-cell quality control (QC) using Scanpy and Lamindb, you need to import several libraries including scanpy, lamindb, seaborn, and rpy2 for R integration. It is recommended to suppress Scanpy's verbose logging and set figure parameters for clean plots.
To load the specific dataset used in this tutorial, use lamindb to connect to the theislab/sc-best-practices instance and retrieve the quality_control_adata.h5ad artifact. Always ensure variable names are unique using adata.var_names_make_unique() to prevent downstream errors.
import lamindb as ln
import numpy as np
import scanpy as sc
import seaborn as sns
from rpy2.robjects import numpy2ri
from rpy2.robjects.conversion import localconverter
from scipy.sparse import csc_matrix
from scipy.stats import median_abs_deviation
# Suppress verbose logging from Scanpy
sc.settings.verbosity = 0
# Set figure parameters for clean, minimal plots
sc.settings.set_figure_params(dpi=80, facecolor="white", frameon=False)
assert ln.setup.settings.instance.slug == "theislab/sc-best-practices"
ln.track()
# Load the dataset
af = ln.Artifact.connect("theislab/sc-best-practices").get(
key="preprocessing_visualization/quality_control_adata.h5ad", is_latest=True
)
adata = af.load()
# Ensure unique variable names
adata.var_names_make_unique()For differentiated normalization, the notebook uses the scran R package to compute size factors based on clusters.
Workflow:
groups).scran::computeSumFactors on a SingleCellExperiment object using the provided clusters.size_factors from R, add them to adata.obs, and perform the normalization: adata.X / adata.obs["size_factors"].# 1. Preliminary clustering
adata_pp = adata.copy()
sc.pp.normalize_total(adata_pp)
sc.pp.log1p(adata_pp)
sc.pp.pca(adata_pp, n_comps=15)
sc.pp.neighbors(adata_pp)
sc.tl.leiden(adata_pp, key_added="groups", flavor="igraph", n_iterations=2, directed=False)
# 2. Transfer to R and 3. Compute size factors
%%R -o size_factors
library(scran)
library(BiocParallel)
size_factors = sizeFactors(
computeSumFactors(
SingleCellExperiment(list(counts=data_mat)),
clusters = input_groups,
min.mean = 0.1,
BPPARAM = MulticoreParam()
)
)
# 4. Apply in Python
adata.obs["size_factors"] = size_factors
scran = adata.X / adata.obs["size_factors"].values[:, None]
scran_logged = np.log1p(scran)
adata.layers["scran_normalization"] = csr_matrix(scran_logged)The template.ipynb file is a guide for creating new chapters for the single-cell best practices book.
Workflow:
CONTRIBUTING.md for project-specific rules.template with your specific chapter name throughout the file.Important Note: Always replace the placeholder name template with your actual chapter name to ensure file paths and labels work correctly.