---
# System prepended metadata

title: Chaillou (16S rRNA) dataset analysis
tags: [tutoriel, ' biostatstics', ' metabarcoding']

---

---
title: Chaillou (16S rRNA) dataset analysis
disqus: hackmd
tags: tutoriel, biostatstics, metabarcoding
description: https://hackmd.io/@migale/HJGW5VFOF
---

###### tags: `tutoriel` `phyloseq` `metabarcoding`

Chaillou (16S rRNA) dataset analysis
===

<!-- css -->
{%hackmd ryWWkX_It %}
<!-- licence -->
{%hackmd H1gUVE_LY %}

:::success metadata
:gb:

Mahendra Mariadassou

01/03/2019 (last update 29/11/2021)

This vignette is a re-analysis of the data set from Chaillou et al. (2015) [^chaillou2015] using the phyloseq R package.
:::

# Introduction

This vignette is a re-analysis of the data set from Chaillou et al. (2015), as suggested in the _**Homeworks**_ section of the slides. Note that this is only one of many analyses that could be done on the data.

# Analysis

## Setup

We first setup our environment by loading a few packages

```r
library(tidyverse)  ## data manipulation
library(phyloseq)   ## analysis of microbiome census data
library(ape)        ## for tree manipulation
library(vegan)      ## for community ecology analyses
library(phyloseq.extended)
library(scales)
## You may also need to install gridExtra with 
## install.packages("gridExtra")
## and load it with 
## library(gridExtra)
## if you want to reproduce some of the side-by-side graphs shown below
```

# Data import

We then load the data from the Chaillou et al. (2015) dataset. We assume that they are located in the `data/chaillou` folder. A quick look at the folder content shows we have access to a tree (in newick format, file extension `.nwk`) and a biom file (extension `.biom`). A glance at the biom file content shows that the taxonomy is 

```!
"k__Bacteria", "p__Tenericutes", "c__Mollicutes", "o__Mycoplasmatales", "f__Mycoplasmataceae", "g__Candidatus Lumbricincola", "s__NA". 
```

This is the so called greengenes format and the taxonomy should thus be parsed using the _parse_taxonomy_greengenes_ function.

```r
food <- import_biom("chaillou_data/chaillou.biom", 
                    parseFunction = parse_taxonomy_greengenes)
metadata <- read.table("chaillou_data/chaillou_metadata.tsv", row.names=1, header=TRUE, sep="\t", stringsAsFactors = FALSE)
sample_data(food) <- metadata
```

We can then add the phylogenetic tree to the `food` object

```r
phy_tree(food) <- read_tree("chaillou_data/tree.nwk")
food
```

```!
## phyloseq-class experiment-level object
## otu_table()   OTU Table:         [ 508 taxa and 64 samples ]
## sample_data() Sample Data:       [ 64 samples by 3 sample variables ]
## tax_table()   Taxonomy Table:    [ 508 taxa by 7 taxonomic ranks ]
## phy_tree()    Phylogenetic Tree: [ 508 tips and 507 internal nodes ]
```

The data consists of 64 samples of food: 8 replicates for each of 8 food types. We have access to the following descriptors


* **EnvType**: Food of origin of the samples
* **FoodType**: Either meat or seafood
* **Description**: Replicate number

We can navigate through the metadata

```r!
head(sample_data(food))
```

```!
#                     EnvType Description FoodType
# DLT0.LOT08       DesLardons        LOT8     Meat
# DLT0.LOT05       DesLardons        LOT5     Meat
# DLT0.LOT03       DesLardons        LOT3     Meat
# DLT0.LOT07       DesLardons        LOT7     Meat
# DLT0.LOT06       DesLardons        LOT6     Meat
# DLT0.LOT01       DesLardons        LOT1     Meat
# DLT0.LOT04       DesLardons        LOT4     Meat
# DLT0.LOT10       DesLardons       LOT10     Meat
# MVT0.LOT05 SaucisseVolaille        LOT5     Meat
# MVT0.LOT01 SaucisseVolaille        LOT1     Meat
```

EnvType is coded in French and with categories in no meaningful order. We will translate them and order them to have food type corresponding to meat first and to seafood second.

```r!
## We create a "dictionary" for translation and order the categories 
## as we want 
dictionary = c("BoeufHache"      = "Ground_Beef", 
               "VeauHache"       = "Ground_Veal", 
               "MerguezVolaille" = "Poultry_Sausage", 
               "DesLardons"      = "Bacon_Dice", 
               "SaumonFume"      = "Smoked_Salmon", 
               "FiletSaumon"     = "Salmon_Fillet", 
               "FiletCabillaud"  = "Cod_Fillet", 
               "Crevette"        = "Shrimp")
env_type <- sample_data(food)$EnvType
sample_data(food)$EnvType <- factor(dictionary[env_type], levels = dictionary)
```

We can also build a custom color palette to remind ourselves which samples correspond to meat and which to seafood.

```r!
my_palette <- c('#67001f','#b2182b','#d6604d','#f4a582',
                 '#92c5de','#4393c3','#2166ac','#053061')
names(my_palette) <- dictionary
```

Before moving on to elaborate statistics, we’ll look at the taxonomic composition of our samples

To use the _plot_composition_ function, we first need to source it:

```r
download.file(url = "https://raw.githubusercontent.com/mahendra-mariadassou/phyloseq-extended/master/R/graphical_methods.R", 
              destfile = "graphical_methods.R")
source("graphical_methods.R")
```

We then look at the community composition at the phylum level. In order to highlight the structure of the data, we split the samples according to their food of origin. We show several figures, corresponding to different taxonomic levels.

## Phylum level

We can see right away that meat products are whereas Bacteroidetes and Proteobacteria are more abundant in seafood.

```r!
plot_composition(food, "Kingdom", "Bacteria", "Phylum", fill = "Phylum") + 
  facet_grid(~EnvType, scales = "free_x", space = "free_x") + 
  theme(axis.text.x = element_blank())
```

![](https://hackmd.io/_uploads/Hk2Q_vGYF.png)

We have access to the following taxonomic ranks:

```r!
rank_names(food)
```

```!
[1] "Kingdom" "Phylum"  "Class"   "Order"   "Family"  "Genus"   "Species"
```

## Within Firmicutes

Given the importance of Firmicutes, we can zoom in within that phylum and investigate the composition at the genus level.

```r!
plot_composition(food, "Phylum", "Firmicutes", "Genus", fill = "Genus", numberOfTaxa = 5) + 
  facet_grid(~EnvType, scales = "free_x", space = "free_x") + 
  theme(axis.text.x = element_blank())
```

![](https://hackmd.io/_uploads/HyPFdDzYY.png)

We see that there is a high diversity of genera across the different types of food and that no single OTU dominates all communities.

# Alpha-diversity

The samples have very similar sampling depths

```r!
sample_sums(food) %>% range()
```

```!
# [1] 11718 11857
```

there is thus no need for rarefaction.

## Graphics

We compare the different types of food in terms of diversity.

```r!
plot_richness(food, x = "EnvType", color = "EnvType",
              measures = c("Observed", "Shannon", "InvSimpson")) + 
  geom_boxplot(aes(group = EnvType)) +    ## add one boxplot per type of food
  scale_color_manual(values = my_palette) ## custom color palette
```

![](https://hackmd.io/_uploads/rJ9JFDztY.png)

Different foods have very different diversities with dice of bacon having the highest number ~ 250) of OTUs. However, most foods have low effective diversities.

## Anova

We can quantify the previous claims by performing an ANOVA of the diversity against the covariates of interest. For the sake of brevity, we focus here on both the observed and effective number of species (as measured by the _InvSimpson_ measure). We first build a data.frame with both covariates and diversity indices.

```r!
div_data <- cbind(estimate_richness(food),  ## diversity indices
                  sample_data(food)         ## covariates
                  )
```

Foods differ significantly in terms of number of observed OTUs…

```r!
model <- aov(Observed ~ EnvType, data = div_data)
anova(model)
```

```!
# Analysis of Variance Table
# 
# Response: Observed
#           Df Sum Sq Mean Sq F value    Pr(>F)    
# EnvType    6  78171 13028.6  12.353 2.029e-08 ***
# Residuals 49  51680  1054.7                      
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
```

… but no in terms of effective number of species

```r!
model <- aov(InvSimpson ~ EnvType, data = div_data)
anova(model)
```

```!
# Analysis of Variance Table
# 
# Response: InvSimpson
#           Df  Sum Sq Mean Sq F value Pr(>F)
# EnvType    6  444.63  74.105  1.4865 0.2025
# Residuals 49 2442.75  49.852
```

# Beta-diversities

We have access to a phylogenetic tree so we can compute all 4 distances seen during the tutorial.

```r!
dist.jac <- distance(food, method = "cc")
dist.bc <- distance(food, method = "bray")
dist.uf <- distance(food, method = "unifrac")
dist.wuf <- distance(food, method = "wunifrac")
```

```r!
p.jac <- plot_ordination(food, 
                         ordinate(food, method = "MDS", distance = dist.jac), 
                         color = "EnvType") + 
  ggtitle("Jaccard") + scale_color_manual(values = my_palette)
p.bc <- plot_ordination(food, 
                         ordinate(food, method = "MDS", distance = dist.bc), 
                         color = "EnvType") + 
  ggtitle("Bary-Curtis") + scale_color_manual(values = my_palette)
p.uf <- plot_ordination(food, 
                         ordinate(food, method = "MDS", distance = dist.uf), 
                         color = "EnvType") + 
  ggtitle("UniFrac") + scale_color_manual(values = my_palette)
p.wuf <- plot_ordination(food, 
                         ordinate(food, method = "MDS", distance = dist.wuf), 
                         color = "EnvType") + 
  ggtitle("wUniFrac") + scale_color_manual(values = my_palette)
gridExtra::grid.arrange(p.jac, p.bc, p.uf, p.wuf,
                        ncol = 2)
```

![](https://hackmd.io/_uploads/SkFhFwfFK.png)

In this example, Jaccard and UniFrac distances provide a clear separation of meats and seafoods, unlike Bray-Curtis and wUniFrac. This means that food ecosystems share their abundant taxa but not the rare ones.

UniFrac also gives a much better separation of poultry sausages and ground beef / veal than Jaccard. This means that although those foods have taxa in common, their specific taxa are located in different parts of the phylogenetic tree. Since UniFrac appears to the most relevant distance here, we’ll restrict downstream analyses to it.

One can also note that dices of bacon are located between meats and seafoods. We’ll come back latter to that point latter.

## Hierarchical clustering

The hierarchical clustering of uniFrac distances using the Ward linkage function (to produce spherical clusters) show a perfect separation according.

```r!
par(mar = c(1, 0, 2, 0))
plot_clust(food, dist = "unifrac", method = "ward.D2", color = "EnvType", 
           palette = my_palette, 
           title = "Clustering of samples (UniFrac + Ward)\nsamples colored by EnvType")
```

![](https://hackmd.io/_uploads/rktJcvMFY.png)

## PERMANOVA

We use the Unifrac distance to assess the variability in terms of microbial repertoires between foods.

```r!
model <- adonis(dist.uf ~ EnvType, data = metadata, permutations = 999)
model
```

```!
# 
# Call:
# adonis(formula = dist.uf ~ EnvType, data = metadata, permutations = 999) 
# 
# Permutation: free
# Number of permutations: 999
# 
# Terms added sequentially (first to last)
# 
#           Df SumsOfSqs MeanSqs F.Model      R2 Pr(>F)    
# EnvType    7    7.6565 1.09379  13.699 0.63132  0.001 ***
# Residuals 56    4.4713 0.07984         0.36868           
# Total     63   12.1278                 1.00000           
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
```

The results show that food origin explains a bit less than 2 thirds of the total variability observed between samples in terms of species repertoires. This is quite strong! Don’t expect such strong results in general.

## Heatmap

We investigate the content of our samples when looking at the raw count table and use a custom color scale to reproduce (kind of) the figures in the paper.

```r!
p <- plot_heatmap(food) + 
  facet_grid(~EnvType, scales = "free_x", space = "free_x") + 
  scale_fill_gradient2(low = "#1a9850", mid = "#ffffbf", high = "#d73027",
                       na.value = "white", trans = log_trans(4),
                       midpoint = log(100, base = 4))
```

```r!
plot(p)
```

![](https://hackmd.io/_uploads/SyqsnDGFt.png)

It looks like: 
- there is some kind of block structure in the data (eg. OTUs that are abundant in seafoods but not meat products or vice-versa) 
- dices of bacon have a lot of OTU in common with seafoods. This is indeed the case and the result of “contamination”, sea salt is usually added to bacon to add flavor. But, in addition to flavor, the grains of salt also bring sea-specific bacterial taxa (at least their genetic material) with them.

## Differential abundance study

To highlight the structure, we’re going to perform a differential analysis (between meat products and seafoods) and look only at the differentially abundant taxa.

```r!
cds <- phyloseq_to_deseq2(food, ~ FoodType)
dds <- DESeq2::DESeq(cds)
results <- DESeq2::results(dds) %>% as.data.frame()
```

We can explore the full result table:

```r!
DT::datatable(results, filter = "top", 
              extensions = 'Buttons', 
              options = list(dom = "Bltip", buttons = c('csv'))) %>% 
  DT::formatRound(columns = names(results), digits = 4)
```

```csvpreview {header="true"}
"","baseMean","log2FoldChange","lfcSE","stat","pvalue","padj"
"otu_00520","0.4767","3.6385","1.9375","1.8779","0.0604","0.0972"
"otu_00555","2.4459","-0.9471","0.8359","-1.1331","0.2572","0.3212"
"otu_00568","0.2887","2.6922","2.2762","1.1828","0.2369","0.2995"
"otu_00566","0.2174","0.6499","2.9188","0.2226","0.8238","0.8472"
"otu_00569","13.3333","1.3857","0.9009","1.5382","0.1240","0.1722"
...
```

Or only select the OTUs with an adjusted p-value lower than 0.05 and sort them according to fold-change.

```r!
da_otus <- results %>% as_tibble(rownames = "OTU") %>% 
  filter(padj < 0.05) %>% 
  arrange(log2FoldChange) %>% 
  pull(OTU)
length(da_otus)
```

```!
# [1] 273
```

We end up with 273 significant OTUs sorted according to their fold-change. We keep only those OTUs in that order in the heat map.

```r!
p <- plot_heatmap(prune_taxa(da_otus, food), ## keep only da otus...
                  taxa.order = da_otus       ## ordered according to fold-change
                  ) + 
  facet_grid(~EnvType, scales = "free_x", space = "free_x") + 
  scale_fill_gradient2(low = "#1a9850", mid = "#ffffbf", high = "#d73027",
                       na.value = "white", trans = log_trans(4),
                       midpoint = log(100, base = 4))
```

```r!
plot(p)
```

![](https://hackmd.io/_uploads/S1e26wGtK.png)


<br>

# A few conclusions

The different elements we’ve seen allow us to draw a few conclusions:

* different foods harbor different ecosystems;
* they may harbor a lot of taxa, the effective number of species is quite low;
* the high observed number of OTUs in dices of bacon is caused to by salt addition, which also moves bacon samples towards seafoods.
* the samples are really different and well separated when considering the UniFrac distance (in which case food origin accounts for 63% of the total variability)
* differential analyses is helpful to select and zoom at specific portions of the count table.


{%hackmd r1T7KyvLY %}

[^chaillou2015]: Chaillou, S., Chaulot-Talmon, A., Caekebeke, H. et al. Origin and ecological selection of core and food-specific bacterial communities associated with meat and seafood spoilage. ISME J 9, 1105–1118 (2015). https://doi.org/10.1038/ismej.2014.202