← Bioinformatics Series

Dimensionality Reduction

A cell measured on twenty thousand genes cannot be plotted. A 2D map of cells is a compression, and each method keeps a different part of the data.

Clustered Heatmaps drew every value of a matrix with a few dozen rows. A single-cell RNA-seq experiment measures around 20,000 genes in each of tens of thousands of cells, so each cell is a point in a 20,000-dimensional space. The dot plot in Five Views summarized cells by type; the types themselves are usually found on a 2D map made by dimensionality reduction, which places cells so that those with similar expression land close together. The common methods keep different things. PCA keeps the large distances and blurs the small ones. t-SNE and UMAP keep each cell's nearest neighbors and let the large distances go. Neither can keep a pattern that the distances themselves no longer contain.

All three run in this page on simulated data, animated as they go. The PCA essay of the algorithms series covers the mechanics of PCA step by step.

A t-SNE map of cells

The data here are 500 simulated immune cells, 100 of each of five types, measured on 24 genes: markers for each type and four genes expressed in every cell. T cells run on a gradient from naive (CCR7) to memory (S100A4), and monocytes from classical (CD14) to non-classical (FCGR3A), so cells of one type are not all alike. Each cell's counts are drawn from its library size, the number of its transcripts that were captured and sequenced. Library size differs …-fold between the smallest and largest cell here, for technical reasons, and …% of the matrix is zero because a gene's transcripts were not captured.

Before any map is made, each cell's counts are divided by its library size, scaled to 10,000 and log-transformed: a count becomes log(1 + 10,000 × count / library size). This is Seurat's default normalization, and the Scanpy tutorials do the same. Without it, a cell that happened to be sequenced deeper differs from the rest of its type in every gene at once.

Counts:

Figure 1. Simulated expression of real marker genes, levels invented. The 500 × 24 matrix, rows grouped by cell type (colored strip, darker means more expression), and beside it (below it on a narrow screen) a t-SNE map of the same rows, computed from the matrix alone; the colors are added afterwards from the known types. Hover, tap or use the arrow keys on either side to find the same cell in the other. The buttons switch between normalized and raw counts and rerun t-SNE.

t-SNE (van der Maaten and Hinton, 2008) turns distances between cells into probabilities that one cell would pick another as a neighbor, then moves points on the page until the neighbor probabilities there match. The run uses scikit-learn's default settings (perplexity 30, 1,000 iterations), from a seeded random start where scikit-learn would start from PCA. By the last iteration the objective has stopped falling: it changes by … over the final 100 iterations. The page computes its own exponentials and logarithms, since browsers can differ in the last digit, so every browser draws this same map. Cells of the same type share markers, so they are each other's neighbors: … of each cell's 10 nearest neighbors on the map are its own type, although t-SNE never saw the type labels.

Switch to raw counts and the five islands survive, but inside each one the cells line up by library size. Within a type, the distance between two cells across the 24 genes now tracks the difference in their library sizes (correlation …, against … after normalization), and t-SNE keeps that order. Figure 3 can color the map by library size to show it.

PCA and t-SNE on noisy data

Principal component analysis (PCA) projects onto the directions of largest variance. It is exact, fast and linear, and after normalization it is the usual first reduction step in a single-cell pipeline. It can miss a signal, though, when the directions of largest variance come from something else: batch effects, technical noise, or biology unrelated to the question.

Figure 2 sets that up with a known answer. 200 points lie on two interlocking half-moons in two dimensions, and ten more dimensions of pure noise are added, so the moons occupy two of 12 dimensions. Under each panel, a score counts how many of each point's 10 nearest neighbors on the page come from its own moon: 100% for the original two dimensions, about 50% for a layout that has lost the moons entirely.

t-SNE input:
The two moon dimensions
PCA of all 12 dimensions
t-SNE of all 12 dimensions

Figure 2. Simulated points. The moons as generated (one color per moon, paler toward one end), the first two principal components of the 12-dimensional data, and t-SNE of either all 12 dimensions or the top two principal components. Hover, tap or use the arrow keys to find a point in all three panels. Releasing a slider regenerates the data or reruns t-SNE.

At the starting noise of 0.3, PCA keeps the moons: … of neighbors stay on their own moon, because the two moon dimensions have the most variance and the top two components lie close to them. t-SNE on all 12 dimensions scores a little lower (…), because it preserves neighborhoods as measured in the full space, where every distance adds ten noise dimensions to the two that carry the moons.

Raise the noise to … and PCA's second component stops following the moons' height and becomes a noise direction. The height has the smaller variance of the two moon dimensions, so noise overtakes it first. PCA's score drops in one step to …, while t-SNE on all 12 dimensions, which still sees the height, keeps 82% at 0.4 and 78% at 0.5 (perplexity 30, first random start). At 0.9 PCA loses the width as well and scores …, about chance, against 61% for t-SNE.

With the top two PCs as t-SNE's input, below the step t-SNE scores 98 to 100% at every perplexity from 5 to 80: PCA has dropped the noise directions and kept the moons. From 0.4 to 0.5 the height is already gone from the top two components, and t-SNE on them scores 72 to 74%, no better than PCA, against 74 to 88% from all 12 dimensions. Single-cell pipelines run t-SNE or UMAP on the top 10 to 50 principal components rather than on raw genes for the first reason, and keep that many components to avoid the second. Perplexity (roughly, how many neighbors each point tries to keep) and the random start move these scores by a few points; averaged over three starts, the order never reverses.

Reading the map

A t-SNE map has no meaningful axes, and neither has a UMAP map (Figures 4 and 5). Cluster identity comes from marker-gene overlays: the island that lights up for CD3D is a T-cell island. Both methods keep a cell's near neighbors far better than its distant ones (Figure 4), so two islands far apart on the page may be similar cell types, and island sizes do not reflect how spread out a population is (Wattenberg, Viégas and Johnson, 2016).

Color by:
The whole map
Enlarged: the T cell island

Figure 3. The map from Figure 1, simulated cells, following its normalized or raw setting. The second panel enlarges the island of the selected cell (the box on the first). The buttons recolor cells by one gene's expression or by library size. Hovering, tapping or arrowing to a cell draws lines to its nearest neighbors measured in the original 24 dimensions: solid when the neighbor is the same cell type, dashed when it is not.

CCR7 is high at one end of the T-cell island and fades toward the other: the island's long axis follows the simulated naive-to-memory gradient (correlation …). … of each cell's 10 nearest neighbors in 24 dimensions are its own type, so the lines stay inside one island. They do not all stay short: on average only … of a cell's 10 nearest neighbors in 24 dimensions are also among its 10 nearest on the map. Within one type, what separates two cells is mostly counting noise spread over many genes at once, and two dimensions cannot hold all of it, so the exact neighbors are partly lost even where the islands are right. The share is about the same in B cells, which have no gradient, as in T cells, which do.

What each method keeps, by scale

The neighbor lines suggest a measure. For each cell, take its k nearest neighbors in 24 dimensions and its k nearest on the map, and count how many are in both. Averaged over cells, that share is the neighbor recall at k. A perfect map scores 1 at every k, and a random layout about k/499.

Figure 4 adds UMAP (McInnes, Healy and Melville, 2018), computed here with umap-learn's defaults. It joins each cell to its 15 nearest neighbors, weighting the nearest fully and the rest by how much farther they are, then lays out that graph over 500 epochs, pulling joined cells together and pushing random pairs apart. Its moves shrink to nothing by the last epoch; running 1,000 or 2,000 epochs moves recall at k = 15 by under two points.

Neighbor recall against k
Every pair of cells: 24 genes against the page

Figure 4. The normalized simulated cells of Figure 1. First panel: neighbor recall against neighborhood size k (log scale) for the t-SNE map, the UMAP map (spectral start, seed 1) and the first two principal components; the dotted line is a random layout. Hover, tap or arrow along it to read one k. Small panels: all 124,750 pairs of cells, their distance across the 24 genes against their distance on the page (scaled to the largest), as shaded bins, darker for more pairs. Dots are the 10 distances between cell-type centers, with monocyte to dendritic marked.

t-SNE wins locally: at k = 10 it keeps … of each cell's neighbors, against … for PCA. At k = 100, the size of one type, t-SNE keeps …: its islands are exactly the types. UMAP keeps … at k = 10 and … at 100. PCA overtakes t-SNE at k = …. The pairwise panels show why. PCA's page distances are a linear projection of the 24-gene distances, so they rise together across the whole range (rank correlation …). t-SNE's and UMAP's form a low band for pairs inside one type and flat clouds for pairs between types (… and …): once two cells are in different islands, how far apart the islands sit says little. Monocytes and dendritic cells share LYZ and CST3 and are the closest pair of types in 24 dimensions. On the PCA projection they are …; on the t-SNE map they are …. Across all ten pairs of type centers, the rank correlation with the 24-gene distances is … for PCA, … for t-SNE and … for UMAP.

What the start decides

UMAP is often said to keep global structure better than t-SNE. Figure 5 checks the claim against where each method starts.

t-SNE start: UMAP start: seed 1
t-SNE, random start
UMAP, spectral start
Rank of each pair of type centers, 1 = closest

Figure 5. The normalized simulated cells of Figure 1, mapped by t-SNE and UMAP from the chosen starts. Drag a box on either map (mouse), or tap or arrow to a cell type, to find the same cells on the other. Below, each line is a pair of cell types, ranked by the distance between their centers in the 24 genes and on each layout; shaded columns are the maps shown. Under each column: rank correlation with the 24-gene distances, and neighbor recall at k = 200. New random starts reruns the seeded layouts; the line under the chart averages the starts so far.

On the first start, t-SNE from a random start scores …, UMAP from its default spectral start … and UMAP from a random start …, against … for PCA. One start says little, so the averages come from 50 starts run outside the page: UMAP scores 0.33 from the spectral start and 0.06 from a random one (umap-learn itself: 0.43 and −0.10), and t-SNE 0.03 over ten random starts. t-SNE started from PCA, which has no randomness, scores …, about what UMAP gets from its spectral start. With the same kind of start, the two methods keep about as much of the arrangement, and neither comes near PCA. Kobak and Linderman (2021) found the same: the start, not the algorithm, decides most of the global layout.

The spectral start is thinner than it sounds. The 15-neighbor graph splits into … pieces, one per type, so umap-learn places the pieces from the distances between their centers, through weights that are almost zero at these distances. Four islands begin within 0.011 of each other on a map 10 units wide, in roughly the right order, and NK cells spread across the rest. Watch the UMAP map at its first epochs: the pile bursts, and part of the order goes with it.

A t-SNE or UMAP map answers which cells resemble this one, and a PCA projection answers how far apart the groups are. Clusters and neighbors are read from the first, distances between groups from the second, and both need normalized counts, because each keeps whatever the distances contain, library size included. Clinical Evidence moves from cells to patients: who responds to a treatment, who lives longer, and whether an effect seen in one cohort holds up in others.