---
title: "FROGSSTAT DESeq2: Visualisation (version 4.1.0)"
output: 
  html_notebook:
    code_folding: hide
params:
  # Path to abundances table phyloseq_object
  abundance_data:
    value: x
  # DESeq2 preprocessed input
  dds:
    value: x
  # tested variable
  var:
    value: x
  # optionnal value 1 of the variable to be compared, default== "None"
  mod1:
    value: x
  # optionnal value 2 of the variable to be compared, default== "None"
  mod2:
    value: x
  # adjusted pvalue threshold
  padj_th:
    value: x
  # Type of analysis
  analysis:
    value: x
  # iPath functions svg file
  ipath_over:
    value: x
  ipath_under:
    value: x
  svg_file_over:
    value: x
  svg_file_under:
    value: x
  libdir:
    value: x
---

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```
Phyloseq `r packageVersion("phyloseq")` and DESeq2 `r packageVersion("DESeq2")`

# {.tabset .tabset-fade .tabset-pills}
## Differentially abundant ASV/FUNCTION table

```{r packages, echo=FALSE, code_folding="show"}
library(phyloseq)
# dealing with plots
library(ggplot2)
# dealing with table
library(DT)
library(dplyr)
source(file.path(params$libdir, "process_ipath.R")) 
```

```{r import}
# Import input files
abundances_input <- get(load(params$abundance_data))
dds_input     <- get(load(params$dds))
base_name <- dirname(params$dds)
run_ipath <- ifelse(params$analysis == "ASV", FALSE, TRUE)
result_names <- DESeq2::resultsNames(dds_input)

# doc <- ""
# if(length(result_names) == 2 || params$mod1 == "None"){
#      results <- DESeq2::results(dds, name = result_names[1])
# } else if(length(result_names)>=3){
#      results <- DESeq2::results(dds_input, contrast = c(params$var, params$mod1, params$mod2) )
#      doc <- paste0("You chose to compare ", params$mod1, " to the reference modality ", params$mod2, ". This implies that a positive log2FoldChange means more abundant in ", params$mod1, " than in ", params$mod2, ".")
#      cat(doc)
# }
doc <- ""
# quantitative variable
if(params$mod1 == "None"){
     results <- DESeq2::results(dds, name = result_names[1])
# qualitative variable
} else {
     results <- DESeq2::results(dds_input, contrast = c(params$var, params$mod1, params$mod2) )
     doc <- paste0("You chose to compare ", params$mod1, " to the reference modality ", params$mod2, ". This implies that a positive log2FoldChange means more abundant in ", params$mod1, " than in ", params$mod2, ".")
     cat(doc)
}

#This implies that a positive log2FoldChange means more abundant in low than in high.")
```

Then we extract significant ASVs or FUNCTIONs at the p-value adjusted threshold (after Benjamini Hochberg correction) and enrich results with taxonomic/functional classification and sort the results by pvalue.

```{r da_abundances}

da.abundances_table <- results %>% as_tibble(rownames = "ID") %>% 
	inner_join(tax_table(abundances_input) %>% as.data.frame() %>% mutate(ID = taxa_names(abundances_input)), by = "ID") %>% 
    filter(padj < params$padj_th) %>% 
    arrange(padj)

```

Using customize datatable we can explore the table of differentially expressed abundances
```{r datatable, message=FALSE}

DT_foldchange <- da.abundances_table %>% datatable(filter = "top",                # ==> add filter for each column
                                             extensions = 'Buttons' , 
                                             options = list(dom = '<fBtlip>', 
                                                scrollX = TRUE, 
                                                 # lengthMenu = list(c(5,10,25,50,100,-1),c(5,10,25,50,100,"All")), # ==> trop couteux en mÃ©moire, mÃªme sur petit jeu
                                                 buttons = list(list(
                                                    extend = 'collection',
                                                    buttons = c('csv', 'excel'),
                                                    text = 'Download')
                                                 )
                                             )) %>% 
  formatSignif(columns = c("baseMean", "log2FoldChange", "lfcSE", "stat", "padj", "pvalue"), digits = 6) %>% 
  formatStyle(columns = "log2FoldChange", color = DT::styleInterval(0, c('#759493', '#C6792B')))

DT_foldchange
```

```{r prepare_ipath3, eval=run_ipath, echo=run_ipath}

func_sign_over <- da.abundances_table %>% 
  filter(log2FoldChange > 0 ) %>%
  select(ID, log2FoldChange, padj) %>%
  mutate(ID = gsub("EC:", "EC", ID))

func_sign_under <- da.abundances_table %>% 
  filter(log2FoldChange < 0 ) %>%
  select(ID, log2FoldChange, padj) %>%
  mutate(ID = gsub("EC:", "EC", ID))
# gsub for create valid EC numbers for iPath3.

func_sign_over <- func_sign_over %>% 
  mutate(Color = if_else(log2FoldChange < 1, "#DE894E",
   if_else(log2FoldChange < 2, "#B26E3E ", "#9B6037 "))) %>%
  mutate(Width = if_else(padj < 0.001, "W11",
   if_else(padj < 0.01, "W10", "W9")))

# func_sign_over <- func_sign_over %>% 
#   mutate(Color = if_else(log2FoldChange < 1, "#9B6037",
#    if_else(log2FoldChange < 2, "#B26E3E ", "#DE894E "))) %>%
#   mutate(Width = if_else(padj < 0.001, "W12",
#    if_else(padj < 0.01, "W10", "W8")))

func_sign_under <- func_sign_under %>% 
  mutate(Color = if_else(log2FoldChange > -1, "#B0C6C5",
   if_else(log2FoldChange > -2, "#8EADAC", "#637978")))%>%
  mutate(Width = if_else(padj < 0.001, "W11",
   if_else(padj < 0.01, "W10", "W9")))

# func_sign_over <- func_sign_over %>% 
#   mutate(Color = if_else(log2FoldChange < 1, "#B0C6C5",
#    if_else(log2FoldChange < 2, "#8EADAC	", "#637978	"))) %>%
#   mutate(Width = if_else(padj < 0.001, "W12",
#    if_else(padj < 0.01, "W10", "W8")))

# func_sign_under <- func_sign_under %>% 
#   mutate(Color = if_else(log2FoldChange > -1, "#9B6037",
#    if_else(log2FoldChange > -2, "#B26E3E", "#DE894E")))%>%
#   mutate(Width = if_else(padj < 0.001, "W12",
#    if_else(padj < 0.01, "W10", "W8")))

func_sign_over <- func_sign_over %>% select(ID, Color, Width)
func_sign_under <- func_sign_under %>% select(ID, Color, Width)

dir_name <- dirname(params$svg_file_over)

write.table(func_sign_over, file= params$ipath_over, sep=" ", quote=FALSE, row.names = FALSE)
write.table(func_sign_under, file= params$ipath_under, sep=" ", quote=FALSE, row.names = FALSE)
svg_file_over <- params$svg_file_over
svg_file_under <- params$svg_file_under
```

```{asis, eval=run_ipath, echo=run_ipath}
## iPath3

### Differentially abundant functions visualized with iPath 3.
```
```{r doc_path, eval=run_ipath, echo=run_ipath, message=FALSE}
# Import input files
if(params$mod1 != "None") {
     doc <- paste0("You chose to compare ", params$mod1, " to the reference modality ", params$mod2, ". This implies that the over-abundants pathways (first image) and under-abundant pathways (second image) are involved in ", params$mod1, " condition.")
     cat(doc)
} else {
     doc <- paste0("The first image (green paths) represents the over-abundants involved pathways. The second image (red paths) represents the under-abundant pathways.")
     cat(doc)
}
```

```{asis, eval=run_ipath, echo=run_ipath}
The darker the path, the higher the absolute log2 fold change (<=1, <=2, or >2).

To visualise and explore metabolic pathways with [IPATH3 website](https://pathways.embl.de/),  use the two files 
**ipath_under.tsv** and **ipath_over.tsv** as inputs. 
```

```{r ipath3, eval=run_ipath, echo=run_ipath}
process_iPath(params$ipath_over, svg_file_over)
process_iPath(params$ipath_under, svg_file_under)
```

```{r, eval=run_ipath, echo=run_ipath}
setwd(dir_name)
knitr::include_graphics(basename(params$svg_file_over))

knitr::include_graphics(basename(params$svg_file_under))
```

## Pie chart
```{r pie, fig.width=8, fig.height=6}

da_pie <- data.frame(
    IDs=c("Differentially Abundant (log-fold change > 0)",
            "Differentially Abundant (log-fold change < 0)",
            "Not Differentially Abundant"
        ),
    Number=c(sum(da.abundances_table$log2FoldChange > 0), 
        sum(da.abundances_table$log2FoldChange < 0), 
        nrow(results) - nrow(da.abundances_table)
        )
    )

Pie <- ggplot(da_pie, aes(x="", y=Number, fill=IDs)) + 
           geom_bar(stat = "identity") + 
           coord_polar("y", start=0) + 
           scale_fill_manual(values=c('#8EADAC', '#DE9F73', '#C0C0C0')) + 
           geom_text(aes(label = Number), position = position_stack(vjust = 0.5), color = "white") +
           labs(x = NULL, y = NULL, fill = NULL, title = "Pie chart to view ASVs or FUNCTIONs number of Differential Abundance test") +
           theme_classic() + theme(
                axis.line = element_blank(), 
                axis.text = element_blank(),
                axis.ticks = element_blank())

Pie
```

## Volcano plot
```{r volcano, fig.width=10, fig.height=8}
## Volcano plot by DESeq2
# Add ASV/FUNCTION names
# Add the -log10 pvalue 
# Add the pre-calculated log2 fold change
da_volcano <- data.frame(
    id      = row.names(results),
    evidence = -log10(results$padj), 
    lfc      = results$log2FoldChange)

# Remove rows that have NA values
da_volcano <- na.omit(da_volcano)
# add a threshol line
y_axix_volcano_line <- -log10(params$padj_th)

# Modify dataset to add new coloumn of colors
da_volcano <- da_volcano %>%
    mutate(
    	color = case_when(
    		lfc > 0 & evidence > y_axix_volcano_line ~ "More", 
    		lfc < 0 & evidence > y_axix_volcano_line ~ "Less", 
    		TRUE                                     ~ "Equal"
    	)
    )

# Color corresponds to fold change directionality
volcano_plot <- ggplot(da_volcano, aes(x = lfc, y = evidence)) + 
    geom_point(aes(color = factor(color)), size = 1.75, alpha = 0.8, na.rm = T) + # add gene points
    theme_bw(base_size = 16) +                                                    # clean up theme
    theme(legend.position = "none") +                                             # remove legend 
    ggtitle(label = "Volcano Plot", subtitle = "Colored by effect sign") +        # add title
    xlab(expression(log[2]("FoldChange"))) +                                      # x-axis label
    ylab(expression(-log[10]("adjusted p-value"))) +                              # y-axis label
    geom_vline(xintercept = 0, colour = "grey80", linetype = 2) +                                # add line at 0
    geom_hline(aes(yintercept = y_axix_volcano_line), yintercept = y_axix_volcano_line, colour = "grey80", linetype = 2) +
    annotate(geom = "text", 
        label = paste("padj =", params$padj_th), 
        x = min(da_volcano$lfc), 
        y = y_axix_volcano_line + 0.25, 
        size = 4,
        colour = "black",
        vjust = 0,
        hjust = 0) + # add pvalue threshold           
    scale_color_manual(values = c("More" = "#C6792B", "Less" = "#759493", "Equal" = "#C0C0C0")) # change colors

# Plot figure
volcano_plot + scale_y_continuous(trans = "log1p")
```

## MA plot
```{r ma}

## Could do with built-in DESeq2 function:
DESeq2::plotMA(results, ylim = c(-10,10),
    ylab = "log2 fold change", alpha=params$padj_th,
    cex  = 0.8, colNonSig="#C0C0C0", colSig="#A2A32F", colLine="#C0C0C0",
    main = "Post Normalisation DESeq2: MA plot of log2FoldChange",
    legend = TRUE)
```

## Heatmap plot

```{r echo = FALSE, eval = (nrow(da.abundances_table) == 0)}
cat("There are no differentially abundant ASVs or FUNCTIONs. Please use a higher threshold for the adjusted p-value or consider a different comparison.")
```

```{r heatmap, fig.width=10, fig.height=8, eval = (nrow(da.abundances_table) > 0)}

da.abundances_table <- da.abundances_table %>% arrange(log2FoldChange)
y_title <- ifelse(params$analysis == "ASV", "ASV", "Functions")
if ( length(result_names)==2 || params$mod1 != "None"){

    subtitle <- result_names[2]
    if ( params$mod1!= "None" ){
    	abundances_input <- prune_samples(get_variable(abundances_input, params$var) %in% c(params$mod1, params$mod2), abundances_input)
        subtitle <- paste(sep="", params$var, "_", params$mod1, "_vs_", params$mod2)
    }

    ##Heatmap plot with 2 conditions
	hplot <- plot_heatmap(prune_taxa(da.abundances_table$ID, abundances_input), 
						  taxa.label = NULL,
						  taxa.order = da.abundances_table$ID, low = "yellow", high = "red", na.value = "white") +
                scale_y_discrete(y_title) +  
				facet_grid(as.formula(paste("~", params$var)), scales = "free_x") + 
				ggtitle("Heatmap plot of DA asv or functions, between 2 conditions", subtitle = subtitle)
## Quantitative variable
} else {
    idx <- order(get_variable(abundances_input, params$var))
    my.sample.order <- sample_names(abundances_input)[idx]
    ##Heatmap plot with all conditions of variable
    hplot <- plot_heatmap(prune_taxa(da.abundances_table$ID, abundances_input), 
    					  taxa.label = NULL,
    	                  taxa.order = da.abundances_table$ID, sample.order = my.sample.order, 
    	                  low = "yellow", high = "red", na.value = "white") + 
             scale_y_discrete(y_title) +
             ggtitle(paste0("Heatmap plot of DA asv or functions with samples ordered according to ", params$var))
}
# ggplotly(hplot)
hplot
```
