---
title: "`r paste0('Your report from GeneTonic - ', project_id)`"
author: "`r paste0('GeneTonic happy_hour (v', utils::packageVersion('GeneTonic'), ')')`"
date: "`r Sys.Date()`"

output: 
  html_document:
    toc: true
    toc_float: true
    code_folding: show
    code_download: true
    number_sections: true
    df_print: kable
    theme: lumen
always_allow_html: yes
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  echo = TRUE, 
  warning = FALSE, 
  message = FALSE,
  error = TRUE
)
# knitr::opts_knit$set(
#   progress = FALSE, 
#   verbose = FALSE
# )
```


# Intro

 <!-- this is the file that gets used in rendering the many outputs via happyhour -->

ideas:

- differentiate the content based on batch running vs generated at runtime?
- print out the values of input$... objects. So one can just repeat them ;)

These are the provided objects

```{r echo = TRUE}
library(GeneTonic)
```


```{r echo = TRUE}
dds
res_de
head(res_enrich)
head(annotation_obj)
res_enrich <- get_aggrscores(res_enrich, res_de, annotation_obj)
```


```{r}
# if (exists("reactive_values$mygenesets")) {
#   selected_genesets <- reactive_values$mygenesets
# } else {
#   selected_genesets <- mygenesets
# }
# 
# if (exists("reactive_values$mygenes")) {
#   selected_genes <- reactive_values$mygenes
# } else {
#   selected_genes <- mygenes
# }

if (usage_mode == "shiny_mode") {
  selected_genesets <- reactive_values$mygenesets
  selected_genes <- reactive_values$mygenes
} else if (usage_mode == "batch_mode"){
  selected_genesets <- mygenesets
  selected_genes <- mygenes
}

selected_genesets
selected_genes
```


```{r}
# myids <- annotation_obj$gene_id[match(selected_genes ,annotation_obj$gene_name)]
myids <- selected_genes
```


```{r}
lapply(myids, function(arg) {
  GeneTonic::gene_plot(dds,gene = arg,intgroup = "condition", 
                       annotation_obj = annotation_obj)})
```

# Something for the selected genesets

```{r}
# corresponding_gs_ids <- res_enrich$gs_id[res_enrich$gs_description %in% selected_genesets]
corresponding_gs_ids <- selected_genesets

gs_mds(res_enrich, res_de, annotation_obj, gs_labels = corresponding_gs_ids)

gs_volcano(res_enrich, volcano_labels = 0, volcano_labels = corresponding_gs_ids)
```

# Geneset content, tabbed! {.tabset .tabset-fade}

Here is a heatmap for each bookmarked set

```{r echo = TRUE, results="asis"}
vst_data <- DESeq2::vst(dds) # or also have it passed as param

for (gs in selected_genesets) {
  corresponding_gs_desc <- res_enrich[gs, "gs_description"]
  cat("## ", corresponding_gs_desc, " {-}\n")
  
  gs_heatmap(vst_data, 
             res_de, 
             res_enrich, 
             annotation_obj, 
             geneset_id = gs, 
             cluster_columns = TRUE)
  
  cat(go_2_html(gs, res_enrich))
  cat("\n\n")
}
```


# Session info {-}

```{r}
sessionInfo()
```

