Showcases the use of SEtools to merge objects of the SummarizedExperiment class.
SEtools 1.20.0
The SEtools package is a set of convenience functions for the Bioconductor class SummarizedExperiment. It facilitates merging, melting, and plotting SummarizedExperiment
objects.
NOTE that the heatmap-related and melting functions have been moved to a standalone package, sechm.
The old sehm
function of SEtools
should be considered deprecated, and most SEtools
functions are conserved for legacy/reproducibility reasons (or until they find a better home).
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("SEtools")
Or, to install the latest development version:
BiocManager::install("plger/SEtools")
To showcase the main functions, we will use an example object which contains (a subset of) whole-hippocampus RNAseq of mice after different stressors:
suppressPackageStartupMessages({
library(SummarizedExperiment)
library(SEtools)
})
data("SE", package="SEtools")
SE
## class: SummarizedExperiment
## dim: 100 20
## metadata(0):
## assays(2): counts logcpm
## rownames(100): Egr1 Nr4a1 ... CH36-200G6.4 Bhlhe22
## rowData names(2): meanCPM meanTPM
## colnames(20): HC.Homecage.1 HC.Homecage.2 ... HC.Swim.4 HC.Swim.5
## colData names(2): Region Condition
This is taken from Floriou-Servou et al., Biol Psychiatry 2018.
se1 <- SE[,1:10]
se2 <- SE[,11:20]
se3 <- mergeSEs( list(se1=se1, se2=se2) )
se3
## class: SummarizedExperiment
## dim: 100 20
## metadata(3): se1 se2 anno_colors
## assays(2): counts logcpm
## rownames(100): AC139063.2 Actr6 ... Zfp667 Zfp930
## rowData names(2): meanCPM meanTPM
## colnames(20): se1.HC.Homecage.1 se1.HC.Homecage.2 ... se2.HC.Swim.4
## se2.HC.Swim.5
## colData names(3): Dataset Region Condition
All assays were merged, along with rowData and colData slots.
By default, row z-scores are calculated for each object when merging. This can be prevented with:
se3 <- mergeSEs( list(se1=se1, se2=se2), do.scale=FALSE)
If more than one assay is present, one can specify a different scaling behavior for each assay:
se3 <- mergeSEs( list(se1=se1, se2=se2), use.assays=c("counts", "logcpm"), do.scale=c(FALSE, TRUE))
Differences to the cbind
method include prefixes added to column names, optional scaling, handling of metadata (e.g. for sechm
)
It is also possible to merge by rowData columns, which are specified through the mergeBy
argument.
In this case, one can have one-to-many and many-to-many mappings, in which case two behaviors are possible:
aggFun
, the features of each object will by aggregated by mergeBy
using this function before merging.rowData(se1)$metafeature <- sample(LETTERS,nrow(se1),replace = TRUE)
rowData(se2)$metafeature <- sample(LETTERS,nrow(se2),replace = TRUE)
se3 <- mergeSEs( list(se1=se1, se2=se2), do.scale=FALSE, mergeBy="metafeature", aggFun=median)
## Aggregating the objects by metafeature
## Merging...
sechm::sechm(se3, features=row.names(se3))
A single SE can also be aggregated by using the aggSE
function:
se1b <- aggSE(se1, by = "metafeature")
## Aggregation methods for each assay:
## counts: sum; logcpm: expsum
se1b
## class: SummarizedExperiment
## dim: 26 10
## metadata(0):
## assays(2): counts logcpm
## rownames(26): A B ... Y Z
## rowData names(0):
## colnames(10): HC.Homecage.1 HC.Homecage.2 ... HC.Handling.4
## HC.Handling.5
## colData names(2): Region Condition
If the aggregation function(s) are not specified, aggSE
will try to guess decent aggregation functions from the assay names.
This is similar to scuttle::sumCountsAcrossFeatures
, but preserves other SE slots.
Calculate an assay of log-foldchanges to the controls:
SE <- log2FC(SE, fromAssay="logcpm", controls=SE$Condition=="Homecage")
## R version 4.4.1 (2024-06-14)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.6.7
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## locale:
## [1] C/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] grid stats4 stats graphics grDevices utils datasets
## [8] methods base
##
## other attached packages:
## [1] SEtools_1.20.0 sechm_1.14.0
## [3] ComplexHeatmap_2.22.0 SummarizedExperiment_1.36.0
## [5] Biobase_2.66.0 GenomicRanges_1.58.0
## [7] GenomeInfoDb_1.42.0 IRanges_2.40.0
## [9] S4Vectors_0.44.0 BiocGenerics_0.52.0
## [11] MatrixGenerics_1.18.0 matrixStats_1.4.1
## [13] BiocStyle_2.34.0
##
## loaded via a namespace (and not attached):
## [1] DBI_1.2.3 rlang_1.1.4 magrittr_2.0.3
## [4] clue_0.3-65 GetoptLong_1.0.5 RSQLite_2.3.7
## [7] compiler_4.4.1 mgcv_1.9-1 png_0.1-8
## [10] vctrs_0.6.5 sva_3.54.0 stringr_1.5.1
## [13] pkgconfig_2.0.3 shape_1.4.6.1 crayon_1.5.3
## [16] fastmap_1.2.0 magick_2.8.5 XVector_0.46.0
## [19] ca_0.71.1 utf8_1.2.4 rmarkdown_2.29
## [22] UCSC.utils_1.2.0 tinytex_0.54 bit_4.5.0
## [25] xfun_0.49 zlibbioc_1.52.0 cachem_1.1.0
## [28] jsonlite_1.8.9 blob_1.2.4 DelayedArray_0.32.0
## [31] BiocParallel_1.40.0 parallel_4.4.1 cluster_2.1.6
## [34] R6_2.5.1 bslib_0.8.0 stringi_1.8.4
## [37] RColorBrewer_1.1-3 limma_3.62.1 genefilter_1.88.0
## [40] jquerylib_0.1.4 Rcpp_1.0.13-1 bookdown_0.41
## [43] iterators_1.0.14 knitr_1.49 splines_4.4.1
## [46] Matrix_1.7-1 tidyselect_1.2.1 abind_1.4-8
## [49] yaml_2.3.10 TSP_1.2-4 doParallel_1.0.17
## [52] codetools_0.2-20 curl_6.0.0 lattice_0.22-6
## [55] tibble_3.2.1 KEGGREST_1.46.0 evaluate_1.0.1
## [58] Rtsne_0.17 survival_3.7-0 zip_2.3.1
## [61] Biostrings_2.74.0 circlize_0.4.16 pillar_1.9.0
## [64] BiocManager_1.30.25 foreach_1.5.2 generics_0.1.3
## [67] ggplot2_3.5.1 munsell_0.5.1 scales_1.3.0
## [70] xtable_1.8-4 glue_1.8.0 pheatmap_1.0.12
## [73] tools_4.4.1 data.table_1.16.2 annotate_1.84.0
## [76] openxlsx_4.2.7.1 locfit_1.5-9.10 registry_0.5-1
## [79] XML_3.99-0.17 Cairo_1.6-2 seriation_1.5.6
## [82] AnnotationDbi_1.68.0 edgeR_4.4.0 colorspace_2.1-1
## [85] nlme_3.1-166 GenomeInfoDbData_1.2.13 randomcoloR_1.1.0.1
## [88] cli_3.6.3 fansi_1.0.6 S4Arrays_1.6.0
## [91] dplyr_1.1.4 V8_6.0.0 gtable_0.3.6
## [94] DESeq2_1.46.0 sass_0.4.9 digest_0.6.37
## [97] SparseArray_1.6.0 rjson_0.2.23 memoise_2.0.1
## [100] htmltools_0.5.8.1 lifecycle_1.0.4 httr_1.4.7
## [103] GlobalOptions_0.1.2 statmod_1.5.0 bit64_4.5.2