---
title: "TCC-GUI Report"
output: html_document
params:
  CountData: NA
  groupList: NA
  filterLowCount: NA
  normMethod: NA
  testMethod: NA
  iteration: NA
  fdr: NA
  floorpdeg: NA
  runMAPlot: NA
  resultTableInPlot_rows_selected: NA
  GeneAttribute: NA
  maFDR: NA
  fdrColor: NA
  runVolcanoPlot: NA
  CutFC: NA
  Cutpvalue: NA
  resultTableInVolcanalPlot_rows_selected: NA
  downColor: NA
  upColor: NA
  xlabs: NA
  ylabs: NA
  graphicTitle: NA
  
---

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

## Report workflow

Developing now...  

### 0. Setting and read file.

```{r setting, results='asis', echo=FALSE}
library(knitr)
library(TCC)
library(plotly)

cat("#### Input Count Data:")
kable(head(params$CountData))

# TCC Parameters: 
params.tcc <- data.frame(rbind(c("Group info", paste(params$groupList, collapse=' VS ')),
                    c("Filter low count", params$filterLowCount),
                    c("Normalized Method", params$normMethod),
                    c("Test Method", params$testMethod),
                    c("Iteration", params$iteration),
                    c("FDR",params$fdr),
                    c("Floor DEGs p-value", params$floorpdeg)))
colnames(params.tcc) <- c("Parameter", "Value")

# MA-Plot parameters:
params.maplot <- data.frame(rbind(c("MA-Plot FDR", params$maFDR),
                                  c("Color", params$fdrColor)))
colnames(params.maplot) <- c("Parameter", "Value")

# Volcano Plot parameters:
params.volcano <- data.frame(rbind(c("Fold change cutoff", paste(params$CutFC)),
                                   c("P-value cutoff", params$Cutpvalue),
                                   c("Down-regulated genes' color", params$downColor),
                                   c("Up-regulated genes' color", params$upColor),
                                   c("X lable", params$xlabs),
                                   c("Y lable", params$ylabs),
                                   c("Title of volcano plot", params$graphicTitle)))
colnames(params.volcano) <- c("Parameter", "Value")

cat("#### Parameter")
kable(params.tcc)
kable(params.maplot)
kable(params.volcano)

```

### 1. Processing  

Creat TCC Object.

```{r tcc-calculation, results='hold'}
# Get count data table
data <- params$CountData

# Get group information
data.cl <- rep(0, ncol(data))
convert2cl <- function(x, df) {
  grep(x, colnames(df))
}

# Covert group information format
for (i in 1:length(params$groupList)) {
  data.cl[unlist(lapply(params$groupList[[i]], convert2cl, df = data))] = i
}

# Creat TCC Object
tcc <- new("TCC", data[data.cl != 0], data.cl[data.cl != 0])
# Filter low count genes (if necessary).
tcc <- filterLowCountGenes(tcc, low.count = params$filterLowCount)
# Run TCC
tcc <- calcNormFactors(
  tcc,
  norm.method = params$normMethod,
  test.method = params$testMethod,
  iteration = params$iteration,
  FDR = params$fdr,
  floorPDEG = params$floorpdeg
  )

# Estimate differential genes
tcc <- estimateDE(tcc,
                  test.method = params$testMethod,
                  FDR = params$fdr)    

# Result table
resultTable <- getResult(tcc, sort = FALSE)
# Normailized data
norData <- tcc$getNormalizedData()

kable(head(resultTable))
kable(head(norData))
```


### 1. MA-Plot

```{r add-ma-plot, results='asis', echo=FALSE}
# Check MA plot has been generated or not.
if(params$runMAPlot != "") {
  if (is.null(params$resultTableInPlot_rows_selected)) {
    annotation <- list()
  } else {
    markerSelect <- resultTable[params$resultTableInPlot_rows_selected,]
  
    annotation <- list(x = markerSelect$a.value,
                       y = markerSelect$m.value,
                       text = markerSelect[, params$GeneAttribute],
                       xref = "x",
                       yref = "y",
                       showarrow = TRUE,
                       arrowhead = 7,
                       ax = 20,
                       ay = 40
                       )
  }
  
  if (params$testMethod != "wad") {
    x <- cut(resultTable$q.value, breaks = c(0, params$maFDR, 1))
  levels(x) <-
    list("DEG" = paste("(0,", params$maFDR, "]", sep = ""),
         "non-DEG" = paste("(", params$maFDR, ",1]", sep = "")
  )

  plot_ly(data = resultTable,
          x = ~ a.value,
          y = ~ m.value,
          type = "scatter",
          mode = "markers",
          color = ~ x,
          colors = c(params$fdrColor, "#000000"),
          marker = list(size = 3),
          hoverinfo = "text",
          text = ~paste("</br>Gene:", resultTable[, params$GeneAttribute],
                        "</br>A value:", round(a.value, 4),
                        "</br>M value:", round(m.value, 4),
                        "</br>Rank:", rank)) %>%
    layout(xaxis = list(title = "A = (log2(G2)+log2(G1))/2"),
           yaxis = list(title = "M = log2(G2)-log2(G1)"),
           title = paste("MA Plot with FDR <", params$maFDR),
           annotations = annotation
           )
  } else {
    plot_ly(data = resultTable,
            x = ~ as.numeric(a.value),
            y = ~ as.numeric(m.value),
            type = "scatter",
            mode = "markers",
            colors = c("#000000"),
            marker = list(size = 3),
            hoverinfo = "text",
            text = ~paste("</br>Gene:", resultTable[, params$GeneAttribute],
                          "</br>A value:", round(a.value, 4),
                          "</br>M value:", round(m.value, 4),
                          "</br>Rank:", rank)) %>%  layout(
                            xaxis = list(title = "A = (log2(G2)+log2(G1))/2"),
                            yaxis = list(title = "M = log2(G2)-log2(G1)"),
                            title = "MA Plot",
                            annotations = annotation
                            )
  }
} else {
  cat("MA Plot has not been generated.")
}

```

### 2. Volcano Plot

```{r add-volcano-plot, results='asis', echo=FALSE}
if (params$runVolcanoPlot != "") {
  dt <- resultTable
  
  downCut <- params$CutFC[1]
  upCut <- params$CutFC[2]
  pCut <- log2(as.numeric(params$Cutpvalue))
  
  dt$color <- ""
  dt[dt$m.value <= downCut, ]$color <- "Down"
  dt[dt$m.value >= upCut, ]$color <- "Up"
  dt[dt$p.value > as.numeric(params$Cutpvalue), ]$color <- "None"
  dt[dt$m.value <= upCut &
       dt$m.value >= downCut, ]$color <- "None"
  
  x <- factor(dt$color)
  levels(x) <- list("Down" = 0,
                    "None" = 1,
                    "Up" = 2)
  
  if (is.null(params$resultTableInVolcanalPlot_rows_selected)) {
    annotation <- list()
  } else {
    markerSelect <- dt[params$resultTableInVolcanalPlot_rows_selected,]
    
    annotation <- list(
      x = markerSelect$m.value,
      y = -log10(markerSelect$p.value),
      text = markerSelect[, params$GeneAttribute],
      xref = "x",
      yref = "y",
      showarrow = TRUE,
      arrowhead = 7,
      ax = 20,
      ay = 40
    )
  }
  
  plot_ly(
    data = dt,
    x = ~ m.value,
    y = ~ -log10(p.value),
    type = "scatter",
    mode = "markers",
    color = ~ x,
    colors = c(params$downColor, "black", params$upColor),
    marker = list(size = 3),
    hoverinfo = "text",
    text = ~ paste("</br>Gene:",resultTable[, params$GeneAttribute],
                   "</br>A value:", round(a.value, 4),
                   "</br>M value:", round(m.value, 4),
                   "</br>p-value:", round(p.value, 4),
                   "</br>q-value:", round(q.value, 4),
                   "</br>Rank:",rank)) %>%
    layout(
      xaxis = list(title = params$xlabs),
      yaxis = list(title = params$ylabs),
      title = params$graphicTitle,
      annotations = annotation,
      shapes = list(
        list(
          type = 'line',
          y0 =  ~ min(-log10(p.value)),
          y1 =  ~ max(-log10(p.value)),
          x0 = upCut,
          x1 = upCut,
          line = list(dash = 'dot', width = 2)
        ),
        list(
          type = 'line',
          y0 =  ~ min(-log10(p.value)),
          y1 =  ~ max(-log10(p.value)),
          x0 = downCut,
          x1 = downCut,
          line = list(dash = 'dot', width = 2)
        ),
        list(
          type = 'line',
          y0 = -log10(as.numeric(params$Cutpvalue)),
          y1 = -log10(as.numeric(params$Cutpvalue)),
          x0 =  ~ min(m.value),
          x1 =  ~ max(m.value),
          line = list(dash = 'dot', width = 2)
        )
      )
    )
} else {
  cat("Volcano Plot has not been generated.")
}
```
