---
title: "Enrichment"
author:
- name: Y-h. Taguchi
  affiliation:  Department of Physics, Chuo University, Tokyo 112-8551, Japan
  email: tag@granular.com
output:   
    BiocStyle::html_document:
    toc: true
bibliography: references.bib
vignette: >
  %\VignetteIndexEntry{Enrichment}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r style, echo = FALSE, results = 'asis'}
BiocStyle::markdown()
```

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  crop = NULL,
  comment = "#>"
)
```



```{r setup}
library(TDbasedUFE)
library(TDbasedUFEadv)
library(RTCGA.rnaseq)
library(RTCGA.clinical)
```

# Introduction

It might be helpful to demonstrate how to evaluate selected genes by enrichment analysis. Here, we show some of useful tools applied to the output from TDbasedUFEadv
In order foe this, we reproduce one example in "How to use TDbasedUFEadv" as follows.

``` {r}
Multi <- list(
  BLCA.rnaseq[seq_len(100), 1 + seq_len(1000)],
  BRCA.rnaseq[seq_len(100), 1 + seq_len(1000)],
  CESC.rnaseq[seq_len(100), 1 + seq_len(1000)],
  COAD.rnaseq[seq_len(100), 1 + seq_len(1000)]
)
Z <- prepareTensorfromList(Multi, 10L)
Z <- aperm(Z, c(2, 1, 3))
Clinical <- list(BLCA.clinical, BRCA.clinical, CESC.clinical, COAD.clinical)
Multi_sample <- list(
  BLCA.rnaseq[seq_len(100), 1, drop = FALSE],
  BRCA.rnaseq[seq_len(100), 1, drop = FALSE],
  CESC.rnaseq[seq_len(100), 1, drop = FALSE],
  COAD.rnaseq[seq_len(100), 1, drop = FALSE]
)
# patient.stage_event.tnm_categories.pathologic_categories.pathologic_m
ID_column_of_Multi_sample <- c(770, 1482, 773, 791)
# patient.bcr_patient_barcode
ID_column_of_Clinical <- c(20, 20, 12, 14)
Z <- PrepareSummarizedExperimentTensor(
  feature = colnames(ACC.rnaseq)[1 + seq_len(1000)],
  sample = array("", 1), value = Z,
  sampleData = prepareCondTCGA(
    Multi_sample, Clinical,
    ID_column_of_Multi_sample, ID_column_of_Clinical
  )
)
HOSVD <- computeHosvd(Z)
cond <- attr(Z, "sampleData")
index <- selectFeatureProj(HOSVD, Multi, cond, de = 1e-3, input_all = 3) # Batch mode
head(tableFeatures(Z, index))
genes <- unlist(lapply(strsplit(tableFeatures(Z, index)[, 1], "|",
  fixed = TRUE
), "[", 1))
entrez <- unlist(lapply(strsplit(tableFeatures(Z, index)[, 1], "|",
  fixed = TRUE
), "[", 2))
```

# Enrichr 

Enrichr[@Enrichr] is one of tools that often provides us significant results 
toward genes selected by TDbasedUFE and TDbasedUFEadv.

```{r enrichr-example, eval=FALSE}
if (!requireNamespace("enrichR", quietly = TRUE)) {
    stop("The enrichR package is required to run this example.")
}

enrichR::setEnrichrSite("Enrichr")

dbs <- c(
    "GO_Molecular_Function_2015",
    "GO_Cellular_Component_2015",
    "GO_Biological_Process_2015"
)

enriched <- enrichR::enrichr(genes, dbs)

enrichR::plotEnrich(
    enriched$GO_Biological_Process_2015,
    showTerms = 20,
    numChar = 40,
    y = "Count",
    orderBy = "P.value"
)
```

Enrichr can provide you huge number of enrichment analyses, 
many of which have good compatibility with the genes selected by
TDbasedUFE as well as TDbasedUFEadv by the experience. 
Please check Enrichr's web site to see what kinds of enrichment
analyses can be done.

# STRING

STRING[@STRING] is enrichment analyses based upon protein-protein interaction, 
which is known to provide often significant results toward genes selected by
TDbasedUFE as well as TDbasedUFEadv.

```{r stringdb-example, eval=FALSE}
if (!requireNamespace("STRINGdb", quietly = TRUE)) {
    stop("The STRINGdb package is required to run this example.")
}

string_cache <- tools::R_user_dir("TDbasedUFEadv", which = "cache")
dir.create(string_cache, recursive = TRUE, showWarnings = FALSE)

string_db <- STRINGdb::STRINGdb$new(
    version = "11.5",
    species = 9606,
    score_threshold = 200,
    network_type = "full",
    input_directory = string_cache
)

example1_mapped <- string_db$map(
    data.frame(genes = genes),
    "genes",
    removeUnmappedRows = TRUE
)

hits <- example1_mapped$STRING_id
string_db$plot_network(hits)
```

# enrichplot

Although these above can provide us enough number of information to evaluate 
the genes selected by TDbasedUFE as well as TDbasedUFEadv, one might need 
all one package for which one does not how to decide which category must
be evaluated in enrichment analysis.

In this case, we would recommend Metascape[@Metascape] that unfortunately  
does not have the ways approached from R. Thus, we recommend RITAN as
an alternative. It can list significant ones among multiple categories.


```{r dose-example}
if (
    requireNamespace("DOSE", quietly = TRUE) &&
    requireNamespace("enrichplot", quietly = TRUE) &&
    requireNamespace("gson", quietly = TRUE) &&
    requireNamespace("ggplot2", quietly = TRUE)
) {
    edo <- DOSE::enrichDGN(entrez)

    enrichplot::dotplot(edo, showCategory = 30) +
        ggplot2::ggtitle("dotplot for ORA")
} else {
    message(
        "Skipping DOSE/enrichplot example because one or more optional packages ",
        "are not installed: DOSE, enrichplot, gson, ggplot2."
    )
}
```
```{r}
sessionInfo()
```

