---
title: 'Clustering and labeling enriched gene sets with geneSetSimplifyR'
author: "Chiao-Yu Hsieh, Ella Hedeboe, and Kristoffer Vitting-Seerup"
date: "`r format(Sys.time(), '%d %B, %Y')`"
package: geneSetSimplifyR
output:
BiocStyle::html_document:
toc_float: true
vignette: >
%\VignetteIndexEntry{Clustering and labeling enriched gene sets}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
warning = FALSE,
message = TRUE,
out.width = "75%",
fig.align = "center"
)
```
# Background and Rationale
Gene-set enrichment analysis (GSEA) is one of the most useful types of
bioinformatic analysis since it summarizes long lists of genes (e.g. TP53, CD1)
into more high-level biological concepts (e.g. "cell cycle", "DNA repair").
While GSEA is potentially extremely powerful for profiling the high-level
biological changes in a system, this is often hampered by very long lists of
significant gene sets. This point is demonstrated by our [recent
article](https://link.springer.com/article/10.1186/s12915-023-01724-w) where we
re-analyze 100 RNA-seq data sets and show the average dataset had 1829 gene sets
significantly enriched among differentially expressed genes.
It is naturally not possible to manually analyze such long lists, so in practice
scientists often just show the most significant/enriched gene-sets or
cherry-pick gene-sets based on prior biological hypothesis. Neither is naturally
desirable (or good scientific practice?), hence approaches to analyzing such
long lists of gene sets are needed.
That is why we created `geneSetSimplifyR`. `geneSetSimplifyR` solves this
problem by:
- Grouping gene-sets into a small number of highly redundant/overlapping
clusters through unsupervised clustering
- Intelligently assigning labels to each cluster making it easy to figure out
the core biology behind a cluster
- Providing a host of intuitive plots enabling both visualization and
exploration of these clusters
`geneSetSimplifyR` works on the output from *any* gene-set enrichment analysis
tool but works especially well with
[fgsea](https://bioconductor.org/packages/fgsea/).
Several tools have been developed to summarize lists of enriched gene sets,
but most of them are limited in how they measure gene-set similarity, or do
not give each cluster a summary label:
- Semantic similarity, used by
[GOSemSim](https://bioconductor.org/packages/GOSemSim/), REVIGO,
[clusterProfiler](https://bioconductor.org/packages/clusterProfiler/)
and, by default,
[simplifyEnrichment](https://bioconductor.org/packages/simplifyEnrichment/),
needs an ontology such as the Gene Ontology (GO).
- Gene overlap, used by EnrichmentMap, GeneSetCluster,
[vissE](https://bioconductor.org/packages/vissE/) and SummArIzeR, gives
all genes the same weight. Genes that are in many gene sets then make
otherwise unrelated gene sets look similar. The Enrichr visualization
weights genes by term frequency-inverse document frequency (TF-IDF),
but it was made for visualization, works only with the Enrichr
libraries, and has not been systematically benchmarked.
- Clusters are summarized by a representative term (REVIGO,
clusterProfiler) or a word cloud (GeneSetCluster 2.0,
simplifyEnrichment, vissE). SummArIzeR writes a label with a large
language model, which needs additional tokens and cannot run offline.
`geneSetSimplifyR` weights genes by TF-IDF, embeds the gene sets with
latent semantic indexing (LSI) and clusters them on a shared nearest
neighbor (SNN) graph, with parameters chosen by benchmarking. It works with
any gene-set collection and gives each cluster one two-word label made
from the gene-set names.
# Installation
`geneSetSimplifyR` can be installed from Bioconductor by copy pasting this
command into R. `BiocManager` installs the dependencies from both CRAN and
Bioconductor:
```{r installation, eval=FALSE}
if (!requireNamespace("BiocManager", quietly = TRUE)) {
install.packages("BiocManager")
}
BiocManager::install("geneSetSimplifyR")
```
The development version can be installed from GitHub:
```{r installation-devel, eval=FALSE}
BiocManager::install("remotes") # needed to install from GitHub
BiocManager::install("IsoformAnalysisGroup/geneSetSimplifyR")
```
# geneSetSimplifyR workflow
## Overview
`geneSetSimplifyR` works on the output from *any* gene-set enrichment analysis
tool but works especially well with
[fgsea](https://bioconductor.org/packages/fgsea/).
The input to `geneSetSimplifyR` is simply:
- A list of the gene-sets that should be clustered
- Optionally(!) metadata about the enrichment analysis that can be used for
downstream visualization.
Specifically `geneSetSimplifyR` supports visualization of
- enrichment scores for each gene-set (e.g. NES, odds ratio, etc)
- p-values for each gene-set (shown as -log10 p-values)
- multi-conditional data (joint comparison of several analyses each finding
gene-sets of interest).
The `geneSetSimplifyR` workflow has three parts:
1) [Cluster and label gene-sets]
2) [Visual exploration of the gene-set clustering]
3) [Exploring and verifying clusters]
Each part is elaborated on in the sections below.
## Cluster and label gene-sets
We start by loading the `geneSetSimplifyR` library and its associated example
data:
```{r load, message=FALSE}
library(geneSetSimplifyR)
library(ggplot2) # for modifying the plots
data("exampleGeneSets")
data("exampleEnrichment")
```
Your own gene sets can be read from a GMT file with `fgsea::gmtPathways()`, or
the path to the GMT file can be passed directly as `geneSetsList` to
`geneSetSimplifyR()`.
Let's explore the data to see what it should look like
```{r explore example data}
### The gene sets are a list
class(exampleGeneSets)
### Where each entry is a gene set and its annotated genes
str(head(exampleGeneSets,2))
```
```{r nt_score and source}
head(exampleEnrichment, 2)
```
The *enrichment_score* can be used for visualization downstream and the *source*
column can be used to compare the results of multiple analyses. A *pval* column
with the p-value of each gene set can also be added; it is then shown as
-log10(p-value) by the plots that summarize the clusters. Here the enrichment
scores, p-values and adjusted p-values come from `fgsea`. In this case the
example data is a subset of the colorectal data analyzed in [Vitting-Seerup &
Sandelin (2017)](https://doi.org/10.1158/1541-7786.MCR-16-0459) where gene-set
enrichment was done both for differentially expressed genes ("Expression") and
differentially spliced genes ("Splicing"). The gene sets are from MSigDB
v7.5.1.
With the data read we are now ready to use `geneSetSimplifyR`. The main
functionalities are:
- Cluster gene-sets based on the overlap in genes annotated to the gene-sets
- Intelligently label the clusters from the most informative words in the
gene-set name
All functionality is wrapped into the *geneSetSimplifyR()* function:
```{r geneSetSimplifyR}
exampleGsList <- geneSetSimplifyR(
geneSetsList = exampleGeneSets,
geneSetsDF = exampleEnrichment,
# for a quick run - delete when running on real data
resolution = c(0.1, 0.6, 1.1, 1.6),
verbose = FALSE,
# TRUE because the gene sets we’re using have the authors’ names as
# their first word
removeFirstWord = TRUE
)
exampleGsList
```
Please note that:
1) While the visualizations in `geneSetSimplifyR` have a deep integration with
the enrichment scores and source annotation, these are neither needed nor
used for the clustering/labeling. Therefore the *geneSetsDF* argument is
optional.
2) The *resolution* argument is the hyper-parameter that controls the "level"
of clustering, with higher values typically resulting in more (smaller)
clusters.
3) Here we only use a few *resolution* values for a quick run on the example
data. In practice we recommend using a wider range of resolutions because:
- They can be utilized and explored downstream
- `geneSetSimplifyR` picks a default *resolution* (the one with the highest
silhouette score) from the ones tested
The default *resolution* picked from the silhouette score can be inspected with:
```{r optimal cluster}
extractResolution(exampleGsList)
```
Note that the silhouette-based default is only a starting point — we strongly
encourage users to inspect the clustering at multiple resolutions (e.g. with
`plotClustree()`, see below) and pick the resolution that best fits their data
and biological question.
## Visual exploration of the gene-set clustering
Now with the clustering and labeling done we are ready to explore the data
visually. If you did not run the code above to generate the example dataset you
can load the example gsList as follows:
```{r load example, message=FALSE}
data("exampleGsList")
exampleGsList
```
To get an overview of the clusters the simplest visualization is a bar chart.
```{r barchart}
plotBarchart(
geneSetsList = exampleGsList,
highlightClusterNo = c(0,1)
)
```
The bar chart shows the number of gene sets in each cluster, per source. With
multi-source data this immediately highlights clusters dominated by one source
versus those shared between sources, which can suggest biology specific to one
source versus shared programs. Here, for example, the "Stem Cell" cluster
consists almost only of gene sets enriched among differentially expressed genes,
while the "Peptidyl Lysine" cluster consists almost only of gene sets enriched
among differentially spliced genes.
Also note that the `highlightClusterNo` argument is available for most plotting
functions, making it easy to visually communicate which clusters you think are
most interesting (shown in bold).
We can also visualize the gene-set clusters in a UMAP plot:
```{r umap highlight}
plotUMAP(
geneSetsList = exampleGsList,
highlightClusterNo = c(0,1),
removeClusterId = TRUE,
pointAlpha = 1 # because there are relatively few gene-sets here
)
```
Which automatically plots the source as the shape. The UMAP plot function has
many arguments enabling fine-tuning of the plot, and `interactive = TRUE` gives
a plot where hovering over a point shows its gene set. Here we just want to
highlight the use of `removeClusterId`. While the cluster numbers are useful for
quick referral to clusters (important later), it is often desirable to remove
them. Hence the `removeClusterId` argument (implemented in most plot functions)
removes the cluster number (e.g. the "1: " prefix in "1: Serum Response").
We already mentioned how we see differences in cluster composition. This can
naturally also be directly visualized. Gene sets found in both sources are
counted once, as "Both":
```{r proportion}
plotProportions(
geneSetsList = exampleGsList,
highlightClusterNo = c(0,1),
removeClusterId = TRUE
)
```
We can also utilize the p-values and enrichment scores we have for each gene
set. The simplest visualization is a lollipop plot of each cluster's median
-log10(p-value):
```{r enrichment}
plotEnrichments(
geneSetsList = exampleGsList,
highlightClusterNo = c(0,1),
removeClusterId = TRUE
)
```
Which value is plotted is controlled by `plotFrom`, and how the gene sets of a
cluster are summarized by `summaryStat` ('median', 'max' or 'mean'). By default
the -log10 p-values are plotted when available, else the enrichment scores. Any
numeric column you supplied in `geneSetsDF` can be used, here the mean
enrichment score:
```{r enrichment mean}
plotEnrichments(
geneSetsList = exampleGsList,
plotFrom = 'enrichment_score',
summaryStat = 'mean',
removeClusterId = TRUE
)
```
But we can also visualize the values of all gene sets (by default the -log10
p-values) in a violin plot:
```{r violin, warning=FALSE}
plotViolin(
geneSetsList = exampleGsList,
highlightClusterNo = c(0,1),
removeClusterId = FALSE
)
```
These plots are useful for showing differences in regulation (one cluster being
up-regulated while another is down-regulated).
With many sources (e.g. many conditions or time points), a heatmap with one row
per source gives a compact overview. Cells of clusters without any gene set from
a source are gray. With three or more sources, each cluster is z-scored across
the sources by default (see the `scaleCols` argument):
```{r heatmap}
plotHeatmap(
geneSetsList = exampleGsList,
removeClusterId = TRUE
)
```
We can investigate how the clusters relate to one another using the per-cluster
average dendrogram:
```{r dendrogram, warning=FALSE}
plotDendrogram(
geneSetsList = exampleGsList,
removeClusterId = TRUE
)
```
The dendrogram complements the UMAP: distances between clusters in a UMAP are
not always meaningful, while the dendrogram is computed directly from the gene
sets the clusters share. The distances are often all close to 1, because few
gene sets of different clusters are neighbors, so the axis is zoomed in on the
range of the merges.
## Exploring and verifying clusters
The most important thing to get right is the clustering "level" controlled by
the `resolution` argument (as shown above in the [Cluster and label gene-sets]
section). `geneSetSimplifyR` picks a sensible default automatically, but we
always recommend visualizing how different cluster resolutions affect the
clusters via the [clustree R
package](https://cran.r-project.org/web/packages/clustree/index.html) here
wrapped in the *plotClustree()* function:
```{r plotClustree, fig.height=9, fig.width=9, out.width="100%"}
plotClustree(
exampleGsList,
# a subset keeps the tree readable
resolutionsToPlot = c(0.1, 0.6, 1.1, 1.6, 2.1)
)
```
This is a very powerful way to see how the gene-sets are distributed into
different clusters and for helping with verifying the clustering "level", as
determined by the resolution, is correct.
This can naturally be followed up by e.g. a UMAP at a different resolution —
here resolution 0.6:
```{r umap new res}
plotUMAP(
geneSetsList = exampleGsList,
resolution = 0.6,
pointAlpha = 1
)
```
Please note the `resolution` parameter is also implemented for most plotting
functions and for `extractTable()`. When it is left at `NULL`, the *active*
resolution is used: the optimal one found by `clusterGeneSets()`, or the one you
last chose with `updateResolution()` or `updateClusterLabels(resolution = ...)`.
Let's try to update the default resolution:
```{r update res}
exampleGsList <- updateResolution(
geneSetsList = exampleGsList,
resolution = 0.6
)
exampleGsList
```
Once we have verified the cluster resolution is suitable (for your analysis), we
can move on to verifying the cluster labels. While we spent a lot of time
optimizing the approach `geneSetSimplifyR` uses to extract informative labels
for each cluster, it is not a perfect process. Therefore we have built
functionality to both explore and change cluster labels.
```{r plot labels, warning=FALSE, out.width="100%"}
plotClusterLabels(
geneSetsList = exampleGsList,
clusterNumber = 1,
maxChar = 35 # shorten gene-set names to fit in vignette
) +
theme_bw(base_size = 8) # make font smaller to fit in vignette
```
From which a LOT of summary info can be seen for this particular cluster:
- The label TF-IDF meaning the two-words that are most enriched in this
cluster compared to other clusters (bottom left)
- The gene-sets most central in the shared nearest neighbor (SNN) graph of the
cluster (top right)
- The weighted TF IDF - that is the TF-IDF score of each label combined with
the centrality of the gene sets it comes from (both scaled to 0-1 and
added). The top label defines the cluster label (top left).
- The most frequent words in the gene-set names (top mid)
- The most frequent genes in the gene-sets (bottom mid)
- The largest gene-sets in the cluster (bottom right)
We recommend that you always inspect the output from *plotClusterLabels()* for
ALL your clusters to ensure the labeling is appropriate.
If you think the auto-generated cluster labels could be more precise, you can
manually override them. For example, cluster 1 contains the serum response gene
sets of Schlosser et al. If you determine that this cluster is better described
as "Serum Response" after checking the [gene-set
information](https://www.gsea-msigdb.org/gsea/msigdb/human/geneset/SCHLOSSER_SERUM_RESPONSE_DN.html),
the gsList can be updated accordingly:
```{r update label, warning=FALSE}
exampleGsList <- updateClusterLabels(
geneSetsList = exampleGsList,
clusterNumber = 1,
newClusterName = 'Serum Response'
)
plotUMAP(
geneSetsList = exampleGsList,
pointAlpha = 1
)
```
To verify the clustering you can naturally also look at all the gene-sets
annotated to a cluster:
```{r extract full table, warning=FALSE}
gsTable <- extractTable(
geneSetsList = exampleGsList,
summarize = FALSE
)
head(gsTable)
```
The `initial_cluster_label` column keeps the automatically generated label of
renamed clusters. We strongly recommend ALWAYS including this table in the
supplementary material of an article, as it is essential for both transparency
and reproducibility.
For reporting purposes this table can also be summarized:
```{r extract summary table, warning=FALSE}
gsSummary <- extractTable(
geneSetsList = exampleGsList,
summarize = TRUE
)
head(gsSummary)
```
## Plot modifications
All visualization functions return a ggplot2 object, except `plotHeatmap()` (a
pheatmap object) and `plotUMAP(interactive = TRUE)` (a plotly object):
```{r ggplot2 example}
data("exampleGsList")
p1 <- plotBarchart(
geneSetsList = exampleGsList,
highlightClusterNo = c(0,1)
)
inherits(p1, "ggplot")
p1
```
This also means all plots can easily be modified using standard ggplot2
operations:
```{r ggplot2 mod}
p1 +
ggtitle('New plot title')
```
Such ggplot2 operations are *really* easy to google since ggplot2 has millions
of users. So if you want to modify any plot in some way, just google that in
conjunction with ggplot2 and you will find your answer (e.g. "ggplot2 change
fill color").
```{r ggplot2 mod2}
p1 +
scale_fill_manual(values = c('black','green'))
```
## Convenience parsers for common GSEA tools
`geneSetSimplifyR` ships small helpers that convert the output of popular
enrichment-analysis tools into the `geneSetsList` + `geneSetsDF` pair
`geneSetSimplifyR()` expects, so you don't have to reshape the data by hand:
- `parsePairedGSEA()` — parses a `pairedGSEA::paired_ora()` result. Each of
the three blocks (`expression`, `splicing`, `paired`) is filtered by its own
padj cutoff (`cutoff_exp`, `cutoff_splicing`, `cutoff_paired`) and
contributes its surviving pathways as a separate `source`.
- `parseFgsea()` — parses a single `fgsea::fgsea()` (GSEA) or `fgsea::fora()`
(ORA) result; filters by a `padj` cutoff and labels rows with a
user-supplied `source` string. To combine several analyses into one
multi-source clustering, run it once per analysis (with a distinct
`source`), `rbind()` the resulting `geneSetsDF`s and combine the
`geneSetsList`s (e.g. `c(a, b)` followed by removing duplicated names).
- `getGeneSets()` — fetches MSigDB gene sets (via
[msigdbr](https://cran.r-project.org/package=msigdbr)) by collection /
sub-collection or by a vector of pathway names. Handy for building a
`geneSetsList` from scratch or for inspecting the members of specific
pathways after clustering.
The two parsers return a list with two elements, `geneSetsList` and
`geneSetsDF`, ready to pass straight into `geneSetSimplifyR()`. `getGeneSets()`
returns a named list of gene sets that can be used as `geneSetsList`. The next
section runs `parseFgsea()` on `fgsea` results.
See `?parsePairedGSEA`, `?parseFgsea`, and `?getGeneSets` for the full argument
list.
# How to do gene-set enrichment analysis/overrepresentation analysis
Let's look at how to produce the input for `geneSetSimplifyR` via the `fgsea`
package and its example data:
```{r fgsea data, message=FALSE}
library(fgsea)
data(examplePathways)
data(exampleRanks)
```
And here are the genes of interest (e.g. significant genes) from the `fgsea`
package:
```{r example gene-sets}
sigGenes <- tail(names(exampleRanks), 750)
head(sigGenes)
```
Then, we can run a simple over-representation analysis (ORA) or gene set
enrichment analysis (GSEA) using the `fgsea` package as follows:
```{r fora fgsea}
# ORA example
oraRes <- fora(
pathways = examplePathways,
genes = sigGenes,
universe = names(exampleRanks)
)
head(oraRes[order(padj), ])
# fgsea example
fgseaRes <- fgsea(pathways = examplePathways,
stats = exampleRanks)
head(fgseaRes[order(padj), ])
```
**Both ORA and GSEA results from fgsea can be used as input to our package in
the same way.**
Below, we use the ORA results to demonstrate this workflow.
```{r fora downstream}
parsed <- parseFgsea(oraRes, examplePathways, cutoff=0.05)
oraGsList <- geneSetSimplifyR(
geneSetsList = parsed$geneSetsList,
geneSetsDF = parsed$geneSetsDF,
verbose = FALSE,
removeFirstWord = TRUE # the names start with an id, e.g. 5990980_Cell_Cycle
)
oraGsList
plotEnrichments(oraGsList) # uses the -log10 p-values kept by parseFgsea()
```
# Session info
```{r session info}
sessionInfo()
```