---
title: "Basic Usage"
vignette: >
  %\VignetteIndexEntry{Basic Usage}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include=FALSE, messages=FALSE, warnings=FALSE}
knitr::opts_chunk$set(comment="", cache=FALSE, fig.align="center")
library(tidyverse)
devtools::load_all(".")
```

# Terminology

All analyses with __hypeR__ must include one or more signatures and genesets.

### Signature
There are multiple types of enrichment analyses (e.g. hypergeometric, kstest, gsea) one can perform. Depending on the type, different kinds of signatures are expected. There are three types of signatures `hypeR()` expects.

```{r}
# Simply a character vector of symbols (hypergeometric)
signature <- c("GENE1", "GENE2", "GENE3")

# A ranked character vector of symbols (kstest)
ranked.signature <- c("GENE2", "GENE1", "GENE3")

# A ranked named numerical vector of symbols with ranking weights (gsea)
weighted.signature <- c("GENE2"=1.22, "GENE1"=0.94, "GENE3"=0.77)
```

### Geneset
A geneset is simply a list of vectors, therefore, one can use any custom geneset in their analyses, as long as it's appropriately defined. Additionally, `hypeR()` recognized object oriented genesets called `gsets` and `rgsets` objects, which are [explained later](https://montilab.github.io/hypeR-docs/articles/docs/data.html) in the documentation.

```{r}
genesets <- list("GSET1" = c("GENE1", "GENE2", "GENE3"),
                 "GSET2" = c("GENE4", "GENE5", "GENE6"),
                 "GSET3" = c("GENE7", "GENE8", "GENE9"))
```

# Usage

### Example Data

In these tutorials, we will use example data. The example data includes pre-computed results from common gene expression analysis workflows such as diffential expression and weighted gene co-expression.

```{r}
data(limma)
```

Using a differential expression dataframe created with Limma, we will extract a signature of upregulated genes for use with a *hypergeometric* test and rank genes descending by their differential expression level for use with a *kstest*.

```{r}
reactable(limma)
```

### Downloading Genesets

We'll also import the latest genesets from [Kegg](https://www.kegg.jp) using another set of functions provided by __hypeR__ for downloading and loading hundreds of open source genesets. 

```{r}
genesets <- msigdb_gsets("Homo sapiens", "C2", "CP:KEGG_LEGACY")
```

See [Downloading Genesets](https://montilab.github.io/hypeR-docs/articles/docs/data.html) for more information.

### Performing Enrichment

All workflows begin with performing enrichment with `hypeR()`. Often we're just interested in a single signature, as described above. In this case, `hypeR()` will return a `hyp` object. This object contains relevant information to the enrichment results, as well as plots for each geneset tested, and is recognized by downstream methods.

The most basic signature is an unranked vector of genes. This could be a differential expression signature, module of co-expressed genes, etc. As an example, we use the differential expression dataframe to filter genes that are upregulated (t > 0) and are sufficiently significant (fdr < 0.001), then extract the gene symbol column as a vector.

#### Unranked Signature

```{r}
signature <- limma %>% 
             dplyr::filter(t > 0 & fdr < 0.001) %>% 
             magrittr::use_series(symbol)
```

```{r}
length(signature)
head(signature)
```

```{r}
hyp_obj <- hypeR(signature, genesets, test="hypergeometric", background=50000, fdr=0.01, plotting=TRUE)
hyp_obj$plots[[1]]
```

#### Ranked Signature

Rather than setting a specific cutoff to define a differential expression signature, one could rank genes by their expression and provide the entire ranked vector as signature. From the differential expression dataframe, we order genes descending so upregulated genes are near the top, then extract the gene symbol column as a vector.

```{r}
signature <- limma %>% 
             dplyr::arrange(desc(t)) %>% 
             magrittr::use_series(symbol)
```

```{r}
length(signature)
head(signature)
```

```{r}
hyp_obj <- hypeR(signature, genesets, test="kstest", fdr=0.05, plotting=TRUE)
hyp_obj$plots[[1]]
```

#### Weighted Signature

In addition to providing a ranked signature, one could also add weights by including the t-statistic of the differential expression. From the differential expression dataframe, we order genes descending so upregulated genes are near the top, then extract and deframe the gene symbol and t-statistic columns as a named vector of weights.

```{r}
signature <- limma %>% 
             dplyr::arrange(desc(t)) %>% 
             dplyr::select(symbol, t) %>% 
             tibble::deframe()
```

```{r}
length(signature)
head(signature)
```

```{r}
hyp_obj <- hypeR(signature, genesets, test="kstest", fdr=0.05, plotting=TRUE)
hyp_obj$plots[[1]]
```

For permutation-based significance testing and normalized enrichment scores, please see [here](https://bioconductor.org/packages/release/bioc/html/fgsea.html).

### Downstream Analysis

#### The `hyp` Object

A `hyp` object contains all information relevant to the enrichment analysis, including the parameters used, a dataframe of results, plots for each geneset tested, as well as the arguments used to perform the analysis. All downstream functions used for analysis, visualization, and reporting recognize `hyp` objects and utilize their data. Adopting an object oriented framework brings modularity to hypeR, enabling flexible and reproducible workflows.

```{r}
print(hyp_obj)
```

All data related to enrichment arguments, parameters, and results and stored into a single `hyp` object. Saving your `hyp` objects will enable reproducible enrichment workflows.
```{r}
hyp_obj$info
```

#### The `hyp` Methods
```{r, eval=FALSE}
# Show interactive table
hyp_show(hyp_obj)

# Plot dots plot
hyp_dots(hyp_obj)

# Plot enrichment map
hyp_emap(hyp_obj)

# Plot hiearchy map (relational genesets)
hyp_hmap(hyp_obj)

# Map enrichment to an igraph object (relational genesets)
hyp_to_graph(hyp_obj)

# Save to excel
hyp_to_excel(hyp_obj)

# Save to table
hyp_to_table(hyp_obj)

# Generate markdown report
hyp_to_rmd(hyp_obj)
```

Continue to [Visualize Results](https://montilab.github.io/hypeR-docs/articles/docs/visualize.html) to see these methods in action.
