---
title: "FROGS Phyloseq: Beta 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
   varExp:
      value: x
   methods:
      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)
library(gridExtra)
library(reshape2)
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 methods of beta diversity you want to compute
  ## to see all available distance methods, type distanceMethodList
  ## The most common one are : cc for Jaccard, bray for Bray-Curtis, unifrac and wunifrac for Unifrac and weighted Unifrac
  ## N.B. if the tree is not available in your RData, you cannot choose Unifrac or Weighted Unifrac
  ## You may precise multiple distance by separating them by a comma
  # methods <- ""

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

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

## store methods in list
methods <- as.list(strsplit(params$methods, ",")[[1]])

## Order samples according to grouping variable
sampleOrder <- levels(reorder(sample_names(data), as.numeric(get_variable(data, params$varExp)))) 
```

### Distance as heatmap plot(s)
```{r, fig.width=8, fig.height=6, warning=FALSE}

for (method in methods){
  dist.a <- distance(data, method = method)
  a <- as.matrix(dist.a)
  write.table(a, paste(sep="", method, ".tsv"), sep="\t", quote=FALSE, col.names=NA)
  pa <- plot_dist_as_heatmap(dist.a, order = sampleOrder, title = paste("Heatmap plot of the beta distance :",method)) + 
    theme(plot.title = element_text(hjust = 0.5))
  plot(pa)
}
```
