```{r "{{cl}}_{{cat}}_ora_enrichResults_config", include = FALSE}
# ###########################################
# ################# READ ME #################
# ###########################################
#
# This template assumes that the following variables exists
# => current_organism_info$Bioconductor_DB[1] :: organism database to translate genes
# => cl :: cluster to be checked
# => cat :: category to be checked
#
# This templates include the following chunk names:
# => <cl>_<cat>_ora_enrichmentsResult
# => <cl>_<cat>_oraEnrich
 
# Load necessary packages
# require(ggplot2)
# require(knitr)
# require(clusterProfiler)
# require(enrichplot)

# Prepare recursive text
chunks <- function(code, options = ""){paste(paste("```{r ",options,"}",sep = ""),code,"```",sep="\n")}
render <- function(text){paste(knitr::knit(text = paste(text,collapse = "\n")),collapse = "\n")}

# Prepare all HPO reports container
out <- NULL
 
# Load necessary functions
if("ExpHunterSuite" %in% (.packages())){ # Package mode, load private functions
	set_default_width <- ExpHunterSuite:::set_default_width
	get_categories <- ExpHunterSuite:::get_categories
	plot_in_div <- ExpHunterSuite:::plot_in_div
	get_genes <- ExpHunterSuite:::get_genes
	calc_width <- ExpHunterSuite:::calc_width
	calc_height <- ExpHunterSuite:::calc_height 
}

```



```{r "{{cl}}_{{cat}}_oraEnrich", results='asis', echo = FALSE, message=FALSE, warning=FALSE}

n_category <- 30
default_width <- set_default_width(enrich_obj = enrichments_ORA_expanded[["{{cat}}"]][["{{cl}}"]], showCategory=n_category)
if(length(get_categories(enrichments_ORA_expanded[["{{cat}}"]][["{{cl}}"]]))>=2){ # Needs, at least, two categories
	# Main label 
	cat("### **{{cat}} - Over Representation Analysis**\n",
		"The ORA method takes a group of significant genes and performs a Fisher's exact test for each term of the selected functional category.\n\n"
	)

	cat(
		"**Barplot**\n\n", 
		"The most highly signficant categories in ascending order, according to adjusted p-value. 
		The x-axis represents the number of significant genes found within the functional category.\n\n"
       )
	pp <- graphics::barplot(enrichments_ORA_expanded[["{{cat}}"]][["{{cl}}"]], showCategory=n_category)
	plot_in_div(pp, fig_width = default_width)




	cat("\n\n**Dotplot**\n\n",
        "The most highly signficant categories in descending in categories of gene ratio, defined as the proportion of significant genes that are found in the functional category.
         The x-axis represents the gene ratio and the dot size the number of genes associated with the functional category.\n\n"
       )
	pp <- enrichplot::dotplot(enrichments_ORA_expanded[["{{cat}}"]][["{{cl}}"]], showCategory=n_category)
	plot_in_div(pp, fig_width = default_width)



	cat("\n\n**Gene-Concept Network**\n\n", 
	  "The network connects the top functional categories (brown nodes) to their associated genes (grey or colored nodes).
 	   The size of the functional category nodes shows the number of connected genes.\n\n"
       )
	enrichments_ORA_symb <- DOSE::setReadable(enrichments_ORA_expanded[["{{cat}}"]][["{{cl}}"]], OrgDb = current_organism_info$Bioconductor_DB[1], keyType="ENTREZID")
	if(length(get_genes(enrichments_ORA_symb)) <= 150){
		pp <- enrichplot::cnetplot(enrichments_ORA_symb, showCategory=n_category, foldChange=geneList) +
			  ggplot2::scale_color_gradient2(name = "fold change", low = "#0000BF", high = "#bf0000", na.value = "#50EA55")
		plot_in_div(pp, fig_width = default_width)
	}else{
		cat("\n\nCnetplot is not readable because more than 150 genes has been enriched in this nomenclature, so is not printed\n\n")
	
	}

	cat("\n\n**Enrich Map plot**\n\n", 
	  "The top functional categories (nodes), connected if they share genes. Edge thickness represents the number of shared genes.
	  Nodes size represents the number of significant genes within the category.\n\n"
	)
	pp <- enrichplot::emapplot(enrichments_ORA_expanded[["{{cat}}"]][["{{cl}}"]], showCategory=n_category)
	plot_in_div(pp, fig_width = default_width)



	cat("\n\n**Heatplot**\n\n", 
	  "Significant genes (x-axis) and the functional categories in which they appear. Colour indicates log2 fold change.\n\n"
	)

	pp <- enrichplot::heatplot(enrichments_ORA_symb, showCategory=n_category, foldChange=geneList)
	width_size <- calc_width(enrichments_ORA_symb)
	height_size <- calc_height(enrichments_ORA_symb) 
	plot_in_div(pp, height_size, width_size)

	cat("\n\n**Upsetplot**\n\n", 
	  "Genes are clustered according to shared enriched categories. 
	  The y-axis shows the number of genes belonging to the different clusters (top) and categories to which they belong (bottom).\n\n"
	)
	pp <- enrichplot::upsetplot(enrichments_ORA_expanded[["{{cat}}"]][["{{cl}}"]], n=n_category)
	plot_in_div(pp, fig_width = default_width)
	cat('\n\nGenes has been enriched in less than 2 categories, so Upsetplot cannot be printed\n\n')

} else{
	# Main label 
	cat("### **{{cat}} - Over Representation Analysis**\n",
		"After execute ORA enrichment, **any significative term have been found**")
}
```



