-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacterial_analysis.Rmd
254 lines (139 loc) · 5.98 KB
/
bacterial_analysis.Rmd
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
---
title: "Characterization of dairy cow rumen bacterial and archaeal communities associated with grass silage and maize silage based diets (Bacterial)"
output: word_document
---
## set up project folders
```{r}
dir.create("bacteria")
dir.create("bacteria/figures")
dir.create("bacteria/tables")
```
## load libraries
```{r, warning=FALSE, message=FALSE}
library(ggplot2)
library(ape)
library(plyr)
library(vegan)
library(RColorBrewer)
library(reshape2)
library(data.table)
library(microbiome)
library(dplyr)
library(phyloseq)
library(knitr) #for interactive tables
library(picante)
```
## Read files to phyloseq
```{r}
pseq.1 <- readRDS("./input/pseq_bacteria.rds")
```
# Phylogenetic diversity
```{r}
dir.create("./bacteria/PD")
my_colors <- c("#CBD588", "#5F7FC7", "orange","#DA5724", "#508578", "#CD9BCD", "#AD6F3B", "#673770","#D14285", "#652926", "#C84248", "#8569D5", "#5E738F","#D1A33D", "#8A7C64", "#599861", "steelblue" )
set.seed(8729)
otu_table_ps1 <- as.data.frame(pseq.1@otu_table)
metadata_table_ps1 <- as.data.frame(pseq.1@sam_data)
treefile_p1 <- pseq.1@phy_tree
df.pd <- pd(t(otu_table_ps1), treefile_p1,include.root=F)
knitr::kable(df.pd)
write.csv(df.pd, "./bacteria/PD/phylogenetic_diversity_10_17.csv")
```
## Plot PD
```{r}
## Figure 2B and Figure 2A
colnames(metadata_table_ps1)
metadata_table_ps1$PhylogeneticDiversity <- df.pd$PD
library(ggpubr)
lev <- levels(metadata_table_ps1$Timepoint) # get the variables
# make a pairwise list that we want to compare.
L.pairs <- combn(seq_along(lev), 2, simplify = FALSE, FUN = function(i)lev[i])
#str(metadata_table_ps1)
metadata_table_ps1$Diet
plot.pd <- ggplot(metadata_table_ps1, aes(Timepoint, PhylogeneticDiversity)) + geom_boxplot(aes(fill = Timepoint)) + geom_point(size = 3) + theme(axis.text.x = element_text(size=14, angle = 90)) + scale_fill_manual(values = c("forestgreen", "steelblue")) + theme_bw()
print(plot.pd)
#write.csv(metadata_table_ps1, "./bacteria/PD/metadata_table_ps1.csv")
ggsave("./bacteria/PD/phylogenetic_diversity_bacteria_timepoint.pdf", height = 6, width = 7)
plot.pd.diet <- ggplot(metadata_table_ps1, aes(Diet, PhylogeneticDiversity)) + geom_boxplot(aes(fill = Diet)) + geom_point(size = 3) + theme(axis.text.x = element_text(size=14, angle = 90)) + scale_fill_manual(values = c("tomato", "olivedrab4", "lightseagreen","mediumpurple", "#508578")) + theme_bw()
print(plot.pd.diet)
ggsave("./bacteria/PD/PD_diet.pdf", height = 6, width = 7)
```
## Test PD
Comparison of Phylogenetic diversity with key variables.
```{r}
kruskal.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Diet, p.adj = "fdr")
pairwise.wilcox.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Timepoint, p.adj = "fdr")
pairwise.wilcox.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Block, p.adj = "fdr")
ggpubr::ggqqplot(metadata_table_ps1, "PhylogeneticDiversity")
```
Comparison of Phylogenetic diversity with key variables.
```{r}
shapiro.test(metadata_table_ps1$PhylogeneticDiversity)
pairwise.t.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Timepoint)
kruskal.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Diet, p.adj = "fdr")
kruskal.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Diet_Timepoint, p.adj = "fdr")
#pairwise.wilcox.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Diet,
# p.adjust.method = "fdr")
kruskal.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Block, p.adj = "fdr")
# pairwise.wilcox.test(metadata_table_ps1$PhylogeneticDiversity, metadata_table_ps1$Block, p.adj = "fdr")
```
## PCoA
#weighted analysis for bacteria
Supplementary Figure 1
```{r}
#For plotting weighted
ps1.rel <- microbiome::transform(pseq.1, "compositional")
set.seed(49275) #set seed for reproducible rooting of the tree
ordu.wt.uni = ordinate(ps1.rel, "PCoA", "unifrac", weighted = TRUE)
wt.unifrac <- plot_ordination(ps1.rel, ordu.wt.uni,
color = "Diet",
shape = "Timepoint")
wt.unifrac <- wt.unifrac + scale_fill_manual(values = c("tomato", "olivedrab4", "lightseagreen","mediumpurple", "#508578")) +
ggtitle("Weighted UniFrac relative abundance") +
geom_point(size = 5) + theme_bw()
print(wt.unifrac)
ggsave("./bacteria/figures/PCoA_weighted_bacteria_coloured by timepoint.pdf", height = 6, width = 7)
```
Figure 2C
```{r}
#unweighted analysis for bacteria
set.seed(475) #set seed for reproducible rooting of the tree
ordu.unwt.uni = ordinate(ps1.rel, "PCoA", "unifrac", weighted = F)
unwt.unifrac <- plot_ordination(ps1.rel, ordu.unwt.uni,
color = "Diet",
shape = "Timepoint") +
ggtitle("Unweighted UniFrac relative abundance") +
geom_point(size = 5) + theme_bw()
print(unwt.unifrac)
ggsave("./bacteria/figures/PCoA_unweighted_bacteria_coloured by timepoint.pdf", height = 6, width = 7)
```
## beta diversity test
```{r}
metadf <- data.frame(sample_data(ps1.rel))
set.seed(28567)
unifrac.dist <- UniFrac(ps1.rel,
weighted = TRUE,
normalized = TRUE,
parallel = FALSE,
fast = TRUE)
```
```{r}
set.seed(92661694)
adonis(unifrac.dist ~ Diet*Timepoint, data = metadf)
```
# beta diversity test unwt
```{r}
set.seed(285667)
unwt.unifrac.dist <- UniFrac(ps1.rel,
weighted = FALSE,
normalized = TRUE,
parallel = FALSE,
fast = TRUE)
```
```{r}
set.seed(961694)
adonis(unwt.unifrac.dist ~ Diet*Timepoint, data = metadf)
```
```{r}
sessionInfo()
```