vignettes/articles/Running_Escape.Rmd
Running_Escape.Rmd
escape is a R package designed for Easy single-cell analysis platform for enrichment. As the tortuous acronym implies, escape was designed as a user-friendly package for gene set enrichment analysis that leverages the heterogeneity of single-cell data.
More information is available at the GitHub Repo.
If using escape, please cite the article: Borcherding, N., Vishwakarma, A., Voigt, A.P. et al. Mapping the immune environment in clear cell renal carcinoma by single-cell genomics. Commun Biol 4, 122 (2021). https://doi.org/10.1038/s42003-020-01625-6.
escape is available via github using:
devtools::install_github("ncborcherding/escape")
remotes::install_github("ncborcherding/escape")
For now, the newest version of escape is available in the Bioconductor dev version (3.19).
For the purposes of the vignette, we will use the scRepertoire full example data. More information is available here. We will subset the data for patient 17 and 18 from the data set in order to speed up the calculations.
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).
Additional parameters include:
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
Several popular methods exist for Gene Set Enrichment Analysis (GSEA). These methods can vary in the underlying assumptions. escape incorporates several methods that are particularly advantageous for single-cell RNA values:
This method calculates the enrichment score using a rank-normalized approach and generating an empirical cumulative distribution function for each individual cell. The enrichment score is defined for a gene set (G) using the number of genes in the gene set (NG) and total number of genes (N).
\[ ES(G,S) \sum_{i = 1}^{n} [P_W^G(G,S,i)-P_{NG}(G,S,i)] \] Please see the following citation for more information.
GSVA varies slightly by estimating a Poisson-based kernel cumulative density function. But like ssGSEA, the ultimate enrichment score reported is based on the maximum value of the random walk statistic. GSVA appears to have better overall consistency and runs faster than ssGSEA.
\[ ES_{jk}^{max} = V_{jk} [max_{l=1,...,p}|v_{jk}(l)] \] Please see the following citation for more information.
In contrast to ssGSEA and GSVA, AUCell takes the gene rankings for each cell and step-wise plots the position of each gene in the gene set along the y-axis. The output score is the area under the curve for this approach.
Please see the following citation for more information.
UCell calculates a Mann-Whitney U statistic based on the gene rank list. Importantly, UCell has a cut-off for ranked genes (\(r_{max}\)) at 1500 - this is per design as drop-out in single-cell can alter enrichment results. This also substantially speeds the calculations up.
The enrichment score output is then calculated using the complement of the U statistic scaled by the gene set size and cut-off.
\[ U_j^` = 1-\frac{U_j}{n \bullet r_{max}} \]
Please see the following citation for more information.
escape has 2 major functions - the first being
escape.matrix()
, which serves as the backbone of enrichment
calculations. Using count-level data supplied from a single-cell object
or matrix, escape.matrix()
will produce an enrichment score
for the individual cells with the gene sets selected and output the
values as a matrix.
method
groups
min.size
normalize
make.positive
Cautionary note: make.positive was added to allow for differential analysis downstream of enrichment as some methods may produce negative values. It preserves log-fold change, but ultimately modifies the enrichment values and should be used with caution.
enrichment.scores <- escape.matrix(scRep_example,
gene.sets = GS.hallmark,
groups = 5000,
min.size = 5)
We can visualize the raw results with a ggplot:
ggplot(data = as.data.frame(enrichment.scores),
mapping = aes(x = `HALLMARK-DNA-REPAIR`, `HALLMARK-G2M-CHECKPOINT`)) +
geom_point() +
theme_classic() +
theme()
Multi-core support is for all methods is available through BiocParallel.
To add more cores, use the argument BPPARAM to
escape.matrix()
. Here we will use the
SnowParam()
for it’s support across platforms and
explicitly call 2 workers (or cores).
enrichment.scores <- escape.matrix(scRep_example,
gene.sets = GS.hallmark,
groups = 5000,
min.size = 5,
BPPARAM = SnowParam(workers = 2))
Alternatively, we can use runEscape()
to calculate the
enrichment score and directly attach the output to a single-cell object.
The additional parameter for ``runEscape
is
new.assay.name, in order to save the enrichment scores
as a custom assay in the single-cell object.
scRep_example <- runEscape(scRep_example,
method = "ssGSEA",
gene.sets = GS.hallmark,
groups = 5000,
min.size = 0,
new.assay.name = "escape.ssGSEA")
We can quickly examine the attached enrichment scores using the
visualization/workflow we prefer - here we will use just
FeaturePlot()
from the Seurat R package.
#Define color palette
colorblind_vector <- hcl.colors(n=7, palette = "inferno", fixup = TRUE)
FeaturePlot(scRep_example, "HALLMARK-APOPTOSIS") +
scale_color_gradientn(colors = colorblind_vector) +
theme(plot.title = element_blank())
Although we glossed over the normalization that can be used in
escape.matrix()
and runEscape()
, it is worth
mentioning here as normalization can affect all downstream analyses.
There can be inherent bias in enrichment values due to drop out in
single-cell expression data. Cells with larger numbers of features and
counts will likely have higher enrichment values.
performNormalization()
will normalize the enrichment values
by calculating the number of genes expressed in each gene set and cell.
This is similar to the normalization in classic GSEA and it will be
stored in a new assay.
scRep_example <- performNormalization(scRep_example,
assay = "escape.ssGSEA",
gene.sets = GS.hallmark)
An alternative for scaling by expressed gene sets would be to use a scaling factor previously calculated during normal single-cell data processing and quality control. This can be done using the scale.factor argument and providing a vector. This approach may be particularly advantageous for large data sets.
scRep_example <- performNormalization(scRep_example,
assay = "escape.ssGSEA",
gene.sets = GS.hallmark,
scale.factor = scRep_example$nFeature_RNA)
performNormalization()
has an additional parameter
make.positive. Across the individual gene sets, if
negative normalized enrichment scores are seen, the minimum value is
added to all values. For example if the normalized enrichment scores
(after the above accounting for drop out) ranges from -50 to 50,
make.positive will adjust the range to 0 to 100 (by
adding 50). This allows for compatible log2-fold change downstream, but
can alter the enrichment score interpretation.
There are a number of ways to look at the enrichment values
downstream of runEscape()
with the myriad plotting and
visualizations functions/packages for single-cell data. escape
include several additional plotting functions to assist in the
analysis.
We can examine the enrichment values across our gene sets by using
heatmapEnrichment()
. This visualization will return the
mean of the group.by variable. As a default - all
visualizations of single-cell objects will use the cluster assignment or
active identity as a default for visualizations. We will use a subset of
the Hallmark Gene Sets to display for the vignette format using the
gene.set.use set to first 12 gene sets. Alternatively,
we can plot all gene sets using gene.set.use =
“all”.
heatmapEnrichment(scRep_example,
group.by = "ident",
gene.set.use = rownames(scRep_example@assays$escape.ssGSEA@data)[1:12],
assay = "escape.ssGSEA")
Most of the visualizations in escape have a defined set of parameters.
group.by
facet.by
scale
In addition, heatmapEnrichment()
allows for the
reclustering of rows and columns using Euclidean distance of the
enrichment scores and the Ward2 methods for clustering using
cluster.rows and cluster.columns.
heatmapEnrichment(scRep_example,
group.by = "ident",
assay = "escape.ssGSEA",
gene.set.use = rownames(scRep_example@assays$escape.ssGSEA@data)[1:12],
scale = TRUE,
cluster.rows = TRUE,
cluster.columns = TRUE)
Each visualization has an additional argument called
palette that supplies the coloring scheme to be used -
available color palettes can be viewed with hcl.pals()
.
hcl.pals()
## [1] "Pastel 1" "Dark 2" "Dark 3" "Set 2"
## [5] "Set 3" "Warm" "Cold" "Harmonic"
## [9] "Dynamic" "Grays" "Light Grays" "Blues 2"
## [13] "Blues 3" "Purples 2" "Purples 3" "Reds 2"
## [17] "Reds 3" "Greens 2" "Greens 3" "Oslo"
## [21] "Purple-Blue" "Red-Purple" "Red-Blue" "Purple-Orange"
## [25] "Purple-Yellow" "Blue-Yellow" "Green-Yellow" "Red-Yellow"
## [29] "Heat" "Heat 2" "Terrain" "Terrain 2"
## [33] "Viridis" "Plasma" "Inferno" "Rocket"
## [37] "Mako" "Dark Mint" "Mint" "BluGrn"
## [41] "Teal" "TealGrn" "Emrld" "BluYl"
## [45] "ag_GrnYl" "Peach" "PinkYl" "Burg"
## [49] "BurgYl" "RedOr" "OrYel" "Purp"
## [53] "PurpOr" "Sunset" "Magenta" "SunsetDark"
## [57] "ag_Sunset" "BrwnYl" "YlOrRd" "YlOrBr"
## [61] "OrRd" "Oranges" "YlGn" "YlGnBu"
## [65] "Reds" "RdPu" "PuRd" "Purples"
## [69] "PuBuGn" "PuBu" "Greens" "BuGn"
## [73] "GnBu" "BuPu" "Blues" "Lajolla"
## [77] "Turku" "Hawaii" "Batlow" "Blue-Red"
## [81] "Blue-Red 2" "Blue-Red 3" "Red-Green" "Purple-Green"
## [85] "Purple-Brown" "Green-Brown" "Blue-Yellow 2" "Blue-Yellow 3"
## [89] "Green-Orange" "Cyan-Magenta" "Tropic" "Broc"
## [93] "Cork" "Vik" "Berlin" "Lisbon"
## [97] "Tofino" "ArmyRose" "Earth" "Fall"
## [101] "Geyser" "TealRose" "Temps" "PuOr"
## [105] "RdBu" "RdGy" "PiYG" "PRGn"
## [109] "BrBG" "RdYlBu" "RdYlGn" "Spectral"
## [113] "Zissou 1" "Cividis" "Roma"
heatmapEnrichment(scRep_example,
group.by = "ident",
gene.set.use = rownames(scRep_example@assays$escape.ssGSEA@data)[1:12],
assay = "escape.ssGSEA",
palette = "Spectral")
Alternatively, we can add an additional layer to the ggplot object
that is returned by the visualizations using something like
scale_fill_gradientn()
for continuous values or
scale_fill_manual()
for the categorical variables.
We can also focus on individual gene sets - one approach is to use
geyserEnrichment()
. Here individual cells are plotted along
the Y-axis with graphical summary where the central dot refers to the
median enrichment value and the thicker/thinner lines demonstrate the
interval summaries referring to the 66% and 95%.
geyserEnrichment(scRep_example,
assay = "escape.ssGSEA",
gene.set = "HALLMARK-INTERFERON-GAMMA-RESPONSE")
To show the additional parameters that appear in visualizations of individual enrichment gene sets - we can reorder the groups by the mean of the gene set using order.by = “mean”.
geyserEnrichment(scRep_example,
assay = "escape.ssGSEA",
gene.set = "HALLMARK-INTERFERON-GAMMA-RESPONSE",
order.by = "mean")
What if we had 2 separate samples or groups within the data? Another parameter we can use is facet.by to allow for direct visualization of an additional variable.
geyserEnrichment(scRep_example,
assay = "escape.ssGSEA",
gene.set = "HALLMARK-INTERFERON-GAMMA-RESPONSE",
facet.by = "Type")
Lastly, we can select the way the color is applied to the plot using the color.by parameter. Here we can set it to the gene set of interest “HALLMARK-INTERFERON-GAMMA-RESPONSE”.
geyserEnrichment(scRep_example,
assay = "escape.ssGSEA",
gene.set = "HALLMARK-INTERFERON-GAMMA-RESPONSE",
color.by = "HALLMARK-INTERFERON-GAMMA-RESPONSE")
Similar to the geyserEnrichment()
the
ridgeEnrichment()
can display the distribution of
enrichment values across the selected gene set. The central line is at
the median value for the respective grouping.
ridgeEnrichment(scRep_example,
assay = "escape.ssGSEA",
gene.set = "HALLMARK-IL2-STAT5-SIGNALING")
We can get the relative position of individual cells along the x-axis using the add.rug parameter.
ridgeEnrichment(scRep_example,
assay = "escape.ssGSEA",
gene.set = "HALLMARK-IL2-STAT5-SIGNALING",
add.rug = TRUE,
scale = TRUE)
Another distribution visualization is a violin plot, which we
separate 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 based on the
split.by parameter with a central boxplot with the
relative distribution across all samples. Here, we split the samples by
Type where B refers to the peripheral blood
and L refers to lung.
splitEnrichment(scRep_example,
assay = "escape.ssGSEA",
gene.set = "HALLMARK-IL2-STAT5-SIGNALING",
split.by = "Type")
densityEnrichment()
is a method to visualize the mean
rank position of the gene set features along the total feature space by
group. This is similar to traditional GSEA analysis, but is not
calculating the walk-based enrichment score. As this function calculates
mean ranks on the fly, it may take some time depending on the size of
the single-cell object.
gene.set.use
gene.sets
densityEnrichment(scRep_example,
gene.set.use = "HALLMARK-IL6-JAK-STAT3-SIGNALING",
group.by = "orig.ident",
gene.sets = GS.hallmark)
It may be advantageous to look at the distribution of multiple gene
sets - here we can use scatterEnrichment()
for a 2 gene set
comparison. The color values are based on the density of points
determined by the number of neighbors, similar to the Nebulosa
R package. We just need to define which gene set to plot on the
x.axis and which to plot on the
y.axis.
scatterEnrichment(scRep_example,
assay = "escape.ssGSEA",
x.axis = "HALLMARK-INTERFERON-GAMMA-RESPONSE",
y.axis = "HALLMARK-IL6-JAK-STAT3-SIGNALING")
The scatter plot can also be converted into a hexbin, another method for summarizing the individual cell distributions along the x and y axis, by setting style = “hex”.
scatterEnrichment(scRep_example,
assay = "escape.ssGSEA",
x.axis = "HALLMARK-INTERFERON-GAMMA-RESPONSE",
y.axis = "HALLMARK-IL6-JAK-STAT3-SIGNALING",
style = "hex")
escape has its own PCA function performPCA()
which will
work on a single-cell object or a matrix of enrichment values. This is
specifically useful for downstream visualizations as it stores the
eigenvalues and rotations. If we want to look at the relative
contribution to overall variance of each component or a Biplot-like
overlay of the individual features, use performPCA()
.
Alternatively, other PCA-based functions like Seurat’s
RunPCA()
or scater’s ``runPCA()
can be used.
These functions are likely faster and would be ideal if we have a larger
number of cells and/or gene sets.
scRep_example <- performPCA(scRep_example,
assay = "escape.ssGSEA",
n.dim = 1:10)
escape has a built in method for plotting PCA
pcaEnrichment()
that functions similarly to the
scatterEnrichment()
function where x.axis
and y.axis are the components to plot.
pcaEnrichment(scRep_example,
dimRed = "escape.PCA",
x.axis = "PC1",
y.axis = "PC2")
pcaEnrichment()
can plot additional information on the
principal component analysis.
add.percent.contribution will add the relative percent contribution of the x and y.axis to total variability observed in the PCA.
display.factors will overlay the magnitude and direction that the features/gene sets contribute to the selected components. The number of gene sets is determined by number.of.factors. This can assist in understanding the underlying differences in enrichment across different cells.
pcaEnrichment(scRep_example,
dimRed = "escape.PCA",
x.axis = "PC1",
y.axis = "PC2",
add.percent.contribution = TRUE,
display.factors = TRUE,
number.of.factors = 10)
Differential enrichment analysis can be performed similar to differential gene expression analysis. For the purposes of finding the differential enrichment values, we can first normalize the enrichment values for the ssGSEA calculations. Here we are using the scale.factor parameter of nFeature_RNA, which is the total feature space for each cell.
scRep_example <- performNormalization(scRep_example,
assay = "escape.ssGSEA",
gene.sets = GS.hallmark,
scale.factor = scRep_example$nFeature_RNA)
## [1] "Normalizing enrichment scores per cell..."
all.markers <- FindAllMarkers(scRep_example,
assay = "escape.ssGSEA_normalized",
min.pct = 0,
logfc.threshold = 0)
head(all.markers)
## p_val avg_log2FC pct.1 pct.2
## HALLMARK-REACTIVE-OXYGEN-SPECIES-PATHWAY 1.840790e-50 0.4954612 0.994 0.927
## HALLMARK-OXIDATIVE-PHOSPHORYLATION 1.730400e-42 0.2908800 1.000 0.998
## HALLMARK-ADIPOGENESIS 5.911400e-36 0.1871677 1.000 1.000
## HALLMARK-APICAL-JUNCTION 7.642810e-19 0.2126941 0.983 0.921
## HALLMARK-DNA-REPAIR 1.817561e-18 0.2708074 0.985 0.945
## HALLMARK-UV-RESPONSE-DN 2.792584e-17 -4.3160697 1.000 1.000
## p_val_adj cluster
## HALLMARK-REACTIVE-OXYGEN-SPECIES-PATHWAY 9.203948e-49 1
## HALLMARK-OXIDATIVE-PHOSPHORYLATION 8.651999e-41 1
## HALLMARK-ADIPOGENESIS 2.955700e-34 1
## HALLMARK-APICAL-JUNCTION 3.821405e-17 1
## HALLMARK-DNA-REPAIR 9.087807e-17 1
## HALLMARK-UV-RESPONSE-DN 1.396292e-15 1
## gene
## HALLMARK-REACTIVE-OXYGEN-SPECIES-PATHWAY HALLMARK-REACTIVE-OXYGEN-SPECIES-PATHWAY
## HALLMARK-OXIDATIVE-PHOSPHORYLATION HALLMARK-OXIDATIVE-PHOSPHORYLATION
## HALLMARK-ADIPOGENESIS HALLMARK-ADIPOGENESIS
## HALLMARK-APICAL-JUNCTION HALLMARK-APICAL-JUNCTION
## HALLMARK-DNA-REPAIR HALLMARK-DNA-REPAIR
## HALLMARK-UV-RESPONSE-DN HALLMARK-UV-RESPONSE-DN