Differential Testing

Workshop 2, Module 4

On Tuesday you built a column

community n
anaerobe 37
lactobacillus 120

These are the week 18 samples only, one per person.

You built community from the species table. Now we test the gene table with it.

The question

For each feature, is its count systematically different between two groups of samples?

In the gene table we ask it about 14,000 times, once per gene.

Counts break the assumptions of a t-test

  • Counts can’t go below zero, and they are skewed.
  • The variance depends on the mean: a gene at 10 counts behaves differently from one at 10,000.
  • Library sizes differ, so raw counts aren’t comparable between samples.
  • Estimating each gene’s variance on its own, from 157 samples, ignores what the other 14,000 genes can tell you.

The variance really does track the mean

Fold changes for low-count genes are much less certain. A method built for counts uses this trend to borrow information across genes.

Running DESeq2

library(DESeq2)

dds <- DESeqDataSetFromMatrix(
  countData = gene_counts,          # features x samples, raw counts
  colData   = sample_table,         # one row per sample, same order
  design    = ~ community
)

dds <- DESeq(dds)
res <- results(dds, contrast = c("community", "anaerobe", "lactobacillus"))

Give it raw counts, never CPM and never logged values. DESeq2 does its own normalisation, and feeding it normalised data breaks it.

Three things distort a count

gene table species table comparing one feature across samples
depth some samples sequenced deeper the same matters
feature yield long genes collect more fragments 16S copy number, primer fit cancels, because a gene is the same length in every sample
composition a few genes take up the reads one species often takes 90% of them matters, most of all for species

Differential testing compares one feature across samples, so it has to handle depth and composition. That is the job of the size factor, and it is why the size factor is not simply the library size.

Length does matter when you compare two genes inside one sample. That is what TPM is for, and it is not what we are doing today.

Inside DESeq()

  1. Size factors. One number per sample that puts the samples on a common scale, correcting for depth and composition.
  2. Dispersion. Estimates each feature’s variance, then pulls extreme estimates toward the trend you just saw.
  3. Test. Fits a model per feature and tests whether the group term is zero.

Size factors: the median of ratios

gene A B geometric mean A ÷ mean B ÷ mean normalised A normalised B
G1 100 200 141 0.71 1.41 141 141
G2 50 100 71 0.71 1.41 71 71
G3 300 600 424 0.71 1.41 424 424
G4 1000 500 707 1.41 0.71 1414 354
size factor 0.71 1.41
  1. Each gene’s geometric mean across samples is its reference: for G1, √(100 × 200) = 141.
  2. Divide each count by its reference. A sample’s size factor is the median of its ratios, so G4, the one gene that changed, cannot move it.
  3. Divide by the size factor. G1 to G3 now match, and only G4 differs.

The library sizes are 1,450 and 1,400, nearly equal, because G4 takes most of A’s reads. Dividing by library size would call G1 to G3 doubled in B.

Example adapted from Afrah Khairallah’s slides on normalising gene expression.

The results table

gene baseMean log2FoldChange stat pvalue padj
MUC22 6710 -2.74 -9.9 5.9e-23 8.2e-19
FAM25A 128 -3.54 -9.5 2.5e-21 1.7e-17
FLG 98986 -3.25 -9.3 1.4e-20 4.8e-17
SERPINB12 46 -4.12 -9.3 1.1e-20 4.8e-17
EDN2 177 -2.51 -9.1 8.8e-20 2.4e-16
  • log2FoldChange of 1 means twice as much, and -1 means half as much.
  • pvalue is per feature, before any correction.
  • padj is what you report.

Testing 13,738 genes at once

If no gene differed at all, how many would have pvalue below 0.05?

About 687, one in every twenty genes. The spike in the first bar is the real signal sitting on top of that.

That is why padj exists. Benjamini-Hochberg controls a different error: of the features you call significant, roughly 5% are wrong.

Volcano plot: anaerobe against Lactobacillus

1,815 significant genes, led by the epithelial barrier

gene name log2FoldChange
MUC22 mucin 22 -2.74
FAM25A family with sequence similarity 25 member A -3.54
FLG filaggrin -3.25
SERPINB12 serpin family B member 12 -4.12
EDN2 endothelin 2 -2.51
XKRX XK related X-linked -2.26
ALOX12 arachidonate 12-lipoxygenase, 12S type -2.97
LCE3D late cornified envelope 3D -3.13
ESAM endothelial cell adhesion molecule -2.39
CNFN cornifelin -2.19

Mucins, filaggrin and cornified envelope proteins are epithelial barrier genes, and all of these are higher where Lactobacillus dominates. We did not put that pattern there; it came with the real samples.

Always plot the top hit

Every fold change here is negative, and every boxplot is higher on the Lactobacillus side, so the picture and the sign agree.

Check the direction every time

Getting the direction of a contrast backwards is the most common error in this analysis. Nothing warns you and the table looks normal, so it regularly survives peer review.

A boxplot of the top gene, like the one on the last slide, shows the direction at a glance.

Quick check

Significant is not the same as large

contrast features tested samples padj < 0.05 and |log2FC| > 1
genes ~ community (from the 16S table) 13,738 157 1,815 303
genes ~ birth outcome 13,738 157 1,698 50
genes ~ Nugent-defined BV 13,721 97 7 3
species ~ birth outcome 53 157 0 0

Compare the first two rows: a similar number of significant genes, and a very different number of large changes.

Similar numbers of hits, different kinds of hit

Community moves a few hundred genes a long way. Birth outcome moves a great many genes a little.

Yesterday, birth outcome looked small

PERMANOVA said birth outcome explains 1.9% of the variation in the gene table, p = 0.005.

Today the same question gives 1,698 significant genes.

Both results hold. Many genes each shift a little, and small shifts in many genes still add up to a small share of a very large amount of variation.

Friday we come back to this: thousands of significant genes, and still a poor prediction of who delivers early.

A warning about proxies

grouping samples genes at padj < 0.05
Lactobacillus fraction, computed from the 16S table 157 1,815
Nugent score of 7 or more, read down a microscope 97 7

Both groupings ask whether the community is disturbed. The one built from data you already have finds hundreds of times as many genes.

Don’t test the species table with community

You built community from the species table, so testing the gene table with it is fine.

Testing the species table with it is circular. You would be asking whether samples with lots of Lactobacillus have lots of Lactobacillus.

The exercise tests species against birth outcome

# the grouping comes from the clinic, not from either count table
dds <- DESeqDataSetFromMatrix(species_counts, sample_table, ~ outcome)
dds <- DESeq(dds)

Before you trust the result, look at how the size factors were made.

Median of ratios needs features found in every sample

table features tested in all 157 samples
genes 13,738 12,112
species 53 1

A geometric mean with a zero in it is zero, so DESeq2 leaves those features out of the size factors.

In the species table that leaves one species, L. iners.

The normalisation decided the result

dds <- DESeqDataSetFromMatrix(species_counts, sample_table, ~ outcome)
dds <- estimateSizeFactors(dds, type = "poscounts")   # built for tables with zeros
dds <- DESeq(dds)
size factors species at padj < 0.05 higher in preterm
default: median of ratios, from L. iners alone 19 18
poscounts: uses every species 0 0

The default run tested each species against L. iners. Nearly all of its hits are familiar BV organisms, which is why nobody would have questioned it.

At one visit, no single species separates preterm from term. That matches the field: preterm birth has modest associations with community type and few clear ones with individual taxa. Check single features, then look beyond them.

Quick check

Other people’s tools

We used DESeq2 on both tables, because the point this week is that both tables are count tables. Not everyone does that.

for gene counts for microbial counts
DESeq2, edgeR ALDEx2, ANCOM-BC
limma-voom MaAsLin 2, corncob
DESeq2, still, often

The microbiome tools exist because that table is compositional and sparse in a way gene tables are not, as the size factors just showed. The case for them is worth reading before you publish a microbiome analysis.

In the exercises

  1. Ask Pi to test the gene table by community, and check the sample order and the direction of the contrast
  2. Look at the result as a volcano plot, an MA plot, a p-value histogram and boxplots
  3. Try several cutoffs and gene filters side by side
  4. Test the species table by outcome, then find out what the size factors did
  5. Save three result files for tomorrow

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

Your turn

Open notebooks/04-differential.qmd.