R Advanced Course 2026

Differential Gene Expression

Ulrike Göbel

Bioinformatics Core Facility CECAD

2026-11-24

Slides & Code

  • [f] Full screen
  • [o] Slide Overview
  • [c] Notes
  • [h] help

git repo

R-Basic


Clone repo

git clone https://github.com/CECADBioinformaticsCoreFacility/Intermediate_R_Course_2026.git


Slides Directly

https://cecadbioinformaticscorefacility.github.io/Advanced_R_Course_2026/

Session 4:: Differential Gene Expression – Models

Understanding Model Formulae

 

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 !

Understanding Model Formulae

Understanding Model Formulae

Understanding Model Formulae

Understanding Model Formulae

Understanding Model Formulae

Understanding Model Formulae

Understanding Model Formulae

Understanding Model Formulae

Understanding Model Formulae

Understanding Model Formulae – Multiple Factors

Understanding Model Formulae – Multiple Factors

Understanding Model Formulae – Multiple Factors

Understanding Model Formulae – Multiple Factors

Understanding Model Formulae – Multiple Factors

An Additive Model of Gene Expression Including All Factors

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.

library(DESeq2)

dds_additive <-
  # High level step 1: set up the object structure
    DESeqDataSetFromMatrix(countData = M,
                           colData = colData,
                           design= ~ age + sex + tissue + genotype 
                           ) |>
    DESeq()

These are the available test results:

resultsNames(dds_additive)
[1] "Intercept"                  "age_young_vs_old"          
[3] "sex_male_vs_female"         "tissue_kidney_vs_intestine"
[5] "tissue_liver_vs_intestine"  "tissue_muscle_vs_intestine"
[7] "tissue_spleen_vs_intestine" "tissue_wat_vs_intestine"   
[9] "genotype_nmrhas2_vs_creer" 

Let’s Have a Look at the Genotype Effect in this Model

res_additive <- 
    results(dds_additive, name="genotype_nmrhas2_vs_creer")
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
      )

Let’s Have a Look at the Genotype Effect in this Model

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.

Let’s Have a Look at the Genotype Effect in this Model – Your Turn!

 

Exercise 5:

  • Use function quick_GOcheck() [see Exercises] to
    • 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?

Yet Another Alternative: Group Contrasts

There is another way to include all variables when testing for a genotype effect:

  • use the “group” factor (= the levels of all variables concatenated)
  • compare two level of "group" at a time,  
    differing only in genotype
  • this captures the effect of genotype in the context of the shared levels
  • notably all these contrasts are computed in a single model,  
    using information from the full dataset

Yet Another Alternative: Group Contrasts

The idea is embedded in a framework similar to Anova. Like Anova, it proceeds in two steps:

  • First we run 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.
## See https://notchbio.app/blog/deseq2-multiple-conditions-contrasts-tutorial/

dds_group <-
  DESeqDataSetFromMatrix(countData = M,
                         colData = colData,
                         design= ~ 0 + group) |>
  DESeq()

Yet Another Alternative: Group Contrasts

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
nrow(contrasts)
[1] 24

Yet Another Alternative: Group Contrasts

  • Then in the 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"])
saveRDS(res_contrasts, file = "res_contrasts.rds") 

Yet Another Alternative: Group Contrasts – Your Turn!

 

Exercise 6:

  • Use function quick_GOcheck() [see Exercises], which
    • extracts a list of significant genes from a DESeq2 result
    • feeds the list to clusterProfiler::enrichGO()
    • returns a dotplot of enriched GO terms
  • Do this for each of the liver-specific res_contrast entries
  • Describe your observations