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

## ----installation, eval=FALSE-------------------------------------------------
# if(!requireNamespace('BiocManager', quietly = TRUE))
#   install.packages('BiocManager')
# 
# BiocManager::install("KEGGemUP")

## ----load_package, message=FALSE----------------------------------------------
library("KEGGemUP")

## ----load_data----------------------------------------------------------------
data(res_de_macro_IFNg_vs_naive, package = "KEGGemUP")

## Inspect this briefly
head(res_de_macro_IFNg_vs_naive)

## ----create_graph-------------------------------------------------------------
kmu_graph <- create_kegg_graph(pathway_id = "hsa00563")

kmu_graph

## ----render_graph-------------------------------------------------------------
render_kegg_graph(g = kmu_graph)

## ----render_notitle-----------------------------------------------------------
render_kegg_graph(g = cleanup_title_node(kmu_graph))

## ----singleDE-----------------------------------------------------------------
head(res_de_macro_IFNg_vs_naive)

## ----listDE-------------------------------------------------------------------
de_results_list <- list(
  rnaseq_limma = list(
    de_table = data.frame(res_de_macro_IFNg_vs_naive),
    value_column = "logFC",
    feature_column = "ENTREZID"
  )
)

## ----graph_mapped, out.width="100%", out.height="800px"-----------------------
kmu_graph_mapped <- map_results_to_graph(g = kmu_graph, 
                                         de_results = de_results_list)
kmu_rendered <- render_kegg_graph(g = kmu_graph_mapped, 
                                  scaling_factor = 1.3)
kmu_rendered

## ----graph_mapped_altpal, out.width="100%", out.height="800px"----------------
kmu_graph_mapped_single <- map_results_to_graph(g = kmu_graph, 
                                                de_results = res_de_macro_IFNg_vs_naive,
                                                feature_column = "ENTREZID",
                                                value_column = "logFC", 
                                                palette = "Spectral")
render_kegg_graph(g = kmu_graph_mapped_single, scaling_factor = 1.3)

## ----subset_graph, out.width="100%", out.height="800px"-----------------------
kmu_cellcycle <- create_kegg_graph(pathway_id = "hsa04110")
kmu_cellcycle_mapped <- map_results_to_graph(g = kmu_cellcycle, 
                                             de_results = de_results_list)

## Selecting a subset of the nodes to keep
KEGGids_to_include <- igraph::V(kmu_cellcycle_mapped)$ids_for_mapping |>
  tail(35) |> 
  strsplit(split = ";") |>
  unlist() |>
  unique()
head(KEGGids_to_include)
length(KEGGids_to_include)

kmu_cellcycle_subset <- subset_kegg_graph(g = kmu_cellcycle_mapped, 
                                          ids_to_include = KEGGids_to_include)

## Specifying a custom title with the `graph_title` parameter...
render_kegg_graph(kmu_cellcycle_subset, graph_title = "The subset of the original graph")

## ----highlight_graph, out.width="100%", out.height="800px"--------------------
kmu_cellcycle_highlighted <- highlight_kegg_graph(g = kmu_cellcycle_mapped, 
                                                  ids_to_highlight = KEGGids_to_include)
render_kegg_graph(kmu_cellcycle_highlighted)

## ----export_graph-------------------------------------------------------------
file_prefix <- tempfile()
export_kegg_graph(g = kmu_cellcycle_highlighted, basename = file_prefix)

## ----retrieve-----------------------------------------------------------------
local_kgml_file <- retrieve_kgml(pathway_id = "hsa04110", path = tempdir())
local_kgml_file

graph_parsed <- create_kegg_graph(pathway_id = "local_kgml", kgml_file = local_kgml_file)
graph_parsed

cpd_db <- get_kegg_db(db_name = "compound", path = tempdir())
head(cpd_db)

## ----cachethings--------------------------------------------------------------
kmu_cached <- display_cache_KEGGemUP()
head(kmu_cached)

## ----deletecache, eval=FALSE--------------------------------------------------
# # this will delete all previously retrieved KGML files, use this judiciously
# reset_cache_KEGGemUP()

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

