Multivariate tests
Combining results of univariate tests
Developed by Gabriel Hoffman
Run on 2024-11-05 15:30:50
Source:vignettes/mvtests.Rmd
mvtests.Rmd
Results from the univariate regressions performed using can be combined in a post-processing step to perform multivariate hypothesis testing. In this example, we fit on transcript-level counts and then perform multivariate hypothesis testing by combining transcripts at the gene-level. This is done with the function.
Import transcript-level counts
Read in transcript counts from the package.
library(readr)
library(tximport)
library(tximportData)
# specify directory
path <- system.file("extdata", package = "tximportData")
# read sample meta-data
samples <- read.table(file.path(path, "samples.txt"), header = TRUE)
samples.ext <- read.table(file.path(path, "samples_extended.txt"), header = TRUE, sep = "\t")
# read assignment of transcripts to genes
# remove genes on the PAR, since these are present twice
tx2gene <- read_csv(file.path(path, "tx2gene.gencode.v27.csv"))
tx2gene <- tx2gene[grep("PAR_Y", tx2gene$GENEID, invert = TRUE), ]
# read transcript-level quatifictions
files <- file.path(path, "salmon", samples$run, "quant.sf.gz")
txi <- tximport(files, type = "salmon", txOut = TRUE)
# Create metadata simulating two conditions
sampleTable <- data.frame(condition = factor(rep(c("A", "B"), each = 3)))
rownames(sampleTable) <- paste0("Sample", 1:6)
Standard dream analysis
Perform standard analysis at the transcript-level
library(variancePartition)
library(edgeR)
# Prepare transcript-level reads
dge <- DGEList(txi$counts)
design <- model.matrix(~condition, data = sampleTable)
isexpr <- filterByExpr(dge, design)
dge <- dge[isexpr, ]
dge <- calcNormFactors(dge)
# Estimate precision weights
vobj <- voomWithDreamWeights(dge, ~condition, sampleTable)
# Fit regression model one transcript at a time
fit <- dream(vobj, ~condition, sampleTable)
fit <- eBayes(fit)
Multivariate analysis
Combine the transcript-level results at the gene-level. The mapping between transcript and gene is stored in as a list.
# Prepare transcript to gene mapping
# keep only transcripts present in vobj
# then convert to list with key GENEID and values TXNAMEs
keep <- tx2gene$TXNAME %in% rownames(vobj)
tx2gene.lst <- unstack(tx2gene[keep, ])
# Run multivariate test on entries in each feature set
# Default method is "FE.empirical", but use "FE" here to reduce runtime
res <- mvTest(fit, vobj, tx2gene.lst, coef = "conditionB", method = "FE")
# truncate gene names since they have version numbers
# ENST00000498289.5 -> ENST00000498289
res$ID.short <- gsub("\\..+", "", res$ID)
Gene set analysis
Perform gene set analysis using on the gene-level test statistics.
# must have zenith > v1.0.2
library(zenith)
library(GSEABase)
gs <- get_MSigDB("C1", to = "ENSEMBL")
df_gsa <- zenithPR_gsa(res$stat, res$ID.short, gs, inter.gene.cor = .05)
head(df_gsa)
## NGenes Correlation delta se p.less p.greater PValue Direction
## M7078_chr2p16 30 0.05 1.4208384 0.5610910 0.99432899 0.005671015 0.01134203 Up
## M14982_chr7p13 26 0.05 1.1335492 0.5777005 0.97512013 0.024879873 0.04975975 Up
## M7314_chr4p14 25 0.05 -1.1344103 0.5825608 0.02575932 0.974240679 0.05151864 Down
## M5824_chr11p13 30 0.05 -1.0120371 0.5612285 0.03568377 0.964316230 0.07136754 Down
## M3783_chr2q37 73 0.05 0.8367603 0.4929617 0.95518099 0.044819012 0.08963802 Up
## M10517_chr4q24 21 0.05 -1.0062435 0.6060832 0.04844305 0.951556955 0.09688609 Down
## FDR
## M7078_chr2p16 0.9992274
## M14982_chr7p13 0.9992274
## M7314_chr4p14 0.9992274
## M5824_chr11p13 0.9992274
## M3783_chr2q37 0.9992274
## M10517_chr4q24 0.9992274
Session info
## R version 4.3.0 (2023-04-21)
## Platform: x86_64-apple-darwin22.4.0 (64-bit)
## Running under: macOS 14.2.1
##
## Matrix products: default
## BLAS: /Users/gabrielhoffman/prog/R-4.3.0/lib/libRblas.dylib
## LAPACK: /usr/local/Cellar/r/4.3.0_1/lib/R/lib/libRlapack.dylib; LAPACK version 3.11.0
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] org.Hs.eg.db_3.17.0 msigdbr_7.5.1 GSEABase_1.62.0
## [4] graph_1.78.0 annotate_1.78.0 XML_3.99-0.14
## [7] AnnotationDbi_1.62.1 IRanges_2.34.1 S4Vectors_0.38.1
## [10] Biobase_2.60.0 BiocGenerics_0.46.0 zenith_1.4.1
## [13] edgeR_3.42.4 variancePartition_1.33.11 BiocParallel_1.34.2
## [16] limma_3.56.2 ggplot2_3.4.4 tximportData_1.28.0
## [19] tximport_1.28.0 readr_2.1.4
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.5 magrittr_2.0.3 nloptr_2.0.3
## [4] rmarkdown_2.22 fs_1.6.2 zlibbioc_1.46.0
## [7] ragg_1.2.5 vctrs_0.6.3 memoise_2.0.1
## [10] minqa_1.2.5 RCurl_1.98-1.12 progress_1.2.2
## [13] htmltools_0.5.5 S4Arrays_1.2.0 curl_5.0.0
## [16] broom_1.0.5 sass_0.4.6 KernSmooth_2.23-21
## [19] bslib_0.4.2 desc_1.4.2 pbkrtest_0.5.2
## [22] plyr_1.8.8 cachem_1.0.8 lifecycle_1.0.3
## [25] iterators_1.0.14 pkgconfig_2.0.3 Matrix_1.5-4.1
## [28] R6_2.5.1 fastmap_1.1.1 GenomeInfoDbData_1.2.10
## [31] rbibutils_2.2.13 MatrixGenerics_1.12.0 digest_0.6.33
## [34] numDeriv_2016.8-1.1 colorspace_2.1-0 rprojroot_2.0.3
## [37] textshaping_0.3.6 GenomicRanges_1.52.0 RSQLite_2.3.1
## [40] filelock_1.0.2 RcppZiggurat_0.1.6 fansi_1.0.4
## [43] httr_1.4.6 abind_1.4-5 compiler_4.3.0
## [46] bit64_4.0.5 aod_1.3.2 withr_2.5.0
## [49] backports_1.4.1 DBI_1.1.3 gplots_3.1.3
## [52] MASS_7.3-60 DelayedArray_0.26.3 corpcor_1.6.10
## [55] gtools_3.9.4 caTools_1.18.2 tools_4.3.0
## [58] remaCor_0.0.17 glue_1.6.2 nlme_3.1-162
## [61] grid_4.3.0 reshape2_1.4.4 generics_0.1.3
## [64] gtable_0.3.3 tzdb_0.4.0 tidyr_1.3.0
## [67] hms_1.1.3 utf8_1.2.3 XVector_0.40.0
## [70] pillar_1.9.0 stringr_1.5.0 babelgene_22.9
## [73] vroom_1.6.3 splines_4.3.0 dplyr_1.1.2
## [76] BiocFileCache_2.8.0 lattice_0.21-8 bit_4.0.5
## [79] tidyselect_1.2.0 locfit_1.5-9.7 Biostrings_2.68.1
## [82] knitr_1.43 SummarizedExperiment_1.30.1 RhpcBLASctl_0.23-42
## [85] xfun_0.39 matrixStats_1.0.0 KEGGgraph_1.60.0
## [88] stringi_1.7.12 yaml_2.3.7 boot_1.3-28.1
## [91] evaluate_0.21 codetools_0.2-19 tibble_3.2.1
## [94] Rgraphviz_2.44.0 cli_3.6.1 xtable_1.8-4
## [97] systemfonts_1.0.4 Rdpack_2.4 munsell_0.5.0
## [100] jquerylib_0.1.4 Rcpp_1.0.11 GenomeInfoDb_1.36.1
## [103] EnvStats_2.7.0 dbplyr_2.3.2 png_0.1-8
## [106] Rfast_2.0.7 parallel_4.3.0 pkgdown_2.0.7
## [109] blob_1.2.4 prettyunits_1.1.1 bitops_1.0-7
## [112] lme4_1.1-34 mvtnorm_1.2-2 lmerTest_3.1-3
## [115] scales_1.2.1 purrr_1.0.2 crayon_1.5.2
## [118] fANCOVA_0.6-1 rlang_1.1.1 EnrichmentBrowser_2.30.1
## [121] KEGGREST_1.40.0
<>