Label transfer, taken apart.

PCA and CCA from Scratch. How Seurat moves cell labels from a reference to a query and puts two datasets together, rebuilt by hand in R and checked against Seurat at every step.

Included with Practical scRNA-seq Analysis with R and with both course bundles. It is not sold separately.

Already enrolled in the scRNA-seq course? It is in your account. Open the course

UMAP of two PBMC datasets in one CCA embedding, colored by cell type, from lesson 6

Two functions annotate your cells.

Can you say what they did to your data?

FindTransferAnchors() and TransferData() give every cell a label and a score. When a label looks wrong, the function names don't tell you where to look: the genes, the scaling, the neighbors, the anchors or the weights. This course opens both functions and rebuilds each step by hand on two public PBMC datasets, then checks the result against Seurat. Our PCA comes out identical to RunPCA(), our CCA matches RunCCA() to 14 decimal places, and 9,972 of the 10,020 anchors we find by hand in the CCA space are exactly Seurat's.

What you will learn.

Six lessons.

Text you can search and code that runs, on real data. Self-paced. Every lesson ends with two exercises and worked solutions. Open a lesson to see what it covers.

Free preview Lesson 1, The reference, the query and the question, is open to everyone. Read it now

Lesson 1 · The reference, the query and the question Free preview

Read this lesson free, no sign-up

  • The question: a reference with labels, a query without
  • The query: 2,700 PBMCs
  • The reference: 9,432 annotated PBMCs in an old Seurat object
  • Keep the genes both datasets have
  • Your turn, with solutions
Lesson 2 · PCA is SVD: rebuild Seurat's PCA by hand
  • PCA is an SVD
  • Genes in rows, cells in columns: transpose first
  • PCA with irlba, the way Seurat runs it
  • Four checks, one of them against RunPCA()
  • How much variance each PC explains
  • What PC1 and PC2 separate
  • Your turn, with solutions
Lesson 3 · Project the query and transfer labels with nearest neighbors
  • Scale the query the way the reference was scaled
  • Project the query with Z = XV
  • Find the nearest neighbors with Annoy
  • Label transfer by majority vote
  • Compare with Seurat's wrapper
  • Which labels are closer to the truth?
  • Your turn, with solutions
Lesson 4 · Mutual nearest neighbors and anchors
  • One-sided and mutual neighbors
  • Find the mutual pairs
  • Who has no mutual neighbor?
  • Labels from mutual neighbors, with a score
  • What Seurat does: anchors
  • Anchors from scratch
  • How TransferData() uses the anchors
  • Your turn, with solutions
Lesson 5 · CCA from scratch
  • What CCA looks for
  • Two matrices with the same genes
  • One SVD of the cross-product
  • Are they really correlated?
  • The same as Seurat's RunCCA()
  • What CC1 and CC2 separate
  • L2 normalization
  • Your turn, with solutions
Lesson 6 · Label transfer and integration with CCA
  • Mutual nearest neighbors in the CCA space
  • Labels from the mutual neighbors
  • Seurat's wrapper with reduction = "cca"
  • How the labels compare
  • One embedding for both datasets
  • What if we do not integrate?
  • PCA or CCA?
  • Your turn, with solutions

The figures you will make.

UMAP of the joint CCA embedding, one panel per dataset: the 3k cells fall on the same islands as the 10k cells
Lesson 6 · two datasets, one embedding
2,700 query cells projected onto PC1 and PC2 of the reference
Lesson 3 · the query in the reference's PCA space
Heatmap of our transferred labels against Seurat's labels
Lesson 3 · our labels against Seurat's
Bar plot of the 20 canonical correlations
Lesson 5 · the real canonical correlations
UMAP of the merged data without integration: the cells separate by dataset
Lesson 6 · the same data without integration

What's included.

  • 6 text lessons with all the code, part of your scRNA-seq course access
  • Real data: two public 10x Genomics PBMC datasets, 2,700 query cells and 9,432 annotated reference cells
  • Twelve exercises with worked solutions
  • Every step checked against Seurat 5, with the lines of Seurat's source that do the work
  • The full code to download: every lesson as .qmd and plain .R scripts, plus the package versions
  • Ask questions right on each lesson section; classmates and I answer there

Who it's for

You use Seurat, and you have run FindTransferAnchors() and TransferData() or are about to. The labels came out, and you could not say why a cell got the label it got. You know basic R. You do not need to remember your linear algebra: if you can read A %*% B, you can follow every lesson.

It follows chapter 4, Cell annotation, of Practical scRNA-seq Analysis with R.

Ming (Tommy) Tang

Your instructor.

Dr. Ming "Tommy" Tang, Director of Bioinformatics in Big Pharma. He trained in a wet lab during his PhD, then spent over ten years in computational biology, including leading the bioinformatics analysis for large clinical trials at Dana-Farber Cancer Institute. This course grew from two posts on his blog, Diving into Genetics and Genomics, checked line by line against Seurat's own source code.

Included with the scRNA-seq course.

Enroll in Practical scRNA-seq Analysis with R and you get its five chapters and this course: one payment, lifetime access and every update.

$299 $599

If you sign up and feel the course isn't right for you, email within seven days for a full refund.

Enroll in the scRNA-seq course

See everything the scRNA-seq course covers.

Also included in both course bundles: see the bundles.

Already bought a course and want a bundle? Reply to your receipt and I'll credit what you paid.

Questions people ask.

Can I buy this course on its own?

No. It comes with Practical scRNA-seq Analysis with R and with both course bundles. If you have any of them, it is in your account now.

I already have the scRNA-seq course. Do I pay again?

No. Log in at learn.chatomics.com with your purchase email and open the course.

Do I have to finish the scRNA-seq course first?

Take it after chapter 4, Cell annotation. That chapter uses the two Seurat functions; this course shows what is inside them. Lesson 1 is free, so you can check the level before you enroll.

How much math do I need?

Matrix multiplication. Every formula is followed by code that tests it, and the lessons point to the best places to learn more linear algebra.

Do I need to install anything?

The one-click RStudio in the browser that comes with the scRNA-seq course has every package this course uses except one, which takes one line to install. On your own computer, the Welcome page lists the packages and how to download the two data files (182 MB).

How can I ask questions?

Right on the lesson: every section has a button to ask about it and to read what classmates already asked. I answer there.