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

output: 
  html_document:
    toc: true
    toc_float: true
    code_folding: hide
    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
)
```


# Introduction - the input data
In this session of `GeDi` you used the following data:

```{r echo = TRUE}
library("GeDi")
```


```{r inputData}
if(is.null(reactive_values$genesets)){
  cat("It seems like you did not provide data in this session.")
}else{
  DT::datatable(reactive_values$genesets, caption = "The input genesets")
}
```

From your initial input data, you have filtered the following genesets

```{r filteredtData}
if(length(reactive_values$filtered_genesets) > 0){
  DT::datatable(reactive_values$filtered_genesets, caption = "The filtered genesets")
}else{
  cat("It seems like you didn't filter genesets from the input data.")
}
```

Potentially you specified the species of your data and downloaded a Protein-Protein-Interaction (PPI) matrix. 

```{r inputPPI}
if(is.null(input$species) || input$species == ""){
  cat("It seems like you didn't specify the species of your data.")
  
}else{
 cat("You specified the species of your data as ", input$species, sep = "")
}

if(!is.null(reactive_values$ppi)){
  DT::datatable(reactive_values$ppi, caption = "The downloaded PPI")
}else{
  cat("It seems like you did not download a PPI matrix.")
}
```


## Parameter settings 

For some of the calculations and visualizations of the app, the selected parameters are important and crucial for the results. This is the current state of the individual parameters.

```{r parameters}
cat("Your parameters of the Distance Scores panel")

scores <- names(reactive_values$scores)

if(length(scores) > 0){
  scoring <- paste(scores, collapse = "\n") 
  cat(paste("You have calculated distance scores of your data using the follow metrics: \n",
            scoring, sep = ""))
  cat(paste("\nFor the clustering method in the dendrogram, you have selected ", input$cluster_method_dendro, ".", sep = ""))
  cat(paste("\nAnd for threshold for the Scoring Network was set to ", input$similarityScores, ".",   sep = ""))
}else{
  cat("It seems like you did not specify a Scoring method and did not score your data.")
}


cat("Your parameters of the Clustering Graph panel")


string <- paste("You have selected ", input$select_clustering, " as clustering method. \n", sep = "")
if(is.null(input$select_clustering)){
  string <- ("It seems like you did not cluster your data in this session")
}else if(input$select_clustering == "Louvain"){
  string <- paste(string, "The threshold you selected for the Louvain clustering was set to ", input$louvain_threshold , ".", sep = "")
}else if (input$select_clustering == "Markov"){
  string <- paste(string, "The threshold you selected for the Markov clustering was set to ", input$markov_threshold , ".",  sep = "")
}else{
  string <- paste("The similarity Threshold was set to ", input$simThreshold, ".\nThe membership Threshold was set to ", input$memThreshold, ". \nThe clustering Threshold was set to ", input$clustThreshold, ".", sep = "")
}
cat(string)
```

# A quick overview on the scoring results

This section shows the visualizations of the Scoring panel.

```{r Scoringpanel}
if(length(reactive_values$scores) == 0){
  cat("It seems like you do not have distance scores calculated for your data.")
}else{
  if(input$plots_distance_score == ""){
    scores <- reactive_values$scores[[1]] 
    cat(paste("Your plots will be generated on the distance scores calculated with ", names(reactive_values$scores)[[1]], sep = ""))
  }else{
    scores <- reactive_values$scores[[input$plots_distance_score]] 
    cat(paste("Your plots will be generated on the distance scores calculated with ", input$plots_distance_score, sep = ""))
  }
  scoring_heatmap <- distanceHeatmap(scores)
  scoring_heatmap
}
```

```{r dendro}
if(!length(reactive_values$scores) == 0) {
  dendro <-
    distanceDendro(scores, input$cluster_method_dendro)
  dendro
} else{
  cat("It seems like you do not have distance scores calculated for your data.")
}
```

```{r scoringNetwork}
library("visNetwork")
if(!length(reactive_values$scores) == 0){
  graph <- reactive_values$scores_graph()

visNetwork::visIgraph(graph) %>%
          visNodes(color = list(
            background = "#0092AC",
            highlight = "gold",
            hover = "gold"
          )) %>%
          visEdges(color = list(
            background = "#0092AC",
            highlight = "gold",
            hover = "gold"
          )) %>%
          visOptions(
            highlightNearest = list(
              enabled = TRUE,
              degree = 1,
              hover = TRUE
            ),
            nodesIdSelection = TRUE
          ) %>%
          visExport(
            name = "distance_scores_network",
            type = "png",
            label = "Save Distance Scores graph"
          )
}else{
    cat("It seems like you do not have distance scores calculated for your data.")
}
```


# A quick overview of the clustering results

```{r clusteringGraph}
if(!is.null(reactive_values$cluster)){
  graph <- reactive_values$cluster_graph()

visNetwork::visIgraph(graph) %>%
          visOptions(
            highlightNearest = list(
              enabled = TRUE,
              degree = 1,
              hover = TRUE
            ),
            nodesIdSelection = TRUE,
            selectedBy = list(variable = "cluster", multiple = TRUE)
          ) %>%
          visExport(
            name = "cluster_network",
            type = "png",
            label = "Save Cluster graph"
          )
} else{
  cat("It seems like you did not cluster your data in this session")
}
```

```{r BipartiteGraph}
if(!is.null(reactive_values$cluster)){
  graph <- reactive_values$bipartite_graph()

visNetwork::visIgraph(graph) %>%
        visIgraphLayout(layout = "layout_as_bipartite") %>%
        visOptions(
          highlightNearest = list(
            enabled = TRUE,
            degree = 1,
            hover = TRUE
          ),
          nodesIdSelection = TRUE
        ) %>%
        visExport(
          name = "bipartite_network",
          type = "png",
          label = "Save Cluster-Geneset bipartite graph"
        )
}else{
  cat("It seems like you did not cluster your data in this session")
}
```


```{r enrichmentcloud, eval = FALSE, echo = FALSE}
if(!is.null(reactive_values$cluster)){
        cluster <- as.numeric(input$cluster_nb)
        genesets <- reactive_values$cluster[[cluster]]
        genesets_df <- reactive_values$genesets[genesets, ]
        enrichmentWordcloud(genesets_df)
}else{
  cat("It seems like you did not cluster your data in this session")
}
```

# Highlighting your bookmarked genesets and clusters

If you have bookmarked any genesets or clusters, they will be displayed here.

```{r bookmarkedGenesets}
if(!(nrow(reactive_values$bookmarked_genesets) == 0)){
  cat("Here is a data table of your bookmarked genesets. \n")
  DT::datatable(reactive_values$bookmarked_genesets, rownames = F)
}else{
  cat("You have not bookmarked any genesets to be displayed here.")
}
```


```{r bookmarkedClusters}
if(!(nrow(reactive_values$bookmarked_cluster) == 0)){
  for(i in 1:nrow(reactive_values$bookmarked_cluster)){
    cluster <- reactive_values$bookmarked_cluster[i, ]
    cluster_nb <- cluster[["Cluster"]]
    cat(paste("Now, we will have a look at ", cluster_nb, " which has the following genesets as members: ", cluster["Cluster_Members"], ".\n", sep = ""))
    cat(paste("The clusters are involved in the following biological processes: ", cluster["Cluster_Member_Description"], ".\n", sep = ""))

    cluster_nb <- as.numeric(unlist(strsplit(
        cluster_nb, split = ' ', fixed = TRUE
      ))[2])
    
    genesets <- reactive_values$cluster[[cluster_nb]]
    genesets_df <- reactive_values$genesets[genesets, ]
    cat(paste("Showing the Enrichment Wordcloud for cluster ", cluster_nb, "\n\n", sep = ""))
    e <- enrichmentWordcloud(genesets_df)
    print(e)
      
  }
}else{
  cat("You have not bookmarked any clusters to be displayed here.")
}
```


# About this report

If you use this report in your scientific work, please cite GeDi:

```{r, eval = FALSE}
utils::citation("GeDi")
```


# Session info {-}

This is the output of the `sessionInfo()` command, reported for improving the reproducibility of the analysis.

```{r}
utils::sessionInfo()
```

