Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Cluster analysis of single cells

KTH JupyterHub Open In Colab Binder

Introduction

Clustering single cells is one of the applications that made unsupervised learning a routine tool in molecular biology. A single-cell experiment delivers a matrix of a few thousand cells that were deliberately not labelled — the whole point of the technique is that we do not have to sort the cells beforehand. Recovering the cell types from the expression data alone is exactly the partitioning problem set up in the clustering chapter, and we solve it here with the two algorithms introduced there, k-means and Gaussian mixture models.

We use the same dataset as in the chapter PCA of single cells: the pbmc3k set of roughly 2,700 peripheral blood mononuclear cells from a healthy donor, distributed by 10x Genomics. What makes it valuable for teaching is that we have an independent way of checking the answer. Immunologists have spent decades establishing marker genes that identify blood cell types, so once the algorithm has produced a partition we can ask what each cluster expresses, and judge whether the clusters are real.

The notebook is self-contained, so the loading and preprocessing steps of the single-cell PCA chapter are repeated here without further comment; see that chapter for the reasoning behind each step.

Loading and preprocessing the data

Notebook Cell

Quality control: keep cells with 200–2,500 detected genes and less than 5% mitochondrial counts, and keep genes seen in at least three cells.

2638 cells x 13656 genes

Normalisation to counts per ten thousand, log⁡(1+x)\log(1+x) transformation, selection of the 1,000 most variable genes, and standardisation.

Clustering in principal component space

As argued at the end of the single-cell PCA chapter, we do not cluster the 1,000-dimensional standardised matrix directly. Both k-means and Gaussian mixture models rely on distances between cells (Euclidean distances for k-means, distances scaled by each component’s variances for a mixture model), and in 1,000 dimensions those distances are dominated by counting noise. We therefore project the cells onto their first 15 principal components and cluster in that subspace.

Clustering space: (2638, 15)

Choosing the number of clusters

k-means requires us to fix the number of clusters kk in advance — one of the drawbacks listed in the theory chapter. Two standard diagnostics help us choose. The inertia, that is the within-cluster sum of squared distances ∑i∑x∈Si∥x−mi∥2\sum_i \sum_{\mathbf{x} \in S_i} \lVert \mathbf{x}-\mathbf{m}_i \rVert^2 that k-means minimises, always decreases as kk grows, so we look for an elbow where the improvement levels off. The silhouette score compares, for each point, its mean distance to the other members of its own cluster with its mean distance to the members of the nearest other cluster; it is largest when clusters are compact and well separated.

<Figure size 1100x400 with 2 Axes>
Loading...

The silhouette score is highest at k=2k=2. That is not an artefact: the deepest split in the data really is the myeloid/lymphoid division we saw along PC1, and a criterion that rewards well-separated compact clusters will find it. But a partition into two groups is a poor description of blood, where we know a handful of cell types are present. The score stays above 0.3 up to k=5k=5 and then falls sharply, from 0.32 to 0.22, between k=5k=5 and k=6k=6, after which it is flat. The elbow plot bends in the same region: the reduction in inertia is large up to k=4k=4–5 and modest thereafter. We therefore take k=5k=5, the largest number of clusters that the data still supports well.

This tension is worth dwelling on. Clustering criteria measure geometric separation, whereas we are interested in biological categories, and the two need not agree. The criteria narrow the range of sensible choices; they do not make the choice for us.

k-means with five clusters

0 310 1 264 2 421 3 335 4 1308 Name: cells per cluster, dtype: int64

We plot the clusters in the plane of the first two principal components, which we know from the single-cell PCA chapter carries most of the lineage information.

<Figure size 1300x500 with 2 Axes>

The clusters occupy contiguous, largely non-overlapping regions, which is reassuring but not by itself evidence that they are biologically meaningful — k-means would have produced five contiguous Voronoi cells even from a structureless cloud of points. To decide whether these clusters are real we need the marker genes.

Annotating the clusters with marker genes

We now compute, for each cluster, the mean log expression of a panel of established PBMC markers. Note that these genes play no part in the clustering itself beyond whatever weight they happened to receive as highly variable genes; the annotation is an external check.

Loading...

The same table is easier to read as a heatmap. We standardise each gene across clusters, so that the colour shows in which cluster a gene is relatively high rather than how strongly it is expressed overall.

<Figure size 1100x400 with 2 Axes>

The pattern is unambiguous, and each cluster is defined by a coherent set of markers rather than by a single gene:

Loading...

The clusters therefore correspond, one to one, to recognisable immune cell populations. Importantly, the proportions are plausible too: T cells are by far the most abundant PBMC population, followed by monocytes, B cells and NK cells, which is what we see.

What the clustering missed

Platelets, marked by PPBP, are not given a cluster of their own at k=5k=5; most of them end up inside the classical monocyte cluster, which is why cluster 1 shows the highest mean PPBP in the heatmap above. The FCER1A-positive dendritic cells fare similarly: they are clearly enriched in cluster 2, but only enough to nudge its mean.

Loading...

Both populations amount to a few dozen cells out of 2,638. k-means minimises a total sum of squares, and a cluster of thirty cells removes far less of that sum than splitting a population of six hundred, so small populations are systematically absorbed into larger ones. Detecting rare cell types requires either a much larger kk — at the price of fragmenting the abundant types — or methods that do not optimise a global variance criterion. This is a real and well-known limitation, not a peculiarity of this dataset.

A Gaussian mixture model of the same cells

k-means assigns every cell to exactly one cluster and, as discussed in the theory chapter, implicitly assumes clusters of similar spherical extent. Cell populations are not equally tight — a population of activated cells is more heterogeneous than a population of resting ones — so it is natural to fit a Gaussian mixture model instead, letting each component have its own variance along each principal component.

Loading...

The mixture model draws its boundaries differently. It merges the two monocyte clusters that k-means kept apart, and instead splits the T cells in two — one component with high CCR7 and no cytotoxic genes, the other with lower CCR7 and a trace of NKG7, which corresponds to the classical distinction between naive and memory/effector T cells. A cross-tabulation shows where the two solutions agree and where they do not.

Loading...

The cross-tabulation also reveals something the marker table alone would not. The mixture model’s cytotoxic component is broad: besides taking over most of the k-means NK cluster (281 of its 310 cells), it absorbs cells from every other cluster, including 97 of the 264 cells in k-means cluster 1, so that almost half of its 522 cells come from the other k-means clusters. That is a direct consequence of letting components have their own variances. A component fitted to a genuinely dispersed population acquires a large covariance, and a large covariance makes it the most probable origin for any cell that lies far from all the tight components. k-means, which treats every cluster as having the same spherical extent, cannot behave this way — the flip side of the drawback noted in the theory chapter.

Neither partition is “correct”. Both are valid five-way divisions of a population that actually contains more than five biologically distinct types; which division an algorithm prefers depends on its assumptions about cluster shape. Presented with the same data, a working immunologist would use several such views side by side.

The advantage of a mixture model is that it is a soft clustering: instead of an assignment it gives the responsibility γnk\gamma_{nk}, the posterior probability that cell nn belongs to component kk. Cells whose largest responsibility is far from one lie in the overlap between components, and their assignment should not be taken at face value.

<Figure size 1200x450 with 3 Axes>
12.8% of cells are assigned with probability below 0.9

The great majority of cells are assigned with near-certainty. The uncertain ones sit along the interfaces between components. Within the lymphoid cloud at low PC1, the split between the two T cell components and the transition towards the cytotoxic cells are both gradual rather than sharp. In the myeloid cloud at high PC1, the uncertain cells lie mostly in its lower left part, and they are torn between the monocyte component and the broad cytotoxic component: the large-covariance effect described above.

Discussion

Clustering pbmc3k works remarkably well, and it is worth being clear about why, because the reasons do not carry over to every dataset.

We had a ground truth. The decisive step in this chapter was not running k-means, which took a fraction of a second, but the marker-gene table that let us name the clusters. Peripheral blood is among the best-characterised tissues in biology, and each of our clusters could be matched to a cell type that immunologists had defined long before single-cell sequencing existed. In a less well-characterised tissue there may be no such marker panel, and a cluster can then only be interpreted through the genes that distinguish it from the others.

The groups were genuinely discrete. A monocyte and a B cell are separate cell types with separate transcriptional programmes, and nothing lies between them; our clusters are therefore separated by empty space. Where cells vary continuously, as they do along a differentiation trajectory, a clustering imposes boundaries on what is really a continuum. Even within our own data we saw a hint of this at the T cell/NK boundary, where the mixture model’s responsibilities became uncertain.

Every cell was measured the same way. All 2,638 cells come from one donor, one library and one sequencing run, so there are no batch effects to be mistaken for biology. Had we combined donors, the first principal component might well have separated donors rather than cell types.

In routine single-cell analysis, the problems we ran into, from the choice of kk to the rare populations and the disagreement between algorithms, are handled by clustering at several resolutions and annotating each of them, rather than by searching for one true value of kk. Clustering is exploratory rather than confirmatory: the algorithm proposes a partition, and the marker genes decide whether it means anything.

What you should know after reading this chapter

You should be able to:

  1. Define the inertia ∑i∑x∈Si∥x−mi∥2\sum_i \sum_{\mathbf{x} \in S_i} \lVert \mathbf{x}-\mathbf{m}_i \rVert^2 and the silhouette score, and interpret elbow and silhouette plots when choosing kk. Explain why these geometric criteria need not agree with the number of biological cell types.

  2. Explain why clusters that look well separated in a principal component plot are not by themselves evidence of real cell types, and interpret a heatmap of mean marker gene expression per cluster to annotate the clusters.

  3. Explain why k-means absorbs rare populations, such as the platelets, into larger clusters, and why a Gaussian mixture component with a large variance draws in outlying cells from the other clusters.

  4. Argue why clustering works well for these blood cells (known marker genes, discrete cell types, a single batch), and why it is harder to interpret when marker genes are lacking, when cells vary continuously, or when several batches are combined.