-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09- Alpha diversity.Rmd
492 lines (415 loc) · 15.9 KB
/
09- Alpha diversity.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
---
title: "Alpha diversity"
summary: "NP_devStages_ampliseq"
author: "Marwa Tawfik"
date: "14 07 2023"
output: html_document
---
Summary
THere is a decreasing trend from stimulus through intermediate to challenge phases of alpha diversity
Pairwise showed stimulus is higher than both interm and chall (no diff between interm and chall although wilcox showed there is!)
M showed higher alpha div than V fish (model) without any differece at each phase
Group + Phase was chosen with AIC (shouldn't be reliable as plot showed the assumptions of lm not met), Group alone was not selected though while Phase was. HOw? although interaction showed no sig diff?
lmp -> intestine alpha div is lower than feed and water, but no diff between water and feed (although diff shown in wilcox)
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, results="hide"}
# load libraries
library(phyloseq)
library(microeco)
library(file2meco)
library(tidyverse)
library(ggplot2)
library(SRS)
library(lmPerm)
library(rstatix)
```
```{r}
# import phyloseq and transform into microtable
ps.prev <- readRDS("phyobjects/ps.prev.f.rds") # vector created in previous steps
meta <- as.data.frame(sample_data(ps.prev))
write.table(meta, "meta_ps.prev.txt", sep = "\t", quote = F, col.names = NA)
meta1 <- read.table("meta_ps.prev1.txt", header = T, sep = "\t")
rownames(meta1) <- meta1$X
meta1 <- meta1[, -1]
sample_data(ps.prev)$Group <- meta1$Group
dataset <- phyloseq2meco(ps.prev)
ps.prev.intes <- subset_samples(ps.prev, sample == "intestine")
ps.prev.intes <- prune_taxa(taxa_sums(ps.prev.intes) > 0, ps.prev.intes)
saveRDS(ps.prev.intes, "phyobjects/ps.prev.intes.f.f.rds") # vector from previous steps
dataset.intes <- phyloseq2meco(ps.prev.intes)
sample_data(ps.prev.intes) #sanity check
ps.prev.feed <- subset_samples(ps.prev, sample == "feed")
ps.prev.feed <- prune_taxa(taxa_sums(ps.prev.feed) > 0, ps.prev.feed)
saveRDS(ps.prev.feed, "phyobjects/ps.prev.feed.f.f.rds") # vector from previous steps
dataset.feed <- phyloseq2meco(ps.prev.feed)
ps.prev.water <- subset_samples(ps.prev, sample == "water")
ps.prev.water <- prune_taxa(taxa_sums(ps.prev.water) > 0, ps.prev.water)
saveRDS(ps.prev.water, "phyobjects/ps.prev.water.f.f.rds") # vector from previous steps
dataset.water <- phyloseq2meco(ps.prev.water)
```
```{r}
# SRS when method = "SRS"
d1 <- clone(dataset)
d1$rarefy_samples(method = "SRS", rngseed = 123)
# View(d2$otu_table)
# d1$sample_sums()
ps.prev <- meco2phyloseq(d1)
```
```{r}
# SRS when method = "SRS"
d2 <- clone(dataset.intes)
d2$rarefy_samples(method = "SRS", rngseed = 123)
# View(d2$otu_table)
# d2$sample_sums()
ps.prev.intes <- meco2phyloseq(d2)
```
```{r}
# SRS when method = "SRS"
d3 <- clone(dataset.feed)
d3$rarefy_samples(method = "SRS", rngseed = 123)
# View(d2$otu_table)
# d2$sample_sums()
ps.prev.feed <- meco2phyloseq(d3)
```
```{r}
# SRS when method = "SRS"
d4 <- clone(dataset.water)
d4$rarefy_samples(method = "SRS", rngseed = 123)
# View(d2$otu_table)
# d2$sample_sums()
ps.prev.water <- meco2phyloseq(d4)
```
```{r}
# use phyloseq package ----
# Calculate Observed Species (eveness), Choa1 and Shannon using estimate_richness function of PhyloSeq:
ps.prev.alpha.div <- estimate_richness(ps.prev, measures = c("Observed", "Chao1", "Shannon", "Simpson"))
ps.prev.intes.alpha.div <- estimate_richness(ps.prev.intes, measures = c("Observed", "Chao1", "Shannon", "Simpson"))
ps.prev.feed.alpha.div <- estimate_richness(ps.prev.feed, measures = c("Observed", "Chao1", "Shannon", "Simpson"))
ps.prev.water.alpha.div <- estimate_richness(ps.prev.water, measures = c("Observed", "Chao1", "Shannon", "Simpson"))
# Add metadata to make plots:
ps.prev.rich <- cbind(as.data.frame(sample_data(ps.prev)), ps.prev.alpha.div)
write.table(ps.prev.rich, "tables/ps.prev.rich.txt", sep="\t", quote=F, col.names=NA)
ps.prev.intes.rich <- cbind(as.data.frame(sample_data(ps.prev.intes)), ps.prev.intes.alpha.div)
#write.table(ps.prev.intes.rich, "tables/ps.prev.intes.rich.txt", sep="\t", quote=F, col.names=NA)
ps.prev.feed.rich <- cbind(as.data.frame(sample_data(ps.prev.feed)), ps.prev.feed.alpha.div)
ps.prev.water.rich <- cbind(as.data.frame(sample_data(ps.prev.water)), ps.prev.water.alpha.div)
head(ps.prev.intes.rich) # sanity check for each subset
```
```{r}
# check if assumption of linear model is met or not
model <- lm(Shannon ~ Group + Phase + Group * Phase, data = ps.prev.intes.rich)
plot(model)
# nearly equal variance for each tested group/treatmnet (plot1)
# linearity of the model is not achieved (plot 2)
```
```{r}
step(lm(Shannon ~ Group + Phase + Group * Phase, data = ps.prev.intes.rich))
```
```{r}
summary(lm(Shannon ~ Group + Phase + Group * Phase, data = ps.prev.intes.rich))
```
```{r}
model <- lmp(Shannon ~ Group + Phase + Group * Phase, data = ps.prev.intes.rich)
summary(model)
```
```{r}
# run non-paraemtic to chek for the pairwise statitiscal signfincance
# pyloric (1) is significantly higher from distal (3) **
# group1 group3 n1=23 n2=34 statistic=574 p=0.003 p.adj=0.008 sig=**
pairwise_wilcox_test(ps.prev.intes.rich,
Shannon ~ Group,
p.adjust.method = "BH")
```
```{r}
pairwise_wilcox_test(ps.prev.intes.rich,
Shannon ~ Phase,
p.adjust.method = "BH")
```
M fish
```{r}
Mfish <- subset(ps.prev.intes.rich, Group %in% "M fish")
model <- lmp(Shannon ~ Phase, data = Mfish)
summary(model)
```
V fish
```{r}
Vfish <- subset(ps.prev.intes.rich, Group %in% "V fish")
model <- lmp(Shannon ~ Phase, data = Vfish)
summary(model)
```
Stimulus
```{r}
stimulus <- subset(ps.prev.intes.rich, Phase %in% "stimulus")
model <- lmp(Shannon ~ Group, data = stimulus)
summary(model)
```
```{r}
interm <- subset(ps.prev.intes.rich, Phase %in% "intermediate")
contr <- list(Group="contr.treatment")
model <- lmp(Shannon ~ Group, data = interm, contrasts = contr)
summary(model)
```
```{r}
chall <- subset(ps.prev.intes.rich, Phase %in% "challenge")
contr <- list(Group="contr.treatment")
model <- lmp(Shannon ~ Group, data = chall, contrasts = contr)
summary(model)
```
interm vs chall
relevel for interm vs chall comparison within the model without splitting or removing the stim
```{r}
class(ps.prev.intes.rich$Phase) # factor
levels(ps.prev.intes.rich$Phase)
ps.prev.intes.rich$Phase1 <- relevel(ps.prev.intes.rich$Phase, ref = "intermediate")
levels(ps.prev.intes.rich$Phase1) # "intermediate" "stimulus" "challenge"
model <- lmp(Shannon ~ Phase1, data = ps.prev.intes.rich)
summary(model)
# create contrast to see names of levels
contr <- list(Phase1="contr.treatment")
model <- lmp(Shannon ~ Phase1, data = ps.prev.intes.rich, contrasts = contr)
summary(model)
```
```{r}
pairwise_wilcox_test(ps.prev.intes.rich,
Shannon ~ Regime,
p.adjust.method = "BH")
```
```{r}
pairwise_wilcox_test(ps.prev.intes.rich,
Shannon ~ Group,
p.adjust.method = "BH")
```
```{r}
pairwise_wilcox_test(ps.prev.rich,
Shannon ~ sample_regime,
p.adjust.method = "BH")
wilcox.ps.prev.rich <- pairwise_wilcox_test(ps.prev.rich,
Shannon ~ sample_regime,
p.adjust.method = "BH")
# write.table(wilcox.ps.prev.rich, "tables/wilcox.ps.prev.rich.txt", sep = "\t", quote = F, col.names = NA)
# gut is lower from water and feed under each regime (regardless of the regime)
# no stat sig. between two regimes for any sample type (MMV vs VMV): gut or feed or water
```
```{r}
contr <- list(sample="contr.treatment")
model <- lmp(Shannon ~ sample, data = ps.prev.rich, contrasts = contr)
summary(model)
```
```{r}
class(ps.prev.rich$sample) # factor
levels(ps.prev.rich$sample)
ps.prev.rich$sample1 <- relevel(ps.prev.rich$sample, ref = "feed")
levels(ps.prev.rich$sample1) # "intermediate" "stimulus" "challenge"
# create contrast to see names of levels
contr <- list(sample1="contr.treatment")
model <- lmp(Shannon ~ sample1, data = ps.prev.rich, contrasts = contr)
summary(model)
```
```{r}
pairwise_wilcox_test(ps.prev.rich,
Shannon ~ sample,
p.adjust.method = "BH")
# gut is lower than water (****) and feed (****)
# feed is lower than water (*)
```
```{r}
#tiff("figures/fig2a.tiff", height = 5, width = 5, units = "in", res = 300, compression = "lzw")
# plot(Shannon ~ jitter(Phase, amount = 0.2), data = ps.prev.intes.rich, xlim = c(0.5, 3.5), axes = F, xlab = "Phase", ylab = "Shannon Weiner", pch = 16)
# box()
# axis(2)
# axis(1, at = c(1,2,3), labels = c("stimulus", "intermediate", "challenge"))
# ps.prev.intes.rich$Phase <- as.numeric(ps.prev.intes.rich$Region)
#
# model <- lmp(Shannon ~ Phase, ps.prev.intes.rich)
# fakeX <- seq(0, 3.5, by = 0.01)
# predictedY <- predict(model, newdata = data.frame(gut.order = fakeX), se.fit = T)
# lines(fakeX, predictedY$fit, lwd = 3, col = "blue")
# lines(fakeX, predictedY$fit + predictedY$se.fit, lwd = 1, lty = 2, col = "blue")
# lines(fakeX, predictedY$fit - predictedY$se.fit, lwd = 1, lty = 2, col = "blue")
#dev.off()
```
```{r}
ggplot(ps.prev.intes.rich, aes(x = Phase, y = Shannon)) +
theme_bw() +
geom_point(size = 1.3, position = position_jitter(width = 0.2), alpha = 0.8) +
theme(text = element_text(size = 12)) +
labs(y = "Shannon-Weiner", x = "Phase") #+
#scale_color_manual(values = c("#C71585", "#008080"))
ggsave("figures/fig2a.tiff", height = 3, width = 4, units = "in", dpi = 300, compression = "lzw")
```
```{r}
ggplot(ps.prev.intes.rich, aes(x = Phase, y = Shannon, color = Group)) +
geom_point(size = 1.1, position = position_jitterdodge(jitter.width = 0.3, dodge.width = 1)) +
theme_bw() +
#facet_wrap(~Phylum) +
labs(y = "Shannon-Weiner", x = "Phase") +
#theme(axis.text.x = element_text(angle = 90)) +
theme(text = element_text(size = 12)) +
scale_color_manual(values = c("#C71585", "#008080"), labels = c("M", "V")) +
guides(color = guide_legend(title = "Group"))
ggsave("figures/fig2b.tiff", height = 3, width = 4, units = "in", dpi = 300, compression = "lzw")
```
```{r}
ggplot(ps.prev.intes.rich, aes(x = Group, y = Shannon, color = Phase)) +
theme_bw() +
geom_point(size = 1.3, position = position_jitter(width = 0.2), alpha = 0.8) +
theme(text = element_text(size = 12)) +
labs(y = "Shannon-Weiner", x = "Group") +
scale_color_manual(values = c("#FF7F00", "#6A3D9A", "#A6CEE3"))
ggsave("figures/fig2c.tiff", height = 3, width = 4, units = "in", dpi = 300, compression = "lzw")
```
```{r}
ggplot(ps.prev.rich, aes(x = sample, y = Shannon)) +
theme_bw() +
geom_point(size = 0.5, position = position_jitter(width = 0.2), alpha = 0.8) +
theme(text = element_text(size = 12)) +
labs(y = "Shannon-Weiner", x = "Sample type")
ggsave("figures/fig2d.tiff", height = 3, width = 4, units = "in", dpi = 300, compression = "lzw")
```
Water
```{r}
# check if assumption of linear model is met or not
model <- lm(Shannon ~ Group + Phase + Group * Phase, data = ps.prev.water.rich)
plot(model)
# nearly equal variance for each tested group/treatmnet (plot1)
# linearity of the model is not achieved (plot 2)
```
```{r}
step(lm(Shannon ~ Group + Phase + Group * Phase, data = ps.prev.water.rich))
```
```{r}
summary(lm(Shannon ~ Group + Phase + Group * Phase, data = ps.prev.water.rich))
```
```{r}
model <- lmp(Shannon ~ Group + Phase + Group * Phase, data = ps.prev.water.rich)
summary(model)
```
```{r}
# run non-paraemtic to chek for the pairwise statitiscal signfincance
# pyloric (1) is significantly higher from distal (3) **
# group1 group3 n1=23 n2=34 statistic=574 p=0.003 p.adj=0.008 sig=**
pairwise_wilcox_test(ps.prev.water.rich,
Shannon ~ Group,
p.adjust.method = "BH")
```
```{r}
pairwise_wilcox_test(ps.prev.water.rich,
Shannon ~ Phase,
p.adjust.method = "BH")
```
M fish
```{r}
Mwater <- subset(ps.prev.water.rich, Group %in% "M water")
model <- lmp(Shannon ~ Phase, data = Mwater)
summary(model)
```
V fish
```{r}
Vwater <- subset(ps.prev.water.rich, Group %in% "V water")
model <- lmp(Shannon ~ Phase, data = Vwater)
summary(model)
```
Stimulus
```{r}
stimulus <- subset(ps.prev.water.rich, Phase %in% "stimulus")
model <- lmp(Shannon ~ Group, data = stimulus)
summary(model)
```
```{r}
interm <- subset(ps.prev.water.rich, Phase %in% "intermediate")
contr <- list(Group="contr.treatment")
model <- lmp(Shannon ~ Group, data = interm, contrasts = contr)
summary(model)
```
```{r}
chall <- subset(ps.prev.water.rich, Phase %in% "challenge")
contr <- list(Group="contr.treatment")
model <- lmp(Shannon ~ Group, data = chall, contrasts = contr)
summary(model)
```
interm vs chall
relevel for interm vs chall comparison within the model without splitting or removing the stim
```{r}
class(ps.prev.water.rich$Phase) # factor
levels(ps.prev.water.rich$Phase)
ps.prev.water.rich$Phase1 <- relevel(ps.prev.water.rich$Phase, ref = "intermediate")
levels(ps.prev.water.rich$Phase1) # "intermediate" "stimulus" "challenge"
model <- lmp(Shannon ~ Phase1, data = ps.prev.water.rich)
summary(model)
# create contrast to see names of levels
contr <- list(Phase1="contr.treatment")
model <- lmp(Shannon ~ Phase1, data = ps.prev.water.rich, contrasts = contr)
summary(model)
```
```{r}
pairwise_wilcox_test(ps.prev.water.rich,
Shannon ~ Regime,
p.adjust.method = "BH")
```
```{r}
contr <- list(sample="contr.treatment")
model <- lmp(Shannon ~ sample, data = ps.prev.rich, contrasts = contr)
summary(model)
```
```{r}
#tiff("figures/fig2a.tiff", height = 5, width = 5, units = "in", res = 300, compression = "lzw")
# plot(Shannon ~ jitter(Phase, amount = 0.2), data = ps.prev.water.rich, xlim = c(0.5, 3.5), axes = F, xlab = "Phase", ylab = "Shannon Weiner", pch = 16)
# box()
# axis(2)
# axis(1, at = c(1,2,3), labels = c("stimulus", "intermediate", "challenge"))
# ps.prev.water.rich$Phase <- as.numeric(ps.prev.water.rich$Region)
#
# model <- lmp(Shannon ~ Phase, ps.prev.water.rich)
# fakeX <- seq(0, 3.5, by = 0.01)
# predictedY <- predict(model, newdata = data.frame(gut.order = fakeX), se.fit = T)
# lines(fakeX, predictedY$fit, lwd = 3, col = "blue")
# lines(fakeX, predictedY$fit + predictedY$se.fit, lwd = 1, lty = 2, col = "blue")
# lines(fakeX, predictedY$fit - predictedY$se.fit, lwd = 1, lty = 2, col = "blue")
#dev.off()
```
```{r}
ggplot(ps.prev.water.rich, aes(x = Phase, y = Shannon, color = Group)) +
theme_bw() +
geom_point(size = 1.3, position = position_jitter(width = 0.2), alpha = 0.8) +
theme(text = element_text(size = 12)) +
labs(y = "Shannon-Weiner", x = "Phase") +
scale_color_manual(values = c("#C71585", "#008080"))
#ggsave("figures/fig2b.tiff", height = 3, width = 4, units = "in", dpi = 300, compression = "lzw")
```
```{r}
ggplot(ps.prev.water.rich, aes(x = Group, y = Shannon, color = Phase)) +
theme_bw() +
geom_point(size = 1.3, position = position_jitter(width = 0.2), alpha = 0.8) +
theme(text = element_text(size = 12)) +
labs(y = "Shannon-Weiner", x = "Group") +
scale_color_manual(values = c("#FF7F00", "#6A3D9A", "#A6CEE3"))
#ggsave("figures/fig2c.tiff", height = 3, width = 4, units = "in", dpi = 300, compression = "lzw")
```
Feed
```{r}
# check if assumption of linear model is met or not
model <- lm(Shannon ~ Regime, data = ps.prev.feed.rich)
plot(model)
# nearly equal variance for each tested group/treatmnet (plot1)
# linearity of the model is not achieved (plot 2)
```
```{r}
step(lmp(Shannon ~ Regime, data = ps.prev.feed.rich))
```
```{r}
model <- lmp(Shannon ~ Regime, data = ps.prev.feed.rich)
summary(model)
```
```{r}
# run non-paraemtic to chek for the pairwise statitiscal signfincance
# pyloric (1) is significantly higher from distal (3) **
# group1 group3 n1=23 n2=34 statistic=574 p=0.003 p.adj=0.008 sig=**
pairwise_wilcox_test(ps.prev.feed.rich,
Shannon ~ Regime,
p.adjust.method = "BH")
```
```{r}
sessionInfo()
```