---  
title: "ExpHunterSuite: All Clusters Report"
author: "SysBioLab"
output: 
  html_document:
    toc: true
    toc_float: true
    df_print: paged
    fig_width: 12
---

<style type='text/css'>
    body .main-container {
        max-width: 90%;

    }

    .plot_real_size {
      overflow: scroll;
      max-height: 600px;
    }

    .plot_real_size img{
      max-width: none;
      max-height: none;
    }

    embed {
   
  }   
</style>

```{r config, include=FALSE, message=FALSE}
require(ggplot2)
require(knitr)
require(clusterProfiler)
require(enrichplot)
require(DOSE)
```

```{r child="func_initial_details.Rmd", eval=!is.null(sample_classes)}
```
```{r child="func_deg_details.Rmd", eval=!is.null(sample_classes)}
```

## **Overview of clusters**
The following table shows the numbers of prevalent vs. non-prevalent genes found in 
each cluster (first two columns), followed by the number  of significantly enriched 
categories found in each cluster for each of the annotation sources found using the ORA method (remaining columns).

```{r func_specific, echo=FALSE, results='asis', eval=!is.null(sample_classes)}

# JRP - to decide - count entrez NAs to make consistent, or remove as not used for enrichements? For now we leave
# DEGH_results_clust_stats <- DEGH_results[! is.na(DEGH_results$entrezgene) &  DEGH_results$Cluster_ID != 0,]
DEGH_results_clust_stats <- DEGH_results[DEGH_results$Cluster_ID != 0,] 
prev_cluster <- t(table(DEGH_results_clust_stats$genes_tag, DEGH_results_clust_stats$Cluster_ID))

enr_per_cluster <- sapply(func_results$WGCNA_ORA_expanded, function(enr_list) {
  sapply(enr_list, nrow)
})
# Have to ensure row names match up
cluster_info <- cbind(prev_cluster, enr_per_cluster[row.names(prev_cluster),])

DT::datatable(cluster_info, filter = 'top', rownames = FALSE, extensions = c('Buttons','ColReorder'),
                    options = list(
                      colReorder = TRUE,
                      dom = 'lftBip',
                        buttons = c('copy', 'csv', 'excel')
))

to_show <- DEGH_results[,c("Cluster_ID","SYMBOL","genes_tag",colnames(DEGH_results)[1])]
to_show$Cluster_ID <- as.character(to_show$Cluster_ID)
DT::datatable(to_show, filter = 'top', rownames = FALSE, extensions = c('Buttons', 'ColReorder'),
                    options = list(
                      colReorder = TRUE,
                      dom = 'lftBip',
                        buttons = c('copy', 'csv', 'excel')
))

cat("The following modules were run:\n")
cat(names(flags_cluster))

cat("Results were found in at least one module for the following:\n")
cat(names(flags_cluster[flags_cluster==TRUE]))

```

```{r multiple_children, echo=FALSE, results='asis'}
res <- list()
for(funsys in names(flags_cluster)) {
  if(flags_cluster[[funsys]] == TRUE) {
    enrich_obj <- enrichments_ORA[[funsys]]
    exp_res <- knitr::knit_expand("ora_plots_compareCluster.Rmd")
    res[[funsys]] <- knitr::knit(text=exp_res, quiet=TRUE)
  } else {
    res[[funsys]] <- paste0("## **No enrichment found for ", funsys, "**\n")
  }
}
cat(unlist(res), sep = '\n')

```

