---
title: "MiMIR report"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
  html_document:
    toc: true
    toc_float:
      collapsed: false
    theme: paper
always_allow_html: true
abstract: "This is a report of the analyses the user performed using MiMIR (Metabolomics-based Models for Imputing Risk), a toolbox enabling data exploration and easy projection of the pre-trained metabolic models developed using Nightingale Health 1H-NMR metabolomics data. With this report you will be able to easily retrieve the pre-processing steps followed and the most useful evaluations of the application. You chose the report without information about the accuracies of the scores on the uploaded dataset."
params:
  metabo_measures: metabo_measures
  mort_score: mort_score
  MetaboAge: MetaboAge
  surrogates: surrogates
  predictors: predictors
  Nmax_miss_metaboAge: Nmax_miss_metaboAge
  Nmax_zero_metaboAge: Nmax_zero_metaboAge
  Nmax_miss_surrogates: Nmax_miss_surrogates
  Nmax_zero_surrogates:  Nmax_zero_surrogates
---

```{r setup, echo=FALSE, results='hide', message=FALSE, warning=FALSE}
# Required packages to load
if (!require("knitr")) install.packages("knitr")
if (!require("tidyr")) install.packages("tidyr")
if (!require("plotly")) install.packages("plotly")
if (!require("plyr")) install.packages("plyr")
```

```{r variables,echo=FALSE}
# IMPORT VARIABLES
metabo_measures<-params$metabo_measures
mort_score<-params$mort_score
MetaboAge<-params$MetaboAge
surrogates<-params$surrogates
predictors<-params$predictors
Nmax_miss_metaboAge<-params$Nmax_miss_metaboAge
Nmax_zero_metaboAge<-params$Nmax_miss_metaboAge
Nmax_miss_surrogates<-params$Nmax_miss_metaboAge
Nmax_zero_surrogates<-params$Nmax_miss_metaboAge
```

# Dataset Characteristics
Dimensions of the uploaded dataset:
```{r showVariables, echo=FALSE}
cat(paste0("Metabolites file dimensions: \nRows= ", dim(metabo_measures)[1], ", Columns= ", dim(metabo_measures)[2]),"\nAll the necessary metabolites were found available!")
```

## Metabolites
### Missing values
The following plot reports the missing values found in the features necessary to calculate the scores. This tab contains a heatmap indicating the available values in grey and missing in white. It also includes two bar plots on the sides: one to show the missingness per sample and the other to show the missing values per metabolite.
Generally, Nightingale Health reports very low missing values in their quantification, but the missingness in the features can affect the values of the scores calculated. For instance, missing values in the 14 metabolites included in the mortality score will cause missingness in the mortality score, while this doesn't happen in MetaboAge thanks to the imputation techniques used
```{r missing metabolites, echo=FALSE}
plot_na_heatmap(t(metabo_measures[,MiMIR::metabolites_subsets$MET57]))
```

### Correlation
Nightingale Health measures are mostly constituted by lipids and are often correlated to one another. Here we report the Pearson correlations of the metabolites needed to calculate each metabolomics-based score. High correlations are indicated in red and low correlations in blue. This plot can help the user to understand the relations between the metabolic features and divide the features in highly correlated groups.
```{r corr metabolites, echo=FALSE}
  res<-cor_assoc(metabo_measures,metabo_measures, MiMIR::metabolites_subsets$MET57, MiMIR::metabolites_subsets$MET57)
  plot_corply(res, main="Metabolites' Correlations", reorder.x=TRUE, abs=F, 
                      resort_on_p= TRUE,reorder_dend=F)
```

### Histogram metabolites
This interactive bar plot shows the distributions of the metabolic features used in the metabolomics-based scores within the uploaded data-set in three batches to allow visibility of the plots.
```{r histograms metabolites, echo=FALSE, out.width="150%", out.height="600%", warning=FALSE}
multi_hist(metabo_measures[,MiMIR::metabolites_subsets$MET63[1:20]])
multi_hist(metabo_measures[,MiMIR::metabolites_subsets$MET63[21:40]])
multi_hist(metabo_measures[,MiMIR::metabolites_subsets$MET63[41:63]])
```

# Predicted scores
In this section we include the results of the metabolomics-based scores in the uploaded data set.

### Quality Control for the predicted values calculated
The selected pre-processing settings for the MetaboAge and the surrogate models, brought to these sample selections:
```{r quality control, echo=FALSE}
cat("The quality control settings chosen for MetaboAge are:\n")
a<-QCprep(as.matrix(metabo_measures[,MiMIR::metabolites_subsets$MET63]),
       MiMIR::PARAM_metaboAge,quiet=FALSE,
       Nmax_miss=Nmax_miss_metaboAge,
       Nmax_zero=Nmax_zero_metaboAge)

cat("The quality control settings chosen for the Surrogates are:\n")
b<- QCprep_surrogates(as.matrix(metabo_measures[,MiMIR::metabolites_subsets$MET63]),
                       MiMIR::PARAM_surrogates,quiet=FALSE,
                       Nmax_miss=Nmax_miss_surrogates,
                       Nmax_zero=Nmax_zero_surrogates)

```


### Missingness of the predicted values
As for the other tables we report the missingness of the metabolomics-based scores.
The missingness reported in this figure is closely related to the one seen in the metabolites features, because the metabolomics-scores are derived from those variables.
```{r missing predicted values, echo=FALSE}
  plot_na_heatmap(t(predictors[,-1]))
```

### Histogram predictors
This interactive bar plot shows the distributions of the metabolomics-based scores within the uploaded data-set in two batches to allow better visibility of the plots.
```{r histograms predicted scores, echo=FALSE, out.width="130%", out.height="400%", warning=FALSE}
  multi_hist(predictors[,-1])
```

### Correlations of the predictors
As for the other tables we report the Pearson's correlations of the metabolomics-based scores. This plot can be used to verify that the scores represent biological markers which correlate to each other accordingly to their original variables' correlations.
```{r corr predictors, echo=FALSE}
res<-cor_assoc(predictors, predictors, colnames(predictors)[-1],colnames(predictors)[-1])
plot_corply(res, main="Correlations of the Metabolic scores", reorder.x=TRUE, abs=F,
                    resort_on_p= TRUE,reorder_dend=F)
```

### Session Info
```{r session info, echo=FALSE}
  sessionInfo()
```
