---
title: "Functional enrichment analysis of high-throughput omics data"
author: |
 | Ludwig Geistlinger and Levi Waldron
 | <small>School of Public Health, City University of New York, New York</small>
date: "July 27, 2017"
abstract: "Overview of existing methods for enrichment analysis of gene 
expression data with regard to functional gene sets, pathways, and networks.
Functionality for differential expression analysis, set- and network-based
enrichment analysis, along with visualization and exploration of results.
Introduction to concepts for enrichment analysis of genomic regions and 
regulatory elements. Outlook on current developments to extend gene set 
enrichment analysis for data derived from multiple high-throughput omics assays."
email:
output:
  BiocStyle::html_document:
    toc: true
    toc_depth: 2
vignette: >
  % \VignetteIndexEntry{Functional enrichment analysis of high-throughput omics data}
  % \VignetteEngine{knitr::rmarkdown}
---

```{r style, echo = FALSE, results = 'asis'}
options(width=100)
knitr::opts_chunk$set(fig.align = "center")
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(cache=TRUE)
```

```{r setup, echo=FALSE}
suppressMessages(suppressWarnings(suppressPackageStartupMessages({
library(EnrichmentBrowser)
library(BiocStyle)
library(ALL)
library(hgu95av2.db)
library(airway)
library(regioneR)
library(MultiAssayExperiment)
library(mogsa)
})))
```
- Required packages: [EnrichmentBrowser][], [ALL][], [hgu95av2.db][], [airway][],
[regioneR][], [BSgenome.Hsapiens.UCSC.hg19.masked][], [MultiAssayExperiment][], 
[readr][], [mogsa][], [limma][], [BiocStyle][], [knitr][], [rmarkdown][]

# Where does it all come from?

Test whether known biological functions or processes are over-represented
(= enriched) in an experimentally-derived gene list, e.g. a list of
differentially expressed (DE) genes. See
[Goeman and Buehlmann, 2007](https://doi.org/10.1093/bioinformatics/btm051) for
a critical review.

Example: Transcriptomic study, in which 12,671 genes have been tested for
differential expression between two sample conditions and 529 genes were found
DE.

Among the DE genes, 28 are annotated to a specific functional gene set, which
contains in total 170 genes. This setup corresponds to a 2x2 contingency table,

```{r deTbl}
deTable <-
     matrix(c(28, 142, 501, 12000),
            nrow = 2,
            dimnames = list(c("DE", "Not.DE"),
                            c("In.gene.set", "Not.in.gene.set")))
deTable
```

where the overlap of 28 genes can be assessed based on the hypergeometric distribution.
This corresponds to a one-sided version of Fisher's exact test, yielding here a
significant enrichment.

```{r fisher}
fisher.test(deTable, alternative = "greater")
```

This basic principle is at the foundation of major public and commercial enrichment
tools such as [DAVID](https://david.ncifcrf.gov/) and 
[Pathway Studio](https://www.pathwaystudio.com).

Although gene set enrichment methods have been primarily developed and applied
on transcriptomic data, they have recently been modified, extended and applied
also in other fields of genomic and biomedical research. This includes novel
approaches for functional enrichment analysis of proteomic and metabolomic data
as well as genomic regions and disease phenotypes,
[Lavallee and Yates, 2016](https://doi.org/10.1002/0471250953.bi1328s53),
[Chagoyen et al., 2016](https://doi.org/10.1007/978-1-4939-3572-7_20),
[McLean et al., 2010](https://doi.org/10.1038/nbt.1630),
[Ried et al., 2012](https://doi.org/10.1002/gepi.21617).


# Gene expression-based enrichment analysis

The first part of the workshop is largely based on the
`r Biocpkg("EnrichmentBrowser")` package, which implements an analysis pipeline
for high-throughput gene expression data as measured with microarrays and
RNA-seq. In a workflow-like manner, the package brings together a selection of
established Bioconductor packages for gene expression data analysis. It
integrates a wide range of gene set enrichment analysis methods and facilitates
combination and exploration of results across methods.

```{r loadEBrowser}
library(EnrichmentBrowser)
```

Further information can be found in the 
[vignette](http://www.bioconductor.org/packages/release/bioc/vignettes/EnrichmentBrowser/inst/doc/EnrichmentBrowser.pdf)
and [publication](https://doi.org/10.1186/s12859-016-0884-1).

## A primer on terminology, existing methods & statistical theory

**Gene sets, pathways & regulatory networks**

Gene sets are simple lists of usually functionally related genes without further
specification of relationships between genes.

Pathways can be interpreted as specific gene sets, typically representing a
group of genes that 
work together in a biological process. Pathways are commonly divided in 
metabolic and signaling pathways.
Metabolic pathways such as glycolysis represent biochemical substrate conversions
by specific enzymes. Signaling pathways such as the MAPK signaling pathway describe 
signal transduction cascades from receptor proteins to transcription factors, 
resulting in activation or inhibition of specific target genes.

Gene regulatory networks describe the interplay and effects of regulatory
factors (such as transcription factors and microRNAs) on the expression of their
target genes.

**Resources**

[GO](http://www.geneontology.org) and [KEGG](http://www.genome.jp/kegg) 
annotations are most frequently used for the enrichment analysis of 
functional gene sets. Despite an increasing number of gene set and pathway 
databases, they are typically the first choice due to their long-standing 
curation and availability for a wide range of species.

*GO*: The Gene Ontology (GO) consists of three major sub-ontologies that classify
gene products according to molecular function (MF), biological process (BP) and 
cellular component (CC). Each ontology consists of GO terms that define MFs, BPs
or CCs to which specific genes are annotated. The terms are organized in a
directed acyclic graph, where edges between the terms represent
relationships of different types. They relate the terms according to a parent-child
scheme, i.e. parent terms denote more general entities, whereas child terms represent
more specific entities.

*KEGG*: The Kyoto Encyclopedia of Genes and Genomes (KEGG) is a collection of 
manually drawn pathway maps representing molecular interaction and reaction networks.
These pathways cover a wide range of biochemical processes that can be divided in
7 broad categories: metabolism, genetic and environmental information processing,
cellular processes, organismal systems, human diseases, and drug development. 
Metabolism and drug development pathways differ from pathways of the other 5
categories by illustrating reactions between chemical compounds.
Pathways of the other 5 categories illustrate molecular interactions between
genes and gene products. 

**Gene set analysis vs. gene set enrichment analysis**

The two predominantly used enrichment methods are:

- Overrepresentation analysis (ORA), testing whether a gene set contains 
disproportional many genes of significant expression change, based on the 
procedure outlined in the first section
- Gene set enrichment analysis (GSEA), testing whether genes of a gene set 
accumulate at the top or bottom of the full gene vector ordered by direction
and magnitude of expression change
[Subramanian et al., 2005](https://doi.org/10.1073/pnas.0506580102)

However, the term *gene set enrichment analysis* nowadays subsumes a general 
strategy implemented by a wide range of methods 
[Huang et al., 2009](https://doi.org/10.1093/nar/gkn923). 
Those methods have in common the same goal, although approach and statistical
model can vary substantially
[Goeman and Buehlmann, 2007](https://doi.org/10.1093/bioinformatics/btm051),
[Khatri et al., 2012](https://doi.org/10.1371/journal.pcbi.1002375).

To better distinguish from the specific method, some authors use the term
*gene set analysis* to denote the general strategy.
However, there is also a specific method from
[Efron and Tibshirani, 2007](https://doi.org/10.1214/07-AOAS101) of this name.

**Underlying null: competitive vs. self-contained**

[Goeman and Buehlmann, 2007](https://doi.org/10.1093/bioinformatics/btm051)
classified existing enrichment methods into *competitive* and *self-contained* 
based on the underlying null hypothesis.

- *Competitive* null hypothesis: the genes in the set of interest are at most as
often DE as the genes not in the set, 
- *Self-contained* null hypothesis: no genes in the set of interest are DE.

Although the authors argue that a self-contained null is closer to the actual 
question of interest, the vast majority of enrichment methods is competitive.

Goeman and Buehlmann further raise several critical issues concerning the 2x2 ORA:

- rather arbitrary classification of genes in DE / not DE
- based on gene sampling, although sampling of subjects is appropriate 
- unrealistic independence assumption between genes, resulting in highly
anti-conservative *p*-values

With regard to these statistical concerns, GSEA is considered superior:

- takes all measured genes into account 
- subject sampling via permutation of class labels  
- the incorporated permutation procedure implicitly accounts for correlations
between genes

However, the simplicity and general applicability of ORA is unmet by subsequent
methods improving on these issues. For instance, GSEA requires the expression data
as input, which is not available for gene lists derived from other experiment types.
On the other hand, the involved sample permutation procedure has been proven 
inaccurate and time-consuming
[Efron and Tibshirani, 2007](https://doi.org/10.1214/07-AOAS101),
[Phipson and Smyth, 2010](https://doi.org/10.2202/1544-6115.1585),
[Larson and Owen, 2015](https://doi.org/10.1186/s12859-015-0571-7).

**Generations: ora, fcs & topology-based**

[Khatri et al., 2012](https://doi.org/10.1371/journal.pcbi.1002375) have taken a
slightly different approach by classifying methods along the timeline of 
development into three generations:

1. Generation: ORA methods based on the 2x2 contingency table test,
2. Generation: functional class scoring (FCS) methods such as GSEA, which compute
gene set (= functional class) scores by summarizing per-gene DE statistics,
3. Generation: topology-based methods, explicitly taking into account interactions
between genes as defined in signaling pathways and gene regulatory networks
([Geistlinger et al., 2011](https://doi.org/10.1093/bioinformatics/btr228) for an example).

Although topology-based (also: network-based) methods appear to be most realistic,
their straightforward application can be impaired by features that are not-detectable
on the transcriptional level (such as protein-protein interactions) and insufficient network knowledge
[Geistlinger et al., 2013](https://doi.org/10.1093/nar/gkt631),
[Bayerlova et al., 2015](https://doi.org/10.1186/s12859-015-0751-5).

Given the individual benefits and limitations of existing methods,
cautious interpretation of results is required to derive valid conclusions. 
Whereas no single method is best suited for all application scenarios, applying
multiple methods can be beneficial.
This has been shown to filter out spurious hits of individual methods, thereby
reducing the outcome to gene sets accumulating evidence from different methods
[Geistlinger et al., 2016](https://doi.org/10.1186/s12859-016-0884-1),
[Alhamdoosh et al., 2017](https://doi.org/10.1093/bioinformatics/btw623).

## Data types

Although RNA-seq (read count data) has become the *de facto* standard for
transcriptomic profiling, it is important to know that many methods for
differential expression and gene set enrichment analysis have been originally
developed for microarray data (intensity measurements).

However, differences in data distribution assumptions (microarray: quasi-normal,
RNA-seq: negative binomial) made adaptations in differential expression analysis
and, to some extent, also in gene set enrichment analysis necessary.

Thus, we consider two example datasets - a microarray and a RNA-seq dataset,
and discuss similarities and differences of the respective analysis steps. 

For microarray data, we consider expression measurements of patients with acute
lymphoblastic leukemia
[Chiaretti et al., 2004](https://doi.org/10.1182/blood-2003-09-3243). A
frequent chromosomal defect found among these patients is a translocation, in
which parts of chromosome 9 and 22 swap places. This results in the oncogenic
fusion gene BCR/ABL created by positioning the ABL1 gene on chromosome 9 to a
part of the BCR gene on chromosome 22.

We load the `r Biocpkg("ALL")` dataset

```{r loadALL}
library(ALL)
data(ALL)
```

and select B-cell ALL patients with and without the BCR/ABL fusion, as described previously
[Gentleman et al., 2005](https://www.bioconductor.org/help/publications/books/bioinformatics-and-computational-biology-solutions).

```{r subsetALL}
ind.bs <- grep("^B", ALL$BT)
ind.mut <- which(ALL$mol.biol %in% c("BCR/ABL", "NEG"))
sset <- intersect(ind.bs, ind.mut)
all.eset <- ALL[, sset]
```

We can now access the expression values, which are intensity measurements
on a log-scale for 12,625 probes (rows) across 79 patients (columns).

```{r showALL}
dim(all.eset)
exprs(all.eset)[1:4,1:4]
```

As we often have more than one probe per gene, we compute gene expression values
as the average of the corresponding probe values.

```{r probe2gene}
all.eset <- probe.2.gene.eset(all.eset) 
head(featureNames(all.eset))
```

For RNA-seq data, we consider transcriptome profiles of four primary human
airway smooth muscle cell lines in two conditions: control and treatment with
dexamethasone
[Himes et al., 2014](https://doi.org/10.1371/journal.pone.0099625).

We load the `r Biocpkg("airway")` dataset 

```{r loadAirway}
library(airway)
data(airway)
```
and create an `Biobase::ExpressionSet`.
```{r processAirway}
air.eset <- as(airway, "ExpressionSet")
annotation(air.eset) <- "hsa"
```
Note: we are switching here from `SummarizedExperiment` to `ExpressionSet` as 
functionality in the EnrichmentBrowser is explicitly designed for gene-level 
expression data, for which `ExpressionSet` can be consistently used.
 
For further analysis, we only keep genes that are annotated to an ENSEMBL gene ID.
```{r processAirway2}
air.eset <- air.eset[grep("^ENSG", rownames(air.eset)), ]
dim(air.eset)
exprs(air.eset)[1:4,1:4]
```

## Differential expression analysis

Normalization of high-throughput expression data is essential to make results
within and between experiments comparable. Microarray (intensity measurements)
and RNA-seq (read counts) data typically show distinct features that need to be
normalized for. As this is beyond the scope of this workshop, we refer to
`r Biocpkg("limma")` for microarray normalization and `r Biocpkg("EDASeq")` for
RNA-seq normalization. See also `EnrichmentBrowser::normalize`, which wraps
commonly used functionality for normalization.

The EnrichmentBrowser incorporates established functionality from the 
`r Biocpkg("limma")` package for differential expression analysis.
This involves the `voom` transformation when applied to RNA-seq data. 
Alternatively, differential expression analysis for RNA-seq data can also be
carried out based on the negative binomial distribution with `r Biocpkg("edgeR")`
and `r Biocpkg("DESeq2")`.

This can be performed using the function `EnrichmentBrowser::de.ana`
and assumes some standardized variable names:
 
- **GROUP** defines the sample groups being contrasted,
- **BLOCK** defines paired samples or sample blocks, as e.g. for batch effects.

For more information on experimental design, see the
[limma user's guide](https://www.bioconductor.org/packages/devel/bioc/vignettes/limma/inst/doc/usersguide.pdf),
chapter 9.  

For the ALL dataset, the **GROUP** variable indicates whether the BCR-ABL gene
fusion is present (1) or not (0).
```{r pdataALL}
pData(all.eset)$GROUP <- ifelse(all.eset$mol.biol == "BCR/ABL", 1, 0)
table(pData(all.eset)$GROUP)
```
For the airway dataset, it indicates whether the cell lines have been treated
with dexamethasone (1) or not (0).

```{r pdataAirway}
pData(air.eset)$GROUP <- ifelse(colData(airway)$dex == "trt", 1, 0)
table(pData(air.eset)$GROUP)
```
Paired samples, or in general sample batches/blocks, can be defined via a
`BLOCK` column in the `pData` slot. For the airway dataset, the sample blocks
correspond to the four different cell lines.

```{r pdataAirway2}
pData(air.eset)$BLOCK <- colData(airway)$cell
table(pData(air.eset)$BLOCK)
```

For microarray data, the `EnrichmentBrowser::de.ana` function carries out
differential expression analysis based on functionality from the
`r Biocpkg("limma")` package. Resulting log2 fold changes and *t*-test derived
*p*-values for each gene are appended to the `fData` slot.

```{r deALL}
all.eset <- de.ana(all.eset)
head(fData(all.eset), n=4)
```

Nominal *p*-values are already corrected for multiple testing (`ADJ.PVAL`)
using the method from Benjamini and Hochberg implemented in `stats::p.adjust`.

For RNA-seq data, the `de.ana` function can be used to carry out differential 
expression analysis between the two groups either based on functionality from
`r Biocpkg("limma")` (that includes the `voom` transformation), or
alternatively, the frequently used `r Biocpkg("edgeR")` or `r Biocpkg("DESeq2")`
package. Here, we use the analysis based on `r Biocpkg("edgeR")`.

```{r deAirway}
air.eset <- de.ana(air.eset, de.method="edgeR")
head(fData(air.eset), n=4)
```

## Gene sets

We are now interested in whether pre-defined sets of genes that are known to 
work together, e.g. as defined in the [Gene Ontology](http://www.geneontology.org) 
or the [KEGG](http://www.genome.jp/kegg) pathway annotation, are coordinately 
differentially expressed. The function `get.kegg.genesets` downloads all KEGG 
pathways for a chosen organism (here: *Homo sapiens*) as gene sets using NCBI 
Entrez Gene IDs.

```{r keggGS, eval=FALSE}
kegg.gs <- get.kegg.genesets("hsa")
```

Analogously, the function `get.go.genesets` retrieves GO terms of a selected
ontology (here: biological process, BP) as defined in the `r Biocpkg("GO.db")`
annotation package.

```{r goGS, eval=FALSE}
go.gs <- get.go.genesets(org="hsa", onto="BP", mode="GO.db")
```

User-defined gene sets can be parsed from GMT file format.
Here, we are using this functionality for reading a list of already downloaded
KEGG gene sets for *Homo sapiens* containing NCBI Entrez Gene IDs.

```{r udefGS}
data.dir <- system.file("extdata", package="EnrichmentBrowser")
gmt.file <- file.path(data.dir, "hsa_kegg_gs.gmt")
hsa.gs <- parse.genesets.from.GMT(gmt.file)
length(hsa.gs)
hsa.gs[1:2]
```

See also the [MSigDb](http://software.broadinstitute.org/gsea/msigdb) for
additional gene set collections.

## GO/KEGG overrepresentation analysis

A variety of gene set analysis methods have been proposed
[Khatri et al., 2012](https://doi.org/10.1371/journal.pcbi.1002375).
The most basic, yet frequently used, method is the over-representation analysis
(ORA) with gene sets defined according to GO or KEGG.
As outlined in the first section, ORA tests the overlap between DE genes
(typically DE *p*-value < 0.05) and genes in a gene set based on the 
hypergeometric distribution.
Here, we choose a significance level $\alpha = 0.2$ for demonstration.    

```{r oraALL}
ora.all <- sbea(method="ora", eset=all.eset, gs=hsa.gs, perm=0, alpha=0.2)
gs.ranking(ora.all)
```

Such a ranked list is the standard output of most existing enrichment tools.
Using the `ea.browse` function creates a HTML summary from which each
gene set can be inspected in more detail.

```{r browseEA, eval=FALSE}
ea.browse(ora.all)
```
<!-- TODO: complex heatmap group colors -->
The resulting summary page includes for each significant gene set

- a gene report, which lists all genes of a set along with fold change and DE
$p$-value (click on links in column `NR.GENES`),
- interactive overview plots such as heatmap and volcano plot (column
`SET.VIEW`, supports mouse-over and click-on),
- for KEGG pathways: highlighting of differentially expressed genes on the
pathway maps (column `PATH.VIEW`, supports mouse-over and click-on).

As ORA works on the list of DE genes and not the actual expression values, it
can be straightforward applied to RNA-seq data. However, as the gene sets here
contain NCBI Entrez gene IDs and the airway dataset contains ENSEMBL gene ids,
we first map the airway dataset to Entrez IDs.  

```{r mapIDs}
air.eset <- map.ids(air.eset, org="hsa", from="ENSEMBL", to="ENTREZID")
ora.air <- sbea(method="ora", eset=air.eset, gs=hsa.gs, perm=0)
gs.ranking(ora.air)
```

Note #1: [Young et al., 2010](https://doi.org/10.1186/gb-2010-11-2-r14), have
reported biased results for ORA on RNA-seq data due to over-detection of
differential expression for long and highly expressed transcripts. The
`r Biocpkg("goseq")` package and `limma::goana` implement possibilities to
adjust ORA for gene length and abundance bias.

Note #2: Independent of the expression data type under investigation, overlap
between gene sets can result in redundant findings. This is well-documented for
GO (parent-child structure,
[Rhee et al., 2008](https://doi.org/10.1038/nrg2363)) and KEGG (pathway
overlap/crosstalk,
[Donato et al., 2013](https://doi.org/10.1101/gr.153551.112)). The
`r Biocpkg("topGO")` package (explicitly designed for GO) and
`r Biocpkg("mgsa")` (applicable to arbitrary gene set definitions) implement
modifications of ORA to account for such redundancies.

## Functional class scoring & permutation testing

A major limitation of ORA is that it restricts analysis to DE genes, excluding
genes not satisfying the chosen significance threshold (typically the vast
majority).

This is resolved by gene set enrichment analysis (GSEA), which scores the
tendency of gene set members to appear rather at the top or bottom of the
ranked list of all measured genes
[Subramanian et al., 2005](https://doi.org/10.1073/pnas.0506580102). The
statistical significance of the enrichment score (ES) of a gene set is assessed
via sample permutation, i.e. (1) sample labels (= group assignment) are
shuffled, (2) per-gene DE statistics are recomputed, and (3) the enrichment
score is recomputed. Repeating this procedure many times allows to determine
the empirical distribution of the enrichment score and to compare the observed
enrichment score against it. Here, we carry out GSEA with 1000 permutations.

```{r gseaALL}
gsea.all <- sbea(method="gsea", eset=all.eset, gs=hsa.gs, perm=1000)  
gs.ranking(gsea.all)
```

As GSEA's permutation procedure involves re-computation of per-gene DE
statistics, adaptations are necessary for RNA-seq. The EnrichmentBrowser
implements an accordingly adapted version of GSEA, which allows incorporation
of limma/voom, edgeR, or DESeq2 for repeated DE re-computation within GSEA.
However, this is computationally intensive (for limma/voom the least, for
DESeq2 the most). Note the relatively long running times for only 100
permutations having used edgeR for DE analysis.

```{r gseaAir, eval=FALSE}
gsea.air <- sbea(method="gsea", eset=air.eset, gs=hsa.gs, perm=100)  
```

While it might be in some cases necessary to apply permutation-based GSEA for
RNA-seq data, there are also alternatives avoiding permutation. Among them is
ROtAtion gene Set Testing (ROAST), which uses rotation instead of permutation
[Wu et al., 2010](https://doi.org/10.1093/bioinformatics/btq401).   

```{r roastAir}
roast.air <- sbea(method="roast", eset=air.eset, gs=hsa.gs)
gs.ranking(roast.air)  
```

A selection of additional methods is also available:

```{r sbeaM}
sbea.methods()
```

## Network-based enrichment analysis
Having found gene sets that show enrichment for differential expression, 
we are now interested whether these findings can be supported by known
regulatory interactions. 

For example, we want to know whether transcription factors and their target
genes are expressed in accordance to the connecting regulations
(activation/inhibition). 
Such information is usually given in a gene regulatory network derived from
specific experiments or compiled from the literature
([Geistlinger et al., 2013](https://doi.org/10.1093/nar/gkt631) for an example).

There are well-studied processes and organisms for which comprehensive and
well-annotated regulatory networks are available, e.g. the
[RegulonDB](http://regulondb.ccg.unam.mx) for *E. coli* and
[Yeastract](http://www.yeastract.com) for *S. cerevisiae*. 

However, there are also cases where such a network is missing or at least
incomplete.
A basic workaround is to compile a network from regulations in the KEGG
database.

We can download all KEGG pathways of a specified organism (here: *Homo sapiens*)
via

```{r dwnldKegg, eval=FALSE}
pwys <- download.kegg.pathways("hsa")
```

For demonstration purposes, we use a selection of already downloaded human KEGG
pathways.

```{r compGRN}
pwys <- file.path(data.dir, "hsa_kegg_pwys.zip")
hsa.grn <- compile.grn.from.kegg(pwys)
head(hsa.grn)
```

Signaling pathway impact analysis (SPIA) is a network-based enrichment analysis
method, which is explicitly designed for KEGG signaling pathways
[Tarca et al., 2009](https://doi.org/ 10.1093/bioinformatics/btn577). The
method evaluates whether expression changes are propagated across the pathway
topology in combination with ORA.

```{r spiaALL, eval=FALSE}
spia.all <- nbea(method="spia", eset=all.eset, gs=hsa.gs, grn=hsa.grn, alpha=0.2)
gs.ranking(spia.all)
```

More generally applicable is gene graph enrichment analysis (GGEA), which
evaluates consistency of interactions in a given gene regulatory network with
the observed expression data
[Geistlinger et al., 2011](https://doi.org/10.1093/bioinformatics/btr228). 

```{r ggeaALL}
ggea.all <- nbea(method="ggea", eset=all.eset, gs=hsa.gs, grn=hsa.grn)
gs.ranking(ggea.all)
```

A selection of additional network-based methods is also available:

```{r nbeaM}
nbea.methods()
```

Note #1: As network-based enrichment methods typically do not involve sample
permutation but rather network permutation, 
thus avoiding DE re-computation, they can likewise be applied to RNA-seq data.

Note #2: Given the various enrichment methods with individual benefits and
limitations, combining multiple methods can be beneficial, e.g. combined
application of a set-based and a network-based method. This has been shown to
filter out spurious hits of individual methods and to reduce the outcome to
gene sets accumulating evidence from different methods
[Geistlinger et al., 2016](https://doi.org/10.1186/s12859-016-0884-1),
[Alhamdoosh et al., 2017](https://doi.org/10.1093/bioinformatics/btw623). 

The function `comb.ea.results` implements the straightforward combination of
results, thereby facilitating seamless comparison of results across methods.
For demonstration, we use the ORA and GSEA results for the ALL dataset from the 
previous section: 

```{r combEA}
res.list <- list(ora.all, gsea.all)
comb.res <- comb.ea.results(res.list)
gs.ranking(comb.res)
```

# Genomic region enrichment analysis

Microarrays and next-generation sequencing are also widely applied for
large-scale detection of variable and regulatory genomic regions, e.g. single
nucleotide polymorphisms, copy number variations, and transcription factor
binding sites. 

Such experimentally-derived genomic region sets are raising similar questions
regarding functional enrichment as in gene expression data analysis. 

Of particular interest is thereby whether experimentally-derived regions
overlap more (enrichment) or less (depletion) than expected by chance with
regions representing known functional features such as genes or
promoters. 

The `r Biocpkg("regioneR")` package implements a general framework for testing
overlaps of genomic regions based on permutation sampling.
This allows to repeatedly sample random regions from the genome, matching size
and chromosomal distribution of the region set under study.
By recomputing the overlap with the functional features in each permutation,
statistical significance of the observed overlap can be assessed. 

```{r loadRegioneR}
library(regioneR)
```

To demonstrate the basic functionality of the package, we consider the overlap
of gene promoter regions and CpG islands in the human genome. We expect to find
an enrichment as promoter regions are known to be GC-rich. Hence, is the
overlap between CpG islands and promoters greater than expected by
chance?

We use the collection of CpG islands described in
[Wu et al., 2010](https://doi.org/10.1093/biostatistics/kxq005) and restrict
them to the set of canonical chromosomes 1-23, *X*, and *Y*.

```{r loadCPGs}
cpgHMM <- toGRanges("http://www.haowulab.org/software/makeCGI/model-based-cpg-islands-hg19.txt")
cpgHMM <- filterChromosomes(cpgHMM, chr.type="canonical")
cpgHMM <- sort(cpgHMM)
cpgHMM
```

Analogously, we load promoter regions in the *hg19* human genome assembly as
available from [UCSC](https://genome.ucsc.edu/):

```{r loadProms}
promoters <- toGRanges("http://gattaca.imppc.org/regioner/data/UCSC.promoters.hg19.bed")
promoters <- filterChromosomes(promoters, chr.type="canonical")
promoters <- sort(promoters)
promoters
```

To speed up the example, we restrict analysis to chromosomes 21 and 22. Note
that this is done for demonstration only. To make an accurate claim, the
complete region set should be used (which, however, runs considerably longer).

```{r subsetCPGProms}
cpg <- cpgHMM[seqnames(cpgHMM) %in% c("chr21", "chr22")]
prom <- promoters[seqnames(promoters) %in% c("chr21", "chr22")]
```

Now, we are applying an overlap permutation test with 100 permutations
(`ntimes=100`), while maintaining chromosomal distribution of the CpG island
region set (`per.chromosome=TRUE`). Furthermore, we use the option
`count.once=TRUE` to count an overlapping CpG island only once, even if it
overlaps with 2 or more promoters. This takes about 2 minutes on a standard
laptop.

```{r permTest}
pt <- overlapPermTest(cpg, prom, genome="hg19", ntimes=100, per.chromosome=TRUE, count.once=TRUE)
pt
summary(pt[[1]]$permuted)
```

The resulting permutation *p*-value indicates a significant enrichment. Out of
the `r length(cpg)` CpG islands, `r pt[[1]]$observed` overlap with at least one
promoter. In contrast, when repeatedly drawing random regions matching the CpG
islands in size and chromosomal distribution, the mean number of overlapping
regions across permutations was `r round(mean(pt[[1]]$permuted), digits=1)`
$\pm$ `r round(sd(pt[[1]]$permuted), digits=1)`. 

Note #1: The function `regioneR::permTest` allows to incorporate user-defined
functions for randomizing regions and evaluating additional measures of overlap
such as total genomic size in bp.

Note #2: The `r Biocpkg("LOLA")` package implements a genomic region ORA, which
assesses genomic region overlap based on the hypergeometric distribution using
a library of pre-defined functional region sets.

# Multi-omics enrichment analysis

Multi-omics experiments are increasingly commonplace in biomedical research as
e.g. apparent in recent large-scale projects such as
[ENCODE](https://www.encodeproject.org) and
[TCGA](https://cancergenome.nih.gov). Such experiments are composed of multiple
complementary data types for a set of samples, thereby adding layers of
complexity to experimental design, data integration, and analysis. In
Bioconductor, the `r Biocpkg("MultiAssayExperiment")` package provides data
structures and methods for representing, manipulating, and integrating
multi-assay genomic experiments.
See the MultiAssayExperiment
[workshop](https://github.com/waldronlab/MultiAssayExperimentWorkshop)
for an introduction. 

```{r loadMAE}
library(MultiAssayExperiment)
```

Here, we consider an example dataset featuring ovarian cancer samples from TCGA.

```{r loadOVMAE}
data.dir <- system.file("extdata", package="enrichOmics")
mae.file <-  file.path(data.dir, "tcga_ov_mae.rds")
mae <- readr::read_rds(mae.file)
mae
```

The dataset contains data for RNA-seq, reverse phase protein array, and
[gistic](https://doi.org/10.1073/pnas.0710052104)
copy number variation.
Sample data is available on BRCA mutation and expression-based subtype
classification.

```{r coldataOVMAE}
head(colData(mae))
```

We first make sure that rows (genes/proteins) and columns (patients) are 
matching between the three assays:

```{r matchOVMAE}
intersectRows(mae)
intersectColumns(mae)
```

Despite the availability of multi-omics data resources, bioinformatic and
statistical methods for the analysis of such experiments are still nascent.
This also holds for multi-omics enrichment analysis, where ongoing developments
are anticipated to produce novel methods for this purpose.
Here, we consider the `r Biocpkg("mogsa")` package, which uses multivariate
extensions of principal component analysis (PCA) to project the data onto a
lower dimensional space so that relationships between datasets can be
identified.

```{r mogsa}
library(mogsa)
assayMats <- assays(mae)
annotSup <- prepSupMoa(as.list(assayMats), geneSets = hsa.gs)
ov.moa <- moa(assayMats, proc.row = "center_ssq1", w.data = "inertia")
smoa <- sup.moa(ov.moa, sup=annotSup, nf=5)
score.moa <- slot(smoa, "score")
rownames(score.moa) <- substring(rownames(score.moa),1,8)
colnames(score.moa) <- substring(colnames(score.moa),1,12)
score.moa[,1:5]
```

This produces individual enrichment scores for each sample.
We use the `r Biocpkg("limma")` for comparison of sample groups, analogous to 
differential expression analysis.
Here, we define the groups according to presence of the BRCA mutation.

```{r limmaMOGSA}
library(limma)
group <- colData(mae)[,"BRCA_Mutation"]
design <- model.matrix(~group)
fit <- lmFit(score.moa, design)
fit <- eBayes(fit)
topTable(fit, number=nrow(score.moa), coef="groupPresent")
``` 

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

[EnrichmentBrowser]: https://bioconductor.org/packages/EnrichmentBrowser
[ALL]: https://bioconductor.org/packages/ALL
[hgu95av2.db]: https://bioconductor.org/packages/hgu95av2.db
[airway]: https://bioconductor.org/packages/airway
[regioneR]: https://bioconductor.org/packages/regioneR
[BSgenome.Hsapiens.UCSC.hg19.masked]: https://bioconductor.org/packages/BSgenome.Hsapiens.UCSC.hg19.masked
[MultiAssayExperiment]: https://bioconductor.org/packages/MultiAssayExperiment
[readr]: https://cran.r-project.org/package=readr
[mogsa]: https://bioconductor.org/packages/mogsa
[limma]: https://bioconductor.org/packages/limma
[BiocStyle]: https://bioconductor.org/packages/BiocStyle
[knitr]: https://cran.r-project.org/package=knitr
[rmarkdown]: https://cran.r-project.org/package=rmarkdown
