---
title: "FROGS Phyloseq: Import Data (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:
  # Path ASV abundance biom file
  biomfile: 
    value: x
  # Path to sample metadata TSV file
  samplefile:
    value: x
  # (optional) Path to ASVs tree NWK file
  treefile:
    value: x
  # (optional) normalisation string option : TRUE or FALSE
  normalisation:
    value: x
  # Path to phyloseq object store in Rdata file
  outputRdata:
    value: x
  # List of taxonomic ranks names
  ranks : 
    value: x
  # Path to the phyloseq-extend R functions : https://github.com/mahendra-mariadassou/phyloseq-extended, typically stored in FROGS/lib/external-lib
  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)
```
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 package_import, message=FALSE, warning=FALSE}

## Import packages
library(phyloseq)
library(ggplot2)
library(ape)

## Settin variables
  ## The ASV abundance matrix with taxonomy annotation file (biom format)
  # biomfile <- ""

  ## The sample metadata file(TSV format)
  # samplefile <- ""

  ## (optional) the ASV tree file (nwk format). Write "None" if you do not have any tree
  # treefile <- ""

  ## The ordered taxonomic levels stored in BIOM. Each level is separated by one space.
  ## default : "Kingdom Phylum Class Order Family Genus Species"
  # ranks <- ""

  ## Do you want to normalise your data ? "True" or "False"
  # normalisation <- ""

## Create input and parameters dataframe
  # params <- data.frame( "biomfile" = biomfile, "samplefile" = samplefile, "tree" = tree, "ranks" = ranks, "normalisation" = normalisation)


## Import data
biomfile <- params$biomfile
data     <- import_biom(biomfile)
sampledata <- read.csv(params$samplefile, sep = "\t", row.names = 1, check.names = FALSE)

# if taxonomy starts with k__ it means that its Greengenes like format
# import need to be done using parse_taxonomy_greengenes function
# in this case user taxonomic rank names are ignored
tax      <- tax_table(data)[[1]]
if ((gregexpr('k__', tax))[[1]][1]>0) { 
  cat("Warning : Taxonomic affiliations come from Greengenes database, user specified ranks names are ignored.")
  data <- import_biom(biomfile, parseFunction = parse_taxonomy_greengenes)
} else {
## else, custumize rank name with the user specified ranks variable
  new_rank <- as.list(strsplit(params$ranks, " ")[[1]])
  colnames(tax_table(data)) <- new_rank
}

## add sample name to metadata, as SampleID variable
sampledata$SampleID <- rownames(sampledata)
sample_data(data) <- sampledata

## add tree metadata if available
if (params$treefile != "None"){
  treefile <- read.tree(params$treefile)
  phy_tree(data) <- treefile
}

## change de sample metadata order as in input samplefile
for ( variable in sample_variables(data)){
  variable.order = as.vector(unique(sampledata[,variable]))
  sample_data(data)[,variable] <- factor(get_variable(data, variable),levels=variable.order)
}

## remove empty samples
empty_samples <- sample_names(data)[which(sample_sums(data)==0)]
sample_to_keep <- sample_names(data)[which(sample_sums(data)>0)]
data <- prune_samples(sample_to_keep, data)

empty_taxa <- taxa_names(data)[which(taxa_sums(data)==0)]
taxa_to_keep <- taxa_names(data)[which(taxa_sums(data)>0)]
data <- prune_taxa(taxa_to_keep, data)

## abundance normalisation
if(params$normalisation){ data <- rarefy_even_depth(data, rngseed = 1121983)}

## save phyloseq object in Rdata file
save(data, file=params$outputRdata)
```

# {.tabset .tabset-fade .tabset-pills}
## Summary
```{r summary}
data
if(length(empty_samples) > 0) {cat(paste('Remove empty samples: ', paste(empty_samples, collapse=",")))}
if(length(empty_taxa) > 0) {cat(paste('Remove empty taxa: ', paste(empty_taxa, collapse=",")))}
if(params$normalisation){cat(paste('Number of sequences in each sample after normalisation: ', head(sample_sums(data))[[1]]))}
```

## Ranks Names
```{r ranks}
cat(paste('Rank names : ',paste(rank_names(data),collapse=', ')))
```

## Sample metadata
```{r sample}
variables <- sample_variables(data)
cat(paste('Sample variables: ', paste(variables, collapse=', ' )))

for (var in variables){
  cat(paste(var,': ',paste(levels(factor(get_variable(data, varName = var))),collapse=', '), '\n\n'))
}
```

## Plot tree
```{r tree, fig.width=10, fig.height=8}

if(params$treefile!="None"){
  p <- plot_tree(data, color=rank_names(data)[2]) + 
          ggtitle(paste("Phylogenetic tree colored by", rank_names(data)[2])) + 
          theme(plot.title = element_text(hjust = 0.5))
  plot(p)
}

if(params$treefile=="None"){
  cat("There is no phylogenetic tree in the object you have provided.")
}
```
