---
title: "Paired Mass Distance(PMD) analysis"
author: "Miao Yu"
date: "`r Sys.Date()`"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
```

## Inputs

### Data format

```{r echo=FALSE}
knitr::include_graphics('https://yufree.github.io/presentation/figure/demomzrt.png')
```

The uploaded file should be one csv file. The first column should be peak ID. The second column should be mass to charge ratio of peaks. The third column should be retention time in seconds. The other column should be peaks intensities in each samples. The header should be 'mz', 'rt', and sample names. The second row should include group information of each sample. Such file could be output by `getcsv` function for a 'mzrt' object from enviGCMS package. However, it's also easy to manually write one csv file as shown above. Here is the demo csv [file](https://raw.githubusercontent.com/yufree/rmwf/master/inst/demodata/demomzrt.csv).

### Parameters

- Cutoff for retention time cluster analysis: Hierarchical Clustering cutoff of retention time bins for peaks from the sample compounds. For HPLC, 5-10s is fine while UPLC should be 1-5s or even lower depending on the column efficiency。

- Cutoff of PMD (within RT cluster)'s  retention time group numbers: For a certain PMD, the minimize numbers of this PMD (within RT cluster) appeared across the retention time groups. For example, 10 means at least 10 retention time groups contain this PMD.

- Cutoff of PMD (cross RT clusters) freqency  for structures or reactions: For a certain PMD, the minimize numbers of this PMD appeared across the retention time groups among GlobalStd peaks. 

- Use intensity: If you check this, correlation coefficient cutoff would be used.

- Cutoff for the correlation coefficient: For a certain PMD, the cutoff of Pearson's correlation coefficient.

- Digits of mass to charge ratio: 2 for ToF and 3 for obitrap.

- Measured mass or mass to charge ratio in digits: 4 for ToF and 4 or 5 for obitrap.

```{r globalstd, echo=FALSE}
library(pmd)
inputPanel(
  fileInput('file',label = 'Upload csv file with m/z, retention time, peaklist and group info',accept = c('.csv')),
  sliderInput("rtcutoff", label = "Cutoff for retention time cluster analysis:",
              min = 1, max = 20, value = 9, step = 1),
  sliderInput("ng", label = "Cutoff of PMD (within RT cluster)'s retention time group numbers:",
              min = 5, max = 200, value = 10, step = 1),
  sliderInput("freqcutoff", label = "Cutoff of PMD (cross RT clusters) freqency  for structures or reactions:",
              min = 1, max = 200, value = 10, step = 1),
  checkboxInput("cor", label = 'Use intensity',value = F),
  sliderInput("corcutoff", label = "Cutoff for the correlation coefficient",
              min = 0, max = 1, value = NULL, step = 0.1),
  sliderInput("digits", label = "Digits of mass to charge ratio",
              min = 1, max = 10, value = 2, step = 1),
  sliderInput("accuracy", label = "Measured mass or mass to charge ratio in digits",
              min = 1, max = 10, value = 4, step = 1)
)
```

## Outputs

```{r output}
datainput <- reactive({
                if (!is.null(input$file)){
                        dataraw <- read.csv(input$file$datapath,skip = 1)
                        mz <- dataraw[,2]
                        rt <- dataraw[,3]
                        data <- dataraw[,-c(1:3)]
                        group <- data.frame(t(read.csv(input$file$datapath,nrows = 1)[-(1:3)]))
                        colnames(group) <- c(1:ncol(group))
                        colnames(data) <- rownames(group)
                        rownames(data) <- dataraw[,1]
                        return(list(data=data,mz=mz,rt=rt,group=group))
                }else{
                        return(NULL)
                }
        })
datastd <- reactive({
        list <- datainput()
        if(!is.null(list)){
          if(input$cor){
            list <- globalstd(list,input$rtcutoff,input$ng,digits=input$digits,accuracy = input$accuracy, freqcutoff=input$freqcutoff, corcutoff=input$corcutoff)
          }else{
            list <- globalstd(list,input$rtcutoff,input$ng,digits=input$digits,accuracy = input$accuracy, freqcutoff=input$freqcutoff, corcutoff=NULL)
          }
                
        }
        return(list)
})

stdcsv <- reactive({
        list <- datastd()
        data <- cbind(mz = list$mz[list$stdmassindex], rt = list$rt[list$stdmassindex], list$data[list$stdmassindex,])
        data <- t(cbind(group = t(cbind(mz = 'mz',rt = 'rt',t(list$group))), t(data)))
        return(data)
})

isocsv <- reactive({
        list <- datastd()
        
        return(list$iso)
})
ionscsv <- reactive({
        list <- datastd()
        
        return(list$multi)
})
sdacsv <- reactive({
        list <- datastd()
        
        return(list$sda)
})
```

### Show the retention time groups

```{r plotrtg, echo=FALSE}
diffcsv <- reactive({
        list <- datastd()
        return(list$diff)
})
paircsv <- reactive({
        list <- datastd()
        
        return(list$paired)
})
renderPlot({
        list <- datastd()
        if(!is.null(list)){
                if(sum(list$pairedindex)>0){
                plotrtg(list)
                }else{
                plot(1,1,type = 'n')
                
        }
        }else{
                plot(1,1,type = 'n')
                
        }
})
```

### Show the Global in-group PMDs distribution

```{r plotpaired,echo=FALSE}
renderPlot({
        list <- datastd()
        if(!is.null(list)){
                if(sum(list$pairedindex)>0){
                plotpaired(list) 
                }else{
                plot(1,1,type = 'n')
                
        }
                
        }else{
                plot(1,1,type = 'n')
                
        }
})
```

#### Show the paired mass distances table

```{r}
renderDataTable({
  list <- datastd()
        if(!is.null(list)&sum(list$diffindex)>0){
          list$diff
          }
})
# show the download
downloadHandler('diff.csv', outputArgs = list(label="in-group peaks' PMDs"), content = function(file) {
                data <- diffcsv()
                write.csv(data, file)
        })
```

#### Show the high freqency paired mass distances table

```{r}
renderDataTable({
  list <- datastd()
        if(!is.null(list)&sum(list$pairedindex)>0){
          list$paired
          }
})
# show the download
downloadHandler('paired.csv', outputArgs = list(label="High frequency in-group peaks' PMDs"), content = function(file) {
                data <- paircsv()
                write.csv(data, file)
        })
```

#### Show the isotope related in-group peaks' PMDs table

```{r}
renderDataTable({
  list <- datastd()
        if(!is.null(list)&sum(list$isoindex)>0){
          list$iso
          }
})
# show the download
downloadHandler('iso.csv', outputArgs = list(label="Isotope related in-group peaks' PMDs"), content = function(file) {
                data <- isocsv()
                write.csv(data, file)
        })
```

#### Show the multiple charged ions related in-group peaks' PMDs table

```{r}
renderDataTable({
  list <- datastd()
        if(!is.null(list)&sum(list$multiindex)>0){
          list$multi
          }
})
# show the download
downloadHandler('multi.csv', outputArgs = list(label="Multiple charged ions related in-group peaks' PMDs"), content = function(file) {
                data <- ionscsv()
                write.csv(data, file)
        })
```

### Show the Global cross-group PMDs distribution

```{r plotstdsda,echo=F}
renderPlot({
        list <- datastd()
        if(!is.null(list)){
                if(sum(list$stdmassindex)>0){
                plotstdsda(list)
                }else{
                plot(1,1,type = 'n')
                
        }
                
        }else{
                plot(1,1,type = 'n')
                
        }
})
```

#### Structure/reaction directed analysis results table

```{r}
renderDataTable({
  list <- datastd()
        if(!is.null(list)&sum(list$multiindex)>0){
          list$sda
          }
})
# show the download
downloadHandler('sda.csv', outputArgs = list(label="Structure/reaction directed analysis results"), content = function(file) {
                data <- sdacsv()
                write.csv(data, file)
        })
```

#### Show the globalstd peaks and all peaks

```{r plotstd,echo=FALSE}
renderPlot({
        list <- datastd()
        if(!is.null(list)){
                if(sum(list$pairedindex)>0){
                plotstd(list)
                }else{
                plot(1,1,type = 'n')
                
        }
                
        }else{
                plot(1,1,type = 'n')
                
        }
})
```

#### Download GlobalStd peaks list

```{r }
# show the download
downloadHandler('globalstd.csv', outputArgs = list(label='GlobalStd peaks'), content = function(file) {
                data <- stdcsv()
                write.csv(data, file)
        })

```
