---
title: "MSstatsPTM TMT Workflow"
author: "Devon Kohler (<kohler.d@northeastern.edu>)"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{MSstatsPTM TMT Workflow}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width=8, 
  fig.height=8
)
```

```{r, message=FALSE, warning=FALSE}
library(MSstatsPTM)
```

This Vignette provides an example workflow for how to use the package 
MSstatsPTM for a TMT dataset.

## Installation

To install this package, start R (version "4.0") and enter:

``` {r, eval = FALSE}
if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("MSstatsPTM")
```

## 1. Workflow

### 1.1 Raw Data Format

**Note: We are actively developing dedicated converters for MSstatsPTM. If you 
have data from a processing tool that does not have a dedicated converter in 
MSstatsPTM please add a github issue 
`https://github.com/Vitek-Lab/MSstatsPTM/issues` and we will add the 
converter.**

We go in depth on all converters included in this package in the 
`MSstatsPTM_LabelFree_Workflow`. For more information about data conversion 
please review the relevant sections there.

### 1.2 Summarization - dataSummarizationPTM_TMT

After loading in the input data, the next step is to use the 
dataSummarizationPTM_TMT function This provides the summarized dataset needed to 
model the protein/PTM abundance. The function will summarize the 
Protein dataset up to the protein level and will summarize the PTM dataset up to
the peptide level. There are multiple options for normalization and missing 
value imputation. These options should be reviewed in the package documentation.

```{r summarize, echo=FALSE, message=FALSE, warning=FALSE}

MSstatsPTM.summary <- dataSummarizationPTM_TMT(raw.input.tmt, 
                                               use_log_file = FALSE, 
                                               append = FALSE, verbose = FALSE)
```

```{r show_summ}
head(MSstatsPTM.summary$PTM$ProteinLevelData)
head(MSstatsPTM.summary$PROTEIN$ProteinLevelData)
```

The summarize function returns a list with PTM and Protein summarization 
information.

### 1.2.1 QCPlot

Once summarized, MSstatsPTM provides multiple plots to analyze the experiment. 
Here we show the quality control boxplot. The first plot shows the modified data
and the second plot shows the global protein dataset.

```{r qcplot, message=FALSE, warning=FALSE}

dataProcessPlotsPTM(MSstatsPTM.summary,
                    type = 'QCPLOT',
                    which.PTM = "allonly",
                    address = FALSE)
```

### 1.2.2 Profile Plot

Here we show a profile plot. Again the top plot shows the modified peptide, and 
the bottom shows the overall protein.

```{r profileplot, message=FALSE, warning=FALSE}
dataProcessPlotsPTM(MSstatsPTM.summary,
                    type = 'PROFILEPLOT',
                    which.Protein = c("Protein_12"),
                    address = FALSE)
```

### 1.3 Modeling - groupComparisonPTM

After summarization, the summarized datasets can be modeled using the 
groupComparisonPTM function. This function will model the PTM and Protein 
summarized datasets, and then adjust the PTM model for changes in overall 
protein abundance. The output of the function is a list containing these three 
models named: `PTM.Model`, `PROTEIN.Model`, `ADJUSTED.Model`.

```{r model, message=FALSE, warning=FALSE}

# Specify contrast matrix
comparison <- matrix(c(1,0,0,-1,0,0,
                       0,1,0,0,-1,0,
                       0,0,1,0,0,-1,
                       1,0,-1,0,0,0,
                       0,1,-1,0,0,0,
                       0,0,0,1,0,-1,
                       0,0,0,0,1,-1),nrow=7, ncol=6, byrow=TRUE)

# Set the names of each row
row.names(comparison)<-c('1-4', '2-5', '3-6', '1-3', 
                         '2-3', '4-6', '5-6')
colnames(comparison) <- c('Condition_1','Condition_2','Condition_3',
                          'Condition_4','Condition_5','Condition_6')
MSstatsPTM.model <- groupComparisonPTM(MSstatsPTM.summary,
                                       ptm_label_type = "TMT",
                                       protein_label_type = "TMT",
                                       contrast.matrix = comparison,
                                       use_log_file = FALSE, append = FALSE)
head(MSstatsPTM.model$PTM.Model)
head(MSstatsPTM.model$PROTEIN.Model)
head(MSstatsPTM.model$ADJUSTED.Model)
```

### 1.3.1 Volcano Plot

The models from the `groupComparisonPTM` function can be used in the model 
visualization function, `groupComparisonPlotsPTM`. Here we show Volcano Plots 
for the models.

``` {r volcano, message=FALSE, warning=FALSE}
groupComparisonPlotsPTM(data = MSstatsPTM.model,
                        type = "VolcanoPlot",
                        which.Comparison = c('1-4'),
                        which.PTM = 1:50,
                        address=FALSE)
```


### 1.3.2 Heatmap Plot

Here we show a Heatmap for the models.

``` {r meatmap, message=FALSE, warning=FALSE}
groupComparisonPlotsPTM(data = MSstatsPTM.model,
                        type = "Heatmap",
                        which.PTM = 1:49,
                        address=FALSE)
```

