---
title: Using escape to perform ssGSEA analyses on single-cell RNA-seq data
author: 
- name: Nick Borcherding
  email: ncborch@gmail.com
  affiliation: Washington University in St. Louis, School of Medicine, St. Louis, MO, USA
- name: Jared Andrews
  email: jared.andrews07@gmail.com
  affiliation: Washington University in St. Louis, School of Medicine, St. Louis, MO, USA

date: "February 9, 2021"

output:
  BiocStyle::html_document:
    toc_float: true
package: escape
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Escape-ingToWork}
  %\VignetteEncoding{UTF-8} 
---

```{r, echo=FALSE, results="hide", message=FALSE}
knitr::opts_chunk$set(error=FALSE, message=FALSE, warning=FALSE)
library(BiocStyle)
```

# Loading Processed Single-Cell Data


For the demonstration of *escape*, we will use the example "pbmc_small" data from *Seurat* and also generate a `SingleCellExperiment` object from it.


```{r}
suppressPackageStartupMessages(library(escape))
suppressPackageStartupMessages(library(dittoSeq))
suppressPackageStartupMessages(library(SingleCellExperiment))
suppressPackageStartupMessages(library(Seurat))
suppressPackageStartupMessages(library(SeuratObject))
pbmc_small <- get("pbmc_small")
data("pbmc_small")
pbmc_small <- DietSeurat(suppressMessages(UpdateSeuratObject(pbmc_small)))
sce <- as.SingleCellExperiment(pbmc_small, assay = "RNA")
```

# Getting Gene Sets


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` GeneSet objects. We can do this for gene set collections from the built-in [Molecular Signature Database](https://www.gsea-msigdb.org/gsea/msigdb/search.jsp) by setting the parameter `library` equal to the collection(s) of interest.
For multiple libraries, just set `library = c("C2", "C5")`. 
To check which collections are available, use `msigdbr::msigdbr_collections()`. 
The `gs_cat` entry indicates the individual gene set collection names (e.g. "C2"). 
Individual pathways/gene sets can be isolated from the selected libraries selected, by setting `gene.sets` = the name(s) of the gene sets of interest. For an overview of the gene sets and collections, see <https://www.gsea-msigdb.org/gsea/msigdb/genesets.jsp>.
If you are using a species other than Homo sapiens, set the `species` parameter to the appropriate scientific name to ensure the gene nomenclature matches your expression data. 
To check which species are available, use `msigdbr::msigdbr_species()`. 


```{r}
## We'll use the HALLMARK gene set collection ("H")
GS.hallmark <- getGeneSets(library = "H")
```


If a user would like to expand beyond the GSEA Molecular Signature Database for pathway analysis, the enrichment function `enrichIt()` can use any list of `GeneSet` objects from the `GSEABase` R package. An [excellent vignette](https://www.bioconductor.org/packages/devel/bioc/vignettes/GSEABase/inst/doc/GSEABase.pdf) exists on how to create GeneSet objects to be used in the enrichment analysis.
Otherwise, you may also provide a simple named list of gene sets of interest. See `?enrichIt()` for details.


# Enrichment

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. 

*Important Note:* This is computationally intensive and is highly dependent on the number of cells and the number of gene sets included.

```{r}
ES.seurat <- enrichIt(obj = pbmc_small, gene.sets = GS.hallmark, groups = 1000, cores = 2)
ES.sce <- enrichIt(obj = sce, gene.sets = GS.hallmark, groups = 1000, cores = 2)
```

We can then easily add these results back to our Seurat or SCE object.

```{r}
## 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
```

****

# Visualizations

The easiest way to generate almost any visualization for single cell data is via `r Biocpkg("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.
```{r}
colors <- colorRampPalette(c("#0348A6", "#7AC5FF", "#C6FDEC", "#FFB433", "#FF4B20"))
```


## Heatmaps

A simple way to approach visualizations for enrichment results is the heatmap, especially if you are using a number of gene sets or libraries. 

```{r}
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 in apoptosis and DNA damage.

```{r}
dittoHeatmap(sce, genes = NULL, 
             metas = c("HALLMARK_APOPTOSIS", "HALLMARK_DNA_REPAIR", "HALLMARK_P53_PATHWAY"), 
             annot.by = "groups", 
             fontsize = 7,
             heatmap.colors = colors(50))
```

## Violin Plots

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.

```{r}
multi_dittoPlot(sce, vars = c("HALLMARK_APOPTOSIS", "HALLMARK_DNA_REPAIR", "HALLMARK_P53_PATHWAY"), 
                group.by = "groups", plots = c("jitter", "vlnplot", "boxplot"), 
                ylab = "Enrichment Scores", 
                theme = theme_classic() + theme(plot.title = element_text(size = 10)))
```


## Hex Density Enrichment Plots

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`. 

```{r}
dittoScatterHex(sce, x.var = "HALLMARK_DNA_REPAIR", 
                    y.var = "HALLMARK_MTORC1_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.

```{r}
dittoScatterHex(sce, x.var = "HALLMARK_DNA_REPAIR", 
                y.var = "HALLMARK_MTORC1_SIGNALING", 
                do.contour = TRUE,
                split.by = "groups")  + 
        scale_fill_gradientn(colors = colors(11)) 
```


## Ridge Plots

Another distribution visualization is using a [Ridge Plot](https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html) 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 seperate 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. 

```{r}
## Seurat object example
ES2 <- data.frame(pbmc_small[[]], Idents(pbmc_small))
colnames(ES2)[ncol(ES2)] <- "cluster"

## plot
ridgeEnrichment(ES2, gene.set = "HALLMARK_DNA_REPAIR", group = "cluster", add.rug = TRUE)
```

In addition to the separation of **letter.idents**, we can also use `ridgeEnrichment()` for better granualarity 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.

```{r}
ridgeEnrichment(ES2, gene.set = "HALLMARK_DNA_REPAIR", group = "cluster", 
                facet = "letter.idents", add.rug = TRUE)
```

## Split Violin Plots

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"`.

```{r}
splitEnrichment(ES2, split = "groups", gene.set = "HALLMARK_DNA_REPAIR")
splitEnrichment(ES2, x.axis = "cluster", split = "groups", gene.set = "HALLMARK_DNA_REPAIR")
```

***

# Expanded Analysis

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.

```{r}
PCA <- performPCA(enriched = ES2, groups = c("groups", "cluster"))
```

```{r}
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.

```{r}
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`. 

```{r}
masterPCAPlot(ES2, PCx = "PC1", PCy = "PC2", top.contribution = 10)
```


## Signficance

We can also look for significant differences using linear modeling from the limma package, Welch's T test or one-way ANOVA using `getSignificance()`. For this, we need to assign a `group` parameter and the type of `fit` (either linear.model, T.test, or ANOVA).

```{r}
output <- getSignificance(ES2, group = "cluster", fit = "linear.model")
```

***

# Support

If you have any questions, comments or suggestions, feel free to visit the [github repository](https://github.com/ncborcherding/escape) or [email me](mailto:ncborch@gmail.com).

```{r}
sessionInfo()
```
