Differential Gene Expression
Bioinformatics Core Facility CECAD
2026-11-24
git clone https://github.com/CECADBioinformaticsCoreFacility/Intermediate_R_Course_2026.git
https://cecadbioinformaticscorefacility.github.io/Advanced_R_Course_2026/
Session 4:: Differential Gene Expression – Models
Model formulae like ~ 1 + genotype provide a high level language
for the experimenter to conceptually express a hypothesis about his or her data
for modeling software to know the structure of the tests to compute
Independent of the actual mathematics of the tests !
We can use a model “~ age + sex + tissue + genotype” to describe the full dataset. At least in theory the model should partition the total variance in the data into the contributions of the experimental variables, such that we can study the genotype effect unconfounded.
These are the available test results:
dt <- DT::datatable(
res_additive |> as.data.frame() |>
dplyr::filter(!is.na(padj), padj <= 0.05) |>
dplyr::arrange(padj),
options=list(scrollY="50000px",
lengthMenu = list(c(10, -1),
c('10','All'))
)
)
# convert numeric columns to scientific notation
dt <- dt |>
DT::formatSignif(sapply(dt$x$data, is.numeric),
digits=2
)We do get quite some more significant genes than with a genotype-only model. However the majority of them has adjusted p-values very close to the 0.05 threshold.
Exercise 5:
extract a list of significant genes from res_additive
feed the list to clusterProfiler::enrichGO()
One biological topic is highly significant, although only at relatively small absolute log2FoldChange values. What is it?
There is another way to include all variables when testing for a genotype effect:
compare two level of "group" at a time, differing only in genotypeall these contrasts are computed in a single model, The idea is embedded in a framework similar to Anova. Like Anova, it proceeds in two steps:
DESeq() with a 0 + group model. This model tests whether the mean read count at a given “group” level is significantly different from zero. It is only meaningful as raw material for the later tests.For the second step we will need a table of the contrasts we want to test:
contrasts <-
data.frame(group= "group",
A = colData |>
dplyr::filter(genotype == "nmrhas2") |>
dplyr::pull(group),
B= colData |>
dplyr::filter(genotype == "creer") |>
dplyr::pull(group)
) |> dplyr::distinct(.keep_all = TRUE) # there is initially one copy per replicate!
head(contrasts,n=6) group A B
1 group old.female.intestine.nmrhas2 old.female.intestine.creer
2 group old.female.kidney.nmrhas2 old.female.kidney.creer
3 group old.female.liver.nmrhas2 old.female.liver.creer
4 group old.female.muscle.nmrhas2 old.female.muscle.creer
5 group old.female.spleen.nmrhas2 old.female.spleen.creer
6 group old.female.wat.nmrhas2 old.female.wat.creer
[1] 24
second "Anova" step, we call results() on the group means model with a contrast argument. This evaluates for every pair of “group” levels in the contrast table, whether their mean counts are significantly different.# a matrix allows to directly extract rows as vectors
# (unlike a data.frame):
m <- as.matrix(contrasts)
res_contrasts <- list()
for(i in 1:nrow(m)) {
res_contrasts[[i]] <- results(dds_group,
contrast=m[i,]
)
}
# the second name in each contrast pair without the trailing ".creer"
# is the background -- use as name:
names(res_contrasts) <- sub("\\.creer$","",contrasts[,"B"])
Exercise 6: