library(knitr) knitr::opts_chunk$set( error = FALSE, tidy = FALSE, message = FALSE, warning = FALSE, fig.align = "center", dev = "jpeg" ) options(width = 80)
The simplifyEnrichment package clusters functional terms into groups by clustering the similarity matrix of the terms with a new proposed method "binary cut" which recursively applies partition around medoids (PAM) with two groups on the similarity matrix and in each iteration step, a score is assigned to decide whether the group of gene sets that corresponds to the current sub-matrix should be split or not. For more details of the method, please refer to the simplifyEnrichment paper.
library(simplifyEnrichment) mat = readRDS(system.file("extdata", "random_GO_BP_sim_mat.rds", package = "simplifyEnrichment")) go_id = rownames(mat)
The major use case for simplifyEnrichment is for simplying the GO enrichment results by clustering the corresponding semantic similarity matrix of the significant GO terms. To demonstrate the usage, we first generate a list of random GO IDs from the Biological Process (BP) ontology category:
library(simplifyEnrichment) set.seed(888) go_id = random_GO(500)
simplifyEnrichment starts with the GO similarity matrix. Users can use
their own similarity matrices or use the GO_similarity()
function to
calculate the semantic similarity matrix. The GO_similarity()
function is
simply a wrapper on GOSemSim::termSim()
. The function accepts a vector of GO
IDs. Note the GO terms should only belong to one same ontology (i.e., BP
,
CC
or MF
).
mat = GO_similarity(go_id)
By default, GO_similarity()
uses Rel
method in GOSemSim::termSim()
. Other
methods to calculate GO similarities can be set by measure
argument, e.g.:
GO_similarity(go_id, measure = "Wang")
With the similarity matrix mat
, users can directly apply simplifyGO()
function to perform the clustering as well as visualizing the results.
df = simplifyGO(mat)
On the right side of the heatmap there are the word cloud annotations which summarize the functions with keywords in every GO cluster. Additionally, enrichment is done on keywords compared to GO background vocabulary and the significance corresponds to the font size of the keywords.
Note there is no word cloud for the cluster that is merged from small clusters (size < 5).
The returned variable df
is a data frame with GO IDs and the
cluster labels:
head(df)
The size of GO clusters can be retrieved by:
sort(table(df$cluster))
Or split the data frame by the cluster labels:
split(df, df$cluster)
plot
argument can be set to FALSE
in simplifyGO()
, so that no plot is
generated and only the data frame is returned.
If the aim is only to cluster GO terms, binary_cut()
or cluster_terms()
functions can be
directly applied:
binary_cut(mat)
or
cluster_terms(mat, method = "binary_cut")
binary_cut()
and cluster_terms()
basically generate the same clusterings, but the labels of clusters might differ.
In the simplifyEnrichment package, there are also functions that compare
clustering results from different methods. Here we still use previously
generated variable mat
which is the similarity matrix from the 500 random GO
terms. Simply running compare_clustering_methods()
function performs all supported
methods (in all_clustering_methods()
) excluding mclust
, because
mclust
usually takes very long time to run. The function generates a figure
with three panels:
In the barplots, the three metrics are defined as follows:
compare_clustering_methods(mat)
If plot_type
argument is set to heatmap
. There are heatmaps for the
similarity matrix under different clusterings methods. The last panel is a
table with the number of clusters.
compare_clustering_methods(mat, plot_type = "heatmap")
Please note, the clustering methods might have randomness, which means,
different runs of compare_clustering_methods()
may generate different clusterings
(slightly different). Thus, if users want to compare the plots between
compare_clustering_methods(mat)
and compare_clustering_methods(mat, plot_type = "heatmap")
, they
should set the same random seed before executing the function.
set.seed(123) compare_clustering_methods(mat) set.seed(123) compare_clustering_methods(mat, plot_type = "heatmap")
compare_clustering_methods()
is simply a wrapper on cmp_make_clusters()
and cmp_make_plot()
functions where the former function performs
clustering with different methods and the latter visualizes the results. To
compare different plots, users can also use the following code without
specifying the random seed.
clt = cmp_make_clusters(mat) # just a list of cluster labels cmp_make_plot(mat, clt) cmp_make_plot(mat, clt, plot_type = "heatmap")
New clustering methods can be added by register_clustering_methods()
,
removed by remove_clustering_methods()
and reset to the default methods by
reset_clustering_methods()
. All the supported methods can be retrieved by
all_clustering_methods()
. compare_clustering_methods()
runs all the clustering methods
in all_clustering_methods()
.
The new clustering methods should be as user-defined functions and sent to
register_clustering_methods()
as named arguments, e.g.:
register_clustering_methods( method1 = function(mat, ...) ..., method2 = function(mat, ...) ..., ... )
The functions should accept at least one argument which is the input matrix
(mat
in above example). The second optional argument should always be ...
so that parameters for the clustering function can be passed by control
argument from cluster_terms()
or simplifyGO()
. If users forget to add
...
, it is added internally.
Please note, the user-defined function should automatically identify the optimized number of clusters. The function should return a vector of cluster labels. Internally it is converted to numeric labels.
There are following examples which we did for the benchmarking in the manuscript:
It is always very common that users have multiple lists of GO enrichment
results (e.g. from multiple groups of genes) and they want to compare the
significant terms between different lists, e.g. to see which biological
functions are more specific in a certain list. There is a function
simplifyGOFromMultipleLists()
in the package which helps this type of analysis.
The input data for simplifyGOFromMultipleLists()
(with the argument lt
) can have three types of formats:
go_id_column
argument and the column of the adjusted p-values can be
specified with padj_column
argument. If the two columns are not specified, they are automatically identified. The GO ID column
is found by checking whether a column contains all GO IDs. The adjusted p-value column is found by comparing the column names of the
data frame to see whether it might be a column for adjusted p-values. These two columns are used to construct a numeric vector
with GO IDs as names.If the GO enrichment results is directly from upstream analysis, e.g. the package clusterProfiler or other similar packages, the results are most probably represented as a list of data frames, thus, we first demonstrate the usage on a list of data frames.
The function functional_enrichment()
in cola package applies functional
enrichment on different groups of signature genes from consensus clustering.
The function internally uses clusterProfiler and returns a list of data frames:
# perform functional enrichment on the signatures genes from cola anlaysis library(cola) data(golub_cola) res = golub_cola["ATC:skmeans"] library(hu6800.db) x = hu6800ENTREZID mapped_probes = mappedkeys(x) id_mapping = unlist(as.list(x[mapped_probes])) lt = functional_enrichment(res, k = 3, id_mapping = id_mapping) names(lt) head(lt[[1]][, 1:7])
By default, simplifyGOFromMultipleLists()
automatically identifies the columns that contain GO IDs and adjusted p-values, so here we directly
send lt
to simplifyGOFromMultipleLists()
. We additionally set padj_cutoff
to 0.001 because under the default cutoff 0.01, there are too many
GO IDs and to save the running time, we set a more strict cutoff.
simplifyGOFromMultipleLists(lt, padj_cutoff = 0.001)
Next we demonstrate two other data types for simplifyGOFromMultipleLists()
. Both usages are straightforward. The first is a list of numeric vectors:
lt2 = lapply(lt, function(x) structure(x$p.adjust, names = x$ID)) simplifyGOFromMultipleLists(lt2, padj_cutoff = 0.001)
And the second is a list of character vectors of GO IDs:
lt3 = lapply(lt, function(x) x$ID[x$p.adjust < 0.001]) simplifyGOFromMultipleLists(lt3)
The process of this analysis is as follows.
Let's assume there are $n$ GO lists, we first construct a global matrix where columns correspond to the $n$ GO lists and rows correspond
to the "union" of all GO IDs in the $n$ lists. The value for the ith GO ID and in the jth list are taken from the corresponding numeric vector
in lt
. If the jth vector in lt
does not contain the ith GO ID, the value defined by default
argument is taken there (e.g. in most cases the numeric
values are adjusted p-values, thus default
is set to 1). Let's call this matrix as $M_0$.
Next step is to filter $M_0$ so that we only take a subset of GO IDs of interest. We define a proper function via argument filter
to remove
GO IDs that are not important for the analysis. Function for filter
is applied to every row in $M_0$ and filter
function needs
to return a logical value to decide whether to keep or remove the current GO ID. For example, if the values in lt
are adjusted p-values, the filter
function
can be set as function(x) any(x < padj_cutoff)
so that the GO ID is kept as long as it is signfiicant in at least one list. After the filtering, let's call
the filtered matrix $M_1$.
GO IDs in $M_1$ (row names of $M_1$) are used for clustering. A heatmap of $M_1$ is attached to the left of the GO similarity heatmap so that the group-specific (or list-specific) patterns can be easily observed and to corresponded to GO functions.
Argument heatmap_param
controls several parameters for heatmap $M_1$:
transform
: A self-defined function to transform the data for heatmap visualization. The most typical case is to transform adjusted p-values by -log10(x)
.breaks
: Break values for color interpolation.col
: The corresponding values for breaks
.labels
: The corresponding labels for legend.name
: Legend title.sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.