```{r "{{gene_set}}_{{funsys}}_ora_enrichResults_config", include = FALSE}
# ###########################################
# ################# READ ME #################
# ###########################################
#
# This template assumes that the following variables exists
# => enrichment_results => enrichResult object
# => This template also need 2 tags for chunk naming gene_set and funsys
# => geneList is a named vector of a gene metric with genes as names and gane_mapping_name is the name of the metric 
```



```{r "{{gene_set}}_{{funsys}}_oraEnrich", results='asis', echo = FALSE, message=FALSE, warning=FALSE}
enrichment_results <- enrichments_for_reports[["{{gene_set}}"]][["{{funsys}}"]]

n_category <- 30
default_width <- set_default_width(enrich_obj = enrichment_results, showCategory=n_category)

if(length(get_categories(enrichment_results))>2){ # Needs, at least, two categories
	# Main label 
	cat("### **{{funsys}} - 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(enrichment_results, 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(enrichment_results, 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(enrichment_results, OrgDb = current_organism_info$Bioconductor_DB[1], keyType="ENTREZID")
	if(length(get_genes(enrichments_ORA_symb)) <= 150){
		if (!is.null(geneList)){
			pp <- enrichplot::cnetplot(enrichments_ORA_symb, showCategory=n_category, foldChange=geneList, layout = "fr") +
			    	ggplot2::scale_color_gradient2(name = gane_mapping_name, low = "#0000BF", high = "#bf0000", na.value = "#50EA55")
		} else {
			pp <- enrichplot::cnetplot(enrichments_ORA_symb, showCategory=n_category)
		}
		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(enrichment_results, 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." 
	)
	if (!is.null(geneList)){
		cat("Colour indicates log2 fold change.\n\n")
		pp <- enrichplot::heatplot(enrichments_ORA_symb, showCategory=n_category, foldChange=geneList)
	} else {
		cat("\n\n")
		pp <- enrichplot::heatplot(enrichments_ORA_symb, showCategory=n_category)
	}
	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(enrichment_results, 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("### **{{funsys}} - Over Representation Analysis**\n",
		"After execute ORA enrichment, **any significative term have been found**")
}
```



