-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTutorial_Examples.Rmd
More file actions
144 lines (97 loc) · 4.21 KB
/
Tutorial_Examples.Rmd
File metadata and controls
144 lines (97 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
output:
html_document: default
pdf_document: default
---
## Table of contents
#### 1. [The Packages](#Packages)
#### 2. [The BDTT function](#Functions)
#### 3. [Example](#Example)
#####3.1. Computing BDTT
#####3.2. Testing the statistical link between BDTT and metadata
## 1. The needed R packages <a name="Packages"></a>
Before running the BDTT analysis, you will need to install and load the following R packages:
* ape
* castor
* matrix
* abind
```{r, message=FALSE}
library(ape)
library(castor)
library(abind)
library(Matrix)
```
## 2. The BDTT function <a name="Functions"></a>
##### The BDTT function
requires the follwing inputs:
###### similarity_slices:
the slices (i.e. the multiple phylogenetic resolutions) at which you want to aggregate the tips of the phylogeny and compute corresponding beta-diversity. 0 corresponds to no aggregation, i.e use the raw tips of the phylogeny as microbial units. Values >0 will aggregate the tips of the phylogeny according to the given value to create aggregated microbial units and compute corresponding beta-diversity. Use the 'getHnodes' function to have an idea of the resolution slices you can explore (see below).
###### tree:
the species (or OTUs, or sequence variants) phylogeny (the names of the tips must match those in the site*species matrix)
###### sampleOTUs:
samples * species (or OTUs, or sequence variants) matrix
###### onlyBeta:
Putting "TRUE" (default) will make the function return beta-diversity dissimilary matrices only
Putting "FALSE" will make the function return beta-diversity dissimilary matrices + matrix detailling the relationship between tips and the aggregated units.
###### metric:
Beta-diversity metric chosen; we provide Jaccard ("Jac") its true turnover component ("Jac_TT") and Bray-Curtis ("Bray").
The function requires the following input:
###### tree:
the species (or OTUs, or sequence variants) phylogeny
## 2. Examples <a name="Example"></a>
### Computing BDTT
```{r, message=FALSE}
library(picante)
data(phylocom)
TreeExample=phylocom$phylo
plot(TreeExample)
SiteSpExample=t(phylocom$sample)
SiteSpExample
source("BDTT_functions.R")
hist(get_all_node_depths(TreeExample))
slices=c(0:3)
Betas=BDTT(similarity_slices = slices,tree = TreeExample,sampleOTUs = (SiteSpExample))
```
### Linking BDTT with environement / metadata
Create random metada catageory
```{r, message=FALSE}
Meta=sample(x=c("Condition_1","Condition_2"),size=dim(SiteSpExample)[2],replace = T)
names(Meta)=colnames(SiteSpExample)
Meta
```
Test statistically the link between metadata and BDTT using PERMANOVA
Load vegan to be able to use adonius function
```{r, message=FALSE}
library(vegan)
```
Example of the test for a given resolution (0) and a given metric (Jaccard); make sure that samples are in the same order
```{r, message=FALSE}
samples=names(Meta)
adonis(Betas["0","Jac",samples,samples]~Meta[samples])
```
Construct table to store results in a ready-to-use format for ggplot
```{r, message=FALSE}
predictors="Conditions1_2"
StatsRes=expand.grid(similarity_slices=as.character(slices),predictors=predictors,metric=c("Jac","Bray"))
StatsRes[["F.Model"]]=StatsRes[["R2"]]=StatsRes[["Pr(>F)"]]=NA
head(StatsRes)
```
Run multiple PERMANOVA across phylogenetic resolution and store results in a table ready to use for ggplot
```{r, message=FALSE}
for (i in as.character(slices))
{
res=unlist(adonis(formula =Betas[i,"Jac",samples,samples]~Meta[samples])$aov.tab[1,c(6,5,4)])
StatsRes[(StatsRes$metric=="Jac")&(StatsRes$similarity_slices==i),4:6]=res
res=unlist(adonis(formula =Betas[i,"Bray",samples,samples]~Meta[samples])$aov.tab[1,c(6,5,4)])
StatsRes[(StatsRes$metric=="Bray")&(StatsRes$similarity_slices==i),4:6]=res
}
```
We can then plot the profiles of R2 along the phylogenetic time scale:
```{r, message=FALSE}
library(ggplot2)
ggplot(aes(y=R2,x=similarity_slices,colour=predictors,group=factor(predictors)),data=StatsRes)+geom_point()+geom_line()+facet_wrap(~metric)
```
or just the profile for the significant effects (not run cause nothing is significant)
```{r, message=FALSE}
#ggplot(aes(y=R2,x=similarity_slices,colour=predictors,group=factor(predictors)),data=StatsRes[StatsRes$`Pr(>F)`<.05,])+geom_point()+geom_line()+facet_wrap(~metric)
```