Single-cell RNA-seq measures each cell on its own. Take a public blood dataset from a count matrix through quality control, clustering and marker genes to named cell types, and learn what the map cannot show.
In Beyond RNA-seq: other assays and pipelines single-cell RNA-seq appeared as one row of a table. This lesson takes it further: from a matrix of counts for thousands of single cells to a map of the cell types in a sample.
From one tube to thousands of cells
Bulk RNA-seq, the subject of this module so far, grinds a whole sample together. A gene's count is the sum over every cell in the tube, so a rare cell type, or a change in only one kind of cell, is averaged away. Single-cell RNA-seq (scRNA-seq) keeps the cells apart and measures the RNA of each one separately. The result is not one column per sample but one column per cell.
In the most common droplet methods, such as 10x Genomics Chromium, each cell is captured in a tiny oil droplet together with a bead. Every bead carries millions of short DNA primers that share one cell barcode, a sequence unique to that bead, and each primer also has a UMI (unique molecular identifier), a short random sequence. When the cell's messenger RNA is copied inside the droplet, every copy gets the droplet's barcode and its own UMI. All the droplets are then pooled and sequenced together, as one library.
After sequencing, the barcode says which cell a read came from, and the UMI says which original molecule. Reads that share a cell barcode, a gene and a UMI are PCR copies of one molecule and are counted once. This is the same duplicate problem you met in Quality control, solved by labelling molecules before they are copied rather than by guessing from read positions. Tools such as Cell Ranger or STARsolo do this step and write a count matrix: one row per cell, one column per gene, and the number of molecules in each cell.
What the matrix looks like
The matrix is large and mostly empty. A typical cell yields a few thousand distinct molecules, spread over perhaps one to three thousand genes, out of more than 30,000 genes in the annotation. Most entries are therefore zero. Some zeros are real, because the gene is off in that cell; many are simply molecules that were present but not captured. Because of this, single-cell data are stored as sparse matrices, which record only the non-zero values, and every method in this lesson is designed to cope with low counts per cell.
This lesson uses a classic public dataset: about 2,700 blood cells (peripheral blood mononuclear cells, or PBMCs) from one healthy donor, released by 10x Genomics and known as PBMC 3k. Blood is a good first dataset because its cell types are well described and each has known marker genes, so you can check whether the analysis finds what should be there.
Quality control and normalisation
The analysis is in Python with Scanpy, a widely used single-cell toolkit. Scanpy stores everything in one object, an AnnData: the count matrix in adata.X, a table of facts about each cell in adata.obs, and a table about each gene in adata.var.
conda create -n sc -c conda-forge scanpy python-igraph leidenalg conda activate sc
import scanpy as sc
adata = sc.datasets.pbmc3k() # downloads the 10x PBMC 3k counts once
adata.var_names_make_unique()
print(adata)
adata.var["mt"] = adata.var_names.str.startswith("MT-")
sc.pp.calculate_qc_metrics(adata, qc_vars=["mt"], percent_top=None,
log1p=False, inplace=True)
sc.pl.violin(adata, ["n_genes_by_counts", "total_counts", "pct_counts_mt"],
multi_panel=True)What this does
sc.datasets.pbmc3k()downloads the counts and returns an AnnData object. Printing it shows its size, about 2,700 cells by about 32,700 genes, and the names of its tables.var_names_make_unique()adds a suffix to the few gene names that occur twice, because later steps look genes up by name.- The mt column marks mitochondrial genes, whose names start with MT- in human data.
calculate_qc_metricsadds three numbers per cell toadata.obs: total_counts (molecules in the cell), n_genes_by_counts (genes with at least one molecule) and pct_counts_mt (the share of molecules from mitochondrial genes).- The violin plots show how each number is spread across all cells.
Reading the QC plots
Each measure flags a different problem. A droplet with very few genes usually held no cell at all, only loose RNA from the surrounding liquid, or a cell whose RNA was badly captured. A cell with a high mitochondrial share is often damaged or dying: its membrane has leaked, so the cytoplasmic RNA has escaped while the RNA inside the mitochondria remains. At the other end, a droplet with far more genes and molecules than the rest may hold two cells, a doublet.
The thresholds are chosen by looking at these plots, not copied from elsewhere. For PBMC 3k a common choice keeps cells with at least 200 genes and under 5% mitochondrial counts, and keeps genes seen in at least 3 cells. Tissues differ: heart and kidney cells are rich in mitochondria, so a 5% cut-off there would remove healthy cells.
sc.pp.filter_cells(adata, min_genes=200) sc.pp.filter_genes(adata, min_cells=3) adata = adata[adata.obs["pct_counts_mt"] < 5].copy() adata.layers["counts"] = adata.X.copy() # keep the raw counts sc.pp.normalize_total(adata, target_sum=1e4) sc.pp.log1p(adata) sc.pp.highly_variable_genes(adata, n_top_genes=2000) print(adata)
What this does
- The first three lines apply the filters.
.copy()makes a new object rather than a view of the old one, which avoids warnings and mistakes later. adata.layers["counts"]stores an untouched copy of the counts. Some later methods, and any comparison between conditions, need the raw numbers, not the transformed ones.normalize_totalscales each cell so its counts add up to 10,000. This is the single-cell version of counts per million from Counting reads per gene: it removes the difference in how many molecules were captured per cell.log1ptakes the natural log of each value plus one, so a gene's tenfold difference counts the same whether it is highly or lowly expressed, and zeros stay zero.highly_variable_genesmarks the 2,000 genes whose expression varies most between cells, relative to their average level. These carry most of the information about cell types; genes that are flat in every cell add only noise to the next steps.
Printing the object again shows fewer cells and genes, and new entries such as highly_variable in adata.var. The exact numbers depend on the thresholds, so note them down with your settings, as you did for trimming in Trimming and filtering.
Doublets deserve a separate check, because a doublet of a T cell and a monocyte looks like a cell expressing both sets of markers, and can form a small false cluster between two real ones. Tools such as Scrublet simulate doublets by adding random pairs of real cells together, then score each real cell by how much it resembles those artificial pairs. Recent Scanpy versions include it as sc.pp.scrublet; run it on the raw counts, before normalisation, and look at the cells it flags before removing them.
Go deeper: empty droplets and ambient RNA
Most droplets in a run contain no cell. They still capture a little free-floating RNA, released by cells that burst during preparation, and that RNA gets a barcode like any other. Cell Ranger separates real cells from empty droplets by looking at the number of molecules per barcode: real cells form a group well above the long tail of empty droplets. Methods such as EmptyDrops go further and test whether a barcode's RNA profile differs from the background.
The same free RNA, called ambient RNA, also enters droplets that do hold a cell. The result is a faint signal of abundant genes from other cell types in every cell, for example a trace of haemoglobin genes in every blood cell when red cells have burst. Tools such as SoupX and CellBender estimate this background and subtract it. In clean samples like PBMC 3k the effect is small; in tissues that need harsh dissociation it can be large enough to blur cell types.
Clusters, maps and cell types
With the cells cleaned and normalised, the next steps find groups of similar cells. Keep the steps apart in your mind: the picture at the end is the least reliable part.
sc.tl.pca(adata, n_comps=50) sc.pp.neighbors(adata, n_neighbors=15, n_pcs=40) sc.tl.leiden(adata, resolution=0.5) sc.tl.umap(adata) sc.pl.umap(adata, color=["leiden", "CD3E", "MS4A1", "CD14", "NKG7"]) sc.tl.rank_genes_groups(adata, "leiden", method="wilcoxon") sc.pl.rank_genes_groups(adata, n_genes=10, sharey=False)
What this does
sc.tl.pcaruns principal component analysis on the highly variable genes. PCA replaces 2,000 correlated genes with 50 new axes, each a weighted mix of genes, ordered by how much of the variation between cells they capture. Genes that rise and fall together, such as the markers of one cell type, end up on the same axis, and much of the noise is left in the later axes.sc.pp.neighborsfinds, for each cell, the 15 cells closest to it in the space of the first 40 principal components, and joins them into a graph. This graph is the real map of the data: everything after it is computed from it.sc.tl.leidensplits the graph into clusters, groups of cells that are more connected to each other than to the rest, using the Leiden algorithm. The resolution setting controls how finely it splits: higher values give more, smaller clusters. Newer Scanpy versions may print a warning recommending extra options; the default still runs.sc.tl.umaplays the graph out in two dimensions for drawing, andsc.pl.umapcolours the cells by cluster and by four marker genes.rank_genes_groupscompares each cluster with all other cells and ranks the genes that are higher in that cluster, using a Wilcoxon rank-sum test. The plot shows the top ten genes per cluster.
Reading the result
The UMAP shows several islands of cells. Colouring by markers shows which island is which: CD3E lights up the T cells, MS4A1 (the gene for CD20) the B cells, CD14 the classical monocytes and NKG7 the natural killer (NK) cells, with some signal in cytotoxic T cells too. The marker list from rank_genes_groups lets you name the remaining clusters in the same way.
| Cell type | Typical marker genes |
|---|---|
| CD4 T cells | IL7R, CCR7 (naive), CD3E |
| CD8 T cells | CD8A, CD3E |
| B cells | MS4A1, CD79A |
| NK cells | GNLY, NKG7 |
| CD14 monocytes | CD14, LYZ |
| FCGR3A monocytes | FCGR3A, MS4A7 |
| Dendritic cells | FCER1A, CST3 |
| Platelets | PPBP |
How to read this table
Each row is a blood cell type and the genes that mark it. A marker gene is one that is clearly higher in that cell type than in the others; it need not be absent elsewhere. Naming a cluster means checking several markers together, not one: CD3E alone says T cell, and CD8A then splits CD8 from CD4 T cells. If a cluster's top genes match no row, it may be a cell type not listed here, a doublet, or a group of stressed or dying cells; its mitochondrial share and gene counts help decide.
Some clusters resist a clean name. CD4 and CD8 T cells often sit close together and may share a cluster at low resolution; a higher resolution may split them, or split one cell type by activation state. The number of clusters is a choice about how finely to describe the cells, so state the resolution you used and why.
Annotation can also be done automatically, by comparing each cell with a labelled reference atlas, with tools such as CellTypist. Automatic labels are a useful start, but check them against markers, because a reference built from different tissue or a different protocol can be confidently wrong.
Worked example · what normalisation does to two cells
cell B: 8,000 molecules, 80 from LYZ → 80 ÷ 8,000 × 10,000 = 100 → log(100 + 1) ≈ 4.62
Step by step. Cell B gave four times as many molecules as cell A, so every raw count in it tends to be four times higher. Dividing by each cell's total and multiplying by 10,000 puts both on the same scale: 20 ÷ 2,000 = 0.01, and 0.01 × 10,000 = 100; 80 ÷ 8,000 = 0.01, giving 100 again. The natural log of 101 is about 4.62.
What the answer means. Raw counts say cell B has four times more LYZ; after normalisation the two cells are the same, which is the fair comparison, because both spend 1% of their captured RNA on this gene. The same caution as in Counting reads per gene applies: this method assumes the total is mostly a technical difference. A cell type that genuinely makes much more RNA in total is partly hidden by it.
What the map can and cannot tell you
A UMAP is the figure everyone remembers, and the easiest to over-read. UMAP's job is to keep each cell near its graph neighbours. It does not preserve distances between islands: two clusters drawn far apart are not necessarily more different than two drawn close together, and the size of an island says little about how varied its cells are. Different random seeds or settings can move islands around while the clusters stay the same. Treat the map as an illustration of the graph, and base conclusions on the clusters, the markers and the numbers.
The second caution is about replicates. PBMC 3k comes from one donor, so it can describe which cell types were in that person's blood, but not how cell types differ between groups of people. When an experiment compares, say, patients with controls, the thousands of cells from one person are not thousands of independent samples; they share that person's genes, age and sample handling. Testing genes cell by cell treats them as independent and produces many false positives. This mistake is called pseudoreplication.
The usual remedy is a pseudobulk analysis. For each person and each cell type, add up the raw counts of all that person's cells of that type. The result is a small count table, one column per person, just like the bulk count table from Counting reads per gene, and it is tested with the same bulk methods, such as DESeq2 or edgeR. The design advice from From sample to reads applies unchanged: the number of people per group, not the number of cells, decides how much you can conclude.
Scanpy is described by Wolf, Angerer and Theis (2018, Genome Biology), and the droplet method that produced PBMC 3k by Zheng et al. (2017, Nature Communications). The Leiden algorithm comes from Traag, Waltman and van Eck (2019, Scientific Reports), UMAP from McInnes, Healy and Melville (2018, arXiv) and Scrublet from Wolock, Lopez and Klein (2019, Cell Systems). Squair et al. (2021, Nature Communications) showed that cell-level tests between conditions give many false discoveries and that pseudobulk methods avoid most of them, and Chari and Pachter (2023, PLOS Computational Biology) set out how much two-dimensional maps distort the data. Report the Scanpy version, every threshold and the resolution used.
Common mistakes
- Copying QC thresholds from another tissue or protocol instead of choosing them from your own plots.
- Reading distances or island sizes on a UMAP as measures of how different cell types are.
- Testing a condition cell by cell, so that one person's thousands of cells count as thousands of replicates.
- Naming a cluster from a single marker gene, or trusting an automatic label without checking markers.
- Throwing away the raw counts after normalisation, when pseudobulk and other methods need them.
Key point
Clusters come from a graph of similar cells; UMAP only draws it. Name clusters with several markers, and compare conditions with one sample per person, not one per cell.
Further reading · Module 4Single-cell best practices: a free online book that follows each step of this lesson in more depth, in Python. · Scanpy documentation: tutorials and the reference for every function used here. · All reading for this course
Check your understanding
1. Why are reads that share a cell barcode, a gene and a UMI counted once?
2. A cell has a very high share of mitochondrial counts. What does this usually suggest?
3. You compare 3 patients with 3 controls, 5,000 cells each. How many independent replicates per group are there?