---
title: "FROGS Phyloseq: Structure Visualization (version 3.2.1)"
output: 
  html_notebook:
    code_folding: hide
params:
   phyloseq:
      value: x
   varExp:
      value: x
   method:
      value: x
   distance:
      value: x
   libdir:
      value: x
---
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
code.r{ /* Code block */
  font-size: 11px;
}
pre { /* Code block */
  font-size: 11px
}
.button {
    background-color: #2a7ecc; /* Blue */
    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: #2a7ecc;
}
</style>

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

```{r import, message=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))

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))
plot(p3)
```
