---
title: "FROGS Phyloseq: Visualize Data Composition (version 3.2.1)"
output:
  html_notebook:
    code_folding : hide
params:
  phyloseq:
    value: x
  varExp:
    value: x
  taxaRank1:
    value: x
  taxaSet1:
    value: x
  taxaRank2:
    value: x
  numberOfTaxa:
    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 functions
library(plotly)
library(phyloseq)
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
  ## Phyloseq object (rdata file)
  # phyloseq <- ""
  
  ## Experimental variable used to group samples (Treatment, Host type, etc).
  # varExp <- "" 
  
  ## Taxonomic level to filter your data" help="ex: Kingdom, Phylum, Class, Order, Family, Genus, Species
  # taxaRank1 <- ""
  
  ## Taxa (at the above taxonomic level) to keep in the dataset"
  ## ex: Bacteria (when filtering at the Kingdom level), Firmicutes (when filtering at the Phylum level). 
  ## Multiple taxa (separated by a space) can be specified, i.e. Firmicutes Proteobacteria 
  # taxaSet1 <- "" 
  
  ## Taxonomic level used for aggregation" help="ex: Family (when filtering at the Phylum level). 
  ## The aggregation level must be below the filtering level.
  # taxaRank2 <- ""
  
  ## Number of most abundant taxa to keep", 
  ## ex: 9, i.e. Tool keeps the 9 most abundant taxa and the remaining taxa are aggregated in a group 'Other
  # numberOfTaxa = 

  ## Create input and parameters dataframe
  # params <- data.frame("phyloseq"=phyloseq, 
  #                      "varExp"=varExp, 
  #                      "taxaRank1" = taxaRank1, 
  #                      "taxaSet1" = taxaSet1, 
  #                      "taxaRank2" = taxaRank2, 
  #                      "numberOfTaxa"=numberOfTaxa )

## Load Phyloseq object
load(params$phyloseq)

# custum graphical facetting
facet <- paste('facet_wrap(~', params$varExp,', scales = "free_x", nrow = 1)')

```

# {.tabset .tabset-fade .tabset-pills}
## Bar plot
```{r bar_plot, fig.width=10, fig.height=8}
p <- plot_bar(data,fill= rank_names(data)[2]) + eval(parse(text = facet)) + 
        ggtitle(paste("Bar plot colored by", rank_names(data)[2])) + 
        theme(plot.title = element_text(hjust = 0.5))
plot(p)
```

## Composition plot
```{r composion_plot, fig.width=10, fig.height=8, warning=FALSE}
taxaSet1 <- unlist(strsplit(params$taxaSet1, " "))
p        <- plot_composition(data, params$taxaRank1, taxaSet1, params$taxaRank2, params$numberOfTaxa, fill=params$taxaRank2) 
p        <- p + eval(parse(text = facet)) + theme(plot.title = element_text(hjust = 0.5))

gp <- ggplotly(p, tooltip = c("x", "y", "fill"))
gp$x$layout$annotations[[1]]$x <- -0.08
gp$x$layout$margin$l = 75
gp$x$layout$margin$b = 100
gp$x$layout$margin$t = 100
gp
```