---
title: "FROGS Phyloseq: Structure 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
   varExp:
      value: x
   method:
      value: x
   distance:
      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, warning=FALSE}
## Import packages
library(phyloseq)
library(plotly)
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 beta diversity distance matrix file
  # distance <- ""

  ## The ordination method you want to use
  ## You can choose between "MDS" (for MDS/PCoA), "NMDS" or "DPCoA"
  # method <- ""

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

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

## Load data
  ## the phyloseq object
load(params$phyloseq)

  ## the distance matrix file
A    <- read.table(file=params$distance, row.names=1)
dist <- as.dist(A)

## Add SampleID variable to physeq metadata
sample_data(data)$SampleID <- sample_names(data)

ord  <- ordinate(data, method = params$method, distance = dist)
var  <- get_variable(data, params$varExp)
```

# {.tabset .tabset-fade .tabset-pills}
## Ordination plot

<h1 id="plot1">
```{r ord1, fig.width=8, fig.height=6, warning=FALSE, message=FALSE}
## plot(p1)
p1 <- plot_ordination(data, ord, color = params$varExp) + 
                      geom_text(aes(label = SampleID), alpha = 0) + ## add invisible labels
                      theme_bw() + ggtitle(paste(params$method,"")) +
                      theme(plot.title = element_text(hjust = 0.5)) 
ggplotly(p1, tooltip = c("colour", "label"))
```
</h1>

<h1 id="plot2" style="display: none;">
```{r ord2, fig.width=8, fig.height=6, warning=FALSE}
## plot(p2)
p2 <- plot_samples(data, ord, color = params$varExp) +
                      geom_text(aes(label = SampleID), alpha = 0) + ## add invisible labels  
                      theme_bw() +
                      ggtitle(paste(params$method,"")) +
                      theme(plot.title = element_text(hjust = 0.5))

# reorder samples as in p1
p2$data[params$varExp] <- factor(p2$data[, params$varExp], levels = levels(p1$data[,params$varExp]))

ggplotly(p2, tooltip = c("colour", "label"))
```
</h1>

<h1 id="plot22" style="display: none;">
```{r ord22, fig.width=8, fig.height=6, warning=FALSE}
## p2 is obtained with plot samples see previously
g <- paste('p22 <- p2 +  stat_ellipse(aes(group =', params$varExp,'))')
eval(parse(text = g))
ggplotly(p22, tooltip = c("colour", "label"))
```
</h1>

<button class="button" onclick="document.getElementById('plot1').style.display='' ; document.getElementById('plot2').style.display='none'; document.getElementById('plot22').style.display='none' ">Without variable names</button>
<button class="button" onclick="document.getElementById('plot2').style.display='' ; document.getElementById('plot1').style.display='none'; document.getElementById('plot22').style.display='none' ">With variable names</button>
<button class="button" onclick="document.getElementById('plot22').style.display='' ; document.getElementById('plot2').style.display='none'; document.getElementById('plot1').style.display='none'">With ellipses</button>

## Heatmap plot
```{r heatmap, fig.width=9, fig.height=7,message=FALSE, warning=FALSE}
p3 <- plot_heatmap(data, method = params$method, distance = dist) + scale_fill_gradient2(low = "#ffff00", 
                                                mid = "#ffa500",
                                               high = "#ff0000", 
                                           na.value = "white",
                                              trans = log_trans(4), 
                                           midpoint = log(100, base = 4))
e  <- paste('~', params$varExp) 
p3 <- p3 + facet_grid(e, scales = "free_x") +
           theme(plot.title = element_text(hjust = 0.5)) +
           ggtitle(paste("Heatmap plot with", params$varExp)) +
           ylab("ASV")
plot(p3)
```
