### {.tabset}

<details>
<summary>**`r paste0("[",tag,"] ",contrast)`**</summary>

```{r }

# Create a directory to store the results by contrast
contrastDir <- 
  paste0(diffDir[[data]],"/",  tag, "_", 
         RFLOMICS:::contrastName2contrastDir(contrast),"/")

dir.create(path=contrastDir, recursive = TRUE, showWarnings = FALSE)

opts_chunk$set(
  fig.path=paste0(contrastDir,data,"_")
)

evalPCA     <- dim(DiffExpAnal[["TopDEF"]][[contrast]])[1] > 1
evalExport  <- dim(DiffExpAnal[["TopDEF"]][[contrast]])[1] > 0
```


#### Pvalues distribution

```{r pvDistrib-plot, out.width = "100%", fig.cap="*fig: Distribution of non-adjusted p-values. The most desirable shape is a peak of p-values at 0 following by a uniform distribution.*"}

RFLOMICS:::.plotPValue_fast(DiffExpAnal[["DEF"]][[contrast]], 
                            contrastName = contrast)

```

#### MAplot

```{r MA-plot, out.width = "100%", fig.cap=paste0("*fig: MA plot (log2 fold change against mean expression/abundance for the features of a given contrast). Red dots are ", variableName," of interest displaying both large magnitude log-fold-changes (y axis) and high mean expression/abundance (x axis). Only the top 20 ",variableName," DE are labeled.*")}

RFLOMICS:::.plotMA_fast(DiffExpAnal[["DEF"]][[contrast]], 
                   contrastName = contrast, 
                   p.adj.cutoff = p.adj.cutoff,
                   logFC.cutoff = logFC.cutoff)
```



#### VolcanoPlot

```{r Volcano-plot, out.width = "100%", fig.cap=paste0("*fig: VolcanoPlot (-Log10 non-adjusted pvalues against log2 fold change for the features of a given contrast). Red dots are ", variableName," of interest displaying both large magnitude log-fold-changes (x axis) and high statistical significance (y axis). Only the top 20 ",variableName," DE are labeled.*")}

RFLOMICS:::.plotVolcano_fast(DiffExpAnal[["DEF"]][[contrast]],
                        contrastName = contrast,
                        p.adj.cutoff = p.adj.cutoff,
                        logFC.cutoff = logFC.cutoff)

```


```{r heatmap-plot, out.width = "100%", fig.cap=paste0( "*fig: Heatmap was performed on DE ", variableName," expression data table which has been transformed by: ", getTransSettings(dataset.SE)$method," method and normalized by: ", getNormSettings(dataset.SE)$method," method. Clustering was independently performed on samples (row) and centered ", variableName," (column) using euclidian distance and complete aggregation method*"),eval=FALSE }
 
 p <- plotHeatmapDesign(object = dataset.SE, contrastName = contrast)
 ggsave(p, filename = paste0(contrastDir,data,"_Heatmap.png"), width = 6, height = 4)
```


```{r PCAonDE, out.width = "50%",fig.show = "hold", eval = evalPCA, fig.cap=paste0( "fig: Factorials maps formed by the axis 1,2 (left) and 1,3 (right) of the PCA performed on DE features." )}

resTable      <- DiffExpAnal[["TopDEF"]][[contrast]]
newDataset.SE <- dataset.SE[rownames(dataset.SE) %in% row.names(resTable)]
newDataset.SE <- runOmicsPCA(newDataset.SE)  

# p1 <- plotOmicsPCA(newDataset.SE, raw = FALSE,
#             axes=c(1, 2), groupColor="groups")
# ggsave(p1, filename = paste0(contrastDir,data,"_DE_PCA_1-2.png"), width = 6, height = 4)

# p2 <- plotOmicsPCA(newDataset.SE, raw = FALSE,
#             axes=c(1, 3), groupColor="groups")
# ggsave(p2, filename = paste0(contrastDir,data,"_DE_PCA_1-3.png"), width = 6, height = 4)

```


#### Table of DE

* results of the differential expression/abundance statistical analysis:
+ `r variableName` ID
+ logFC: log2 fold change
+ Abundance: mean expression/abundance for the factor's level
+ t: t-statistic (limma-lmFit, prot/metabo)
+ pvalue: p-values
+ Adj.value: adjusted p-value (BH)
+ LR: likelihood ratio test (edgeR-glmLRT, RNAseq)
+ B: log-odds that the prot/metabo is differentially expressed (limma-topTable)
+ Regulation: Up (green) or Down (red) regulated

```{r table , eval = evalExport, fig.cap=paste0( "tab: table presenting the statisticals results of the differential analysis" )}
df <- signif(DiffExpAnal[["TopDEF"]][[contrast]],3)

datatable(df, rownames = TRUE , class = 'cell-border stripe',
          options = list(pageLength = 6, scrollX = F, lengthChange = F)) |> 
  formatStyle(
    'logFC',
    color = styleInterval(c(0), c('red', 'green'))
  )

```


```{r , eval = evalExport}
write.table(DiffExpAnal[["TopDEF"]][[contrast]],
            file=paste0(contrastDir,data,"_DE_Tab_Filt_pval_",
                        getDiffSettings(dataset.SE)[["p.adj.cutoff"]],"_FC_",
                        getDiffSettings(dataset.SE)[["abs.logFC.cutoff"]],".txt"),
            sep="\t",quote=FALSE,col.names=TRUE,row.names=TRUE)

write.table(DiffExpAnal[["DEF"]][[contrast]],
            file=paste0(contrastDir,data,"_DE_Tab_no_filter.txt"),
            sep="\t",quote=FALSE,col.names=TRUE,row.names=TRUE)
```

### {-}