| community | n |
|---|---|
| anaerobe | 37 |
| lactobacillus | 120 |
Workshop 2, Module 4
| 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.
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.

Fold changes for low-count genes are much less certain. A method built for counts uses this trend to borrow information across genes.
Give it raw counts, never CPM and never logged values. DESeq2 does its own normalisation, and feeding it normalised data breaks it.
| 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.
DESeq()| 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 |
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.
| 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.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.
| 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.
Every fold change here is negative, and every boxplot is higher on the Lactobacillus side, so the picture and the sign agree.
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.
| 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.
Community moves a few hundred genes a long way. Birth outcome moves a great many genes a little.
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.
| 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.
communityYou 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.
Before you trust the result, look at how the size factors were made.
| 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.

| 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.
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.
As before, Pi writes the code. Every step ends with something for you to check or decide.
Open notebooks/04-differential.qmd.