---
title: "Figure S2 Storage"
author: "Mads Albertsen"
date: "`r format(Sys.time(), '%d-%m-%Y')`"
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
All samples are subset to 25.000 reads and then only OTUs which are seen at least 10 / 25000 times in a single sample is kept for further ordination analysis.
```{r subset, message=FALSE}
storage <- subset_samples(V13, Exp.storage == "YES") %>%
  rarefy_even_depth(sample.size = 25000, rngseed = 712) %>%
  filter_taxa(function(x) max(x) >= 10, TRUE)
```

## Figure S2A: 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 = storage, 
             plot.color = "Storage", 
             plot.point.size = 3,
             plot.theme = "clean",
             envfit.factor = "Storage",
             envfit.show = F,
             output = "complete"
             )
```

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

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

The model reports a p-value of `r pca$eff.model$factors$pvals`, hence there no overall effect of storage methods.
```{r pca_model}
pca$eff.model
```

## Figure S2B: Cluster analysis of beta diversity using Bray-Curtis
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 = storage, 
                         group = "Storage", 
                         method = "bray", 
                         plot.color = "Storage", 
                         plot.label = "Storage",
                         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 distinct effect of storage.
```{r beta_cluster, fig.align='center', fig.height=3, fig.width=4}
beta$plot_cluster +
  theme(legend.position = "none")
```

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

# Figure 2C: Variance compared to time-series data
```{r storage_time_subset, message=FALSE}
storage_time <- subset_samples(V13, Exp.storage == "YES"| Exp.time == "YES") %>%
  rarefy_even_depth(sample.size = 25000, rngseed = 712) %>%
  filter_taxa(function(x) max(x) >= 10, TRUE)
```

Looking at the data using PCA. It seems like we can't seperate the timpoints within 2 weeks now.
```{r PCA_time, fig.align='center', fig.height=3, fig.width=5}
amp_ordinate(data = storage_time, 
             plot.color = "Date", 
             plot.point.size = 3,
             plot.theme = "clean"
             ) +
  scale_color_discrete(name = "Sampling date") +
  theme(legend.key.height = unit(3, "mm"))
```

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

## Figure 2D: Using clustering to estimate classification resolution
```{r beta_time}
beta_time <- amp_test_cluster(data = storage_time, 
                              group = "Storage", 
                              method = "bray", 
                              plot.color = "Date", 
                              plot.label = c("Storage"),
                              plot.theme = "clean")
```

```{r beta_time_plot, fig.align='center', fig.height=3, fig.width=4}
beta_time$plot_cluster +
  theme(legend.position = "none")
```

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