---
title: "An R interface to the Enrichr database"
author: "Wajid Jawaid"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
bibliography: enrichr.bib
vignette: >
  %\VignetteIndexEntry{An R interface to the Enrichr database}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Installation

**enrichR** can be installed from Github or soon from CRAN.

## Github

```{r, echo = FALSE, eval = TRUE}
websiteLive <- TRUE
```

```{r, echo = TRUE, eval = FALSE}
library(devtools)
install_github("wjawaid/enrichR")
```

## CRAN

The package can be downloaded from CRAN using:

```{r, echo = TRUE, eval = FALSE}
install.packages("enrichR")
```

# Usage example

**enrichR** provides an interface to the Enrichr database [@kuleshov_enrichr:_2016] hosted at https://maayanlab.cloud/Enrichr/.  

By default human genes are selected otherwise select your organism of choice. (This functionality was contributed by Alexander Blume)

```{r, echo = TRUE, eval = TRUE}
library(enrichR)
websiteLive <- getOption("enrichR.live")
if (websiteLive) {
    listEnrichrSites()
    setEnrichrSite("Enrichr") # Human genes   
}
```

Then find the list of all available databases from Enrichr.

```{r, echo = TRUE, eval = TRUE}
if (websiteLive) dbs <- listEnrichrDbs()
```

```{r, echo = TRUE, eval = TRUE}
## if (is.null(dbs)) websiteLive <- FALSE
if (websiteLive) head(dbs)
```

```{r, echo = FALSE, results='asis'}
library(knitr)
if (websiteLive) kable(head(dbs[c(1:6),-4]))
```

View and select your favourite databases. Then query enrichr, in this case I have used genes associated with embryonic haematopoiesis.

```{r, echo = TRUE, eval = TRUE}
dbs <- c("GO_Molecular_Function_2015", "GO_Cellular_Component_2015", "GO_Biological_Process_2015")
if (websiteLive) {
    enriched <- enrichr(c("Runx1", "Gfi1", "Gfi1b", "Spi1", "Gata1", "Kdr"), dbs)
}
```

Now view the results table.

```{r, echo = TRUE, eval = FALSE}
if (websiteLive) enriched[["GO_Biological_Process_2015"]]
```

```{r, echo = FALSE, results = 'asis'}
success <- ifelse(exists("enriched"), TRUE, FALSE) & websiteLive
success <- success & (length(enriched) >= 3)
success <- success & all(dim(enriched[[3]]) > 2)                  
if (success) {
    x <- head(enriched[["GO_Biological_Process_2015"]])
    x[,1] <- gsub("GO:", "GO_", x[,1])
    kable(x)
}
```

Plot Enrichr GO-BP output. (Plotting function contributed by I-Hsuan Lin)

```{r, echo = TRUE, eval = TRUE, fig.width = 8, fig.height = 6, fig.align = "center", dpi = 100}
if (websiteLive) {
    plotEnrich(enriched[[3]], showTerms = 20, numChar = 40, y = "Count", orderBy = "P.value")
}
```

# References
