LSMA

LONDON SCHOOL OF MULTIOMICS & AI

LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 31 lessons

Single-cell QC and normalisation

Single-cell QC and normalisation

Single-cell QC and normalisation

Each column of a single-cell matrix is one cell, and not every column is a healthy one. This lesson loads 2,700 blood cells, checks each with three numbers, removes the damaged ones and prepares the rest for clustering.

From droplets to a count matrix

Predict and check honestly, the project that closed Module 5, ended by predicting breast cancer subtype and checking the prediction on tumours that played no part in building it.

Module 4 measured gene expression in whole samples: each column of the airway count matrix was millions of cells ground together. Module 5 gave you the tools for finding structure in many measurements at once: PCA, clustering, heatmaps and honest checks. This module joins the two. Single-cell RNA sequencing measures every cell separately, so a blood sample becomes thousands of columns rather than one, and the structure you look for is the set of cell types hiding inside it.

In the most common method, cells flow one at a time into tiny oil droplets, each holding a bead with a unique barcode. Every RNA molecule captured in a droplet is tagged with that barcode, so reads can be traced back to their cell. Each molecule also gets a short random tag, the UMI (unique molecular identifier), so that the many copies made during amplification are counted only once. The result is a cell-by-gene UMI matrix: one row per gene, one column per barcode, and in each entry the number of distinct molecules of that gene found in that cell.

Two packages do most of the work in this module. Seurat, installed from CRAN with install.packages("Seurat"), stores the data and runs the analysis. SeuratData, installed from GitHub with remotes::install_github("satijalab/seurat-data"), downloads ready-made example datasets. The one used here, pbmc3k, is a public 10x Genomics dataset of about 2,700 peripheral blood mononuclear cells (PBMCs: the white blood cells with a single round nucleus, mainly lymphocytes and monocytes) from one healthy donor.

R · run once
SeuratData::InstallData("pbmc3k")
R
library(Seurat)
library(SeuratData)
library(ggplot2)

pbmc <- LoadData("pbmc3k")
pbmc <- UpdateSeuratObject(pbmc)
pbmc
An object of class Seurat 13714 features across 2700 samples within 1 assay Active assay: RNA (13714 features, 0 variable features) 2 layers present: counts, data

What this does

  • LoadData("pbmc3k") loads the dataset as a Seurat object, a single container for the counts, the information about each cell and, later, every result you compute.
  • UpdateSeuratObject() brings an object saved by an older version of Seurat up to the current structure. It prints a few messages and changes nothing in the data.
  • Printing the object gives its size. Seurat calls genes features and cells samples: 13,714 genes across 2,700 cells. The data sit in an assay called RNA, which holds layers: counts for the raw UMIs, data for normalised values, and later scale.data.

The rest of the object follows a pattern worth learning once. Information about cells, such as quality measures and cluster labels, lives in a data frame you reach with pbmc[[]], and a single column with pbmc$name. Results of dimension reduction, such as PCA, go into pbmc[["pca"]]. You will add to the same object at every step of this module, as you added results to dds in Module 4.

R
counts <- LayerData(pbmc, assay = "RNA", layer = "counts")
class(counts)
dim(counts)
counts[c("CD3E", "MS4A1", "LYZ", "PPBP"), 1:4]

1 - length(counts@x) / prod(dim(counts))     # share of zeros
format(object.size(counts), units = "MB")
format(object.size(as.matrix(counts)), units = "MB")
[1] "dgCMatrix" … [1] 13714 2700 4 x 4 sparse Matrix of class "dgCMatrix" AAACATACAACCAC AAACATTGAGCTAC AAACATTGATCAGC AAACCGTGCTTCCG CD3E 2 . 2 . MS4A1 . 6 . . LYZ 1 3 2 24 PPBP . . . 1 [1] 0.9383443 [1] "27.2 Mb" [1] "283.5 Mb"

Reading the output

The column names are the cell barcodes. The dots are zeros: a cell's few thousand captured molecules cover only a small part of its RNA, so most genes are simply not seen in most cells. Here 93.8% of all entries are zero. A sparse matrix, class dgCMatrix from the Matrix package, stores only the non-zero values and their positions, which is why it takes 27 MB rather than the 284 MB of an ordinary matrix. With 100,000 cells, that difference decides whether the data fit in memory.

You can already guess at biology. The second cell has six UMIs of MS4A1, a B-cell gene, and the fourth has 24 of LYZ, typical of monocytes. Zeros, though, are weak evidence: a zero means "not captured", not "not expressed".

Quality control: three numbers per cell

Not every barcode is a healthy single cell. Some droplets caught a damaged cell, some caught no cell at all, and some caught two. Quality control (QC) means describing every cell with a few numbers and removing the ones that do not look like intact single cells. Seurat computes two numbers when the object is created and you add the third.

R
pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
head(pbmc[[]], 3)
summary(pbmc$nCount_RNA)
summary(pbmc$nFeature_RNA)
quantile(pbmc$percent.mt, c(0.5, 0.9, 0.95, 0.99))
orig.ident nCount_RNA nFeature_RNA seurat_annotations percent.mt AAACATACAACCAC pbmc3k 2419 779 Memory CD4 T 3.0177759 AAACATTGAGCTAC pbmc3k 4903 1352 B 3.7935958 AAACATTGATCAGC pbmc3k 3147 1129 Memory CD4 T 0.8897363 Min. 1st Qu. Median Mean 3rd Qu. Max. 546 1756 2196 2365 2762 15818 Min. 1st Qu. Median Mean 3rd Qu. Max. 212.0 690.0 816.0 845.5 952.0 3400.0 50% 90% 95% 99% 2.030801 3.403736 4.014896 5.882921

What each number means

  • nCount_RNA is the total number of UMIs in the cell: its library size, as in RNA-seq counts and exploration. The median cell here has 2,196.
  • nFeature_RNA is the number of different genes with at least one UMI. A droplet with no cell, holding only stray RNA from the surrounding liquid, has very few; the median cell here has 816.
  • percent.mt is the share of UMIs from mitochondrial genes, whose names start with MT- in human data (the pattern is a regular expression; mouse genes start with mt-). When a cell's membrane breaks, the RNA in its cytoplasm leaks away but RNA inside the mitochondria stays, so a damaged or dying cell shows a high share.
  • seurat_annotations is a column of published cell-type labels that comes with this dataset. Ignore it for now; you will use it in Markers and cell types to check your own labels.

Worked example · one cell's percent.mt by hand

The first cell has 2,419 UMIs and a percent.mt of 3.018. Working backwards gives the number of mitochondrial UMIs:

mitochondrial UMIs = 3.018 ÷ 100 × 2,419 = 73
check: 73 ÷ 2,419 × 100 = 3.02%
99% of cells are below 5.88%; the highest is 22.57%

What the answer means. About 1 UMI in 33 from this cell came from genes of the mitochondrial genome. That is ordinary for blood cells. A cell at 22.6% has lost so much of its cytoplasmic RNA that its profile mostly records damage, not cell type.

What counts as "normal" depends on the tissue. Heart and kidney cells are packed with mitochondria and healthy ones can exceed 20%, so a fixed cut-off copied from a blood dataset would throw away good cells there.

Choosing thresholds by looking

There is no universal cut-off. The habit from Describing data applies: plot the distributions first, then decide. A violin plot of each metric and a scatter plot of genes against UMIs show most problems at once.

R
VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"),
        ncol = 3, pt.size = 0.1)

qc <- pbmc[[]]
ggplot(qc, aes(nCount_RNA, nFeature_RNA, colour = percent.mt > 5)) +
  geom_point(size = 0.6) +
  scale_x_log10() + scale_y_log10() +
  scale_colour_manual(values = c("grey60", "#D55E00")) +
  theme_classic()

mad_hi <- function(x, n = 3) median(x) + n * mad(x)
mad_lo <- function(x, n = 3) median(x) - n * mad(x)
10^mad_lo(log10(pbmc$nFeature_RNA))
10^mad_hi(log10(pbmc$nFeature_RNA))
mad_hi(pbmc$percent.mt)
[1] 399.7497 [1] 1665.682 [1] 4.436775

Reading the plots and numbers

On log scales, genes rise almost in a straight line with UMIs: cells with more molecules show more genes. The cells with more than 5% mitochondrial UMIs, in orange, sit mostly at the low end of both axes, which is what damaged cells look like. A small group of cells at the top right has two to three times the typical UMIs; these are candidates for doublets, discussed below.

The functions mad_hi() and mad_lo() give a data-driven guide. The MAD (median absolute deviation) is a spread measure that, like the median in Describing data, is not pulled by outliers. "Three MADs from the median" flags values unusually far out. Gene counts are skewed, so the rule is applied on the log scale and turned back with 10^: it suggests keeping cells with about 400 to 1,670 genes and a mitochondrial share below 4.4%.

Use such rules as a starting point, not as the answer. In a sample with several cell types, some types naturally carry more RNA: monocytes, for example, are larger than the small lymphocytes. A strict three-MAD rule on genes would remove more than 1% of cells at the top, the 99th percentile being 1,739 genes, and many of those may be healthy large cells. The thresholds below are deliberately loose on genes and close to the rule on mitochondria.

R
table(low_genes = pbmc$nFeature_RNA < 250, high_mt = pbmc$percent.mt >= 5)

pbmc <- subset(pbmc, subset = nFeature_RNA >= 250 & nFeature_RNA <= 2500 &
                 percent.mt < 5)
ncol(pbmc)
high_mt low_genes FALSE TRUE FALSE 2640 57 TRUE 3 0 [1] 2635

What this does

  • The table counts how many cells each rule would remove: 57 for mitochondria and 3 for too few genes, with no overlap.
  • subset() keeps cells that pass every condition, joined with & as in Cleaning data with dplyr and tidyr. The upper limit of 2,500 genes removes 5 further cells with an unusually high gene count.
  • 2,635 of 2,700 cells remain: 65 removed, 2.4%. Record the thresholds and the number removed for your methods.

Doublets are droplets that caught two cells. They look like one cell expressing the genes of both, for example a T-cell and monocyte hybrid, and can form small false clusters between real ones. They tend to have more UMIs and genes than single cells, which is why an upper limit removes some, but a doublet of two small cells looks normal. Packages such as scDblFinder simulate doublets from the data and score each cell by how much it resembles them. Empty droplets are the opposite problem: barcodes with no cell, only ambient RNA. The 10x software and the DropletUtils package call cells from the full list of barcodes before you ever see this matrix, and the lower limit on genes removes what slips through.

Go deeper: why not use the doublet rate you expect?

The rate of doublets rises with the number of cells loaded: by the manufacturer's figures, a little under 1% for every 1,000 cells recovered. With 2,700 cells, that is a few dozen doublets, more than the upper threshold removed. Doublets of two cells of the same type do little harm; doublets of two different types create false clusters.

Some experiments mix cells from several people and assign each droplet to a person from its genetic variants. A droplet containing variants from two people is then a known doublet. You will meet a dataset built this way in Comparing conditions with pseudobulk, where every cell carries a singlet or doublet label.

Normalise, choose variable genes, scale

Cells differ in how many molecules were captured, just as samples differ in sequencing depth. Seurat's default, LogNormalize, divides each count by the cell's total UMIs, multiplies by 10,000 and takes the natural log of that value plus one.

R
pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize",
                      scale.factor = 1e4)

norm  <- LayerData(pbmc, layer = "data")
cell1 <- colnames(pbmc)[1]
counts["LYZ", cell1]; sum(counts[, cell1])
log1p(counts["LYZ", cell1] / sum(counts[, cell1]) * 1e4)
norm["LYZ", cell1]
[1] 1 [1] 2419 [1] 1.635873 [1] 1.635873

Checking the arithmetic

The first cell has 1 UMI of LYZ out of 2,419. Dividing gives 0.000413; times 10,000 is 4.13; and log1p(), the natural log of 1 + x, gives 1.636. Seurat's stored value matches exactly. The "+1" keeps zeros at zero, since log(1) = 0, as the pseudocount did in RNA-seq counts and exploration.

This method assumes every cell holds about the same total amount of RNA, which is not true when cell types differ in size. SCTransform is an alternative that fits a model of each gene's counts against the cell's library size and uses the leftovers, the residuals, as normalised values. One call, SCTransform(pbmc), replaces the next three steps. This module keeps LogNormalize because each step is visible and the numbers can be checked by hand.

R
pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
head(VariableFeatures(pbmc), 12)

pbmc <- ScaleData(pbmc)
sc <- LayerData(pbmc, layer = "scale.data")
dim(sc)
round(c(mean = mean(sc["LYZ", ]), sd = sd(sc["LYZ", ])), 3)

saveRDS(pbmc, "pbmc_qc.rds")
[1] "PPBP" "LYZ" "S100A9" "IGLL5" "GNLY" "FTL" "PF4" [8] "FTH1" "GNG11" "S100A8" "FCER1A" "HLA-DRA" [1] 2000 2635 mean sd 0 1

What this does

  • FindVariableFeatures() picks the 2,000 genes that vary most between cells, after allowing for the fact that highly expressed genes vary more simply because they are counted more. The "vst" method fits that mean–variance trend and ranks genes by how far above it they lie. Most of the 13,714 genes vary only by sampling noise; keeping the informative ones makes the next steps faster and cleaner.
  • The top of the list already reads like a blood-cell catalogue: PPBP and PF4 from platelets, LYZ and S100A8/A9 from monocytes, GNLY from NK cells, FCER1A from dendritic cells, and IGLL5 from B cells.
  • ScaleData() centres each variable gene to mean 0 and scales it to standard deviation 1 across cells, the scale() step you used before PCA in Clustering, PCA and heatmaps. Without it, a few very highly expressed genes would dominate the PCA. The check confirms LYZ now has mean 0 and SD 1.
  • saveRDS() saves the whole object, so Clustering and UMAP can start from here.

Keep the three layers straight. counts holds the raw UMIs, which pseudobulk tests later in the module need. data holds the log-normalised values, used for plotting genes and testing markers. scale.data holds the scaled variable genes, used only for PCA.

Common mistakes

  • Copying thresholds from another dataset. "Under 5% mitochondrial" suits these blood cells; it would discard healthy heart muscle. Plot your own distributions first.
  • Filtering too hard. A rare cell type with a distinct size or activity can look like an outlier. Check which cells a rule removes before accepting it.
  • Treating zeros as absence. With a few thousand UMIs per cell, most expressed genes are missed in most cells.
  • Testing on scaled data. scale.data is for PCA. Use the normalised data layer for markers, and raw counts for differential expression between conditions.

The pbmc3k cells were profiled with the droplet method described by Zheng et al. 2017, "Massively parallel digital transcriptional profiling of single cells" (Nature Communications). Seurat version 5 is described in Hao et al. 2024 (Nature Biotechnology), and SCTransform in Hafemeister & Satija 2019 (Genome Biology). Luecken & Theis 2019, "Current best practices in single-cell RNA-seq analysis: a tutorial" (Molecular Systems Biology), discusses QC thresholds and why they must be chosen per dataset. Record the Seurat version you used, because defaults such as the UMAP method have changed between versions.

Key point

Quality control is a judgement made by looking at the data. Describe every cell with its UMIs, genes and mitochondrial share, plot them, choose thresholds that remove damaged cells and empty or double droplets, and report what you removed.

Further reading · Module 6Luecken & Theis 2019: a practical guide to every step of a single-cell analysis. · All reading for this course

Check your understanding

1. A cell has 3,000 UMIs, of which 600 come from genes starting with MT-. What does this most likely mean in a blood sample?

2. Why does Seurat store the counts as a sparse matrix?

3. Which layer of the Seurat object should PCA use?

Ask LSMA