---
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.**

The first step is to load in the raw dataset for both the PTM and Protein 
datasets. Each dataset can formatted using dedicated converters in `MSstatsPTM`,
such as `MaxQtoMSstatsPTMFormat`, or converters from base `MSstatsTMT` such
as `PDtoMSstatsTMTFormat`, `MaxQtoMSstatsTMTFormat`,
`SpectroMinetoMSstatsTMTFormat`, ect. If using converters from `MSstatsTMT` note 
they will need to be run both on the global protein and PTM datasets.

Please note for the PTM dataset, both the protein and modification site (or 
peptide), must be added into the `ProteinName` column. This allows for the 
package to summarize to the peptide level, and avoid the off chance there are 
matching peptides between proteins. For an example of how this can be done 
please see the code below.

#### 1.1.1 Raw Data Converter

```{r raw_data_ptm, eval = FALSE}
# Run MSstatsPTM converter with modified and unmodified datasets.
raw.input <- MaxQtoMSstatsPTMFormat(raw_ptm_df, annotation, evidence_file, 
                                    proteinGroupsfile)
```

The output of the converter is a list with two formatted data.tables. One each 
for the PTM and Protein datasets.

Given there is not a dedicated MSstatsPTM converter for the processing tool, 
base MSstats converters can be used as follows. Please note ProteinName column 
must be a combination of the Protein Name and sitename.

```{r raw_data, eval = FALSE}
# Add site into ProteinName column
raw_ptm_df$ProteinName <- paste(raw_ptm_df$ProteinName,
                                raw_ptm_df$Site, sep = "_")

# Run MSstats Converters
PTM.data <- MaxQtoMSstatsTMTFormat(raw_ptm_df)
PROTEIN.data <- MaxQtoMSstatsTMTFormat(raw_protein_df)

# Combine into one list
raw.input <- list(PTM = PTM.data,
                  PROTEIN = PROTEIN.data)
```

Both of these conversion methods will output the same results.

``` {r}
head(raw.input.tmt$PTM)
head(raw.input.tmt$PROTEIN)
```

### 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,
                                       data.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)
```

