---
title: "shinyÉPICo Workflow Report"
output: 
  html_document:
    toc: true
    toc_float:
        collapsed: true
  theme: sandstone
  highlight: pygments
toc-title: "Table of contents"
params:
  #input 
  rval_sheet: NA
  rval_sheet_target: NA
  name_var: NA
  grouping_var: NA
  donor_var: NA
  
  #normalization 
  normalization_mode: NA
  dropsnps: NA
  dropcphs: NA
  dropsex: NA
  maf: NA
  probes: NA
  
  #DMP 
  limma_voi: NA
  limma_covar: NA
  limma_inter: NA
  limma_arrayweights: NA
  limma_ebayes_trend: NA
  limma_ebayes_robust: NA
  

  rval_design: NA
  rval_contrasts: NA
  rval_voi: NA
  rval_dendrogram: NULL
  
  #heatmap 
  min_deltabeta: NA
  max_fdr: NA
  max_pvalue: NA
  clusteralg: NA
  groups2plot: NA
  contrasts2plot: NA
  Colv: NA
  distance: NA
  scale: NA
  removebatch: NA
  
  #DMRs
  dmrs_contrasts: NULL
  dmrs_rval_dendrogram: NULL
  dmrs_min_deltabeta: NA
  dmrs_max_fdr: NA
  dmrs_max_pvalue: NA
  dmrs_clusteralg: NA
  dmrs_groups2plot: NA
  dmrs_contrasts2plot: NA
  dmrs_regions2plot: NA
  dmrs_Colv: NA
  dmrs_distance: NA
  dmrs_scale: NA
  dmrs_removebatch: NA
  table_dmrs: NA
  filteredmcsea2heatmap: NULL
  
  #plots

  plot_densityplotraw: NA
  plot_densityplot: NA
  plot_pcaplot: NA
  plot_corrplot: NA
  plot_boxplotraw: NA
  plot_boxplot: NA
  plot_qcraw: NA
  plot_bisulfiterawII: NA
  plot_sexprediction: NA
  plot_snpheatmap: NA
  plot_plotSA: NA
  
  table_pcaplot: NA 
  table_corrplot: NA 
  data_sexprediction: NA
  
  table_dmps: NA
  filteredlist2heatmap: NULL
---

```{r, out.width = "150px", echo=FALSE}
knitr::include_graphics("images/logo.png")
```
<br>
This report was created with shinyÉPICo. 
<br>
&copy; 2020 Octavio Morante-Palacios
<br>

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

knitr::knit_engines$set(asis = function(options) {
  if (options$echo && options$eval) knitr::knit_child(text = options$code)
})

```


```{css, echo=FALSE}
div, code {overflow-x:auto !important}
```

## Variable and samples selection

The group variable selected was __`r params$grouping_var`__, and the donor variable was __`r params$donor_var`__ .

Finally, the selected samples added to the RGSet file were:

```{r sample_sheet, echo=FALSE, warning=FALSE, message = FALSE}
DT::datatable(params$rval_sheet_target, extensions = 'Buttons',
    style = "bootstrap",
    rownames = FALSE,
    selection = "single",
    options = list(dom = 'Blfrtip',
    columnDefs = list(list(
      targets = match("Basename",colnames(params$rval_sheet_target)) - 1, visible = FALSE)),
     #scrollX = TRUE,
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
    lengthMenu = list(c(10,25,50,-1), c(10,25,50,"All"))))
```

The following samples were excluded for the analysis:

```{r samples_excluded, echo=FALSE, warning=FALSE, message = FALSE, comment=NA}

cat(params$rval_sheet[[params$name_var]][!(params$rval_sheet[[params$name_var]] %in% params$rval_sheet_target[[params$name_var]])], 
    sep = ", ")

```

<br>

## Bad probes filtering

After Minfi Normalization, 'bad' probes in RGChannelSet are calculated using a standard p.value threshold (0.01). They will be filtered in the normalized data:

```{r bad_probes, eval=FALSE, echo=TRUE}
  detP <- minfi::detectionP(rgset)
  bad_pos <- row.names(as.data.frame(detP))[rowMeans(detP) > detectP]
```

<br>

## QC and Array Normalization

The selected normalization method was __`r params$normalization_mode`__. In the next plots, we can see the comparison of the processed data with the raw data.

<br>

### QC Signal plot

In this plot, the average methylated signal vs the average unmethylated signal of each sample is represented. Both average signals should be higher than the signal threshold (10) to be considered OK. Lower or very variable signals can reflect problems in the sample material or in the scanning, but the user is responsible for deciding whether a sample should be excluded.

```{r qc_signal_plot, echo=FALSE, warning=FALSE, message = FALSE}
params$plot_qcraw
```

### QC Bisulfite Conversion plot

Illumina 450k and EPIC arrays have probes to determine if bisulfite conversion has been successful or not. In this graph, the minimum ratio of converted/non-converted signal of bisulfite conversion II probes for each sample is represented.

```{r bisulfite_conversion_plot, echo=FALSE, warning=FALSE, message = FALSE}
params$plot_bisulfiterawII

```

### Density plot  

__Raw__

```{r density_plot_raw, echo=FALSE, warning=FALSE, message = FALSE}
params$plot_densityplotraw
```

__Processed (`r params$normalization_mode`)__

```{r density_plot_processed, echo=FALSE, warning=FALSE, message = FALSE}
params$plot_densityplot
```


<br>

### Boxplot

__Raw__

```{r boxplot_raw, echo=FALSE, warning=FALSE, message = FALSE}
   params$plot_boxplotraw
```

__Processed (`r params$normalization_mode`)__

```{r boxplot_processed, echo=FALSE, warning=FALSE, message = FALSE}
   params$plot_boxplot
```

### SNPs Heatmap

The methylation array has 65 specific SNP probes. These SNP probes are intended to be used for sample tracking and sample mixups. Each SNP probe ought to have values clustered around 3 distinct values corresponding to homo-, and hetero-zygotes. Therefore, different samples of the same donor should cluster together.

__Processed (`r params$normalization_mode`)__

```{r snps_heatmap, echo=FALSE, warning=FALSE, message = FALSE}
params$plot_snpheatmap
```

### Sex Prediction

Depending on the average chromosome Y signal and the average chromosome X signal is possible to predict the sex of the sample donors.

```{r sex_prediction_plot, echo=FALSE, warning=FALSE, message = FALSE}
params$plot_sexprediction
```

```{r sex_prediction_table, echo=FALSE, warning=FALSE, message = FALSE}
knitr::kable(data.frame(name=params$rval_sheet_target[[params$name_var]],sex=params$data_sexprediction))
```

### Principal Component Analysis

Plot of PCA is shown, with the selected principal components.

```{r pca_plot, echo=FALSE, warning=FALSE, message = FALSE}
params$plot_pcaplot
```

```{r pca_table, echo=FALSE, warning=FALSE, message = FALSE}
DT::datatable(
  format(params$table_pcaplot, digits=2),
  selection = "single",
  rownames = TRUE,
  style = "bootstrap",
  class = "table-condensed",
  options(list(
    #scrollX = TRUE,
    lengthChange = FALSE,
    paging = FALSE,
    searching = FALSE,
    info = FALSE,
    ordering = FALSE
  ))
)
```

### Correlations

Correlating principal components with variables we can determine if Beta values are related to our variable of interest or other variables. This can also be useful to determine possible errors in the sample hybridization randomization and to select covariables to add to the linear model.


```{r corrplot, echo=FALSE, warning=FALSE, message = FALSE}
params$plot_corrplot
```

Not useful variables are discarded and the variable type is autodetected. The autodetected variable types were:

```{r corrtable, echo=FALSE, warning=FALSE, message = FALSE}
DT::datatable(params$table_corrplot, extensions = 'Buttons',
    style = 'bootstrap',
    rownames = FALSE,
    selection = "single",
    options = list(dom = 'Blfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
    lengthMenu = list(c(10,25,50,-1),
    c(10,25,50,"All"))))
```


## Predict sex and drop SNPs 

After Minfi normalization, we obtained a GenomicRatioSet with some transformations depending on the type of normalization chosen. To understand better the different classes of Minfi packages, and their relations depending of the normalization method, please read this [vignette](https://www.bioconductor.org/help/course-materials/2015/BioC2015/methylation450k.html) of Minfi creators (it is also valid for Illumina EPIC arrays).

Options selected:

* Drop SNPs: __`r params$dropsnps`__ *`r if(params$dropsnps) paste('minimum MAF filtered:', params$maf)`*
* Drop CpHs: __`r params$dropcphs`__
* Drop X/Y Chromosomes: __`r params$dropsex`__

Moreover, we also removed SNP loci, CpHs and X/Y chromosome positions if these options are selected:

```{r dropsnps, eval=FALSE, echo=TRUE}
minfi::dropLociWithSnps(gset, maf = 0))

```

```{r dropcphs, eval=FALSE, echo=TRUE}
minfi::dropMethylationLoci(gset, dropRS = TRUE, dropCH = TRUE)

```

```{r dropsex, eval=FALSE, echo=TRUE}
gset[rownames(minfi::getAnnotation(gset))[!(minfi::getAnnotation(gset)$chr %in% c("chrX","chrY"))],]
```

Finally, we added sex information:

```{r addsex, eval=FALSE, echo=TRUE}
minfi::addSex(gset)

```


After normalization, __`r params$probes`__ DNA methylation positions remained.

<br>

## Model generation

Then, the linear model was generated using __`r params$limma_voi`__ as variable of interest,  __`r if(length(params$limma_covar) == 0) "None" else paste(params$limma_covar, collapse=", ")`__ as covariable(s), and __`r if(length(params$limma_inter) == 0) "None" else paste(params$limma_inter, collapse=", ")`__ as interaction(s). 

The resulting design matrix was:

```{r design, echo=FALSE, warning=FALSE, message = FALSE}
DT::datatable(
  params$rval_design,
  selection = "single",
  rownames = TRUE,
  style = "bootstrap",
  options(list(
    #scrollX = TRUE,
    lengthChange = FALSE,
    searching = FALSE
  ))
)
```

Optionally, ArrayWeights can be activated to weigh the value of each sample depending on their adjust to the linear model:

```{r limma, echo=TRUE, eval=FALSE}

      if (weights_option){
        try({weights = limma::arrayWeights(MValues, design = design})
      }
        else { weights = NULL}
        
      fit = limma::lmFit(MValues, design, weights = weights)

```

Where `weights` is the option specified in the form. In this case, weights is  __`r params$limma_arrayweights`__ .


The relation variance/average of the fit model was:
```{r fit, echo=FALSE, warning=FALSE, message = FALSE}
  params$plot_plotSA

```

With this sample groups, the possible contrasts were:

```{r contrasts, echo=FALSE, warning=FALSE, message = FALSE, comment = NA}

cat(params$rval_contrasts, sep = "\n")

```

Using the calculated model, the t-statistics and p.values are obtained automatically for each contrast:

```{r ebayes, eval=FALSE, echo=TRUE}
  mcontrast = makeContrasts(contrasts=contrast, levels = design)
  fitting = contrasts.fit(fit, mcontrast)
  fitting = eBayes(fitting, trend=trend, robust=robust)
  toptable = topTable(fitting, coef= 1,  adjust.method = "fdr", number =  Inf)

```

Where `trend` and `robust` are the options specified in the form. In this case, trend is  __`r params$limma_ebayes_trend`__ and robust is __`r params$limma_ebayes_robust`__ .


## Filtered results (DMPs)

For each contrast ( __`r paste(params$dmrs_contrasts, collapse=", ")`__ ) , the statistics for each CpG were generated and, after, filtered with these criteria:

* Max. FDR: __`r params$max_fdr`__
* Min. Δβ:   __`r params$min_deltabeta`__
* Max. pvalue:   __`r params$max_pvalue`__

### Table with all contrasts (DMPs)

```{r filtered_table_all,echo=FALSE,message=FALSE, warning=FALSE}
knitr::kable(params$table_dmps)
```

### Custom heatmap (DMPs)

* Column dendogram:  __`r params$Colv`__
* Scale: __`r params$scale`__
* Cluster Algorithm: __`r params$clusteralg`__
* Distance: __`r params$distance`__

The data used for the representation are  __`r if(!params$removebatch) "the normalized beta values" else "the normalized beta values after removing covariable effects on the data (with the limma removeBatchEffect function), preserving the variable of interest"`__.

In the custom heatmap the selected contrasts to plot were: __`r paste(params$contrasts2plot, collapse=", ")`__ and the groups were: __`r paste(params$groups2plot, collapse=", ")`__ .

```{r heatmap_dmps,echo=FALSE,message=FALSE, warning=FALSE, fig.width = 9, fig.height = 12}

    if(!is.null(params$filteredlist2heatmap)) create_heatmap(params$filteredlist2heatmap, factorgroups =  factor(params$rval_voi[params$rval_voi %in% params$groups2plot], levels = params$groups2plot), groups2plot = params$rval_voi %in% params$groups2plot, Colv = as.logical(params$Colv), clusteralg = params$clusteralg, distance = params$distance, scale = params$scale, static=TRUE, ColSideColors = TRUE, RowSideColors = params$rval_dendrogram) else cat("Differences are not in the plotting range (<12000, >1)")
  
```

DMPs in heatmap:  __`r if(is.null(nrow(params$filteredlist2heatmap))) "None" else nrow(params$filteredlist2heatmap)`__ 

<br>



```{asis filt_res, echo=TRUE, eval=!is.null(params$dmrs_contrasts)}
## Filtered results (DMRs)

For each contrast ( __`r paste(params$rval_contrasts, collapse=", ")`__ ) , the statistics for each CpG were generated and, after, filtered with these criteria:

* Max. FDR: __`r params$dmrs_max_fdr`__
* Min. Δβ:   __`r params$dmrs_min_deltabeta`__
* Max. pvalue:   __`r params$dmrs_max_pvalue`__
```

### Table with all contrasts (DMRs)

```{r filtered_table_all_dmrs,echo=FALSE,message=FALSE, warning=FALSE}
if(!is.null(params$dmrs_contrasts)) knitr::kable(params$table_dmrs) else cat("DMRs were not calculated in this analysis.")
```


```{asis custom_heatmap, echo=TRUE, eval=!is.null(params$dmrs_contrasts)}
### Custom heatmap (DMRs)

* Column dendogram:  __`r params$dmrs_Colv`__
* Scale: __`r params$dmrs_scale`__
* Cluster Algorithm: __`r params$dmrs_clusteralg`__
* Distance: __`r params$dmrs_distance`__

The data used for the representation are  __`r if(!params$dmrs_removebatch) "the normalized beta values" else "the normalized beta values after removing covariable effects on the data (with the limma removeBatchEffect function), preserving the variable of interest"`__.

In the custom heatmap the selected contrasts to plot were: __`r paste(params$dmrs_contrasts2plot, collapse=", ")`__ and the groups were: __`r paste(params$dmrs_groups2plot, collapse=", ")`__ .
```

```{r heatmap_dmrs,echo=FALSE,message=FALSE, warning=FALSE, fig.width = 9, fig.height = 12}

if(!is.null(params$filteredmcsea2heatmap) & !is.null(params$dmrs_contrasts)) create_heatmap(params$filteredmcsea2heatmap, factorgroups =  factor(params$rval_voi[params$rval_voi %in% params$dmrs_groups2plot], levels = params$dmrs_groups2plot), groups2plot = params$rval_voi %in% params$dmrs_groups2plot, Colv = as.logical(params$dmrs_Colv), clusteralg = params$dmrs_clusteralg, distance = params$dmrs_distance, scale = params$dmrs_scale, static=TRUE, ColSideColors = TRUE, RowSideColors = params$dmrs_rval_dendrogram) else cat("Differences are not in the plotting range (<12000, >1)")
  
```

DMRs in heatmap:  __`r if(is.null(nrow(params$filteredmcsea2heatmap))) "None" else nrow(params$filteredmcsea2heatmap)`__ 

## System and analysis information

This report was generated at __`r Sys.time()`__.

The session information was the following:

```{r system_info, echo=FALSE, message=FALSE, warning=FALSE, comment=NA}
print(sessionInfo())
```






