---
title: "Enrichment Vignette"
author: "Michael T. Zimmermann"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Enrichment Vignette}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Introduction to RITAN

RITAN is an R package for Rapid Integration of Term Annotation and Network resources. It is to be applied to a list of gene symbols - for example, those identified by differential gene expression analysis.

The prevalence of gene expression analysis has led to the common outcome: Researchers identified a list of genes-of-interest and are wondering how they fit together. A common example would be a research group studying the effect of a treatment and identifying the differentially expressed genes between the treatment and control groups. Two logical questions are, "What functions are achieved by this group of genes?" and "How do these genes work together to achieve that function?" The RITAN package consolidates multiple annotation resources to help answer both questions.


# Quick Start

Our process of rapidly leveraging a comprehensive set of functional annotations begins from a list of prioritized genes.  RITAN is distributed with example genesets indexed by MSigDB. For this example we chose a study of influenza response under multiple conditions. In this vignette we will demonstrate analysis across multiple annotation resources. This can be achieved in one function call as exemplified in the following "quick start" example of RITAN's annotation capabilities. 

```{r first_load, echo=TRUE, warning=FALSE, results='hide', message=FALSE}
library(RITANdata)
library(RITAN)
```

Users may use RITAN to query multiple annotation resources simultaneously, and performing FDR-adjustment across the combined set:
```{r quick_start, echo=TRUE, warning=FALSE}
# We define a query using gene symbols
my_genes <- c('ABCA2','ACAT2','ACSS2','CD9','CPEB2','CTNNB1','FASN','LDLR','LPL','LSS')

# Now, you can rapidly get enrichment results by:
# e <- term_enrichment(my_genes)

# However, we will make two simplifications here, for expediency.
#  1) only look at two resources (of the many available)
#  2) use a cached background gene list of human protien coding genes
resources <- c("ReactomePathways", "MSigDB_Hallmarks")
e <- term_enrichment(my_genes, resources = resources, all_symbols = cached_coding_genes)
summary(e)
```

*Please cite each resource used in your analysis and register at [MSigDB](http://software.broadinstitute.org/gsea/msigdb/index.jsp) if you use their data. Doing so will help to ensure the future availability and extension of these valuable resources.*


# Full Example of Enrichment Analysis in RITAN

This version of RITAN is distributed with the following annotation resources:
```{r echo_geneset_list_names, echo=TRUE }
names(geneset_list)
```

Each resource is a list of gene symbols. For example:
```{r echo_geneset_list_names2, echo=TRUE }
head(geneset_list$MSigDB_C7$GSE9988_LPS_VS_LOW_LPS_MONOCYTE_UP)
```


We now demonstrate the typical use-case of term enrichment: Annotating a list of genes-of-interest by the biologic functions or pathways they participate in. Your genes-of-interest could come from many types of analysis, such as differential gene expression.

The "geneset_list" object included in the package, contains multiple annotation resources defined by the gene symbols from each resource. We select an example from a study of "Innate Immune responses to TREM-1 Activation" deposited in GEO under [GSE9988](www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE9988) and with the top deferentially expressed genes under each condition indexed by MSigDB. These MSigDB genesets are included within RITAN under the "C7" modules.

```{r make_study_selection, echo = TRUE }
selection <- grepl( 'GSE9988_(LOW_)*LPS_VS_.+UP', names(geneset_list$MSigDB_C7), perl=TRUE )
study_set <- geneset_list$MSigDB_C7[selection]
str(study_set)
```

From the above, we can see that 7 lists of gene symbols have been selected and that each is the result of comparing one treatment group to another and identifying the 200 most differentially expressed genes. The treatments used that we will consider in this example are LPS, Anti-TREM1 Antibody, Vehicle-only, and IgG Control. 

The default set of annotation resources consists of GO, Reactome Pathways, KEGG Filtered Canonical Pathways, and MSigDB Hallmarks. Thus, by default, a total of 17,724 functional terms and pathways will be considered by RITAN.

The function term_enrichment_by_subset() uses these annotation resources to evaluate enrichment simultaneously across each set of input gene lists. In this example those gene lists contain the genes differentially expressed between each condition. Note that we have loaded all 4 resources together, making the q-value adjustment across them. For brevity, we will consider a filtered set of Canonical Pathways and Hallmark functions.


```{r apply_enrichment_to_selection, echo=TRUE }
e <- term_enrichment_by_subset( study_set, q_value_threshold = 1e-5, 
                                resources = resources,
                                all_symbols = cached_coding_genes )
```

You can see that between these two resources, 188 pathways were tested for enrichment by RITAN. We identify the representation (or enrichment) of each, across the input study sets. After performing false discovery adjustment across the group, we save the significant associations (in this case, q < 10^-5^).

We next plot a heatmap of the enrichment results:


```{r ep1, echo=TRUE, fig.width = 7, fig.height = 6 }
plot( e, show_values = FALSE, label_size_y = 7, label_size_x = 7 )
```

The plot shows four of the study results with highly significant enrichment for "Hallmark of TNF-alpha signaling via NFKB." The enrichment is so strong that it dominates the shading of the heatmap as the shading scales by statistical significance; values shown are -log10(q-value). 

In order to make better use of the color scale, we set the "cap" parameter to 10 such that the q-values more significant than 1x10^-10^ are "capped" at a value of 1x10^-10^.  This results in a more informative visualization.

```{r ep1_cap, echo=TRUE, fig.width = 7, fig.height = 6 }
plot( e, show_values = FALSE, label_size_y = 7, label_size_x = 7, cap=10 )
```


Importantly, the full data is available to the user.
For simplicity, we print the data for the top 3 terms, but omit the column names as they are long and visible in the above plot.

```{r ep1_data, echo=TRUE, fig.width = 7, fig.height = 6 }
prmatrix( e[1:3,], collab = c('name','n.set',1:7) )
```


## Add an Annotation Matrix 
An annotation matrix for which gene list can facilitate a visual evaluation of relationships between sample or study conditions and enrichment. In this instance, we could parse the name of each geneset for information about it, but for simplicity, we manually construct the matrix.
```{r make_ann_mat, echo=TRUE }
mat <- matrix(c("LPS","LPS","LPS","LPS","LOW_LPS","LOW_LPS","LOW_LPS",
                "LOW_LPS","LPS_AND_ANTI_TREM1","CTRL_TREATED",
                "VEHICLE_TREATED","ANTI_TREM1_AND_LPS","CTRL_TREATED","VEHICLE_TREATED"),
                nrow = 2, byrow = TRUE )
rownames(mat) <- c('Condition1','Condition2')
colnames(mat) <- sprintf('Sample%s', 1:7)
print(mat)
```
```{r show_ann_mat, echo=TRUE, fig.width = 7, fig.height = 8 }
plot( e, show_values = TRUE, label_size_y = 7, label_size_x = 7, cap=10, 
      annotation_matrix = mat, grid_line_color = 'black' )
```

### Show the Number of Genes
While enrichment accounts for the size of each geneset and the number of genes selected, in generating a significance score, sometimes it is informative to know also the counts.

```{r apply_enrichment_to_selection_n, echo=TRUE }
n <- term_enrichment_by_subset( study_set, q_value_threshold = 1e-5,
                                resources = resources,
                                all_symbols = cached_coding_genes,
                                display_type = 'n', phred = FALSE )
```
```{r apply_enrichment_to_selection_n_plot, echo=TRUE, fig.width = 7, fig.height = 6 }
plot( n, show_values = TRUE, label_size_y = 7, label_size_x = 7, cap = 20 )
```

Thus, we generate a similar plot as previous, but now the number of genes overlapping each term or pathway is displayed.



# Term Enrichment

This function uses all terms within a set of resources, the default consisting of GO, ReactomePathways, KEGG_filtered_canonical_pathways, MSigDB_Hallmarks, to evaluate the level of overlap with an input vector of gene names. The output of the term_enrichment function is a data frame that contains information about how well the input gene symbols overlap with known annotation data sets. The first field in the data frame is the name of the known gene set. The second field is the hypergeomtric p value computed from the number of gene names in the test gene set that are also in the published gene set. The third field contains the number of genes names that overlap both the test gene set and the published gene set. The fourth field contains the number of genes in the published gene set. The fifth field gives the list of gene names in common between the two gene sets and the sixth field is a measure of the false discovery rate derived from the p value.

Input resources are either taken from our pre-loaded list of resources or an input GMT file. For an explanation of the GMT file format, see the [GSEA help page](http://www.broadinstitute.org/cancer/software/gsea/wiki/index.php/Data_formats)



```{r term_enrichment1, eval=FALSE }
data("vac1.day0vs31.de.genes")
te <- term_enrichment( geneset = vac1.day0vs31.de.genes )
```

The R object, geneset_list, included with the package contains 15 possible annotation annotation resources. They are: `r names(geneset_list)` .

The term_enrichment function can be modified to produce results from different gene sets by setting the "resources" parameter to one or more of these annotation resources.


# Term Enrichment Using Other Resources

The term_enrichment function can be modified to produce results from different gene sets by setting the "resources" parameter to one or more of the annotation resources available to RITAN.


```{r term_enrichment2, eval=FALSE }
e <- term_enrichment( geneset = vac1.day0vs31.de.genes, verbose = TRUE, 
                      resources = c("Blood_Translation_Modules", "MSigDB_C7") )
```

An efficient way to include all available resources would be:
```{r term_enrichment3, eval=FALSE }
e <- term_enrichment( geneset = vac1.day0vs31.de.genes, verbose = TRUE, 
                      resources = names(geneset_list) )
```

*However, one must be cautious about the appropriateness of doing so. Running enrichment across all available sources will provide a comprehensive view of what biologic functions your list of genes may influence, but it is likely that the terms across resources are related to one another. For instance, there are GO terms that closely correspond to certain pathway functions. The non-independence of terms could make false discovery adjustment too conservative. 
Point being: The broad stroke can be helpful, but the more focused analysis will likely be clearer and more effective.


# Term Enrichment for User-Input Resource

Below, we will load a user-defined resource generated from DisGeNet. DisGeNet is a resource of disease-gene relationships. We have arranged the DisGeNet data such that each disease name is treated as a "term." This way, we can query if our genes-of-interest are enriched for association with disease states.

As an example, we will use genes-of-interest from a previous study of the differential expression of influenza infection which has been deposited in MSigDB.

```{r term_enrichment_add_gmt, echo=TRUE}
gs  <- geneset_list$MSigDB_C7[['GSE6269_HEALTHY_VS_FLU_INF_PBMC_UP']]
gmt <- system.file("extdata", "curated_gene_disease_associations.gmt.gz", package="RITAN")

# -->> Not running here for brevity
# geneset_list$DisGeNet <- readGMT(gmt)
# str(head(geneset_list$DisGeNet))
```

The format of the gmt file is a simple tab-delimited style where the first column is the term name, the second is a reference to its origin, and all columns following are the genes associated with that term.

 Disease                      Source         Gene1  Gene2    Gene3...
--------                      ------         -----  ------  -------
Dermatitis, Allergic Contact	umls:C0162820  NAMPT  CALCRL	IGSF6
Hyperlipoproteinemia Type II	umls:C0020445  CETP   ABCA1	  EPHX2
Disease Progression	          umls:C0242656	 CDH2   ABCF2	  TRAP1
Lymphoma, Non-Hodgkin	        umls:C0024305	 CSF2   CSF3	  CYP2E1

Now, run the term enrichment for disease association.
```{r term_enrichment_provided_gmt, echo=TRUE}
e2  <- term_enrichment( gs, resources = gmt, all_symbols = cached_coding_genes )
print( e2[1:3,-5] )
```

From the default output which includes the top 5 most enriched terms, you can see that few terms are significantly enriched.

In order to better understand the resource and if it is a comprehensive definition of influenza infection, we can read in the GMT file and show the genes that define "Influenza" within DisGeNet:

```{r term_enrichment_search_gmt, echo=TRUE}
geneset_list$DisGeNet <- readGMT(gmt)
print( geneset_list$DisGeNet[['Influenza, Human']] )
```


As a quality control step, the size distribution of the active genesets can be visualized by using the show_active_genesets_hist() function. If the input file (.gmt) is improperly formatted, you may see that all terms have the same size, or that there are fewer terms loaded than expected.

```{r show_hist, echo=TRUE}
show_active_genesets_hist()
length(active_genesets)
```


# Term Enrichment for User-Defined Terms

From the above example of adding a new resource, you can see that the geneset formats used by RITAN are simple lists. Thus, if you have your own list of interest, it can be added to RITAN and used in enrichment tests alongside other resources such as GO. For example, if you wanted to define your own term for how low-density lipoprotein (LDL) is brought into cells:

```{r LDL, echo=TRUE}
geneset_list$LDL = list( LDL_import = c('APOB','APOE','LDLR'), 
                         LDL_processing = c('HMGR','ACAT2','HMGCS1',
                                            'HMGCR','MVD','MVK',
                                            'PMVK','IDI1','IDI2') )
```

There are now two terms within the "LDL" resource that can be used in term enrichment by:
```{r LDLe, eval=FALSE}
e <- term_enrichment( gs, resources = c('GO','LDL') )
```


# References
1.	Ashburner M, Ball CA, Blake JA, Botstein D, Butler H, Cherry JM, Davis AP, Dolinski K, Dwight SS, Eppig JT et al: Gene ontology: tool for the unification of biology. The Gene Ontology Consortium. Nat Genet 2000, 25(1):25-29.

2.	Schaefer CF, Anthony K, Krupa S, Buchoff J, Day M, Hannay T, Buetow KH: PID: the Pathway Interaction Database. Nucleic Acids Res 2009, 37(Database issue):D674-679.

3.	Kanehisa M, Goto S: KEGG: kyoto encyclopedia of genes and genomes. Nucleic Acids Res 2000, 28(1):27-30.

4.	Kanehisa M, Goto S, Sato Y, Furumichi M, Tanabe M: KEGG for integration and interpretation of large-scale molecular data sets. Nucleic Acids Res 2012, 40(Database issue):D109-114.

5.	Croft D, O'Kelly G, Wu G, Haw R, Gillespie M, Matthews L, Caudy M, Garapati P, Gopinath G, Jassal B et al: Reactome: a database of reactions, pathways and biological processes. Nucleic Acids Res 2011, 39(Database issue):D691-697.

6.	Cerami EG, Gross BE, Demir E, Rodchenkov I, Babur O, Anwar N, Schultz N, Bader GD, Sander C: Pathway Commons, a web resource for biological pathway data. Nucleic Acids Res 2011, 39(Database issue):D685-690.

7.	Subramanian A, Tamayo P, Mootha VK, Mukherjee S, Ebert BL, Gillette MA, Paulovich A, Pomeroy SL, Golub TR, Lander ES et al: Gene set enrichment analysis: a knowledge-based approach for interpreting genome-wide expression profiles. Proc Natl Acad Sci U S A 2005, 102(43):15545-15550.

8.	Chaussabel D, Quinn C, Shen J, Patel P, Glaser C, Baldwin N, Stichweh D, Blankenship D, Li L, Munagala I et al: A modular analysis framework for blood genomics studies: application to systemic lupus erythematosus. Immunity 2008, 29(1):150-164.

9.	Li S, Rouphael N, Duraisingham S, Romero-Steiner S, Presnell S, Davis C, Schmidt DS, Johnson SE, Milton A, Rajam G et al: Molecular signatures of antibody responses derived from a systems biology study of five human vaccines. Nat Immunol 2014, 15(2):195-204.

10.	Kandasamy K, Mohan SS, Raju R, Keerthikumar S, Kumar GS, Venugopal AK, Telikicherla D, Navarro JD, Mathivanan S, Pecquet C et al: NetPath: a public resource of curated signal transduction pathways. Genome Biol 2010, 11(1):R3.

