---
title: "FROGS Phyloseq: Alpha Diversity Visualisation (version `r params$version`)"
copyright: "Copyright (C) 2024 INRAE"
license: "GNU General Public License"
version: "5.0.0"
output: 
  html_notebook:
    code_folding: hide
params:
   phyloseq:
      value: x
   measures:
      value: x
   varExp:
      value: x
   fileAlpha:
      value: x
   libdir:
      value: x
   version:
      value: x
---

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="author" content="Ta Thi Ngan - SIGENAE/GABI & Maria Bernard - SIGENAE/GABI" />
<meta name="version" content="5.0.0" />
<meta name="copyright" content="Copyright (C) 2024 INRAE" />

<script>
function update_theme(val){
				if(val == "Coral" ){
					/*
					:root {
						--frogsColor: #388697; 
						--frogsColorHover: rgb(12,10,62,0.8);
						--frogsColorShadow: #6c5b64;
						--frogsColor2: #FA8883;
						--frogsColor3: #EFC7C2;
						--frogsColor4: #737f75;
						--frogsColorZoomHighCharts: rgb(191,211,193,0.25);
						--frogsButtonColor: rgb(105,79,93,0.6);
						--frogsButtonBorderColor: rgb(12,10,62,0.6);
						--frogsOddTable: rgba(249,86,79, 0.1);
						--frogsCircleFontColor: #FFF;
						--frogsCircleBackgroundColor: #FA8883;
						--frogsCircleBorderColor: #FA8883;
						--frogsPreColor: rgba(191, 211, 193, 0.25);
					}
					*/
					document.documentElement.style.setProperty('--frogsColor', "#388697");
					document.documentElement.style.setProperty('--frogsColorHover', "rgb(12,10,62,0.8)");
					document.documentElement.style.setProperty('--frogsColorShadow', "#6c5b64");
					document.documentElement.style.setProperty('--frogsColor2', "#FA8883");
					document.documentElement.style.setProperty('--frogsColor3', "#ffef99");
					document.documentElement.style.setProperty('--frogsColor4', "#737f75");
					document.documentElement.style.setProperty('--frogsColorZoomHighCharts', "rgb(191,211,193,0.25)");
					document.documentElement.style.setProperty('--frogsButtonColor', "rgb(12,10,62,0.6)");
					document.documentElement.style.setProperty('--frogsButtonBorderColor', "rgb(12,10,62,0.8)");
					document.documentElement.style.setProperty('--frogsOddTable', "rgba(249,86,79, 0.1)");
					document.documentElement.style.setProperty('--frogsCircleFontColor', "#FFF");
					document.documentElement.style.setProperty('--frogsCircleBackgroundColor',"#FA8883");
					document.documentElement.style.setProperty('--frogsCircleBorderColor',"#FA8883");
					document.documentElement.style.setProperty('--frogsPreColor', "rgba(191, 211, 193, 0.25)");
					
					var root = document.documentElement;
					var style = getComputedStyle(root);
					var frogsColor = style.getPropertyValue('--frogsColor');
					var frogsColor2 = style.getPropertyValue('--frogsColor2');
					var frogsColor3 = style.getPropertyValue('--frogsColor3');
					var frogsColor4 = style.getPropertyValue('--frogsColor4');
					var frogsColorZoomHighCharts = style.getPropertyValue('--frogsColorZoomHighCharts');
					var frogsColorHover = style.getPropertyValue('--frogsColorHover');
					var frogsButtonColor = style.getPropertyValue('--frogsButtonColor');
					var frogsCircleFontColor = style.getPropertyValue('--frogsCircleFontColor');
					var frogsColorShadow = style.getPropertyValue('--frogsColorShadow');
					
					
				}else if(val == "Plum" ){
					/*
					:root {
						--frogsColor: #694F5D; 
						--frogsColorHover: rgb(105,79,93,0.8);
						--frogsColorShadow: #6c5b64;
						--frogsColor2: #BFD3C1;
						--frogsColor3: #EFC7C2;
						--frogsColor4: #737f75;
						--frogsColorZoomHighCharts: rgb(222,159,115,0.25);
						--frogsButtonColor: rgb(105,79,93,0.6);
						--frogsButtonBorderColor: rgb(105,79,93,0.8);
						--frogsOddTable: #f2f2f2;
						--frogsCircleFontColor: #694F5D;
						--frogsCircleBackgroundColor: #BFD3C1;
						--frogsCircleBorderColor: #BFD3C1;
					}*/
					document.documentElement.style.setProperty('--frogsColor', "#694F5D");
					document.documentElement.style.setProperty('--frogsColorHover', "rgb(105,79,93,0.8)");
					document.documentElement.style.setProperty('--frogsColorShadow', "#6c5b64");
					document.documentElement.style.setProperty('--frogsColor2', "#BFD3C1");
					document.documentElement.style.setProperty('--frogsColor3', "#4f5d69");
					document.documentElement.style.setProperty('--frogsColor4', "#737f75");
					document.documentElement.style.setProperty('--frogsColorZoomHighCharts', "rgb(222,159,115,0.25)");
					document.documentElement.style.setProperty('--frogsButtonColor', "rgb(105,79,93,0.6)");
					document.documentElement.style.setProperty('--frogsButtonBorderColor', "rgb(105,79,93,0.8)");
					document.documentElement.style.setProperty('--frogsOddTable', "#f2f2f2");
					document.documentElement.style.setProperty('--frogsCircleFontColor', "#694F5D");
					document.documentElement.style.setProperty('--frogsCircleBackgroundColor',"#BFD3C1");
					document.documentElement.style.setProperty('--frogsCircleBorderColor',"#BFD3C1");
					document.documentElement.style.setProperty('--frogsPreColor', "rgba(191, 211, 193, 0.25)");
					
					var root = document.documentElement;
					var style = getComputedStyle(root);
					var frogsColor = style.getPropertyValue('--frogsColor');
					var frogsColor2 = style.getPropertyValue('--frogsColor2');
					var frogsColor3 = style.getPropertyValue('--frogsColor3');
					var frogsColor4 = style.getPropertyValue('--frogsColor4');
					var frogsColorZoomHighCharts = style.getPropertyValue('--frogsColorZoomHighCharts');
					var frogsColorHover = style.getPropertyValue('--frogsColorHover');
					var frogsButtonColor = style.getPropertyValue('--frogsButtonColor');
					var frogsCircleFontColor = style.getPropertyValue('--frogsCircleFontColor');
					var frogsColorShadow = style.getPropertyValue('--frogsColorShadow');
					
					
				}else if(val == "FROGS4" ){
					/*
					:root {
						--frogsColor: #8eadac; 
						--frogsColorHover: #648a89;
						--frogsColorShadow: #648a89;
						--frogsColor2: #de9f73;
						--frogsColor3: #ad8e9f;
						--frogsColor4: #a2a32f;
						--frogsColorZoomHighCharts: rgb(100, 138, 137, 0.25);
						--frogsButtonColor: #8EADAC;
						--frogsButtonBorderColor: #8EADAC;
						--frogsOddTable: #f2f2f2;
						--frogsCircleFontColor: #FFF;
						--frogsCircleBackgroundColor: #8eadac;
						--frogsCircleBorderColor: #648a89;
					}*/
					document.documentElement.style.setProperty('--frogsColor', "#8eadac");
					document.documentElement.style.setProperty('--frogsColorHover', "#648a89");
					document.documentElement.style.setProperty('--frogsColorShadow', "#648a89");
					document.documentElement.style.setProperty('--frogsColor2', "#de9f73");
					document.documentElement.style.setProperty('--frogsColor3', "#ad8e9f");
					document.documentElement.style.setProperty('--frogsColor4', "#a2a32f");
					document.documentElement.style.setProperty('--frogsColorZoomHighCharts', "rgb(100, 138, 137, 0.25)");
					document.documentElement.style.setProperty('--frogsButtonColor', "#8EADAC");
					document.documentElement.style.setProperty('--frogsButtonBorderColor', "#8EADAC");
					document.documentElement.style.setProperty('--frogsOddTable', "#f2f2f2");
					document.documentElement.style.setProperty('--frogsCircleFontColor', "#FFF");
					document.documentElement.style.setProperty('--frogsCircleBackgroundColor',"#8eadac");
					document.documentElement.style.setProperty('--frogsCircleBorderColor',"#648a89");
					document.documentElement.style.setProperty('--frogsPreColor', "rgba(226, 234, 227, 0.25)");
					
					var root = document.documentElement;
					var style = getComputedStyle(root);
					var frogsColor = style.getPropertyValue('--frogsColor');
					var frogsColor2 = style.getPropertyValue('--frogsColor2');
					var frogsColor3 = style.getPropertyValue('--frogsColor3');
					var frogsColor4 = style.getPropertyValue('--frogsColor4');
					var frogsColorZoomHighCharts = style.getPropertyValue('--frogsColorZoomHighCharts');
					var frogsColorHover = style.getPropertyValue('--frogsColorHover');
					var frogsButtonColor = style.getPropertyValue('--frogsButtonColor');
					var frogsCircleFontColor = style.getPropertyValue('--frogsCircleFontColor');
					var frogsColorShadow = style.getPropertyValue('--frogsColorShadow');
					
				 }
			}
</script>

<style type="text/css">
:root {
			--frogsColor: #388697; 
			--frogsColorHover: rgb(12,10,62,0.8);
			--frogsColorShadow: #6c5b64;
			--frogsColor2: #FA8883;
			--frogsColor3: #EFC7C2;
			--frogsColor4: #737f75;
			--frogsColorZoomHighCharts: rgb(191,211,193,0.25);
			--frogsButtonColor: rgb(12,10,62,0.6);
			--frogsButtonBorderColor: rgb(12,10,62,0.25);
			--frogsPreColor: rgba(191, 211, 193, 0.25);
		}
h1{
	color: var(--frogsColor);
}
h2{
	color: var(--frogsColor);
}
a {
	color: var(--frogsColor);
}
a:hover{
	color: var(--frogsColorHover);
}
.nav-pills > li.active > a, .nav-pills > li.active > a:focus{
	color: #fff;
	background-color: var(--frogsColor);
	border-color: #dee2e6 #dee2e6 #fff;
}
.nav-pills > li.active > a:hover {
	background-color: var(--frogsColorHover);
}
.button {
    background-color: var(--frogsButtonColor)
    border          : none;
    color           : white;
    padding         : 5px 10px;
    text-align      : center;
    text-decoration : none;
    display         : inline-block;
    font-size       : 12px;
    margin          : 4px 2px;
    cursor          : pointer;
    border-radius   : 8px;
}
h4 { 
    display      : block;
    font-size    : 1em;
    margin-top   : 1.33em;
    margin-bottom: 1.33em;
    margin-left  : 0;
    margin-right : 0;
    font-weight  : bold;
    color        : var(--frogsColor);
}
code.r{ /* Code block */
  font-size: 11px;
}
pre{
  font-size: 11px ;
  background-color: var(--frogsPreColor) !important;
}
.frogsversion{
	position: absolute;
	right: 5%;
	color: var(--frogsColor);
	font-style: italic;
	font-size: smaller;
	padding:8px;
}
.frogsversion > a{
	border: none !important;
	display: inline-block;
}
</style>

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

```{=html}
<select id="themechoice" class="form-select" onchange="update_theme($(this).val())" style="font-size: smaller;" aria-label="Default select example">
	<option selected disabled>Switch theme</option>
	<option value="Plum">Plum</option>
	<option value="Coral">Coral</option>
	<option value="FROGS4">FROGS4</option>
</select>
```

```{r import, message=FALSE}
## Import packages
library(phyloseq)
library(ggplot2)
source(file.path(params$libdir, "graphical_methods.R"))
## Alternative to source all extra function from a github repo
## source("https://raw.githubusercontent.com/mahendra-mariadassou/phyloseq-extended/master/load-extra-functions.R")

## Setting variables
  ## The Phyloseq object (format rdata)
  # phyloseq <- ""

  ## The experiment variable that you want to analyse
  # varExp <- ""

  ## "The alpha diversity indices to compute. Multiple indice may be indicated by separating them by a comma.
  ## Available indices are : Observed, Chao1, Shannon, InvSimpson, Simpson, ACE, Fisher
  # measures <- ""

## Create input and parameters dataframe
  # params <- data.frame( "phyloseq" = phylose, "measures" = measures, "varExp" = varExp)

## Load data
load(params$phyloseq)

## Convert measures to list
measures <- as.list(strsplit(params$measures, ",")[[1]])

## Compute numeric values of alpha diversity indices
alpha.diversity <- estimate_richness(data, measures = measures)

## Export diversity indices to text file
write.table(alpha.diversity, params$fileAlpha, sep="\t", quote = FALSE, col.names = NA)
```

# {.tabset .tabset-fade .tabset-pills}
## Richness plot
```{r richness, fig.width=10, fig.height=8, warning=FALSE}
p <- plot_richness(data, x = params$varExp, color = params$varExp, measures = measures) + ggtitle(paste("Alpha diversity distribution in function of", params$varExp))+ theme(plot.title = element_text(hjust = 0.5))
plot(p)
```

## Richness plot with boxplot
```{r richnessBoxplot, fig.width=10, fig.height=8, message=FALSE, warning=FALSE}
p <- p + geom_boxplot(alpha = 0.2) +
         geom_point()+ theme_grey() +
         theme(axis.text.x = element_text(angle=90, hjust=0.5)) +
         theme(plot.title = element_text(hjust = 0.5))
plot(p)
```

## Alpha Diversity Indice Anova Analysis
```{r anova}
anova_data <- cbind(sample_data(data), alpha.diversity)
anova_data$Depth <- sample_sums(data)

variables <- paste(sep=" + ", "Depth", params$varExp )

## Perform ANOVA on observed richness, which effects are significant
for (m in measures){
    f <- paste(m," ~ ", variables)
    cat(sep = "", "###############################################################\n#Perform ANOVA on ",m,", which effects are significant\nanova.",m," <-aov( ",f,", anova_data)\nsummary(anova.",m,")\n")
    anova_res <- aov( as.formula(f), anova_data)
    res <- summary(anova_res)
    print(res)
    cat("\n\n")
}
```

## Rarefaction curves
```{r rarefaction, message=FALSE}
##code of Mahendra Mariadassou, INRA

## Import additionnal packages
# library(parallel)
library(vegan)

## Rarefaction curve, ggplot style (additionnal phyloseq-extend function, not yet released)
ggrare <- function(physeq, step = 10, label = NULL, color = NULL, plot = TRUE, parallel = FALSE, se = TRUE) {
    ## Args:
    ## - physeq: phyloseq class object, from which abundance data are extracted
    ## - step: Step size for sample size in rarefaction curves
    ## - label: Default `NULL`. Character string. The name of the variable
    ##          to map to text labels on the plot. Similar to color option
    ##          but for plotting text.
    ## - color: (Optional). Default ‘NULL’. Character string. The name of the
    ##          variable to map to colors in the plot. This can be a sample
    ##          variable (among the set returned by
    ##          ‘sample_variables(physeq)’ ) or taxonomic rank (among the set
    ##          returned by ‘rank_names(physeq)’).
    ##
    ##          Finally, The color scheme is chosen automatically by
    ##          ‘link{ggplot}’, but it can be modified afterward with an
    ##          additional layer using ‘scale_color_manual’.
    ## - color: Default `NULL`. Character string. The name of the variable
    ##          to map to text labels on the plot. Similar to color option
    ##          but for plotting text.
    ## - plot:  Logical, should the graphic be plotted.
    ## - parallel: should rarefaction be parallelized (using parallel framework)
    ## - se:    Default TRUE. Logical. Should standard errors be computed. 
    ## require vegan
    x <- as(otu_table(physeq), "matrix")
    if (taxa_are_rows(physeq)) { x <- t(x) }

    ## This script is adapted from vegan `rarecurve` function
    tot <- rowSums(x)
    S   <- rowSums(x > 0)
    nr  <- nrow(x)

    rarefun <- function(i) {
        # cat(paste("rarefying sample", rownames(x)[i]), sep = "\n")
        n <- seq(1, tot[i], by = step)
        if (n[length(n)] != tot[i]) {
            n <- c(n, tot[i])
        }
        y <- rarefy(x[i, ,drop = FALSE], n, se = se)
        if (nrow(y) != 1) {
	    rownames(y) <- c(".S", ".se")
            return(data.frame(t(y), Size = n, Sample = rownames(x)[i]))
        } else {
            return(data.frame(.S = y[1, ], Size = n, Sample = rownames(x)[i]))
        }
    }
    if (parallel) {
        out <- mclapply(seq_len(nr), rarefun, mc.preschedule = FALSE)
    } else {
        out <- lapply(seq_len(nr), rarefun)
    }
    df <- do.call(rbind, out)
    
    ## Get sample data 
    if (!is.null(sample_data(physeq, FALSE))) {
        sdf <- as(sample_data(physeq), "data.frame")
        sdf$Sample <- rownames(sdf)
        data <- merge(df, sdf, by = "Sample")
        labels <- data.frame(x = tot, y = S, Sample = rownames(x))
        labels <- merge(labels, sdf, by = "Sample")
    }
    
    ## Add, any custom-supplied plot-mapped variables
    if( length(color) > 1 ){
        data$color <- color
        names(data)[names(data)=="color"] <- deparse(substitute(color))
        color <- deparse(substitute(color))
    }
    if( length(label) > 1 ){
        labels$label <- label
        names(labels)[names(labels)=="label"] <- deparse(substitute(label))
        label <- deparse(substitute(label))
    }
    
    p <- ggplot(data = data, aes_string(x = "Size", y = ".S", group = "Sample", color = color))
    p <- p + labs(x = "Sample Size", y = "ASV Richness")
    if (!is.null(label)) {
        p <- p + geom_text(data = labels, aes_string(x = "x", y = "y", label = label, color = color),
                           size = 4, hjust = 0)
    }
    p <- p + geom_line()
    if (se) { ## add standard error if available
        p <- p + geom_ribbon(aes_string(ymin = ".S - .se", ymax = ".S + .se", color = NULL, fill = color), alpha = 0.2)
    }
    if (plot) {
        plot(p)
    }
    invisible(p)
}

rare.level <- sample_sums(data)[[1]]
facet <- paste('facet_wrap(~',params$varExp,')')

p <- ggrare(data, step = 500, color = params$varExp, plot = FALSE) + 
        geom_vline(xintercept = rare.level, color = "gray60") + eval(parse(text = facet))
plot(p)
```
