---
title: "MQmetrics Report"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: pdf_document
params:
  input_dir: ''
  remove_contaminants: ''
  log_base: ''
  long_names: ''
  sep_names: ''
  intensity_type: ''
  palette: ''
  UniprotID: ''
  segment_width: ''
  show_shade: ''
  percent_proteins: ''
  show_calibrated_rt: ''
  tolerance: ''
  show_max_value: ''
  peptides_modified: ''
  show_median: ''
  size_median: ''
  binwidth: ''
  plot_unmodified_peptides: ''
  aggregate_PTMs: ''
  combine_same_residue_ptms: ''
  PTM_of_interest: ''
  plots_per_page: ''
---

```{r setup, include=FALSE}
## global options
knitr::opts_chunk$set(echo=FALSE,
                      warning=FALSE,
                      error=TRUE,
                      message=FALSE,
                      comment = NA,
                      fig.height = 12,
                      fig.width = 10)
```

```{r library}
library(MQmetrics)
MQPathCombined <- params$input_dir
MQCombined <- make_MQCombined(MQPathCombined,
                              remove_contaminants = params$remove_contaminants) 
```



```{r ExperimentDuration, strip.white=FALSE}
MaxQuantAnalysisInfo(MQCombined) 
```

\newpage

```{r PlotProteins}

if (! is.null(MQCombined$proteinGroups.txt)) {
  
  PlotProteinsIdentified(MQCombined,
                       intensity_type =params$intensity_type,
                       long_names = params$long_names, 
                       sep_names = params$sep_names, 
                       palette = params$palette)
}else{
  print('proteinGroups.txt is missing, can not create plot')
}


```

\newpage
```{r protein_coverage_all}
if (! is.null(MQCombined$proteinGroups.txt)) {

  PlotProteinOverlap(MQCombined)

}else{
  print('proteinGroups.txt is missing, can not create plot')
}

```

\newpage
```{r PeptidesIdentified}
if ((!is.null(MQCombined$summary.txt )) &&
     (!is.null(MQCombined$parameters.txt)))  {
  
  PlotPeptidesIdentified(MQCombined, 
                       long_names = params$long_names, 
                       sep_names = params$sep_names, 
                       palette = params$palette)
}else{
  print('parameter.txt and/or peptides.txt is missing, can not create plot')
}


```

\newpage
```{r ratio}

if ((!is.null(MQCombined$summary.txt == TRUE )) &&
     (!is.null(MQCombined$parameters.txt)== TRUE))  {
  
  PlotProteinPeptideRatio(MQCombined,
                          intensity_type = params$intensity_type, 
                          long_names = params$long_names, 
                          sep_names = params$sep_names)
  
}else{
  print('summary.txt and/or proteinGroups.txt is missing, can not create plot')
}  
```


\newpage
```{r PlotMSMS}

if ((!is.null(MQCombined$summary.txt == TRUE )) &&
     (!is.null(MQCombined$parameters.txt)== TRUE)) {
  
  PlotMsMs(MQCombined,
           long_names = params$long_names, 
           sep_names = params$sep_names, 
           palette = params$palette)
}else{
  print('summary.txt and/or parameters.txt is missing, can not create plot')
}    
```


\newpage
```{r PlotPeaks}

if ((!is.null(MQCombined$summary.txt == TRUE )) &&
     (!is.null(MQCombined$parameters.txt)== TRUE))  {
  
  PlotPeaks(MQCombined, 
            long_names = params$long_names, 
            sep_names = params$sep_names, 
            palette = params$palette)
}else{
  print('summary.txt and/or parameters.txt is missing, can not create plot')
}   
```


\newpage
```{r isotope}
if ((!is.null(MQCombined$summary.txt == TRUE )) &&
     (!is.null(MQCombined$parameters.txt)== TRUE)) {
  PlotIsotopePattern(MQCombined,
                     long_names = params$long_names, 
                     sep_names = params$sep_names, 
                     palette = params$palette)
}else{
  print('summary.txt and/or parameters.txt is missing, can not create plot')
} 
```


\newpage

```{r n_pages, include = FALSE}
# To avoid the report of printing [[1]] .. plot , [[2]] .., 
# I create a variable called n_pages, that I will use to print the pages of 
# some of the functions directly.

n_pages <- ceiling(nrow(MQCombined$summary.txt)/params$plots_per_page)
```




```{r Charge}
if (! is.null(MQCombined$evidence.txt )) {

  for (page in seq_len(n_pages)) {
    print(
      PlotCharge(MQCombined, 
             palette = params$palette,
             plots_per_page = params$plots_per_page)[[page]]
      )
  }
}else{
  print('evidence.txt is missing, can not create plot')
}   


```

\newpage
```{r missed_cleavages}

if (! is.null(MQCombined$peptides.txt )) {
  
  for(page in seq_len(n_pages)){
    print(
      PlotProteaseSpecificity(MQCombined, 
                          palette = params$palette,
                          plots_per_page = params$plots_per_page)[[page]]
      )
  }
}else{
  print('peptides.txt is missing, can not create plot')
}   
```


\newpage
```{r PlotHydrophobicity}
if (! is.null(MQCombined$peptides.txt )) {
  
  for(page in seq_len(n_pages)){
    print(
      PlotHydrophobicity(MQCombined,
                       palette = params$palette,
                       show_median =  params$show_median,
                       size_median = params$size_median,
                       binwidth = params$binwidth,
                       plots_per_page = params$plots_per_page)[[page]]
      )
  }
}else{
  print('peptides.txt is missing, can not create plot')
} 
```

\newpage
```{r PlotAndromedaScore}
if (! is.null(MQCombined$peptides.txt )) {  

  for(page in seq_len(n_pages)){
    print(
      PlotAndromedaScore(MQCombined, 
                         palette = params$palette,
                         plots_per_page = params$plots_per_page)[[page]]
      )
  }
}else{
  print('peptides.txt is missing, can not create plot')
} 
```




\newpage
```{r PlotIntensity}
if (! is.null(MQCombined$proteinGroups.txt )) {

  PlotIntensity(MQCombined, 
                intensity_type = params$intensity_type, 
                log_base = params$log_base, 
                long_names = params$long_names, 
                sep_names = params$sep_names,
                palette = params$palette)
}else{
  print('proteinGroups.txt is missing, can not create plot')
}   
  
```
\newpage

```{r PlotPCA}
if (! is.null(MQCombined$proteinGroups.txt )) {
  
  PlotPCA(MQCombined,
          intensity_type = params$intensity_type,
          palette = params$palette)

}else{
  print('proteinGroups.txt is missing, can not create plot')
} 
```

\newpage
```{r DynamicRange}
if (! is.null(MQCombined$proteinGroups.txt )) {
  
  PlotCombinedDynamicRange(MQCombined,
                           show_shade = params$show_shade, 
                           percent_proteins = params$percent_proteins) 

}else{
  print('proteinGroups.txt is missing, can not create plot')
} 
```

\newpage
```{r DynamicRangeAll}

if (! is.null(MQCombined$proteinGroups.txt )) {
  
  PlotAllDynamicRange(MQCombined,
                      show_shade = params$show_shade, 
                      percent_proteins = params$percent_proteins)
  
  
}else{
  print('proteinGroups.txt is missing, can not create plot')
} 
```

\newpage

```{r ProteinCoverage}

if ((! is.null(MQCombined$peptides.txt  ) == TRUE) && 
    (!is.null(MQCombined$proteinGroups.txt  ) == TRUE)) {
  
  p <- PlotProteinCoverage(MQCombined,
                            UniprotID = params$UniprotID,
                            log_base = params$log_base,
                            segment_width = params$segment_width,
                            palette = params$palette,
                            plots_per_page = params$plots_per_page)




  if(is.null(params$UniprotID)){
  
      print('No UniprotID provided.')
  
  } else if (is.null(p)) {
    print(paste0('The UniprotID: ',params$UniprotID,
                 ' was not found in the analysis.'))
  }else{
    
    # For each different page
    for(page in seq_len(n_pages)){
      
      # For each different Uniprot 
      for (plot in seq_len(length(p[[1]]))) {
        
              print(p[[page]][[plot]])
        }
      }
  }
  
}else{
  print('proteinGroups.txt and/or peptides.txt is missing, can not create plot')
} 
```



\newpage
```{r irt_peps1}
if (! is.null(MQCombined$evidence.txt )) {


  p <- PlotiRT(MQCombined, 
              show_calibrated_rt = params$show_calibrated_rt,
              tolerance =  params$tolerance,
              plots_per_page = params$plots_per_page)
          
  
  if (is.null(p)) {
    print('iRT peptides not found')
    
  } else{
    
    for(page in seq_len(n_pages)){
      print(p[[page]])
      }
  }

} else{
  print('evidence.txt is missing, can not create plot')
} 
```
\newpage

```{r irt_peps2}
if (! is.null(MQCombined$evidence.txt )) {

  p <-  PlotiRTScore(MQCombined,
                   tolerance =  params$tolerance,
                   plots_per_page = params$plots_per_page)
  
  if (is.null(p)) {
    print('iRT peptides not found')
  } else{
    
    for(page in seq_len(n_pages)){
      print(p[[page]])
    }
  }

} else{
  print('evidence.txt is missing, can not create plot')
} 

```



\newpage
```{r TotalIonCurrent}

if (! is.null(MQCombined$msmsScans.txt )) {
  
  for(page in seq_len(n_pages)){
    print(
      PlotTotalIonCurrent(MQCombined,
                          show_max_value = params$show_max_value,
                          palette = params$palette,
                          plots_per_page = params$plots_per_page)[[page]]
        )
  }
  
}else{
  print('msmsScans.txt is missing, can not create plot')
} 
```


\newpage
```{r PlotAcquisition}

if (! is.null(MQCombined$msScans.txt )) {
  
  for(page in seq_len(n_pages)){
    print(
      PlotAcquisitionCycle(MQCombined, 
                           palette = params$palette,
                           plots_per_page = params$plots_per_page)[[page]]
        )
  }

}else{
  print('msScans.txt is missing, can not create plot')
} 
```

\newpage
```{r PTM}
if (! is.null(MQCombined$modificationSpecificPeptides )) {

  for(page in seq_len(n_pages)){
    print(
      PlotPTM(MQCombined, 
              peptides_modified = params$peptides_modified,
              log_base = params$log_base,
              plot_unmodified_peptides = params$plot_unmodified_peptides,
              palette = params$palette,
              aggregate_PTMs = params$aggregate_PTMs,
              combine_same_residue_ptms = params$combine_same_residue_ptms,
              plots_per_page = params$plots_per_page)[[page]]
        )
  }
  
}else{
  print('modificationSpecificPeptides is missing, can not create plot')
}   
```

\newpage
```{r PTMAcrossSamples}
if (! is.null(MQCombined$modificationSpecificPeptides )) {
  
  p <- PlotPTMAcrossSamples(MQCombined, 
                      PTM_of_interest = params$PTM_of_interest,
                      log_base = params$log_base, 
                      long_names = params$long_names, 
                      sep_names = params$sep_names)
  
  for (index in seq_len(length(params$PTM_of_interest))) {
    print(p[[index]])
  }

}else{
  print('modificationSpecificPeptides is missing, can not create plot')
} 
```

\newpage
```{r, echo=FALSE, message=FALSE, warning=FALSE}
a <- ReportTables(MQCombined,
                  log_base = params$log_base,
                  intensity_type = params$intensity_type,
                  long_names = params$long_names, 
                  sep_names = params$sep_names)


kable(a[[1]], caption = 'Protein Information')
kable(a[[2]], caption =  paste0('Information of: Log',
                                params$log_base,
                                ' ',
                                params$intensity_type))   
kable(a[[3]], caption = 'Charge Information')
kable(a[[4]], caption = 'Peptide hydrophobicity (GRAVY)')
kable(a[[5]], caption = 'Missed Enzymatic Cleavages')
kable(a[[6]], caption = 'Protein Identification Overlap Between Samples')

```
