## ----style, echo = FALSE, results = 'asis'------------------------------------
BiocStyle::markdown()

## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  crop = NULL,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(TDbasedUFE)
library(TDbasedUFEadv)

## -----------------------------------------------------------------------------
example <- readRDS(system.file(
  "extdata", "enrichment_example.rds",
  package = "TDbasedUFEadv",
  mustWork = TRUE
))

Multi <- example$Multi
Z <- prepareTensorfromList(Multi, 10L)
Z <- aperm(Z, c(2, 1, 3))
Z <- PrepareSummarizedExperimentTensor(
  feature = example$feature,
  sample = array("", 1), value = Z,
  sampleData = example$cond
)
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-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"
# )

## ----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)

## ----dose-example-------------------------------------------------------------
if (
    requireNamespace("DOSE", quietly = TRUE) &&
    requireNamespace("enrichplot", quietly = TRUE) &&
    requireNamespace("gson", quietly = TRUE) &&
    requireNamespace("ggplot2", quietly = TRUE)
) {
    edo <- DOSE::enrichDO(entrez, ont = "HDO")

    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."
    )
}

## -----------------------------------------------------------------------------
sessionInfo()

