Distances and Ordination

Workshop 2, Module 3

From one feature to whole samples

This morning you looked at one sample or one feature at a time. This afternoon the question is how similar two whole samples are.

That gives one number per pair of samples, and then a picture of all of them at once.

A distance matrix

This toy has 12 samples. Ours has 471, which makes 110,685 pairs. The diagonal is zero, the matrix is symmetric, and there is nothing else in it.

Choosing a distance

  • Bray-Curtis works on proportions and measures how much of the two communities overlaps. Abundant features dominate the answer.
  • Jaccard works on presence and absence: the share of features present in either sample that are present in both. Abundance is ignored entirely.
  • Aitchison is Euclidean distance on centred log-ratios, built for data where only the ratios between features mean anything.

Two invented samples:

species A B
L. crispatus 900 300
G. vaginalis 60 400
A. vaginae 30 200
Prevotella 10 0
Sneathia 0 100
distance A to B
Bray-Curtis 0.61
Jaccard 0.4
Aitchison, pseudocount 1 5.53

The microViz pipeline

library(microViz)
source("R/count_tables.R")

as_counts(table = "microbiome") |>
  tax_transform("identity", rank = "species") |>   # knob 1: the transform
  dist_calc("bray") |>                             # knob 2: the distance
  ord_calc("PCoA") |>                              # knob 3: the layout
  ord_plot(colour = "community")
transform distance
"identity" on proportions "bray"
"binary" "jaccard"
"clr" "euclidean"

The species ordination under each distance

All three plots come from the same table. Which would you put in a paper, and how would you defend it?

Variation held by each axis

The first two axes of the species ordination hold about 49% of the variation. For the genes, about 29%.

Quick check

Colour the same plot by the thing you care about

Community type splits the plot cleanly. Birth outcome is the label you actually care about, and by eye you cannot see it.

Testing a label with PERMANOVA

as_counts(table = "microbiome") |>
  tax_transform("identity", rank = "species") |>
  dist_calc("bray") |>
  dist_permanova(variables = c("community", "outcome", "time_point", "rnaseq_batch"),
                 n_perms = 999, by = "margin")

PERMANOVA asks: if I shuffled this label across samples, how often would the groups end up this far apart by chance?

R2 is the share of the total variation that the label explains.

The species table

term R2 F p
community 17.5% 115.3 0.005
outcome 0.5% 3.0 0.010
time_point 8.7% 28.8 0.005
rnaseq_batch 0.2% 1.0 0.420
  • Community type explains 17.5%. Enormous, and expected, since we defined it from this table.
  • Visit explains 8.7%. These communities move over pregnancy.
  • Birth outcome explains 0.5%. Significant and tiny.
  • The RNA sequencing batch explains 0.2%, not significant. It should not touch the 16S data.

Now the gene table

The species code from earlier:

as_counts(table = "microbiome") |>
  tax_transform("identity", rank = "species") |>
  dist_calc("bray") |>
  ord_calc("PCoA") |>
  ord_plot(colour = "community")

The gene version filters out rare genes and swaps the transform, rank and distance. The rest is the same:

as_counts(table = "rnaseq", min_count = 10, min_samples = 100) |>
  tax_transform("clr", rank = "symbol") |>
  dist_calc("euclidean") |>
  ord_calc("PCoA") |>
  ord_plot(colour = "community")

Gene ordination: community, outcome, batch

The uncomfortable table

In the gene table, the sequencing batch explains as much as the biology does, and it is just as significant.

Dealing with batch

  • The ordination did not show it. PERMANOVA measured it.
  • Put it in the model: ~ rnaseq_batch + outcome.
  • If batch is confounded with your groups, nothing fixes it, so check that first.
  • None of this is possible if nobody wrote the batch down.

Significant and small

Birth outcome explains 1.9% of the variation in the gene table, p = 0.005.

Small is not the same as absent. Tomorrow afternoon you ask the same question one gene at a time, and the answer looks very different. Keep this number in mind until then.

Quick check

In the exercises

  1. Ask Pi for the species ordination three ways and argue about which to show
  2. Ask Pi what axis 1 means, then ask for the numbers that would back it up
  3. Put a number on each label with PERMANOVA, and build a null distribution by shuffling one
  4. Run the identical pipeline on the gene table
  5. Measure the batch effect, let Pi remove it, and work out what it did

As this morning, Pi writes the code. Every step ends with something for you to check or decide.

Your turn

Open notebooks/03-ordination.qmd.