---
title: "`r params$title`"
author: "Analysis were performed with the RFLOMICS package (`r params$rflomicsVersion`)"
date: "`r params$date`"
bibliography: bibliography.bib
output: 
  html_document:
    df_print: paged
    toc: true
    toc_depth: 2
    toc_float:
      collapsed: true
      smooth_scroll: true
    number_sections: false
    theme: flatly
    css: www/style.css
params: 
  outDir: NA
  FEdata: NA
  title: NA
  rflomicsVersion: NA
  date: NA
---

```{r library, include = FALSE}
library(knitr)
library(RFLOMICS)
library(enrichplot)
library(UpSetR)
library(ggplot2)
#library(ggtext)
library(DT)
library(htmltools)

library(dplyr)
```

```{r setting, include=FALSE}
opts_chunk$set(message = FALSE, warning = FALSE, echo = FALSE, error = TRUE)
options(knitr.duplicate.label = "allow")
```

`r if(params$rflomicsVersion != packageVersion('RFLOMICS')) {"<p style=\"color:red;font-weight:bold;\">Warning: the version of rflomics used to generate the analysis is different from the one used to generate the report. This may lead to errors due to significant changes in the functions.</p>"}`

```{r parameters, include=FALSE}

load(params$FEdata)
outDir <- params$outDir
projectName <- params$title

```

```{r}
# Create a directory to store all the results (tables and figures) of the project in a tmpdir
# user may download this results as an archive from the shiny interface
dir.create(path = outDir, showWarnings = FALSE)
```

<!-- tabset color -->

```{css}
.scroll-200 {
  max-height: 200px;
  overflow-y: auto;
  background-color: inherit;
}
```

<!-- enable to zoom by clicking on graphs in the report -->

```{css zoom-lib-src}
### ZOOM 
script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"
```

```{js zoom-jquery}
$(document).ready(function() {
$('body').prepend('<div class=\"zoomDiv\"><img src=\"\" class=\"zoomImg\"></div>');
// onClick function for all plots (img's)
$('img:not(.zoomImg)').click(function() {
$('.zoomImg').attr('src', $(this).attr('src')).css({width: '100%'});
$('.zoomDiv').css({opacity: '1', width: 'auto', border: '1px solid white', borderRadius: '5px', position: 'fixed', top: '50%', left: '50%', marginRight: '-50%', transform: 'translate(-50%, -50%)', boxShadow: '0px 0px 50px #888888', zIndex: '50', overflow: 'auto', maxHeight: '100%'});
});
// onClick function for zoomImg
$('img.zoomImg').click(function() {
$('.zoomDiv').css({opacity: '0', width: '0%'}); 
});
});
```

<!-- Experimental design -->

```{r design, eval=(nrow(getDesignMat(rflomics.MAE)) != 0)} 

out <- NULL
child <- paste0(path.package("RFLOMICS"), "/RFLOMICSapp/","report_02_design.Rmd")
out <- c(out, knit_child(child, quiet=TRUE))  

asis_output(out)

```

<!-- single omics analysis -->

```{r single_omics, eval=(length(getOmicsTypes(rflomics.MAE)) != 0 )}

out <- NULL
child <- paste0(path.package("RFLOMICS"), "/RFLOMICSapp/","report_01_omics.Rmd") 
out   <- c(out, knit_child(child, quiet=TRUE))  

asis_output(out)

```

<!-- data integration  -->

```{r, include=FALSE}
condMulti <- length(getAnalyzedDatasetNames(rflomics.MAE)$DataProcessing) > 1
condMOFA <- !is.null(getMOFA(rflomics.MAE))
condMO <- !is.null(getMixOmics(rflomics.MAE))
condres <- (condMulti && condMOFA) || (condMulti && condMO)
```



<br>

`r if(!condMulti) {"\\begin{comment}"}`

---

# 7 - Multi-Omics data integration
`r if(!condMulti) {"\\end{comment}"}`

`r if(condMulti && !(condres)) {"<p style=\"color:orange\"> No multi-omics integration analysis was performed on these data.</p>"}` 
<br>

<!-- ## with MOFA -->
`r if(!condres) {"\\begin{comment}"}`
## 7.1 - MOFA2 Integration

`r if(!condMOFA) {"<p style=\"color:orange\"> Integration analysis using MOFA2 was not performed on these data.</p>"}` 

`r if(!condres) {"\\end{comment}"}`

```{r , eval = condMOFA, hide = !condMOFA}
library(MOFA2)
```


```{r MOFA2, eval = condMOFA, hide = !condMOFA }

out <- NULL

out <- c(out, knit_child(paste0(path.package("RFLOMICS"), 
                                       "/RFLOMICSapp/","report_07_MOFA2_integration.Rmd"), quiet=TRUE))

asis_output(out)

```

<!-- ## with MixOmics -->

<br>

`r if(!condres) {"\\begin{comment}"}`
## 7.2 - MixOmics Integration

`r if(!condMO) {"<p style=\"color:orange\"> Integration analysis using mixOmics was not performed on these data.</p>"}` 

`r if(!condres) {"\\end{comment}"}`

```{r, eval = condMO, hide = !condMO}
library(mixOmics)
MOres <- NULL
funcMixo <- NULL
MOres <- getMixOmicsSettings(rflomics.MAE)
funcMixo <- ifelse(MOres$sparsity, "splsda",  "plsda")
funcMixo <- ifelse(length(MOres$selectData) > 1, paste0("block.", funcMixo), funcMixo)
```

```{r , eval = condMO, hide = !condMO }
HTML(paste0("MixOmics analyses were performed on <span style=\"color:blue\">",  
                       paste(MOres$selectData, collapse = ", "), 
                       "</span> datatables with function  <span style=\"color:DarkOrchid\">", 
                       funcMixo, 
                       "</span> applied on each selected response variable (<span style=\"color:blue\">",
                       paste(MOres$selectedResponse, collapse = ", "), 
                       "</span>). Option <span style=\"color:blue\"> scale views </span> was set to", 
                       MOres$scale_views, "."))
```

```{r , eval = condMO, hide = !condMO, results="asis"}
listSel <- NULL
listSel <- MOres$selectionMethod

listSel2 <- lapply(names(listSel),  FUN = function(nam) {
    dat <- listSel[[nam]]
    varNam <- RFLOMICS:::.omicsDic(rflomics.MAE, SE.name = nam)$variableName
    switch(dat[1], 
           "diff" = {
               paste0("Only differentially expressed ", 
                      varNam, 
                      " were processed in the multi-omics analysis, taking the ", 
                      ifelse(dat[2], "union", "intersection"), " of all contrasts lists.")
           },
           "none" = paste0("No features selection was applied, all ",
                           varNam,
                           " were taken into account."))
})
names(listSel2) <- names(listSel)

for (nam in names(listSel2)) {
    cat("* ", nam, ": ", listSel2[[nam]], "\n")
}
rm(listSel, listSel2)
```


```{r mixOmics, eval = condMO, hide = !condMO }

if (MOres$sparsity) {
    HTML(paste0("Sparse analysis was performed on these data with <span style=\"color:blue\">",  
                           MOres$cases_to_try, 
                           "</span> sets of features tested for each datatable."))
}

out <- NULL
for (selectedResponse in MOres$selectedResponse) { 
  
  child <-  paste0(path.package("RFLOMICS"), 
                   "/RFLOMICSapp/",
                   "report_08_mixOmics_integration.Rmd") 
  
  out <- c(out, knit_child(child, quiet = TRUE))  
}

asis_output(out)

```

<br>
<br>

---
# R Session

This information session corresponds to the system/environment used during the analysis.

<details>
<summary>Click here</summary>

```{r session}
metadata(rflomics.MAE)$sessionInfo
```
</details>

<br>

# Bibliographie

<div id="refs"></div>


