---
title: "Figure S4 Input"
author: "Mads Albertsen"
date: "March 10, 2015"
output: html_document
---

## Load packages
```{r Load_packages, message=FALSE, warning=FALSE, results='hide'}
library("ampvis")
```

## Load data
```{r load_data}
data(DNAext_1.0)
```

## Subset to the relevant dataset
Note: The "input.mg" samples were extracted using another beadbeating setting (40s at 6m/s) compared to the time-series data (160s at 6m/s)!
```{r subset, message=FALSE}
input <- subset_samples(V13, Exp.input.mg == "YES") %>%
  rarefy_even_depth(sample.size = 25000, rngseed = 712) %>%
  filter_taxa(function(x) max(x) > 10, TRUE)
```

## Figure S4A: Overall differences between samples using PCA
PCA with square root transformed OTU abundances. The effect of sampling from different tanks is tested using the envfit function in vegan (permutation test).
```{r PCA}
pca <- amp_ordinate(data = input, 
             plot.color = "Input.mg", 
             plot.point.size = 3,
             plot.theme = "clean",
             envfit.factor = "Input.mg",
             envfit.show = F,
             output = "complete"
             )
```

Plot the PCA. It looks like there might be some significant grouping. At least the 22 mg seem different.
```{r pca_plot, fig.align='center', fig.height=3, fig.width=3}
pca$plot +
  theme(legend.position = "none")
```

```{r save_S4A, eval=FALSE}
ggsave("plots/S4A.eps", width = 55, height = 55, units = "mm")
```

The model reports a p-value of `r pca$eff.model$factors$pvals`, hence there is an effect of the amount of input material.
```{r pca_model}
pca$eff.model
```

## Figure S4B: Overall differences between samples using beta diversity
The Bray-Curtis dissimilarity index is used as an alternative method to test for significant groupings in the dataset. 
```{r beta}
beta <- amp_test_cluster(data = input, 
                         group = "Input.mg", 
                         method = "bray", 
                         plot.color = "Input.mg", 
                         plot.label = "Input.mg",
                         plot.theme = "clean")
```

Using adonis we also a small significant effect of storage method as the p-value is `r beta$adonis$aov.tab$"Pr(>F)"[1]`.
```{r beta_adonis}
beta$adonis
```

Clustering the data also shows that there is no difference between using 0.9 and 2.2 mg of input material, while there is an effect from using 22 mg of input material.
```{r beta_cluster, fig.align='center', fig.height=3, fig.width=4}
beta$plot_cluster +
  theme(legend.position = "none")
```

```{r save_S4B, eval=FALSE}
ggsave("plots/S4B.eps", width = 60, height = 55, units = "mm")
```
