---
title: "MSPMS Report"
date: "`r Sys.Date()`"
output:
  html_document:
        theme: cosmo
        toc: true
        toc_depth: 6
        toc_float:
            collapsed: true
            smooth_scroll: true
        code_folding: hide
        highlight: tango
        df_print: paged
params:
  prepared_data: NULL
  peptide_library: mspms::peptide_library
  n_residues: 4
---

```{r setup, include=FALSE}
library(dplyr)
ggplot2::theme_set(ggplot2::theme_bw())
knitr::opts_chunk$set(echo = TRUE,warning = FALSE, message = FALSE)
```

# Info

This report has been automatically generated by the mspms package. 
It contains standard results of the mspms analysis pipeline. 


R session info is as follows:  

```{r}
sessionInfo()
```

# Processed Data 

```{r}
# The standard mspms data processing pipeline is used here. 
processed_qf = mspms::process_qf(prepared_data)

mspms_data = mspms_tidy(processed_qf,"peptides_norm")

DT::datatable(mspms_data)

mspms_data %>%
  downloadthis::download_this(
    output_name = "mspms_data",
    output_extension = ".csv",
    button_label = "Download data",
    button_type = "warning",
    has_icon = TRUE,
    icon = "fa fa-save",
    csv2 = FALSE
  )
```

**Table 1.** Imputed and normalized mspms data. 

# QC Checks 


```{r}
mspms::plot_qc_check(processed_qf)

```

**Figure 1.** Here the results of the qc checks are displayed. 


```{r}
mspms::plot_nd_peptides(processed_qf)
```
  
**Figure 2.** Here the library peptides not detected in each samples are shown. 

# Overview 
```{r}
mspms_tidy_data <- mspms_tidy(processed_qf)
mspms::plot_pca(mspms_tidy_data)
```

**Figure 3.** Here a PCA of the experiment is displayed. The color represents
time, while shape and linetype of eclipse 
(if there are enough points to calculate) represents condition. 


```{r,fig.height = 8, fig.width = 10}
mspms::plot_heatmap(mspms_tidy_data)
```

**Figure 4.** Here an interactive heatmap of the experiment is displayed. 
Mouse over to see more information on each cell. Column side colors 
represent condition and time. 

# Statistics 

```{r}
t2 = mspms::log2fc_t_test(processed_qf) %>% 
  dplyr::arrange(p.adj)

DT::datatable(t2)

t2 %>%
  downloadthis::download_this(
    output_name = "mspms_t-tests",
    output_extension = ".csv",
    button_label = "Download ttests",
    button_type = "warning",
    has_icon = TRUE,
    icon = "fa fa-save",
    csv2 = FALSE
  )
```

**Table 4.** Here T-test results are displayed as compared to time 0 for each 
condition


```{r}
mspms::plot_volcano(t2)
```

**Figure 5.** Here the Log2fc and T tests are displayed as volcano plots 
relative to T0 for each condition. 

# Cleavage Motifs

```{r,fig.height = 14, fig.width = 4}
sig_cleavage_data <- t2 %>% 
  dplyr::filter(p.adj <= 0.05, log2fc >3)

tryCatch({
  print(mspms::plot_all_icelogos(sig_cleavage_data))
}, error = function(e) {
  print(e)
})
```
  
**Figure 6.** Here the cleavage motifs enriched in each condition are shown. 
Enriched cleavage motifs are defined as having a p.adj <= 0.05 and a log2fc >3
relative to time 0. 


# Position Specificity 

```{r}

d = mspms::plot_cleavages_per_pos(sig_cleavage_data)

tryCatch({
  print(d)
}, error = function(e) {
  print(e)
})
```

**Figure 7.** Here the count of significant cleavages at each position of the 
peptide library are shown for each condition (facet) per time point (color). 
Significant cleavages are defined as having a p.adj <= 0.05 and a log2fc > 3 
relative to time 0.  




