---
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;
  	}

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

    embed {
   		width: 800px;
  		height: 800px;
  }   
</style>

```{r config, include = FALSE}
 
# Load necessary packages
require(ggplot2)
require(knitr)

# Prepare recursive text

# Prepare all HPO reports container
out <- NULL
n_category <- 30
cl <- miRNA #this is for make compatible ora_enrichResult.Rmd 

```




# **DEgenes Hunter - miRNA `r miRNA` - Functional analysis report** 


## **General description**
Functional information for the `r miRNA` putative target genes.

Over representation analysis (ORA) was performed for the following functional systems:

* KEGG pathways
* GO:
	+ Biological Process
	+ Molecular Function
	+ Cellular Component
* Reactome pathways
* Custom nomenclature

The ORA method performs an enrichment of functional terms in putative target genes by a Fisher exact test.


```{r expression_profiles, echo = FALSE, message = FALSE, warning = FALSE, eval = !is.null(opt$RNAseq_folder) && !is.null(opt$RNAseq_folder) }

# Load genes expressions
targets <- unique(raw_data[raw_data$miRNA == miRNA, "Target_ID"])

targets_profiles <- RNAseq$normalized_counts[,colnames(RNAseq$normalized_counts) %in% targets]

# Prepare regular cluster info

miRNA_id <- unique(raw_data[raw_data$miRNA == miRNA, "miRNA_ID"])
miRNA_id <- miRNA_id[!is.na(miRNA_id)] # sometimes a miRNA has no mapping in multimiR
miRNA_mod <- miRNAseq$DH_results[miRNAseq$DH_results$gene_name == miRNA_id, "Cluster_ID"]
# save(list = ls(all.names = TRUE),file = "/home/bio267lab/proyectos/target_miRNA_2020/test_func.RData")
#   save(enr_go, file = "/home/bio267lab/proyectos/target_miRNA_2020/test_func.RData")

if (miRNA_strat == "d") {
  # save(miRNAseq,miRNA_id, file = "/mnt/scratch/users/bio_267_uma/josecordoba/NGS_projects/LaforaRNAseq/target_miRNA/test.RData")
  # q()
  # Count <- miRNAseq[['normalized_counts']][, miRNA_id]
  Count <- scale_data_matrix(as.matrix(miRNAseq[['normalized_counts']][, miRNA_id]), norm_by_col = TRUE)[,1]
  miRNA_profile <- as.data.frame(Count)
  miRNA_profile$Sample <- rownames(miRNA_profile)
  miRNA_profile$type <- paste0(miRNA_id, " normalized profile")


} else if (miRNA_strat == "h") {

  Count <- hub_miRNAs[,as.character(miRNA_mod)]
  miRNA_profile <- as.data.frame(Count)
  miRNA_profile$Sample <- rownames(miRNA_profile)
  miRNA_profile$type <- paste0("Hub gene of module ", miRNA_mod)

} else if (miRNA_strat == "E") {

  # miRNAseq$Eigengene <- as.data.frame(as.table(miRNAseq$Eigengene), stringsAsFactors = FALSE)
  # colnames(miRNAseq$Eigengene) <- c("Sample","Cluster_ID","Count") 
  # tgt_eigvalues_gnorm <- miRNAseq$Eigengene
  # tgt_eigvalues_gnorm$Count <- (tgt_eigvalues_gnorm$Count + 1) / 2 

  miRNA_profile <- tgt_eigvalues_gnorm[tgt_eigvalues_gnorm$Cluster_ID == miRNA_mod, c("Sample", "Count")]
  miRNA_profile$type <- paste0("Eigengene of module ", miRNA_mod)

}

# cl_profile <- cl_eigvalues_gnorm[cl_eigvalues_gnorm$Cluster_ID == ,]
# message(cl_profile)
# cl_profile <- cl_eigvalues_gnorm[grepl(paste(c("ME",{{cl}},"$"),collapse=""), cl_eigvalues_gnorm$Cluster_ID),]
# message(cl_profile)
# q()
# Plot
# targets_profiles <- RNAseq$normalized_counts[tgt_indx, ]
        # save(enr_ORA_expanded, file = "/mnt/scratch/users/bio_267_uma/josecordoba/NGS_projects/LaforaRNAseq/target_miRNA/test.RData")

# save(miRNA_profile, targets_profiles, file = "/mnt/scratch/users/bio_267_uma/josecordoba/NGS_projects/LaforaRNAseq/target_miRNA/test.RData")
# q()
targets_profiles <- as.data.frame(as.table(scale_data_matrix(data_matrix = targets_profiles, norm_by_col = TRUE)))
        colnames(targets_profiles) <- c("Sample","Gene","Count")
# save(targets, RNAseq,file = "/mnt/scratch/users/bio_267_uma/josecordoba/NGS_projects/LaforaRNAseq/target_miRNA/test.RData")
# q()
pp <- ggplot2::ggplot(mapping = ggplot2::aes(x = Sample)) + 
    ggplot2::geom_line(data = targets_profiles, alpha = 0.05, mapping = ggplot2::aes(y = Count, group = Gene)) +
    ggplot2::geom_line(data = miRNA_profile, mapping = ggplot2::aes(y = Count, group = type), color = "blue", linetype = "dashed", size = 0.5) +
    # scale_y_continuous(sec.axis = sec_axis(~.*2-1, name = "miRNA-targets correlation")) +
      ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 30, hjust = 1))
plot(pp)
```


```{r load_ORA_results, echo = FALSE, eval = exists("enrichments_ORA_expanded")}
# Per each category defined, perform enrichment
curr_out <- unlist(lapply(names(enrichments_ORA_expanded),function(cat){
  sec_text <- paste(c("## **",cat," enrichment results**\n"),collapse="") # Header
  sec_text <- c(sec_text,knitr::knit_expand("partial/ora_enrichResult.Rmd")) # Body
  return(sec_text)
}))
```
`r paste(knitr::knit(text = curr_out), collapse = '\n')`


```{r load_ORA_customResults, echo = FALSE}
curr_out <- ""

if(exists("custom_cls_ORA")){
  # Per each custom category defined, perform enrichment
  curr_out <- unlist(lapply(names(custom_cls_ORA),function(cat){
    sec_text <- paste(c("## **",basename(cat)," custom enrichment results**\n"),collapse="") # Header
    sec_text <- c(sec_text,knitr::knit_expand("partial/ora_customEnrichResult.Rmd")) # Body
    return(sec_text)
  }))
}
```
`r paste(knitr::knit(text = curr_out), collapse = '\n')`

