Exercise: single-cell RNA-seq data & QC
Goal
By the end of this exercise you should be able to:
- describe the single-cell count matrix (cells × genes, integer counts, sparse) and why it is stored sparsely,
- compute the core per-cell QC metrics by hand — total counts and genes detected — on a tiny matrix,
- explain what an empty droplet is, and the signature it leaves in the QC metrics,
- carry out library-size normalization and the log1p transform on a small example, by hand and in R,
- explain why we select highly variable genes, and why “highest variance” is the wrong criterion for count data.
This is a concept + data-handling day. There is no pen-and-paper algebra to memorise; instead the by-hand tasks build a concrete feel for how each QC number is computed from the matrix. The R shown is illustrative — you can run the small base-R snippets, but the Seurat pipeline is read-only pseudocode.
Background
A single-cell experiment produces a count matrix \(X\) with cells in rows and genes in columns; entry \(X_{cg}\) is the number of UMIs (molecules) of gene \(g\) detected in cell \(c\). It is a non-negative integer, and most entries are 0.
For each cell \(c\) we compute:
- total counts (library size): \(\;n_c = \sum_g X_{cg}\)
- genes detected: \(\;\#\{g : X_{cg} > 0\}\)
QC removes cells that are empty droplets or broken cells (very low counts and few genes = mostly ambient background). Unusually high counts are also suspicious.
Preprocessing then makes cells comparable and tames the dynamic range:
- Library-size normalize: \(\;\tilde X_{cg} = \dfrac{X_{cg}}{n_c}\times s\;\) (scale factor \(s\), e.g. 10,000).
- Pseudolog transform: \(\;Y_{cg} = \log(1 + \tilde X_{cg})\).
Finally we keep the highly variable genes (HVGs) — genes that vary more than expected for their mean — because in count data variance grows with the mean, so raw variance alone just picks the highest-expressed genes.
Throughout, use this tiny toy matrix of 4 cells × 5 genes.
\[ X=\begin{array}{c|ccccc} & G1 & G2 & G3 & G4 & G5\\\hline \text{Cell A} & 4 & 0 & 6 & 0 & 0\\ \text{Cell B} & 0 & 0 & 0 & 3 & 2\\ \text{Cell C} & 10 & 8 & 0 & 1 & 1\\ \text{Cell D} & 0 & 0 & 1 & 0 & 0 \end{array} \]
By hand
B1. For each of the four cells, compute the total counts \(n_c\) and the number of genes detected.
B2. Compare the library sizes from B1: what is the mean total count, and how many times larger is the biggest library than the smallest? Why does this mean raw counts cannot be compared directly between cells?
B3. Suppose your QC rule is: keep a cell only if it has at least 2 genes detected and total counts \(\ge 5\). Apply both filters and list which cells survive. For each cell you drop, name the likely problem.
B4. Take Cell C (the row 10, 8, 0, 1, 1). Library-size normalize it with scale factor \(s = 100\) (we use 100 instead of 10,000 so the numbers stay friendly): divide each entry by the cell’s total, then multiply by 100. Then apply the log1p transform \(\log(1+x)\) to the normalized values. (Use natural log; a calculator is fine.)
B5. The matrix has 20 entries. How many are zero? What fraction is that? Explain, in one sentence each, the two different reasons a zero might appear in a real dataset.
B6. Consider two hypothetical cells from a much larger experiment, where most cells have 3,000–10,000 total counts:
- Cell X: total counts = 150, genes detected = 90.
- Cell Y: total counts = 6,000, genes detected = 2,400.
For each, say whether you would suspect an empty droplet or a healthy cell, and give your reason.
B7. (concept) You have two genes measured across many cells: ACTB (a housekeeping gene, very high mean, similar in all cells) and CD3E (a T-cell marker, zero in most cells but high in T cells). ACTB has the larger raw variance. Which gene is more useful for distinguishing cell types, and why is ranking genes by raw variance a poor way to select features?
In R
You can run the small base-R snippets below. The Seurat block at the end is read-only — no single-cell packages are installed for this course, and you are not expected to run it.
Build the toy matrix
# cells in rows, genes in columns
X <- matrix(c(4, 0, 6, 0, 0,
0, 0, 0, 3, 2,
10, 8, 0, 1, 1,
0, 0, 1, 0, 0),
nrow = 4, byrow = TRUE)
rownames(X) <- c("A", "B", "C", "D")
colnames(X) <- c("G1", "G2", "G3", "G4", "G5")
XPer-cell QC metrics
total_counts <- rowSums(X) # library size per cell
genes_detected <- rowSums(X > 0) # count of non-zero genes per cell
cbind(total_counts, genes_detected)
mean(total_counts) # B2
max(total_counts) / min(total_counts)Apply the QC filter (B3)
keep <- genes_detected >= 2 & total_counts >= 5
X[keep, , drop = FALSE] # surviving cellsNormalize + log a single cell (B4)
cellC <- X["C", ]
norm <- cellC / sum(cellC) * 100 # library-size normalize, scale = 100
log1p(norm) # log(1 + x), the standard transformRead-only: the same steps in Seurat
library(Seurat)
obj <- CreateSeuratObject(counts, min.cells = 3, min.features = 200)
obj <- subset(obj, subset = nFeature_RNA > 200 & nCount_RNA > 500)
obj <- NormalizeData(obj) # library-size + log1p
obj <- FindVariableFeatures(obj, nfeatures = 2000) # highly variable genesTasks
R1. Run the QC-metrics block and confirm it matches your hand answers from B1 and B2.
R2. Run the filter block (B3) and check the surviving cells match your by-hand list. Change the total_counts >= 5 threshold to total_counts >= 6 and see which cell drops out.
R3. Run the normalize + log block for Cell C (B4) and confirm it matches your hand computation. Then confirm that a genuine zero stays zero after log1p (try log1p(0)).
Hints
Total counts for a cell is just the sum of its row. Genes detected counts how many entries in the row are strictly greater than 0 (the number of non-zero genes, not their sum). In R, rowSums(X) gives totals and rowSums(X > 0) gives genes detected (because X > 0 is a TRUE/FALSE matrix and TRUE counts as 1).
The mean is the sum of the four totals divided by 4. For the ratio, divide the largest total by the smallest. If one cell simply captured 20 times more RNA, every gene in it looks about 20 times higher — that is a technical difference, not biology.
A cell must pass both conditions to be kept. Evaluate each condition per cell (TRUE/FALSE), then keep a cell only where every condition is TRUE. In R, combine conditions with &. Very low counts / few genes suggest an empty droplet or near-empty capture.
Normalize first: divide each count by the cell’s total (\(n_c\)) so the row sums to 1, then multiply by the scale factor \(s\). Only after that do you apply log1p \(=\log(1+x)\). The \(+1\) is what keeps a zero at zero: \(\log(1+0)=\log 1 = 0\).
Empty droplet: very low total counts / few genes compared with the bulk of the cells — it is mostly ambient background RNA. A cell in the typical range for the experiment is consistent with a healthy cell.
In count data, variance grows with the mean — a highly expressed gene has a large variance just because it is big, not because it carries cell-type information. A housekeeping gene like ACTB is high and roughly constant across cells (high variance, low information); a marker like CD3E is zero in most cells and high only in one type (lower raw variance, but exactly the signal you want). HVG selection ranks genes by variance relative to the mean–variance trend, not raw variance.
Solutions
B1. Sum each row for total counts; count the non-zero entries for genes detected.
| Cell | Row | Total counts \(n_c\) | Genes detected |
|---|---|---|---|
| A | 4, 0, 6, 0, 0 | \(4+6 = 10\) | 2 (G1, G3) |
| B | 0, 0, 0, 3, 2 | \(3+2 = 5\) | 2 (G4, G5) |
| C | 10, 8, 0, 1, 1 | \(10+8+1+1 = 20\) | 4 (G1, G2, G4, G5) |
| D | 0, 0, 1, 0, 0 | \(1\) | 1 (G3) |
B2. Mean total count: \((10+5+20+1)/4 = 36/4 = 9\). The largest library (Cell C, 20) is \(20/1 = 20\) times the smallest (Cell D, 1).
So a gene in Cell C can look many times higher than in another cell just because Cell C’s droplet captured more RNA overall — a technical difference (capture efficiency, sequencing depth), not biology. That is why we library-size normalize (B4) before comparing cells.
B3. Apply the two rules (keep only if both hold): genes detected \(\ge 2\), total counts \(\ge 5\).
| Cell | genes ≥ 2? | total ≥ 5? | Keep? |
|---|---|---|---|
| A | 2 ✓ | 10 ✓ | Keep |
| B | 2 ✓ | 5 ✓ | Keep |
| C | 4 ✓ | 20 ✓ | Keep |
| D | 1 ✗ | 1 ✗ | Drop |
Survivors: A, B and C. Cell D has just 1 count in 1 gene → empty droplet / too little data (almost no real RNA captured). Cell B only just passes (exactly 5 counts) — a sign that the threshold matters.
B4. Cell C is 10, 8, 0, 1, 1 with total \(n_C = 20\). Normalize (divide by 20, ×100 → i.e. ×5), then log1p.
Normalized values (\(X_{cg}/20 \times 100 = X_{cg}\times5\)):
\[10\to50,\quad 8\to40,\quad 0\to0,\quad 1\to5,\quad 1\to5.\]
Now \(\log(1+x)\) (natural log):
- \(\log(1+50) = \log 51 \approx 3.932\)
- \(\log(1+40) = \log 41 \approx 3.714\)
- \(\log(1+0) = \log 1 = 0\)
- \(\log(1+5) = \log 6 \approx 1.792\)
- \(\log(1+5) = \log 6 \approx 1.792\)
So the transformed Cell C is approximately \((3.93,\ 3.71,\ 0,\ 1.79,\ 1.79)\). Note the zero stayed exactly 0, and the large 50 was pulled down close to the 40 — the log compresses the dynamic range.
B5. The 20 entries are: A (4,0,6,0,0), B (0,0,0,3,2), C (10,8,0,1,1), D (0,0,1,0,0). Zeros: A has 3, B has 3, C has 1, D has 4 → \(3+3+1+4 = 11\) zeros out of 20, i.e. 55% (real data is often 90%+).
The two reasons a zero appears: - Biological zero: the gene is genuinely not expressed in that cell. - Technical zero (dropout): the gene is expressed but its molecules were not captured/sequenced — a false zero from limited sensitivity.
B6. - Cell X (150 counts, 90 genes): far below the typical 3,000–10,000 counts, with few genes → suspect an empty droplet (or a broken/near-empty cell) — mostly ambient background, little real RNA. - Cell Y (6,000 counts, 2,400 genes): right in the typical range for this experiment → consistent with a healthy cell; nothing in these two numbers argues for removing it.
B7. CD3E is far more useful: it is off in most cells and on only in T cells, so it separates a cell type — exactly the signal we want. ACTB is high everywhere and roughly constant, so despite its large raw variance it tells you almost nothing about which cell is which. Ranking by raw variance is poor because in count data variance scales with the mean: high-mean genes look variable just from their size. HVG selection instead scores each gene by how much its variance exceeds the mean–variance trend, catching genes like CD3E that vary more than their expression level alone would predict.
R1–R3. The R blocks reproduce the hand answers directly:
X <- matrix(c(4, 0, 6, 0, 0,
0, 0, 0, 3, 2,
10, 8, 0, 1, 1,
0, 0, 1, 0, 0),
nrow = 4, byrow = TRUE)
rownames(X) <- c("A", "B", "C", "D")
colnames(X) <- c("G1", "G2", "G3", "G4", "G5")
total_counts <- rowSums(X) # A 10, B 5, C 20, D 1
genes_detected <- rowSums(X > 0) # A 2, B 2, C 4, D 1
mean(total_counts) # 9
max(total_counts) / min(total_counts) # 20
keep <- genes_detected >= 2 & total_counts >= 5
X[keep, , drop = FALSE] # A, B and C survive
cellC <- X["C", ]
log1p(cellC / sum(cellC) * 100) # 3.93 3.71 0 1.79 1.79
log1p(0) # 0 -> a true zero stays zeroRaising the count threshold to total_counts >= 6 drops Cell B (exactly 5 counts) as well — showing how sensitive the surviving set is to a threshold you must choose from the data.
Where this leads
- Log and pseudolog transforms (optional): why the log1p step in B4 is there, why you average counts before logging, and how the pseudocount and scale factor change low counts.
- Nonlinear embeddings & clustering (next unit): the cleaned, normalized, log-transformed, HVG-selected matrix is fed to PCA (the eigen-decomposition of a covariance matrix you built in the linear algebra block) and then UMAP, to visualize and cluster cells by type.
- Count models (Error models, week 4): the noisy, overdispersed counts motivate Poisson and negative-binomial models and more principled normalization — the simple library-size + log1p here is a first-pass baseline.
- Ambient-RNA correction has dedicated tools (
EmptyDrops,SoupX); today you learned the concepts and the QC metrics they build on. - Single-cell QC in depth (optional): mitochondrial and ribosomal fractions, and doublets.