Main effect contrasts
Main effects average across all levels of the secondary factor. For example, the main effect of KO vs WT is the average difference across all time points:
| ContrastName | Contrast |
|---|
When working with factorial experimental designs (e.g. two conditions
crossed with three time points), specifying all the relevant contrasts
by hand is tedious and error-prone.
annotation_add_contrasts() generates them from the two
factor columns of a sample annotation. The contrast strings can then be
passed directly to build_contrast_analysis().
This vignette demonstrates how to:
build_contrast_analysis()In a typical prolfquapp workflow, you start with a sample annotation table that has columns for the two factors. Consider a design with genotype (WT, KO) and time point (T0, T150, T300):
library(prolfqua)
# an annotation table of 36 samples: factor_A (genotype WT / KO),
# factor_B (time T0 / T150 / T300), 6 replicates each
design <- expand.grid(
rep = 1:6, factor_B = c("T0", "T150", "T300"), factor_A = c("WT", "KO"),
stringsAsFactors = FALSE
)
annotation <- data.frame(
Name = paste(design$factor_A, design$factor_B, design$rep, sep = "_"),
factor_A = design$factor_A,
factor_B = design$factor_B
)
head(annotation)## Name factor_A factor_B
## 1 WT_T0_1 WT T0
## 2 WT_T0_2 WT T0
## 3 WT_T0_3 WT T0
## 4 WT_T0_4 WT T0
## 5 WT_T0_5 WT T0
## 6 WT_T0_6 WT T0
annotation_add_contrasts() unites the two factors into a
Group column, generates all contrasts, and binds them
alongside the annotation:
result <- annotation_add_contrasts(
annotation,
primary_col = "factor_A",
secondary_col = "factor_B",
prefix = "primary"
)
# The annotation with united Group and contrast columns
knitr::kable(head(result$annot[, c("Name", "Group", "ContrastName", "Contrast")], 10),
row.names = FALSE)| Name | Group | ContrastName | Contrast |
|---|---|---|---|
| WT_T0_1 | WT_T0 | WT_vs_KO | (G_WT_T0 + G_WT_T150 + G_WT_T300)/3 - (G_KO_T0 + G_KO_T150 + G_KO_T300)/3 |
| WT_T0_2 | WT_T0 | WT_vs_KO_at_T0 | G_WT_T0 - G_KO_T0 |
| WT_T0_3 | WT_T0 | WT_vs_KO_at_T150 | G_WT_T150 - G_KO_T150 |
| WT_T0_4 | WT_T0 | WT_vs_KO_at_T300 | G_WT_T300 - G_KO_T300 |
| WT_T0_5 | WT_T0 | interaction_WT_vs_KO_at_T150_vs_T0 | (G_WT_T150 - G_KO_T150) - (G_WT_T0 - G_KO_T0) |
| WT_T0_6 | WT_T0 | interaction_WT_vs_KO_at_T300_vs_T0 | (G_WT_T300 - G_KO_T300) - (G_WT_T0 - G_KO_T0) |
| WT_T150_1 | WT_T150 | interaction_WT_vs_KO_at_T300_vs_T150 | (G_WT_T300 - G_KO_T300) - (G_WT_T150 - G_KO_T150) |
| WT_T150_2 | WT_T150 | NA | NA |
| WT_T150_3 | WT_T150 | NA | NA |
| WT_T150_4 | WT_T150 | NA | NA |
## [1] "DEA_primary_dataset.csv"
The contrast strings name each cell of the design
G_<primary>_<secondary>, for example
G_KO_T300. These are the coefficients of a model
~ G_ fitted on a factor G_ that holds the
Group column; prolfquapp builds this factor from the
Group column of the annotation.
Main effects average across all levels of the secondary factor. For example, the main effect of KO vs WT is the average difference across all time points:
| ContrastName | Contrast |
|---|
These compare primary factor levels at each individual level of the secondary factor:
| ContrastName | Contrast |
|---|
Interaction contrasts test whether the difference between primary levels changes across secondary levels (difference of differences):
| ContrastName | Contrast |
|---|---|
| interaction_WT_vs_KO_at_T150_vs_T0 | (G_WT_T150 - G_KO_T150) - (G_WT_T0 - G_KO_T0) |
| interaction_WT_vs_KO_at_T300_vs_T0 | (G_WT_T300 - G_KO_T300) - (G_WT_T0 - G_KO_T0) |
| interaction_WT_vs_KO_at_T300_vs_T150 | (G_WT_T300 - G_KO_T300) - (G_WT_T150 - G_KO_T150) |
interactions = FALSE leaves them out.
Swapping primary and secondary factors gives main effects for time points averaged across genotypes, and time differences within each genotype:
result2 <- annotation_add_contrasts(
annotation,
primary_col = "factor_B",
secondary_col = "factor_A",
prefix = "secondary"
)
contrasts2 <- result2$annot[!is.na(result2$annot$ContrastName), c("ContrastName", "Contrast")]
knitr::kable(head(contrasts2, 9), row.names = FALSE)| ContrastName | Contrast |
|---|---|
| T150_vs_T0 | (G_T150_KO + G_T150_WT)/2 - (G_T0_KO + G_T0_WT)/2 |
| T300_vs_T0 | (G_T300_KO + G_T300_WT)/2 - (G_T0_KO + G_T0_WT)/2 |
| T300_vs_T150 | (G_T300_KO + G_T300_WT)/2 - (G_T150_KO + G_T150_WT)/2 |
| T150_vs_T0_at_KO | G_T150_KO - G_T0_KO |
| T150_vs_T0_at_WT | G_T150_WT - G_T0_WT |
| T300_vs_T0_at_KO | G_T300_KO - G_T0_KO |
| T300_vs_T0_at_WT | G_T300_WT - G_T0_WT |
| T300_vs_T150_at_KO | G_T300_KO - G_T150_KO |
| T300_vs_T150_at_WT | G_T300_WT - G_T150_WT |
On simulated data with the two factors Treatment (A, B)
and Background (X, Z), the contrasts of
annotation_add_contrasts() go to
build_contrast_analysis() as a named vector. The data get a
factor G_ holding the Group column:
sim <- sim_lfq_data(design = "two_factor", n_proteins = 30, with_missing = FALSE)
samples <- unique(as.data.frame(sim$data)[c("sampleName", "Treatment", "Background")])
annot <- annotation_add_contrasts(samples, "Treatment", "Background")$annot
contr <- annot[!is.na(annot$ContrastName), ]
contrasts_ab <- setNames(contr$Contrast, contr$ContrastName)
contrasts_ab## B_vs_A B_vs_A_at_X
## "(G_B_X + G_B_Z)/2 - (G_A_X + G_A_Z)/2" "G_B_X - G_A_X"
## B_vs_A_at_Z interaction_B_vs_A_at_Z_vs_X
## "G_B_Z - G_A_Z" "(G_B_Z - G_A_Z) - (G_B_X - G_A_X)"
groups <- annot[!is.na(annot$sampleName), c("sampleName", "Group")]
data <- merge(as.data.frame(sim$data), groups, by = "sampleName")
data$G_ <- data$Group
config <- sim$config$clone(deep = TRUE)
config$factors <- list(G_ = "Group")
config$factor_depth <- 1
lfq <- LFQData$new(data, config)
fa <- build_contrast_analysis(lfq, "~ G_", contrasts_ab, method = "lm")
head(fa$get_contrasts()[, c("protein_Id", "contrast", "diff", "p.value", "FDR")])## # A tibble: 6 × 5
## protein_Id contrast diff p.value FDR
## <chr> <chr> <dbl> <dbl> <dbl>
## 1 0EfVhX~2956 B_vs_A -0.576 0.267 0.364
## 2 0m5WN4~6730 B_vs_A 1.84 0.0106 0.0309
## 3 7cbcrd~2687 B_vs_A -1.10 0.0656 0.151
## 4 7QuTub~6175 B_vs_A -0.0342 0.946 0.946
## 5 9VUkAq~3402 B_vs_A 1.64 0.0000481 0.000288
## 6 At886V~7759 B_vs_A 0.765 0.187 0.299
## R version 4.6.2 (2026-10-29)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 26.04.1 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] prolfqua_1.10.0 dplyr_1.2.1 BiocStyle_2.43.0
##
## loaded via a namespace (and not attached):
## [1] tidyselect_1.2.1 viridisLite_0.4.3 farver_2.1.2
## [4] S7_1.0.0 fastmap_1.2.0 promises_1.5.0
## [7] digest_0.6.39 mime_0.13 lifecycle_1.0.5
## [10] cluster_2.1.8.3 statmod_1.5.2 magrittr_2.0.5
## [13] compiler_4.6.2 progress_1.2.3 rlang_1.3.0
## [16] sass_0.5.0 tools_4.6.2 utf8_1.2.6
## [19] yaml_2.3.12 data.table_1.18.6.1 knitr_1.52
## [22] prettyunits_1.2.0 labeling_0.4.3 htmlwidgets_1.6.4
## [25] RColorBrewer_1.1-3 KernSmooth_2.23-27 withr_3.0.3
## [28] purrr_1.2.2 BiocGenerics_0.61.1 sys_3.4.3
## [31] grid_4.6.2 stats4_4.6.2 xtable_1.8-8
## [34] colorspace_2.1-4 ggplot2_4.0.5 scales_1.4.0
## [37] iterators_1.0.14 MASS_7.3-66 cli_3.6.6
## [40] rmarkdown_2.32 crayon_1.5.3 generics_0.1.4
## [43] otel_0.3.0 httr_1.4.9 rjson_0.2.23
## [46] cachem_1.1.0 stringr_1.6.0 parallel_4.6.2
## [49] BiocManager_1.30.27 matrixStats_1.5.0 vctrs_0.7.3
## [52] jsonlite_2.0.0 hms_1.1.4 IRanges_2.49.0
## [55] GetoptLong_1.1.1 S4Vectors_0.53.1 ggrepel_0.9.8
## [58] clue_0.3-68 maketools_1.3.2 crosstalk_1.2.2
## [61] foreach_1.5.2 limma_4.1.3 plotly_4.12.1
## [64] tidyr_1.3.2 jquerylib_0.1.4 glue_1.8.1
## [67] codetools_0.2-20 stringi_1.8.9 gtable_0.3.6
## [70] shape_1.4.6.1 later_1.4.8 ComplexHeatmap_2.31.0
## [73] tibble_3.3.1 pillar_1.11.1 htmltools_0.5.9
## [76] circlize_0.4.18 R6_2.6.1 doParallel_1.0.17
## [79] evaluate_1.0.5 shiny_1.15.0 png_0.1-9
## [82] httpuv_1.6.17 bslib_0.12.0 Rcpp_1.1.2
## [85] gridExtra_2.3.1 xfun_0.63 buildtools_1.0.0
## [88] pkgconfig_2.0.3 GlobalOptions_0.1.4