## ----loadPackages, echo=FALSE, eval=TRUE--------------------------------------
  
suppressPackageStartupMessages({
  library(daVis)
  library(pander)
})
  

## ----optionsChunkDoNotModify, echo = FALSE, message = FALSE, warning = FALSE----
  
## Chunk with options for knitr. This chunk should not be modified.

knitr::opts_chunk$set(
  eval = TRUE,
  echo = TRUE,
  warning = FALSE,
  dpi = 90,
  fig.width = 7,
  fig.height = 4
)


## ----echo = TRUE, eval = FALSE------------------------------------------------
# if (!requireNamespace("BiocManager", quietly = TRUE))
#   install.packages("BiocManager")
# 
# BiocManager::install("daVis")

## ----echo = TRUE, eval = FALSE------------------------------------------------
# library(daVis)

## ----loadData, echo=TRUE, eval=TRUE-------------------------------------------
tmpDir <- tempfile(); dir.create(tmpDir)
exampleData <- createExampleData(
	path = tmpDir, 
	output = c("limma", "edgeR", "deseq2", "topTable")
)
res.limma <- exampleData$limma
res.edger <- exampleData$edgeR
res.deseq <- exampleData$DESeq2
topTableList <- exampleData$topTable

## ----topTable1----------------------------------------------------------------
length(topTableList)


## ----topTable2----------------------------------------------------------------
names(topTableList)


## ----topTable3, echo=FALSE----------------------------------------------------
pander(topTableList[[1]][seq_len(6), c(1, 2, 5, 6, 8, 9)], row.names = FALSE)


## ----limmaModel---------------------------------------------------------------
class(res.limma)


## ----edgerModel---------------------------------------------------------------
class(res.edger)


## ----deseqModel---------------------------------------------------------------
class(res.deseq)


## ----volcanoPlotHelp, eval=FALSE----------------------------------------------
# help("daVolcanoPlot", "daVis")
# 

## ----volcanoPlot-top-genes----------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP")
genesOfInterest <- c("497097", "20671", "239273", "14862", "27395", "76408")

daVolcanoPlot(
  input = res.limma, 
  coef = coefs, 
  coefLabel = c("A", "B"),
  topGenes = 5,
  topGenesVar = "SYMBOL",
  genesToHighlight = genesOfInterest,
  genesToHighlightVar = "SYMBOL",
  colorVar = "adj.P.Val",
  facetNCol = 2
)


## ----volcanoPlot-logFC-threshold, eval=FALSE----------------------------------
# coefs <- c("B.LvsP", "L.LvsP")
# 
# daVolcanoPlot(
#   input = res.limma,
#   coef = coefs,
#   coefLabel = c("A", "B"),
#   facetNCol = 2,
#   additionalThresholdsAdjPValue = 0.1,
#   colorVar = "adj.P.Val"
# )
# 

## ----volcanoPlot-interactive, eval=FALSE--------------------------------------
# coefs <- c("B.LvsP", "L.LvsP")
# 
# daVolcanoPlot(
#   input = res.limma,
#   coef = coefs,
#   coefLabel = c("A", "B"),
#   typePlot = "interactive"
# )
# 

## ----logRatioHelp, eval=FALSE-------------------------------------------------
# help("daLogRatioPlot", "daVis")
# 

## ----logRatio-gene-color------------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")

features <- c(
  "72243", "66704", "11781", "226101", "14620", "381290", "16012", 
  "100504225", "231830", "78896", "103889", "231991", "77034", "19687", 
  "100043805", "16669", "226162", "24117", "55987", "11419" 
)
dt <- topTableList[['B.LvsP']]
adjPVal <- dt[match(features, dt$ENTREZID), "adj.P.Val"]
signFeature <- ifelse(adjPVal <= 0.05, "red", "grey")

daLogRatioPlot(
  input = topTableList,
  features = features,
  featuresIdVar = "ENTREZID",
  featuresVar = c("SYMBOL", "GENENAME"),
  featuresMaxNChar = 35,
  coef = coefs,
  coefLabel = c("A", "B", "C", "D"),
  facetNCol = 4,
  featuresColor = signFeature,
  errorBars = FALSE
)


## ----logRatio-nested-facet----------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")

coefsLabel <- list(
  sub("(.+)\\.(.+)", "\\2", coefs),
  sub("(.+)\\.(.+)", "\\1", coefs)
)

colorPalette <- c(
  `B.PvsV` = "darkgreen", `L.PvsV` = "lightgreen",
  `B.LvsP` = "darkblue", `L.LvsP` = "lightblue"
)

daLogRatioPlot(
  input = topTableList,
  featuresIdVar = "ENTREZID",
  coef = coefs,
  coefLabel = coefsLabel,
  facetNCol = 4,
  errorBars = FALSE,
  color = colorPalette
)

# Note: using coef labels as names of the color palette also works 
# (if coefLabel is NOT a list) - for back-compatibility
# colorPalette <- c(
#   C = "darkgreen", D = "lightgreen",
#   A = "darkblue", B = "lightblue"
# )

## ----logRatio-mixed-input-----------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV", "A")

daLogRatioPlot(
  input = list(res.limma, A = topTableList[["B.LvsP"]]),
  featuresIdVar = "ENTREZID",
  coef = coefs,
  facetNCol = 5,
  errorBars = TRUE
)

## ----logRatio-order-significance----------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")

daLogRatioPlot(
  input = res.limma,
  featuresIdVar = "ENTREZID",
  features = features, featuresOrder = "significance",
  coef = coefs,
  facetNCol = 4,
  errorBars = TRUE
)

## ----logRatio-text-significanceStar-------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")
# Note: the format.pval function can be used to format the p-value as a text
getSignifStar <- function(topTable){
  with(topTable, as.character(
    stats::symnum(
      x = adj.P.Val, 
      cutpoints = c(0, .001, .01, .05, .1, 1),
      symbols = c("***","**","*","."," "),
      corr = FALSE
    )
  ))
}
daLogRatioPlot(
  input = res.limma,
  coef = coefs,
  coefLabel = c("A", "B", "C", "D"),
  color = colorPalette,
  facetNCol = 4,
  text = getSignifStar
)

## ----logRatio-text-logFC------------------------------------------------------
daLogRatioPlot(
  input = res.limma,
  coef = coefs,
  coefLabel = c("A", "B", "C", "D"),
  color = colorPalette,
  facetNCol = 4,
  text = function(topTable)
    with(topTable, round(logFC, digits = 1)),
  textCex = 3
)

## ----heatmapHelp, eval=FALSE--------------------------------------------------
# help("daHeatmap", "daVis")
# 

## ----heatmap-gene-color-------------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")

daHeatmapLogFC(
  input = topTableList,
  features = features,
  featuresIdVar = "ENTREZID",
  featuresVar = c("SYMBOL", "GENENAME"),
  featuresMaxNChar = 35,
  coef = coefs,
  coefLabel = c("A", "B", "C", "D"),
  featuresColor = signFeature
)


## ----heatmap-nested-labels----------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")

coefsLabel <- list(
  sub("(.+)\\.(.+)", "\\2", coefs),
  sub("(.+)\\.(.+)", "\\1", coefs)
)

daHeatmapLogFC(
  input = res.limma,
  features = features,
  featuresIdVar = "ENTREZID",
  featuresVar = c("SYMBOL", "GENENAME"),
  featuresMaxNChar = 35,
  coef = coefs,
  coefLabel = coefsLabel
)


## ----upsetHelp, eval=FALSE----------------------------------------------------
# help("daUpset", "daVis")
# 

## ----upset-down---------------------------------------------------------------
daUpset(
  input = topTableList,
  coef = coefs,
  featuresIdVar = "SYMBOL",
  fdr = 0.05,
  dir = "down",
  ylab = paste("Number of (shared) significantly \n", 
               "down-regulated genes"),
  xlab = paste("Number of significantly \n", 
               "down-regulated genes")
)


## ----upset-return-sets, eval=FALSE--------------------------------------------
# out <- daUpset(
#   input = topTableList,
#   coef = coefs,
#   featuresIdVar = "SYMBOL",
#   fdr = 0.05,
#   dir = "down",
#   ylab = paste("Number of (shared) significantly \n",
#                "down-regulated genes"),
#   xlab = paste("Number of significantly \n",
#                "down-regulated genes"),
#   returnAnalysis = TRUE
# )
# out$sets
# out$plot

## ----pcpHelp, eval=FALSE------------------------------------------------------
# help("daScatterPlot", "daVis")
# 

## ----pcp-genes-of-interest----------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")
genesOfInterest <- c("497097", "20671", "239273", "14862", "27395", "76408")

daScatterPlot(
  input = topTableList,
  coef = coefs,
  coefLabel = c("A", "B", "C", "D"),
  genesToHighlight = genesOfInterest,
  genesToHighlightVar = "SYMBOL",
  topGenes = 5,
  topGenesVar = "SYMBOL",
  facetNCol = 3
)


## ----pcp-interactive-annotation, eval=FALSE-----------------------------------
# coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")
# 
# daScatterPlot(
#   input = topTableList,
#   coef = coefs,
#   coefLabel = c("A", "B", "C", "D"),
#   featuresIdVar = "ENTREZID",
#   featuresVar = c("SYMBOL", "GENENAME"),
#   facetNCol = 3,
#   typePlot = "interactive"
# )
# 

## ----pcp-correlation----------------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")

daScatterPlot(
  input = topTableList,
  coef = coefs[c(1, 2)],
  coefLabel = c("A", "B"),
  featuresIdVar = "ENTREZID",
  correlation = TRUE
)


## ----waterfallHelp, eval=FALSE------------------------------------------------
# help("daWaterfallPlot", "daVis")
# 

## ----waterfall-more-coef------------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")
features <- c(
  "24117", "381290", "226101", "78896", "231830", "16012", "16669", "55987",
  "231991", "14620", "20317", "74747", "11636", "20482", "194126", "270150",
  "17131", "16878", "20564", "73847" 
)

daWaterfallPlot(
  input = res.limma,
  coef = coefs,
  coefLabel = c("A", "B", "C", "D"),
  featuresIdVar = "ENTREZID",
  featuresVar = "SYMBOL",
  features = features,
  facetNCol = 4,
  colorVar = "adj.P.Val",
  color = c("maroon", "orange"),
  fillVar = "adj.P.Val",
  fill = c("maroon", "orange"),
  typePlot = "static"
)


## ----maplotHelp, eval=FALSE---------------------------------------------------
# help("daMAplot", "daVis")
# 

## ----maplot-top-genes-and-genes-of-interest-----------------------------------
coefs <- c("B.LvsP", "L.LvsP")

daMAplot(
  input = res.limma, 
  coef = coefs[1], 
  coefLabel = "A",
  featuresIdVar = "ENTREZID",
  topGenes = 5,
  topGenesVar = "SYMBOL",
  genesToHighlight = genesOfInterest,
  genesToHighlightVar = "SYMBOL",
  direction = TRUE,
  color = c("steelblue", "firebrick", "grey")
)


## ----barplotHelp, eval=FALSE--------------------------------------------------
# help("daSignificantGenesBarplot", "daVis")
# 

## ----barplot-nested-labels----------------------------------------------------
coefs <- c("B.LvsP", "L.LvsP", "B.PvsV", "L.PvsV")

coefsLabel <- list(
  sub("(.+)\\.(.+)", "\\2", coefs),
  sub("(.+)\\.(.+)", "\\1", coefs)
)

daSignificantGenesBarplot(
  input = res.limma, 
  coef = coefs, 
  coefLabel = coefsLabel,
  addPercentage = TRUE
)


## ----sessionInfo--------------------------------------------------------------
sessionInfo()

