escape 1.8.0
For the demonstration of escape, we will use the example “pbmc_small” data from Seurat and also generate a SingleCellExperiment
object from it.
suppressPackageStartupMessages(library(escape))
suppressPackageStartupMessages(library(dittoSeq))
suppressPackageStartupMessages(library(SingleCellExperiment))
suppressPackageStartupMessages(library(Seurat))
suppressPackageStartupMessages(library(SeuratObject))
pbmc_small <- get("pbmc_small")
pbmc_small <- DietSeurat(suppressMessages(UpdateSeuratObject(pbmc_small)))
sce <- as.SingleCellExperiment(pbmc_small, assay = "RNA")
The first step in the process of performing gene set enrichment analysis is identifying the gene sets we would like to use. The function getGeneSets()
allows users to isolate a whole or multiple libraries from a list of GSEABase GeneSetCollection objects. We can do this for gene set collections from the built-in Molecular Signature Database by setting the parameter library equal to library/libraries of interest. For multiple libraries, just set library = c(“Library1”, “Library2”, etc).
In Addition:
- Individual pathways/gene sets can be isolated from the libraries selected, by setting gene.sets = the name(s) of the gene sets of interest.
- Subcategories of the invidual libaries can be selected using the subcategory parameter.
If the sequencing of the single-cell data is performed on a species other than “Homo sapiens”, make sure to use the species parameter in getGeneSets()
in order to get the correct gene nomenclature.
GS.hallmark <- getGeneSets(library = "H")
data("escape.gene.sets", package="escape")
gene.sets <- escape.gene.sets
gene.sets <- list(Tcell_signature = c("CD2","CD3E","CD3D"),
Myeloid_signature = c("SPI1","FCER1G","CSF1R"))
The next step is performing the enrichment on the RNA count data. The function enrichIt()
can handle either a matrix of raw count data or will pull that data directly from a SingleCellExperiment or Seurat object. The gene.sets parameter in the function is the GeneSets, either generated from getGeneSets()
or from the user. The enrichment scores will be calculated across all individual cells and groups is the n size to break the enrichment by while the cores is the number of cores to perform in parallel during the enrichment calculation too.
enrichIt()
can utilize two distinct methods for quantification using the method parameter - either the “ssGSEA” method described by Barbie et al 2009 or “UCell” described by Andreatta and Carmona 2021.
To prevent issues with enrichment calculations for gene sets with a low number of genes represented in the count data of a single-cell object - min.size was instituted to remove gene sets with less than the indicated genes.
Important Note: This is computationally intensive and is highly dependent on the number of cells and the number of gene sets included.
ES.seurat <- enrichIt(obj = pbmc_small,
gene.sets = GS.hallmark,
groups = 1000, cores = 2,
min.size = 5)
## [1] "Using sets of 1000 cells. Running 1 times."
## Setting parallel calculations through a SnowParam back-end
## with workers=2 and tasks=100.
## Estimating ssGSEA scores for 17 gene sets.
## [1] "Calculating ranks..."
## [1] "Calculating absolute values from ranks..."
ES.sce <- enrichIt(obj = sce,
gene.sets = GS.hallmark,
method = "UCell",
groups = 1000, cores = 2,
min.size = 5)
## [1] "Using sets of 1000 cells. Running 1 times."
We can then easily add these results back to our Seurat or SCE object.
## if working with a Seurat object
pbmc_small <- Seurat::AddMetaData(pbmc_small, ES.seurat)
## if working with an SCE object
met.data <- merge(colData(sce), ES.sce, by = "row.names", all=TRUE)
row.names(met.data) <- met.data$Row.names
met.data$Row.names <- NULL
colData(sce) <- met.data
The easiest way to generate almost any visualization for single cell data is via dittoSeq, which is an extremely flexible visualization package for both bulk and single-cell RNA-seq data that works very well for both expression data and metadata. Better yet, it can handle both SingleCellExperiment and Seurat objects.
To keep things consistent, we’ll define a pleasing color scheme.
colors <- colorRampPalette(c("#0D0887FF","#7E03A8FF","#CC4678FF","#F89441FF","#F0F921FF"))
A simple way to approach visualizations for enrichment results is the heatmap, especially if you are using a number of gene sets or libraries.
dittoHeatmap(pbmc_small, genes = NULL, metas = names(ES.seurat),
annot.by = "groups",
fontsize = 7,
cluster_cols = TRUE,
heatmap.colors = colors(50))
A user can also produce a heatmap with select gene sets by providing specific names to the metas parameter. For example, we can isolated gene sets involved immune response.
dittoHeatmap(sce, genes = NULL,
metas = c("HALLMARK_IL2_STAT5_SIGNALING",
"HALLMARK_IL6_JAK_STAT3_SIGNALING",
"HALLMARK_INFLAMMATORY_RESPONSE"),
annot.by = "groups",
fontsize = 7,
heatmap.colors = colors(50))
Another way to visualize a subset of gene set enrichment would be to graph the distribution of enrichment using violin, jitter, boxplot, or ridgeplots. We can also compare between categorical variables using the group.by parameter.
multi_dittoPlot(sce, vars = c("HALLMARK_IL2_STAT5_SIGNALING",
"HALLMARK_IL6_JAK_STAT3_SIGNALING",
"HALLMARK_INFLAMMATORY_RESPONSE"),
group.by = "groups", plots = c("jitter", "vlnplot", "boxplot"),
ylab = "Enrichment Scores",
theme = theme_classic() + theme(plot.title = element_text(size = 10)))
We can also compare the distribution of enrichment scores of 2 distinct gene sets across all single cells using the dittoScatterHex()
function. Here, we use our SingleCellExperiment object with results of enrichIt()
and specify gene sets to the x.var and y.var parameters to produce a density plot. We can also add contours to the plot, by passing do.contour = TRUE.
dittoScatterHex(sce, x.var = "HALLMARK_IL2_STAT5_SIGNALING",
y.var = "HALLMARK_IL6_JAK_STAT3_SIGNALING",
do.contour = TRUE) +
scale_fill_gradientn(colors = colors(11))
We can also separate the graph using the split.by parameter, allowing for the direct comparison of categorical variables.
dittoScatterHex(sce, x.var = "HALLMARK_IL2_STAT5_SIGNALING",
y.var = "HALLMARK_IL6_JAK_STAT3_SIGNALING",
do.contour = TRUE,
split.by = "groups") +
scale_fill_gradientn(colors = colors(11))
Another distribution visualization is using a Ridge Plot from the ggridges R package. This allows the user to incorporate categorical variables to seperate the enrichment scores along the y-axis in addition to the faceting by categorical variables.
Like above, we can explore the distribution of the “HALLMARK_DNA_REPAIR” gene set between groups by calling ridgeEnrichment()
with the ES2 object. We specify group = letters.idents, which will separate groups on the y-axis. We can also add a rug plot (add.rug = TRUE) to look at the discrete sample placement along the enrichment ridge plot.
## Seurat object example
ES2 <- data.frame(pbmc_small[[]], Idents(pbmc_small))
colnames(ES2)[ncol(ES2)] <- "cluster"
## plot
ridgeEnrichment(ES2, gene.set = "HALLMARK_IL2_STAT5_SIGNALING", group = "cluster", add.rug = TRUE)
In addition to the separation of letter.idents, we can also use ridgeEnrichment()
for better granularity of multiple variables. For example, instead of looking at the difference just between “Type”, we can set group = “cluster” and then facet = “letter.idents”. This gives a visualization of the enrichment of DNA Repair by cluster and Type.
ridgeEnrichment(ES2, gene.set = "HALLMARK_IL2_STAT5_SIGNALING", group = "cluster",
facet = "letter.idents", add.rug = TRUE)
Another distribution visualization is a violin plot, which we seperate and directly compare using a binary classification. Like ridgeEnrichment()
, this allows for greater use of categorical variables. For splitEnrichment()
, the output will be two halves of a violin plot bassed on the split
parameter with a central boxplot with the relative distribution accross all samples.
Like above, we can explore the distribution of the “HALLMARK_DNA_REPAIR” gene set between groups by calling splitEnrichment()
with the ES2 object and split = “groups”. We can also explore the cluster distribution by assigning the x-axis = “cluster”.
splitEnrichment(ES2, split = "groups", gene.set = "HALLMARK_IL2_STAT5_SIGNALING")
splitEnrichment(ES2, x.axis = "cluster", split = "groups", gene.set = "HALLMARK_IL2_STAT5_SIGNALING")
New to the dev version of the escape is enrichmentPlot()
that takes the single-cell expression object (pbmc_small) and calculates mean rank order for a gene set across groups. The function requires the name of the specific gene set (gene.set) and the library of gene sets (gene.sets) to extract the rank positions from the count data stored in the Seurat or single-cell expression object.
enrichmentPlot(pbmc_small,
gene.set = "HALLMARK_IL2_STAT5_SIGNALING",
gene.sets = GS.hallmark,
group = "groups")
One frustration of Gene Set Enrichment is trying to make sense of the values. In order to move away from just selecting pathways that may be of interest, escape offers the ability to performPCA()
on the enrichment scores. Like the other functions, we will need to provide the output of enrichIt()
to the enriched parameter and the groups to include for later graphing.
PCA <- performPCA(enriched = ES2, gene.sets = names(GS.hallmark), groups = c("groups", "cluster"))
pcaEnrichment(PCA, PCx = "PC1", PCy = "PC2", contours = TRUE)
We can also look at the pcaEnrichment()
output separated by categorical factors using the facet parameter, for example using the cluster assignment.
pcaEnrichment(PCA, PCx = "PC1", PCy = "PC2", contours = FALSE, facet = "cluster")
We can more closely examine the construction of the PCA by looking at the contribution of each gene set to the respective principal component using masterPCAPlot()
with the same input as above with the pcaEnrichment()
. We can also control the number of gene sets plotted with top.contribution.
masterPCAPlot(ES2, gene.sets = names(GS.hallmark), PCx = "PC1", PCy = "PC2", top.contribution = 10)
We can also look for significant differences between groups of variables using getSignificance()
. For this, we need to assign a group parameter and the type of fit including: T.test, logistic regression (LR), Wilcoxon Rank Sum Test (Wilcoxon), ANOVA, and Kruskal-Wallis (KW) test. getSignificance()
will pull any numeric values, to ensure only gene sets, use the gene.sets parameter, similar to the PCAplot functions above.
Returned is a test statistic, raw p value, FDR value , and the median values for each group. In addition, ANOVA and the Kruskal-Wallis test will automatically return the corrected p-values for each comparison in the group.
output <- getSignificance(ES2,
group = "groups",
gene.sets = names(ES.seurat),
fit = "T.test")
If you have any questions, comments or suggestions, feel free to visit the github repository or email me.
sessionInfo()
## R version 4.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [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
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] sp_1.5-0 SeuratObject_4.1.2
## [3] Seurat_4.2.0 SingleCellExperiment_1.20.0
## [5] SummarizedExperiment_1.28.0 Biobase_2.58.0
## [7] GenomicRanges_1.50.0 GenomeInfoDb_1.34.0
## [9] IRanges_2.32.0 S4Vectors_0.36.0
## [11] BiocGenerics_0.44.0 MatrixGenerics_1.10.0
## [13] matrixStats_0.62.0 dittoSeq_1.10.0
## [15] ggplot2_3.3.6 escape_1.8.0
## [17] BiocStyle_2.26.0
##
## loaded via a namespace (and not attached):
## [1] utf8_1.2.2 reticulate_1.26
## [3] tidyselect_1.2.0 RSQLite_2.2.18
## [5] AnnotationDbi_1.60.0 htmlwidgets_1.5.4
## [7] grid_4.2.1 BiocParallel_1.32.0
## [9] Rtsne_0.16 munsell_0.5.0
## [11] ScaledMatrix_1.6.0 codetools_0.2-18
## [13] ica_1.0-3 future_1.28.0
## [15] miniUI_0.1.1.1 withr_2.5.0
## [17] spatstat.random_3.0-0 colorspace_2.0-3
## [19] progressr_0.11.0 highr_0.9
## [21] knitr_1.40 ROCR_1.0-11
## [23] tensor_1.5 listenv_0.8.0
## [25] labeling_0.4.2 GenomeInfoDbData_1.2.9
## [27] polyclip_1.10-4 farver_2.1.1
## [29] bit64_4.0.5 pheatmap_1.0.12
## [31] rhdf5_2.42.0 parallelly_1.32.1
## [33] vctrs_0.5.0 generics_0.1.3
## [35] xfun_0.34 R6_2.5.1
## [37] rsvd_1.0.5 isoband_0.2.6
## [39] msigdbr_7.5.1 bitops_1.0-7
## [41] rhdf5filters_1.10.0 spatstat.utils_3.0-1
## [43] cachem_1.0.6 DelayedArray_0.24.0
## [45] assertthat_0.2.1 promises_1.2.0.1
## [47] scales_1.2.1 rgeos_0.5-9
## [49] gtable_0.3.1 beachmat_2.14.0
## [51] globals_0.16.1 goftest_1.2-3
## [53] rlang_1.0.6 splines_4.2.1
## [55] lazyeval_0.2.2 hexbin_1.28.2
## [57] spatstat.geom_3.0-3 broom_1.0.1
## [59] BiocManager_1.30.19 yaml_2.3.6
## [61] reshape2_1.4.4 abind_1.4-5
## [63] backports_1.4.1 httpuv_1.6.6
## [65] tools_4.2.1 bookdown_0.29
## [67] ellipsis_0.3.2 spatstat.core_2.4-4
## [69] jquerylib_0.1.4 RColorBrewer_1.1-3
## [71] ggridges_0.5.4 Rcpp_1.0.9
## [73] plyr_1.8.7 sparseMatrixStats_1.10.0
## [75] zlibbioc_1.44.0 purrr_0.3.5
## [77] RCurl_1.98-1.9 rpart_4.1.19
## [79] deldir_1.0-6 pbapply_1.5-0
## [81] cowplot_1.1.1 zoo_1.8-11
## [83] ggrepel_0.9.1 cluster_2.1.4
## [85] magrittr_2.0.3 magick_2.7.3
## [87] data.table_1.14.4 scattermore_0.8
## [89] lmtest_0.9-40 RANN_2.6.1
## [91] fitdistrplus_1.1-8 patchwork_1.1.2
## [93] mime_0.12 evaluate_0.17
## [95] GSVA_1.46.0 xtable_1.8-4
## [97] XML_3.99-0.12 gridExtra_2.3
## [99] compiler_4.2.1 UCell_2.2.0
## [101] tibble_3.1.8 KernSmooth_2.23-20
## [103] crayon_1.5.2 htmltools_0.5.3
## [105] mgcv_1.8-41 later_1.3.0
## [107] snow_0.4-4 tidyr_1.2.1
## [109] DBI_1.1.3 MASS_7.3-58.1
## [111] babelgene_22.9 Matrix_1.5-1
## [113] cli_3.4.1 parallel_4.2.1
## [115] igraph_1.3.5 pkgconfig_2.0.3
## [117] plotly_4.10.0 spatstat.sparse_3.0-0
## [119] annotate_1.76.0 bslib_0.4.0
## [121] XVector_0.38.0 stringr_1.4.1
## [123] digest_0.6.30 sctransform_0.3.5
## [125] RcppAnnoy_0.0.20 graph_1.76.0
## [127] spatstat.data_3.0-0 Biostrings_2.66.0
## [129] rmarkdown_2.17 leiden_0.4.3
## [131] uwot_0.1.14 DelayedMatrixStats_1.20.0
## [133] GSEABase_1.60.0 shiny_1.7.3
## [135] lifecycle_1.0.3 nlme_3.1-160
## [137] jsonlite_1.8.3 Rhdf5lib_1.20.0
## [139] BiocNeighbors_1.16.0 viridisLite_0.4.1
## [141] fansi_1.0.3 pillar_1.8.1
## [143] lattice_0.20-45 KEGGREST_1.38.0
## [145] fastmap_1.1.0 httr_1.4.4
## [147] survival_3.4-0 glue_1.6.2
## [149] png_0.1-7 bit_4.0.4
## [151] stringi_1.7.8 sass_0.4.2
## [153] HDF5Array_1.26.0 blob_1.2.3
## [155] BiocSingular_1.14.0 memoise_2.0.1
## [157] dplyr_1.0.10 irlba_2.3.5.1
## [159] future.apply_1.9.1