-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts_diversity_16s.R
384 lines (301 loc) · 13.3 KB
/
scripts_diversity_16s.R
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
### Scripts originally created by Ruben Garrido-Oter and modified by Ka-Wai Ma
# The scipt can be used to generate the PCA and CPcoA plots. Change the name of
# the files and samples e.g. strain ID accordingly for all 3 experiments
options(warn=-1)
# cleanup
rm(list=ls())
# load plotting functions
source("plotting_functions.R")
source("cpcoa.func.R")
if (!require(tidyverse)) install.packages("tidyverse")
if (!require(FSA)) install.packages("FSA")
if (!require(multcomp)) install.packages("multcomp")
library(tidyverse)
library(multcomp)
library(FSA)
# directories
results.dir <- "../results/"
data.dir <- "../data/"
figures.dir <- "../figures/"
# files
design.file <- paste(data.dir, "pwer_001_design.txt", sep="")
otu_table.file <- paste(results.dir, "otu_table_pwer1.txt", sep="")
otu_table_unfiltered.file <- paste(results.dir, "otu_table_unfiltered_pwer1.txt", sep="")
taxonomy.file <- paste(data.dir, "pwer_001_strains.txt", sep="")
# load data
design <- read.table(design.file, header=T, sep="\t")
otu_table <- read.table(otu_table.file, sep="\t", header=T, check.names=F)
otu_table_unfiltered <- read.table(otu_table_unfiltered.file, sep="\t", header=T, check.names=F)
taxonomy <- read.table(taxonomy.file, sep="\t", header=T, check.names=F)
# re-order data matrices
idx <- design$SampleID %in% colnames(otu_table)
design <- design[idx, ]
idx <- match(design$SampleID, colnames(otu_table))
otu_table <- otu_table[, idx]
idx <- match(design$SampleID, colnames(otu_table_unfiltered))
otu_table_unfiltered <- otu_table_unfiltered[, idx]
# normalize otu tables
design$depth <- colSums(otu_table)
design$depth_unfiltered <- colSums(otu_table_unfiltered)
otu_table_norm <- apply(otu_table, 2, function(x) x/sum(x))
otu_table_unfiltered_norm <- apply(otu_table_unfiltered, 2, function(x) x/sum(x))
idx <- rownames(otu_table_unfiltered_norm) %in% c("good", "noisy", "chimera", "other")
design <- cbind(design, t(otu_table_unfiltered_norm[idx, ]))
design$exact <- 1-rowSums(t(otu_table_unfiltered_norm[!idx, ]))
idx <- rownames(otu_table_norm) == "ecoli_dh5a"
ecoli <- otu_table_norm[idx, ]
otu_table <- otu_table[!idx, ]
otu_table_norm <- apply(otu_table, 2, function(x) x/sum(x))
design <- cbind(design, ecoli)
design$ecoli_density <- design$ecoli / design$root.weight
idx <- rownames(otu_table_norm) %in% taxonomy$strain[taxonomy$syncom=="NS"]
aggRANS <- colSums(otu_table_norm[idx, ])
design <- cbind(design, aggRANS)
idx <- rownames(otu_table_norm) %in% taxonomy$strain[taxonomy$syncom=="S"]
aggRAS <- colSums(otu_table_norm[idx, ])
design <- cbind(design, aggRAS)
design$cond <- paste(design$SynCom, design$flg22, design$genotype)
### compute beta diversity
colors <- data.frame(group=c("input", "agar", "agar_plant","root"),
color=c(c_black, c_grey, c_dark_brown, c_orange))
# PCoA Bray-Curtis
# subset samples of interest
# compartment-specific effect of samples inoculated with different SynComs
# we will use all data point except for replicate c in experiment 1 due to
# potential contamination or PCR error.
# SynCom NS
idx <- design$compartment %in% c("input", "root", "agar", "agar_plant") &
design$SynCom %in% c("NS") &
!design$biological.replicate=="c" &
TRUE
design_fullNS <- design[idx, ]
otu_table_norm_fullNS <- otu_table_norm[, idx]
otu_table_unfiltered_norm_NS_full <- otu_table_unfiltered_norm[, idx]
bray_curtis <- vegdist(t(otu_table_norm_fullNS), method="bray")
k <- 2
pcoa <- cmdscale(bray_curtis, k=k, eig=T)
points <- pcoa$points
eig <- pcoa$eig
points <- as.data.frame(points)
colnames(points) <- c("x", "y")
points_fullNS <- cbind(points, design[match(rownames(points), design$SampleID), ])
colors_pcoa <- colors[colors$group %in% points_fullNS$compartment, ]
points_fullNS$compartment <- factor(points_fullNS$compartment, levels=colors_pcoa$group)
# SynCom S
idx <- design$compartment %in% c("input", "root", "agar", "agar_plant") &
design$SynCom %in% c("S") &
!design$biological.replicate =="c" &
TRUE
design_fullS <- design[idx, ]
otu_table_norm_fullS <- otu_table_norm[, idx]
otu_table_unfiltered_norm_S_full <- otu_table_unfiltered_norm[, idx]
bray_curtis <- vegdist(t(otu_table_norm_fullS), method="bray")
k <- 2
pcoa <- cmdscale(bray_curtis, k=k, eig=T)
points <- pcoa$points
eig <- pcoa$eig
points <- as.data.frame(points)
colnames(points) <- c("x", "y")
points_fullS <- cbind(points, design[match(rownames(points), design$SampleID), ])
colors_pcoa <- colors[colors$group %in% points_fullS$compartment, ]
points_fullS$compartment <- factor(points_fullS$compartment, levels=colors_pcoa$group)
# SynCom NS+S
idx <- design$compartment %in% c("input", "root", "agar", "agar_plant") &
design$SynCom %in% c("NS+S") &
!design$biological.replicate=="c" &
TRUE
design_fullNSS <- design[idx, ]
otu_table_norm_fullNSS <- otu_table_norm[, idx]
otu_table_unfiltered_norm_NSS_full <- otu_table_unfiltered_norm[, idx]
bray_curtis <- vegdist(t(otu_table_norm_fullNSS), method="bray")
k <- 2
pcoa <- cmdscale(bray_curtis, k=k, eig=T)
points <- pcoa$points
eig <- pcoa$eig
points <- as.data.frame(points)
colnames(points) <- c("x", "y")
points_fullNSS <- cbind(points, design[match(rownames(points), design$SampleID), ])
colors_pcoa <- colors[colors$group %in% points_fullNSS$compartment, ]
points_fullNSS$compartment <- factor(points_fullNSS$compartment, levels=colors_pcoa$group)
### plot PCo 1 and 2
ggplot(points_fullNS, aes(x=x, y=y, color=compartment)) +
geom_point(alpha=.7, size=4.0) +
scale_colour_manual(values=as.character(colors_pcoa$color)) +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
theme(axis.title=element_text(size=13)) +
labs(x=paste("PCoA 1 (", format(100 * eig[1] / sum(eig), digits=4), "%)", sep=""),
y=paste("PCoA 2 (", format(100 * eig[2] / sum(eig), digits=4), "%)", sep="")) +
main_theme +
theme(legend.position="top") +
ggsave(paste(figures.dir, "PCoA_NS.pdf", sep=""), width=7, height=5)
# ggsave(paste(figures.dir, "PCoA_NS.tiff", sep=""), width=7, height=5)
ggplot(points_fullS, aes(x=x, y=y, color=compartment)) +
geom_point(alpha=.7, size=4.0) +
scale_colour_manual(values=as.character(colors_pcoa$color)) +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
theme(axis.title=element_text(size=13)) +
labs(x=paste("PCoA 1 (", format(100 * eig[1] / sum(eig), digits=4), "%)", sep=""),
y=paste("PCoA 2 (", format(100 * eig[2] / sum(eig), digits=4), "%)", sep="")) +
main_theme +
theme(legend.position="top") +
ggsave(paste(figures.dir, "PCoA_S.pdf", sep=""), width=7, height=5)
# ggsave(paste(figures.dir, "PCoA_S.tiff", sep=""), width=7, height=5)
ggplot(points_fullNSS, aes(x=x, y=y, color=compartment)) +
geom_point(alpha=.7, size=4.0) +
scale_colour_manual(values=as.character(colors_pcoa$color)) +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
theme(axis.title=element_text(size=13)) +
labs(x=paste("PCoA 1 (", format(100 * eig[1] / sum(eig), digits=4), "%)", sep=""),
y=paste("PCoA 2 (", format(100 * eig[2] / sum(eig), digits=4), "%)", sep="")) +
main_theme +
theme(legend.position="top") +
ggsave(paste(figures.dir, "PCoA_NSS.pdf", sep=""), width=7, height=5)
# ggsave(paste(figures.dir, "PCoA_NSS.tiff", sep=""), width=7, height=5)
### CPCoA Bray-Curtis of samples conditioned by technical factors
colors <- data.frame(group=c(
"NS FALSE pWER::FLS2-GFP",
"NS TRUE pWER::FLS2-GFP",
"NS FALSE Col-0",
"NS TRUE Col-0",
"S FALSE pWER::FLS2-GFP",
"S TRUE pWER::FLS2-GFP",
"S FALSE Col-0",
"S TRUE Col-0",
"NS+S FALSE pWER::FLS2-GFP",
"NS+S TRUE pWER::FLS2-GFP",
"NS+S FALSE Col-0",
"NS+S TRUE Col-0"
),
color=c(
"#f8766d",
"#f8766d",
"#f8766d",
"#f8766d",
"#00bfc4",
"#00bfc4",
"#00bfc4",
"#00bfc4",
"#800080",
"#800080",
"#800080",
"#800080"
))
shapes <- data.frame(group=c(
"NS FALSE pWER::FLS2-GFP",
"NS TRUE pWER::FLS2-GFP",
"NS FALSE Col-0",
"NS TRUE Col-0",
"S FALSE pWER::FLS2-GFP",
"S TRUE pWER::FLS2-GFP",
"S FALSE Col-0",
"S TRUE Col-0",
"NS+S FALSE pWER::FLS2-GFP",
"NS+S TRUE pWER::FLS2-GFP",
"NS+S FALSE Col-0",
"NS+S TRUE Col-0"
),
shape=c(
2,
17,
1,
16,
2,
17,
1,
16,
2,
17,
1,
16
))
sqrt_transform <- T
# CPcoA for root samples in different genotypes
idx <- design$compartment %in% c("root") &
design$genotype %in% c("pWER::FLS2-GFP") &
#design$genotype %in% c("Col-0") &
!design$biological.replicate %in% c("c") &
T
d <- design[idx, ]
idx <- colnames(otu_table_norm) %in% d$SampleID
bray_curtis <- vegdist(t(otu_table_norm[, idx]), method="bray")
capscale.gen <- capscale(bray_curtis ~ flg22 * SynCom + Condition(biological.replicate * technical.replicate), data=d, add=F, sqrt.dist=sqrt_transform)
perm_anova.gen <- anova.cca(capscale.gen)
# generate variability tables and calculate confidence intervals for the variance
var_tbl.gen <- variability_table(capscale.gen)
eig <- capscale.gen$CCA$eig
variance <- capscale.gen$CCA$tot.chi / capscale.gen$tot.chi
variance <- var_tbl.gen["constrained", "proportion"]
p.val <- perm_anova.gen[1, 4]
points_cpcoa <- capscale.gen$CCA$wa[, 1:2]
colnames(points_cpcoa) <- c("x", "y")
points_cpcoa <- cbind(points_cpcoa, d[match(rownames(points_cpcoa), d$SampleID), ])
# plot CPCo 1 and 2
colors_cpcoa <- colors[colors$group %in% points_cpcoa$cond, ]
points_cpcoa$cond1 <- factor(points_cpcoa$cond, levels=colors_cpcoa$group)
shapes_cpcoa <- shapes[shapes$group %in% points_cpcoa$cond, ]
points_cpcoa$cond2 <- factor(points_cpcoa$cond, levels=shapes_cpcoa$group)
ggplot(points_cpcoa, aes(x=x, y=y, color=cond1, shape=cond2)) +
geom_point(alpha=.7, size=4.0) +
stat_ellipse(aes(color=cond1), linetype=2, type="t", size=1.0, alpha=.5) +
scale_colour_manual(values=as.character(colors_cpcoa$color)) +
scale_shape_manual(values=shapes_cpcoa$shape) +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
theme(axis.title=element_text(size=13)) +
labs(x=paste("CPCoA 1 (", format(100 * eig[1] / sum(eig), digits=4), "%)", sep=""),
y=paste("CPCoA 2 (", format(100 * eig[2] / sum(eig), digits=4), "%)", sep="")) +
ggtitle("CPCoA of root samples in pwer", paste(format(100 * variance, digits=3), " % of variance; P=", format(p.val, digits=2), sep="")) +
main_theme +
theme(legend.position="bottom")+
ggsave(paste(figures.dir, "CPCoA_root_pwert.pdf", sep=""), height=6, width=5)
# ggsave(paste(figures.dir, "CPCoA_root_pwert.tiff", sep=""), height=6, width=5)
# Repeat the above cPCoA for samples in different geneotype including Col-0
# Permutation analysis for samples inoculated by different SynCom e.g. NS, S and NS+S
idx <- design$compartment %in% c("root") &
design$genotype %in% c("pWER::FLS2-GFP") &
design$SynCom %in% c("NS+S") &
!design$biological.replicate %in% c("c") &
T
d <- design[idx, ]
idx <- colnames(otu_table_norm) %in% d$SampleID
bray_curtis <- vegdist(t(otu_table_norm[, idx]), method="bray")
capscale.gen <- capscale(bray_curtis ~ flg22 * SynCom + Condition(biological.replicate * technical.replicate), data=d, add=F, sqrt.dist=sqrt_transform)
perm_anova.gen <- anova.cca(capscale.gen)
print(perm_anova.gen)
#repeat the same for Col-0 data
### calculate simpson diversity index
trans_otu<-t(otu_table)
#simpson diversity index
simpson<-diversity(trans_otu, "simpson")
simpson<-as.data.frame(simpson)
trans_otu_simpson <- cbind(design,simpson)
dfpwersimpson <- trans_otu_simpson %>% filter(compartment == "root" &
genotype == "pWER::FLS2-GFP" &
!biological.replicate == "c")
dfpwersimpson$cond<-as.factor(dfpwersimpson$cond)
levels(dfpwersimpson$cond)
ggplot(dfpwersimpson, aes(x=cond, y=simpson, fill=flg22)) +
geom_boxplot(alpha=1, outlier.size=0, size=1.0, width=.8) +
geom_jitter(position=position_jitterdodge(0.25), size=2, alpha=0.7) +
scale_x_discrete(limits=c("NS FALSE pWER::FLS2-GFP", "NS TRUE pWER::FLS2-GFP", "S FALSE pWER::FLS2-GFP", "S TRUE pWER::FLS2-GFP","NS+S FALSE pWER::FLS2-GFP", "NS+S TRUE pWER::FLS2-GFP")) +
scale_fill_manual(values=c("white","grey")) +
theme_bw() +
main_theme+
labs(y="Simpson Index")+
ggtitle("Simpson Index of pWER::FLS2-GFP plants")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
ggsave(paste(figures.dir, "Simpson_pwer.pdf", sep=""), height=8, width=7)
# ggsave(paste(figures.dir, "simpson_pwer.tiff", sep=""), height=8, width=7)
# ANOVA
dfpwersimpson$SynCom_flg <- paste(dfpwersimpson$SynCom, dfpwersimpson$flg22)
dfpwersimpson$SynCom_flg<- as.factor(dfpwersimpson$SynCom_flg)
dfpwersimpson$biological.replicate <- design$biological.replicate[match(dfpwersimpson$SampleID, design$SampleID)]
dfpwersimpson$technical.replicate <- design$technical.replicate[match(dfpwersimpson$SampleID, design$SampleID)]
aov <-aov(simpson~ SynCom_flg+biological.replicate*technical.replicate, data=dfpwersimpson)
summary(aov)
tuk <- glht(aov, linfct = mcp(SynCom_flg = "Tukey"))
confint(tuk, level=0.95)
summary(tuk)