-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalpha and beta diversity analysis.R
2349 lines (1721 loc) · 120 KB
/
alpha and beta diversity analysis.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
######Alpha and beta diversity analysis
library(phangorn)
library(ggplot2)
library(BiocStyle)
library(devtools)
library(gridExtra)
library(knitr)
library(phyloseq)
library(DECIPHER)
library(dplyr)
library(microbiome)
library(vegan)
library(viridis)
library(RColorBrewer)
library(DESeq2)
library(decontam)
library(DAtest)
library(samr)
library(rcompanion)
library(dplyr)
library(ggplot2)
library(ggpubr)
library(rstatix)
library(ComplexHeatmap)
library(zCompositions)
library(reshape)
library(ZIBR)
library(lme4)
library(nlme)
library(reshape)
library(fantaxtic)
library(FSA)
library(plotly)
library(tidyverse)
library(splinectomeR)
library(tibble)
library(reshape2)
library(tidyr)
###Alpha diversity
psnoncontam_tru_final_unrarified
ps_total_div_all <- estimate_richness(psnoncontam_tru_final_unrarified)
write.csv(ps_total_div_all, "alpha_div_all.csv")
###Keeping only the measures/indices that I need to plot and importing it here
alpha_time_data <- read.table("alpha_div_plot_time_data.csv", sep=",", check.names = F, header = T)
alpha_time_data
#melting the data frame to use it for box plot and statistical test
alpha_time_data_melt <- melt(alpha_time_data)
alpha_time_data_melt
#Changing the column name variable to alpha, as term variable is used internally in the code, so to avoid conflict..
colnames(alpha_time_data_melt)[3] <- "Alpha"
alpha_time_data_melt
#Rstatix package for doing a pairwise comparison
stat.test.alpha <- alpha_time_data_melt %>%
group_by(Alpha) %>%
wilcox_test(value ~ TimePoint) %>%
adjust_pvalue(method = "fdr") %>%
add_significance()
stat.test.alpha
#if(!require(devtools)) install.packages("devtools")
#devtools::install_github("kassambara/rstatix")
#Adding the xy position for plotting p-values on the box plot
stat.test.alpha <- stat.test.alpha %>% add_xy_position(x = "TimePoint", scales = "free")
stat.test.alpha
#Box plot using the ggboxplot function from ggpubr, using ggplot function gives an error for adding the p-values
bxp.alpha <- ggboxplot(data= alpha_time_data_melt, x = "TimePoint", y= "value", fill = "TimePoint", xlab = "Time Point", ylab= "Alpha diversity measure/index")
bxp.alpha <- facet(p = bxp.alpha,facet.by = "Alpha",scales = "free_y")
bxp.alpha + scale_fill_manual(values=c("#ffb6b9", "#fae3d9", "#bbded6", "#8ac6d3")) + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(legend.position="none") + scale_y_continuous(expand = c(0, 0), limits = c(0, NA)) + geom_point(position = position_dodge(0.6))
bxp.alpha + stat_pvalue_manual(stat.test.alpha, label= "p.adj.signif", tip.length=0.03, hide.ns = T, step.increase = 0.3) + scale_y_continuous(expand = c(0, 0), limits = c(0, NA))
#multiple group comparison
kruskal.test(x= alpha_time_data$Chao1, g= alpha_time_data$TimePoint, p.adjust.method = "fdr")
kruskal.test(alpha_time_data$Shannon, alpha_time_data$TimePoint, p.adjust.method = "fdr")
kruskal.test(alpha_time_data$InvSimpson, alpha_time_data$TimePoint, p.adjust.method = "fdr")
#Multiple pairwise comparison
pairwise.wilcox.test(ps_total_div_all$Observed, sample_data(psnoncontam_tru_final_unrarified)$Description, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(ps_total_div_all$Shannon, sample_data(psnoncontam_tru_final_unrarified)$Description, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(ps_total_div_all$InvSimpson, sample_data(psnoncontam_tru_final_unrarified)$Description, p.adjust.method = "fdr", paired = FALSE)
#######For beta diversity analysis CLR transformed dataset was used using following steps
#Write the phyloseq object as a CSV table with ASVs and counts
psnoncontam_tru_final_rare
write_phyloseq(psnoncontam_tru_final_rare)
#Read the CSV file
abund_table <- read.csv("otu_table_noncontam_tru_final_rare.csv", row.names=1, check.names= FALSE) #Added header "Features" to the first clumn of ASVs
abund_table_t<-t(abund_table)
#View(abund_table_t)
#install.packages("zCompositions")
#library (zCompositions)
abund_table_r <- t(cmultRepl((abund_table_t), method="CZM", output="prop")) #prop calculates the imputed proportion is similar to "counts" and then taking proportions in the previous version; CZM: count zero multiplicative
#Check the before and after files
#View(abund_table)
#View(abund_table_r)
#Apply CLR transformation
abund_clr <- t(apply(abund_table_r, 2, function(x){log(x) - mean (log(x))}))
#View(abund_clr)
#Replacing original counts with CLR transformed values
ps_noncontam_zclr_rare <- psnoncontam_tru_final_rare
otu_table(ps_noncontam_zclr_rare) <- otu_table(abund_clr, taxa_are_rows = F)
ps_noncontam_zclr_rare
ps_noncontam_zclr_rare_pseudo <- ps_noncontam_zclr_rare
otu_table(ps_noncontam_zclr_rare_pseudo) <- otu_table(ps_noncontam_zclr_rare_pseudo) + 3
ps_noncontam_clr_plot_rare_bray <- plot_ordination(ps_noncontam_zclr_rare_pseudo, ordinate(ps_noncontam_zclr_rare_pseudo, "MDS", "bray"), color = "Description") + geom_point(size = 3)
ps_noncontam_clr_plot_rare_bray + theme(panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_color_manual(values=c("#ffb6b9", "#fae3d9", "#bbded6", "#8ac6d3")) + theme(legend.position = "right")
#PERMANOVA
#comparing the samples at different time points
metadata5 <- as(sample_data(ps_noncontam_zclr_rare_pseudo), "data.frame")
adonis(phyloseq::distance(ps_noncontam_zclr_rare_pseudo, method = "bray")~Description, data = metadata5, p.adjust.methods= "fdr", permutations = 99999, strata = metadata5$SubjectID)###added infant IDs as strata to account for repeated measures.
#beta-dispersion test
ps_total_noncontam_dist <- phyloseq::distance(ps_noncontam_zclr_rare_pseudo, method = "bray")
sampledf5 <- data.frame(sample_data(ps_noncontam_zclr_rare_pseudo))
beta5 <- betadisper(ps_total_noncontam_dist, sampledf5$Description, bias.adjust = TRUE)
permutest(beta5, p.adjust.methods= "fdr", permutations = 99999)
#Jaccard distance on presence/absence of ASVs. Used rarified but untranformed phyloseq file here
ps_noncontam_rare_plot_jaccard <- plot_ordination(psnoncontam_tru_final_rare, ordinate(psnoncontam_tru_final_rare, "MDS", "jaccard"), color = "Description") + geom_point(size = 3)
ps_noncontam_rare_plot_jaccard + theme(panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_color_manual(values=c("#ffb6b9", "#fae3d9", "#bbded6", "#8ac6d3")) + theme(legend.position = "right")
#PERMANOVA
#comparing the samples at different time points
metadata5 <- as(sample_data(psnoncontam_tru_final_rare), "data.frame")
adonis(phyloseq::distance(psnoncontam_tru_final_rare, method = "jaccard")~Description, data = metadata5, p.adjust.methods= "fdr", permutations = 99999, strata = metadata5$SubjectID)###added infant IDs as strata to account for repeated measures.
#beta-dispersion test
ps_total_noncontam_dist <- phyloseq::distance(psnoncontam_tru_final_rare, method = "jaccard")
sampledf5 <- data.frame(sample_data(psnoncontam_tru_final_rare))
beta5 <- betadisper(ps_total_noncontam_dist, sampledf5$Description, bias.adjust = TRUE)
permutest(beta5, p.adjust.methods= "fdr", permutations = 99999)
######################################Bacterial abundance over time##################################
##Extracted phylum and genus level OTU tables from phyloseq and added additional information
##Extracted phylum and genus level OTU tables from phyloseq and added additional information
##Aggolomerating at phyla level
phylum_data_total <- tax_glom(psnoncontam_tru_final_rare,taxrank = "Phylum")
phylum_data_total
#Relative abundances
#phylum_data_total.rel <- transform(phylum_data_total, "compositional")
#phylum_data_total.rel
#Using CLR transformation here
#Write the phyloseq object as a CSV table with ASVs and counts
phylum_data_total
write_phyloseq(phylum_data_total)
#Read the CSV file
abund_table <- read.csv("phylum_rare_clr.csv", row.names=1, check.names= FALSE) #Added header "Features" to the first clumn of ASVs
abund_table_t<-t(abund_table)
#View(abund_table_t)
#install.packages("zCompositions")
#library (zCompositions)
abund_table_r <- t(cmultRepl((abund_table_t), method="CZM", output="prop")) #prop calculates the imputed proportion is similar to "counts" and then taking proportions in the previous version; CZM: count zero multiplicative
#Check the before and after files
#View(abund_table)
#View(abund_table_r)
#Apply CLR transformation
abund_clr <- t(apply(abund_table_r, 2, function(x){log(x) - mean (log(x))}))
#View(abund_clr)
#Replacing original counts with CLR transformed values
phylum_data_total_zclr <- phylum_data_total
otu_table(phylum_data_total_zclr) <- otu_table(abund_clr, taxa_are_rows = F)
phylum_data_total_zclr
#replacing ASVs with short string
taxa_names(phylum_data_total_zclr) <- paste0("SV", seq(ntaxa(phylum_data_total_zclr)))
#View(tax_table(phylum_data_total_zclr))
#Extracted the phyloseq file in csv
write_phyloseq(phylum_data_total_zclr, type= 'all')
#Replaced string names with phyla names, transposed and added metadata for time point
##Note: Made some modifications in excel here
#importing the abundance table
phylum_time_data <- read.table("otu_table_phylum_total_data_clr_new.csv", header = TRUE, sep= ",", check.names = F)
phylum_time_data
#class(phylum_time_data)
trans_phylum_time_data <- melt(phylum_time_data)
trans_phylum_time_data
#Changing the column name from 'variable' to 'Phylum'
colnames(trans_phylum_time_data)[3] <- "Phylum"
trans_phylum_time_data
#class(trans_phylum_time_data$variable)
#Rstatix
stat.test.phyla <- trans_phylum_time_data %>%
group_by(Phylum) %>%
wilcox_test(value ~ TimePoint) %>%
adjust_pvalue(method = "fdr") %>%
add_significance()
stat.test.phyla
#Adding XY position
stat.test.phyla <- stat.test.phyla %>% add_xy_position(x = "TimePoint")
stat.test.phyla
#Box plot
bxp <- ggboxplot(data= trans_phylum_time_data, x = "TimePoint", y= "value", facet.by = "Phylum", xlab = "Time Points", ylab= "Abundance (CLR)", fill="Phylum")
bxp + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(legend.position="bottom") + stat_pvalue_manual(stat.test.phyla, label= "p.adj.signif", tip.length=0.03, hide.ns = T) + scale_fill_manual(values=c("#5B7E00", "#C3CFA2", "#3A5200"))
##There are no significant differences after fdr correction, so no brackets seen in the plot below
##Multiple group comparison
kruskal.test(phylum_time_data$Actinobacteria, phylum_time_data$TimePoint)
kruskal.test(phylum_time_data$Firmicutes, phylum_time_data$TimePoint)
kruskal.test(phylum_time_data$Proteobacteria, phylum_time_data$TimePoint)
#Posthoc using wilcoxon sum rank test
pairwise.wilcox.test(phylum_time_data$Actinobacteria, phylum_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(phylum_time_data$Firmicutes, phylum_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(phylum_time_data$Proteobacteria, phylum_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
##Genus level abundance over time
##Extracted phylum and genus level OTU tables from phyloseq and added additional information
psnoncontam_tru_final_rare
##Aggolomerating at genus level
genus_data_total <- tax_glom(psnoncontam_tru_final_rare,taxrank = "Genus")
genus_data_total
#Relative abundances
#genus_data_total.rel <- transform(genus_data_total, "compositional")
#genus_data_total.rel
#Using CLR transformed abundances here
#Write the phyloseq object as a CSV table with ASVs and counts
genus_data_total
write_phyloseq(genus_data_total)
#Read the CSV file
abund_table <- read.csv("genus_rare_clr_new.csv", row.names=1, check.names= FALSE) #Added header "Features" to the first clumn of ASVs
abund_table_t<-t(abund_table)
#View(abund_table_t)
#install.packages("zCompositions")
#library (zCompositions)
abund_table_r <- t(cmultRepl((abund_table_t), method="CZM", output="prop")) #prop calculates the imputed proportion is similar to "counts" and then taking proportions in the previous version; CZM: count zero multiplicative
#Check the before and after files
#View(abund_table)
#View(abund_table_r)
#Apply CLR transformation
abund_clr <- t(apply(abund_table_r, 2, function(x){log(x) - mean (log(x))}))
#View(abund_clr)
#Replacing original counts with CLR transformed values
genus_data_total_zclr <- genus_data_total
otu_table(genus_data_total_zclr) <- otu_table(abund_clr, taxa_are_rows = F)
genus_data_total_zclr
#replacing ASVs with short string
taxa_names(genus_data_total_zclr) <- paste0("SV", seq(ntaxa(genus_data_total_zclr)))
#View(tax_table(genus_data_total_zclr))
#Extracting short strings and their respective taxonomy
genus_list<- tax_table(genus_data_total_zclr)@.Data
write.csv(genus_list, "genus_total_list.csv")
#extracting as csv and replacing string names with phyla names, transposed and added metadata
write_phyloseq(genus_data_total_zclr, type= 'all')
genus_time_data <- read.csv("otu_table_genus_total_data_clr_new.csv", header = TRUE, sep= ",")
genus_time_data
#library(reshape)
#class(genus_time_data)
trans_genus_time_data <- melt.data.frame(genus_time_data)
trans_genus_time_data
##multiple group comparison using Kruskal wallis
kruskal.test(genus_time_data$Staphylococcus, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Enterobacteriaceae..F., genus_time_data$TimePoint)
kruskal.test(genus_time_data$Erwinia, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Clostridium, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Enterococcus, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Bifidobacterium, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Propionibacterium, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Veillonella, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Clostridiales.XI..F., genus_time_data$TimePoint)
kruskal.test(genus_time_data$Peptostreptococcaceae..F., genus_time_data$TimePoint)
kruskal.test(genus_time_data$Pasteurellaceae..F., genus_time_data$TimePoint)
kruskal.test(genus_time_data$Lactobacillus, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Bacillus, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Streptococcus, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Dialister, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Peptoniphilus, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Lachnoclostridium, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Corynebacterium, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Acinetobacter, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Anaerococcus, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Varibaculum, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Comamonas, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Gemella, genus_time_data$TimePoint)
kruskal.test(genus_time_data$Lactobacillales..O., genus_time_data$TimePoint)
#####Pairwise comparison for time points
pairwise.wilcox.test(genus_time_data$Staphylococcus, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Enterobacteriaceae..F., genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Erwinia, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Clostridium, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Enterococcus, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Enterococcus, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Bifidobacterium, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Propionibacterium, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Veillonella, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Clostridiales.XI..F., genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Peptostreptococcaceae..F., genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Pasteurellaceae..F., genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Lactobacillus, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Bacillus, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Streptococcus, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Dialister, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Peptoniphilus, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Lachnoclostridium, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Corynebacterium, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Acinetobacter, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Anaerococcus, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Varibaculum, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Comamonas, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Gemella, genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
pairwise.wilcox.test(genus_time_data$Lactobacillales..O., genus_time_data$TimePoint, p.adjust.method = "fdr", paired = FALSE)
#Replotting box plots with genera
#importing the abundance table
genus_time_data <- read.table("otu_table_genus_total_data_clr_new.csv", header = TRUE, sep= ",", check.names = F)
genus_time_data
#Melting the data frame to use it for boxplot and statistics
trans_genus_time_data <- melt(genus_time_data)
trans_genus_time_data
#Changing the column name from 'variable' to 'Genus'
colnames(trans_genus_time_data)[3] <- "Genus"
trans_genus_time_data
#Rstatix
stat.test.genus <- trans_genus_time_data %>%
group_by(Genus) %>%
wilcox_test(value ~ TimePoint) %>%
adjust_pvalue(method = "fdr") %>%
add_significance()
stat.test.genus
#Adding XY position
stat.test.genus <- stat.test.genus %>% add_xy_position(x = "TimePoint")
stat.test.genus
#Box plot
bxp.genus <- ggboxplot(data= trans_genus_time_data, x = "TimePoint", y= "value", facet.by = "Genus", xlab = "Time Points", ylab="Abundance (CLR)", fill = "Genus", lwd=0.3, fatten=0.3) + scale_y_continuous(expand = expansion(mult = c(0.05, 0.3)))
#scale_fill_manual(values=c("#FDD349", "#80D7E2", "#D8AE92", "#F6CDC4", "#C36200", "#B8EE8A", "#004B8D", "#C89F00", "#537345", "#FF4739", "#CBA96E", "#FFB55C", "#54565A", "#4288A4", "#9E0000", "#E80076", "#C3CB3C", "#6600CC", "#773F05"))
bxp.genus
#modifying the plot to use individual y-scale for each facet
bxp.genus <- facet(p = bxp.genus,facet.by = "Genus",scales = "free_y") + theme(legend.position = "bottom") + scale_fill_manual(values = c("#F7D363", "#20899F", "#94D5E0", "#D2AF96", "#F0CEC6", "#B56602", "#C3EB95", "#044B88", "#935272", "#0DA703", "#653723", "#C29F00", "#5B724B", "#F35733", "#C6AA76", "#FFB86C", "#53565A", "#EAF0F3", "#B00D33", "#D52F75", "#C3CB3C", "#6600CC", "#773F05", "#DDA0DD"))
bxp.genus
#To add significance for pairwise comparisons
#bxp.genus + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(legend.position="none") + stat_pvalue_manual(stat.test.genus, label="p.adj.signif", tip.length=0.02, hide.ns = T)
##################Section 2: Gestational age and changes in microbial diversity######################
#Using AGA as a continous gradient to colour
#For CLR and bray curtis
ps_noncontam_zclr_rare
ps_noncontam_zclr_rare_pseudo <- ps_noncontam_zclr_rare
otu_table(ps_noncontam_zclr_rare_pseudo) <- otu_table(ps_noncontam_zclr_rare_pseudo) + 3
ps_noncontam_clr_plot_rare_bray <- plot_ordination(ps_noncontam_zclr_rare_pseudo, ordinate(ps_noncontam_zclr_rare_pseudo, "MDS", "bray"), color = "GestationalAge") + geom_point(size = 3)
ps_noncontam_clr_plot_rare_bray + theme(panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_color_gradient(low= "#FE3700", high = "#FAE4E3") + theme(legend.position = "right")
#comparing the samples at different time points
metadata5 <- as(sample_data(ps_noncontam_zclr_rare_pseudo), "data.frame")
adonis(phyloseq::distance(ps_noncontam_zclr_rare_pseudo, method = "bray")~GestationalAge, data = metadata5, p.adjust.methods= "fdr", permutations = 99999)###do a pairwise adonis here
#beta-dispersion test
#ps_total_noncontam_dist <- phyloseq::distance(ps_noncontam_zclr_rare_pseudo, method = "bray")
#sampledf5 <- data.frame(sample_data(ps_noncontam_zclr_rare_pseudo))
#beta5 <- betadisper(ps_total_noncontam_dist, sampledf5$GestationalAge, bias.adjust = TRUE)
#permutest(beta5, p.adjust.methods= "fdr", permutations = 99999)
#Jaccard distance on presence/absence of ASVs
ps_noncontam_rare_plot_jaccard <- plot_ordination(psnoncontam_tru_final_rare, ordinate(psnoncontam_tru_final_rare, "MDS", "jaccard"), color = "GestationalAge") + geom_point(size = 3)
ps_noncontam_rare_plot_jaccard + theme(panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_color_gradient(low= "#FE3700", high = "#FAE4E3") + theme(legend.position = "right")
#comparing the samples at different time points
metadata5 <- as(sample_data(psnoncontam_tru_final_rare), "data.frame")
adonis(phyloseq::distance(psnoncontam_tru_final_rare, method = "jaccard")~GestationalAge, data = metadata5, p.adjust.methods= "fdr", permutations = 99999)###do a pairwise adonis here
#beta-dispersion test
#ps_total_noncontam_dist <- phyloseq::distance(psnoncontam_tru_final_rare, method = "jaccard")
#sampledf5 <- data.frame(sample_data(psnoncontam_tru_final_rare))
#beta5 <- betadisper(ps_total_noncontam_dist, sampledf5$GestationalAge, bias.adjust = TRUE)
#permutest(beta5, p.adjust.methods= "fdr", permutations = 99999)
###########################Section 3: RCT groups and microbial composition##########################
####Composition of each group at phylum level
psnoncontam_tru_final_rare
#subset control group samples
ps_control_final <- subset_samples(psnoncontam_tru_final_rare,Group%in%c("Control_group"))
ps_control_final
ps_control_final <- prune_taxa(taxa_sums(ps_control_final)>0, ps_control_final)
ps_control_final
####further separating the samples from control group at different time points
#before fortification samples
ps_control_bf <- subset_samples(ps_control_final,Description%in%c("T1"))
ps_control_bf
ps_control_bf <- prune_taxa(taxa_sums(ps_control_bf)>0, ps_control_bf)
ps_control_bf
#during fortification samples (after 7 days)
ps_control_df <- subset_samples(ps_control_final,Description%in%c("T2"))
ps_control_df
ps_control_df <- prune_taxa(taxa_sums(ps_control_df)>0, ps_control_df)
ps_control_df
#at the end of fortification samples (week 33)
ps_control_ef <- subset_samples(ps_control_final,Description%in%c("T3"))
ps_control_ef
ps_control_ef <- prune_taxa(taxa_sums(ps_control_ef)>0, ps_control_ef)
ps_control_ef
#follow up samples (week 35)
ps_control_ff <- subset_samples(ps_control_final,Description%in%c("T4"))
ps_control_ff
ps_control_ff <- prune_taxa(taxa_sums(ps_control_ff)>0, ps_control_ff)
ps_control_ff
####plotting composition of control samples at each time point
ps_control_bf.rel <- microbiome::transform(ps_control_bf, "compositional")
plot_t1_c_phylum <- plot_bar(ps_control_bf.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4)
plot_t1_c <- plot_t1_c_phylum + labs(x="", y= "Relative abundance")
plot_t1_c
ps_control_df.rel <- microbiome::transform(ps_control_df, "compositional")
plot_t2_c_phylum <- plot_bar(ps_control_df.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4)
plot_t2_c <- plot_t2_c_phylum + labs(x="", y= "Relative abundance")
plot_t2_c
ps_control_ef.rel <- microbiome::transform(ps_control_ef, "compositional")
plot_t3_c_phylum <- plot_bar(ps_control_ef.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4)
plot_t3_c <- plot_t3_c_phylum + labs(x="", y= "Relative abundance")
plot_t3_c
ps_control_ff.rel <- microbiome::transform(ps_control_ff, "compositional")
plot_t4_c_phylum <- plot_bar(ps_control_ff.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4)
plot_t4_c <- plot_t4_c_phylum + labs(x="", y= "Relative abundance")
plot_t4_c
####subset treatment group samples and separating the samples from treatment group at different time points
#subset treatment group samples
ps_treatment_final <- subset_samples(psnoncontam_tru_final_rare,Group%in%c("Treatment_group"))
ps_treatment_final
ps_treatment_final <- prune_taxa(taxa_sums(ps_treatment_final)>0, ps_treatment_final)
ps_treatment_final
#before fortification samples
ps_treatment_bf <- subset_samples(ps_treatment_final,Description%in%c("T1"))
ps_treatment_bf
ps_treatment_bf <- prune_taxa(taxa_sums(ps_treatment_bf)>0, ps_treatment_bf)
ps_treatment_bf
#during fortification samples (after 7 days)
ps_treatment_df <- subset_samples(ps_treatment_final,Description%in%c("T2"))
ps_treatment_df
ps_treatment_df <- prune_taxa(taxa_sums(ps_treatment_df)>0, ps_treatment_df)
ps_treatment_df
#at the end of fortification samples (week 33)
ps_treatment_ef <- subset_samples(ps_treatment_final,Description%in%c("T3"))
ps_treatment_ef
ps_treatment_ef <- prune_taxa(taxa_sums(ps_treatment_ef)>0, ps_treatment_ef)
ps_treatment_ef
#follow up samples (week 35)
ps_treatment_ff <- subset_samples(ps_treatment_final,Description%in%c("T4"))
ps_treatment_ff
ps_treatment_ff <- prune_taxa(taxa_sums(ps_treatment_ff)>0, ps_treatment_ff)
ps_treatment_ff
####plotting composition of treatment samples at each time point
ps_treatment_bf.rel <- microbiome::transform(ps_treatment_bf, "compositional")
plot_t1_t_phylum <- plot_bar(ps_treatment_bf.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4)
plot_t1_t <- plot_t1_t_phylum + labs(x="", y= "Relative abundance")
plot_t1_t
ps_treatment_df.rel <- microbiome::transform(ps_treatment_df, "compositional")
plot_t2_t_phylum <- plot_bar(ps_treatment_df.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4)
plot_t2_t <- plot_t2_t_phylum + labs(x="", y= "Relative abundance")
plot_t2_t
ps_treatment_ef.rel <- microbiome::transform(ps_treatment_ef, "compositional")
plot_t3_t_phylum <- plot_bar(ps_treatment_ef.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4)
plot_t3_t <- plot_t3_t_phylum + labs(x="", y= "Relative abundance")
plot_t3_t
ps_treatment_ff.rel <- microbiome::transform(ps_treatment_ff, "compositional")
plot_t4_t_phylum <- plot_bar(ps_treatment_ff.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4)
plot_t4_t <- plot_t4_t_phylum + labs(x="", y= "Relative abundance")
plot_t4_t
####Trying out the combined mean abundance bar chart RCT group wise at phylum level
psnoncontam_tru_final_rare
#Seperating out samples based on time points
ps_bf_total <- subset_samples(psnoncontam_tru_final_rare,Description%in%c("T1"))
ps_bf_total <- prune_taxa(taxa_sums(ps_bf_total)>0, ps_bf_total)
ps_bf_total
ps_df_total <- subset_samples(psnoncontam_tru_final_rare,Description%in%c("T2"))
ps_df_total <- prune_taxa(taxa_sums(ps_df_total)>0, ps_df_total)
ps_df_total
ps_ef_total <- subset_samples(psnoncontam_tru_final_rare,Description%in%c("T3"))
ps_ef_total <- prune_taxa(taxa_sums(ps_ef_total)>0, ps_ef_total)
ps_ef_total
ps_ff_total <- subset_samples(psnoncontam_tru_final_rare,Description%in%c("T4"))
ps_ff_total <- prune_taxa(taxa_sums(ps_ff_total)>0, ps_ff_total)
ps_ff_total
#for first time point at phyla level
ps_bf_total
ps_bf_total_merge <- merge_samples(ps_bf_total, "Group")
ps_bf_total_merge
ps_bf_total_merge.rel <- microbiome::transform(ps_bf_total_merge, "compositional")
plot_t1_phylum <- plot_bar(ps_bf_total_merge.rel, fill = "Phylum") + geom_bar(stat="identity", geom_params = list(width=.5)) + theme(panel.background = element_blank()) + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(legend.position = "right") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 0, hjust= 0.5), aspect.ratio = 4/4)
plot_t1<- plot_t1_phylum + labs(x="", y= "Relative abundance")
plot_t1
#for second time point at phyla level
ps_df_total
ps_df_total_merge <- merge_samples(ps_df_total, "Group")
ps_df_total_merge
ps_df_total_merge.rel <- microbiome::transform(ps_df_total_merge, "compositional")
plot_t2_phylum <- plot_bar(ps_df_total_merge.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + theme(panel.background = element_blank()) + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(legend.position = "right") +theme(legend.position = "right") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 0, hjust= 0.5), aspect.ratio = 4/4)
plot_t2<- plot_t2_phylum + labs(x="", y= "Relative abundance")
plot_t2
#for third time point at phyla level
ps_ef_total
ps_ef_total_merge <- merge_samples(ps_ef_total, "Group")
ps_ef_total_merge
ps_ef_total_merge.rel <- microbiome::transform(ps_ef_total_merge, "compositional")
plot_t3_phylum<- plot_bar(ps_ef_total_merge.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + theme(panel.background = element_blank()) + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(legend.position = "right") + theme(legend.position = "right") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 0, hjust= 0.5), aspect.ratio = 4/4)
plot_t3<- plot_t3_phylum + labs(x="", y= "Relative abundance")
plot_t3
#for fourth time point at phyla level
ps_ff_total
ps_ff_total_merge <- merge_samples(ps_ff_total, "Group")
ps_ff_total_merge
ps_ff_total_merge.rel <- microbiome::transform(ps_ff_total_merge, "compositional")
plot_t4_phylum <- plot_bar(ps_ff_total_merge.rel, fill = "Phylum") + geom_bar(aes( fill=Phylum), stat="identity", position = "stack") + theme(panel.background = element_blank()) + scale_fill_manual(values=c("#40530A","#637C33", "#C4CEA7")) + theme(legend.position = "right") + theme(legend.position = "right") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 0, hjust= 0.5), aspect.ratio = 4/4)
plot_t4<- plot_t4_phylum + labs(x="", y= "Relative abundance")
plot_t4
###Combining all the phylum level plots for RCT groups
ggarrange(plotlist= list(plot_t1_c, plot_t1_t, plot_t1, plot_t2_c, plot_t2_t, plot_t2, plot_t3_c, plot_t3_t, plot_t3, plot_t4_c, plot_t4_t, plot_t4), ncol=3, nrow = 4, legend = "none")
###########Genus composition at sample level and group level (group mean) for each group (either RCT or MOM groups) at discrete time points. For this analysis it is important that the agglomeration at genus level followed by relative abundance transformation takes place independently. So I have plotted them separately and tried to maintain the order of genus in the per sample plot similar to the group mean plot. This was done comparing the list of genera in individual sample plots with the group mean plots. The order was then set accordinly by sorting the list and adding 'other genus' at the end (details below in the code)
##For Control and treatment samples at 4 time points, group mean and indvidual sample bar plots
####As first step setting up a color palette for all the genera
###I have created a color palette in excel with name of genera and hex codes and imported them as a data frame, so that my plots can pick up the same color for each respective genus every time
##There are other options available for working on color palettes such as the ggsci package, however the number of colors are limited and picking up distinct colours was important to avoid confusion.
##Setting up color palette
color_palette <- read.csv("/Users/shreyas/Documents/Prolacta study docs/sequence data/combined_analysis/color_palette.csv", sep=",", header = T)
color_palette$Genus <- as.character(color_palette$Genus)
color_palette$HEX <- as.character(color_palette$HEX)
color_genus <- setNames(color_palette$HEX, color_palette$Genus)
###As a next step seperating out samples based on time points
ps_bf_total <- subset_samples(psnoncontam_tru_final_rare,Description%in%c("T1"))
ps_bf_total <- prune_taxa(taxa_sums(ps_bf_total)>0, ps_bf_total)
ps_bf_total
ps_df_total <- subset_samples(psnoncontam_tru_final_rare,Description%in%c("T2"))
ps_df_total <- prune_taxa(taxa_sums(ps_df_total)>0, ps_df_total)
ps_df_total
ps_ef_total <- subset_samples(psnoncontam_tru_final_rare,Description%in%c("T3"))
ps_ef_total <- prune_taxa(taxa_sums(ps_ef_total)>0, ps_ef_total)
ps_ef_total
ps_ff_total <- subset_samples(psnoncontam_tru_final_rare,Description%in%c("T4"))
ps_ff_total <- prune_taxa(taxa_sums(ps_ff_total)>0, ps_ff_total)
ps_ff_total
#for first time point split by RCT groups
ps_bf_total
ps_bf_rct_merge <- merge_samples(ps_bf_total, "Group")
ps_bf_rct_merge
#for second time point split by RCT groups
ps_df_total
ps_df_rct_merge <- merge_samples(ps_df_total, "Group")
ps_df_rct_merge
#for third time point split by RCT groups
ps_ef_total
ps_ef_rct_merge <- merge_samples(ps_ef_total, "Group")
ps_ef_rct_merge
#for fourth time point split by RCT groups
ps_ff_total
ps_ff_rct_merge <- merge_samples(ps_ff_total, "Group")
ps_ff_rct_merge
#####Group mean T1, Combined plot###
psbf.rct.gglom <- tax_glom(physeq = ps_bf_rct_merge,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
psbf.rct.gglom.prop <- transform_sample_counts(psbf.rct.gglom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(psbf.rct.gglom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, psbf.rct.gglom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose
temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_bf_rct_merge)))
# combine plot
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp.levels,'Other Genus'))
plot_t1_rct_g_group <- ggplot(data = ps.final.melt,mapping = aes(x = Sample, y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + theme(panel.background = element_blank()) + scale_fill_manual(values = color_genus) + theme(legend.position = "right") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 0, hjust= 0.5), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t1_rct_g_group
###################
####per sample bar plot control
ps.glom <- tax_glom(physeq = ps_control_bf,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
ps.glom.prop <- transform_sample_counts(ps.glom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(ps.glom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, ps.glom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose
#temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_control_bf)))
# per sample genus bar plot for control at T1
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus = as.character(ps.final.melt$Genus)
###In order to pick up the same order for genera in individual plots I am picking up the levels from the group mean plot and filter/add the genera that are missing after comparing the list of genera in individual plot with the group mean plot above
rct_genus_t1_levels <- temp.levels[!(temp.levels %in% "Other Genus")]
target_genus = as.character(unique(ps.final.melt$Genus)[!(unique(ps.final.melt$Genus) %in% "Other Genus")])
temp = c(rct_genus_t1_levels[rct_genus_t1_levels %in% target_genus], target_genus[!(target_genus %in% rct_genus_t1_levels)])
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp, "Other Genus"))
levels(ps.final.melt$Genus)
plot_t1_c_genus <- ggplot(data = ps.final.melt,mapping = aes(x = SampleName,y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + scale_fill_manual(values = color_genus[c(temp, "Other Genus")]) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t1_c_genus
# per sample genus bar plot for treatment at T1
ps.glom <- tax_glom(physeq = ps_treatment_bf,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
ps.glom.prop <- transform_sample_counts(ps.glom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(ps.glom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, ps.glom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose. I used it previously to order the taxa according to their abundance.
#temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_treatment_bf)))
# plot for treatment at T1
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus = as.character(ps.final.melt$Genus)
###Again as done for the control plot, in order to pick up the same order for genera in individual plots I am picking up the levels from the group mean plot and filter/add the genera that are missing after comparing the list of genera in individual plot with the group mean plot above
rct_genus_t1_levels <- temp.levels[!(temp.levels %in% "Other Genus")]
target_genus = as.character(unique(ps.final.melt$Genus)[!(unique(ps.final.melt$Genus) %in% "Other Genus")])
temp = c(rct_genus_t1_levels[rct_genus_t1_levels %in% target_genus], target_genus[!(target_genus %in% rct_genus_t1_levels)])
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp, "Other Genus"))
levels(ps.final.melt$Genus)
plot_t1_t_genus <- ggplot(data = ps.final.melt,mapping = aes(x = SampleName,y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + scale_fill_manual(values = color_genus[c(temp, "Other Genus")]) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t1_t_genus
###For time point T2
#####Group mean T2, Combined plot###
psdf.rct.gglom <- tax_glom(physeq = ps_df_rct_merge,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
psdf.rct.gglom.prop <- transform_sample_counts(psdf.rct.gglom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(psdf.rct.gglom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, psdf.rct.gglom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose
temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_df_rct_merge)))
# combine plot
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp.levels,'Other Genus'))
plot_t2_rct_g_group <- ggplot(data = ps.final.melt,mapping = aes(x = Sample, y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + theme(panel.background = element_blank()) + scale_fill_manual(values = color_genus) + theme(legend.position = "right") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 0, hjust= 0.5), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t2_rct_g_group
###################
####per sample bar plot control
ps.glom <- tax_glom(physeq = ps_control_df,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
ps.glom.prop <- transform_sample_counts(ps.glom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(ps.glom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, ps.glom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose
#temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_control_df)))
# per sample genus bar plot for control at T2
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus = as.character(ps.final.melt$Genus)
###In order to pick up the same order for genera in individual plots I am picking up the levels from the group mean plot and filter/add the genera that are missing after comparing the list of genera in individual plot with the group mean plot above
rct_genus_t2_levels <- temp.levels[!(temp.levels %in% "Other Genus")]
target_genus = as.character(unique(ps.final.melt$Genus)[!(unique(ps.final.melt$Genus) %in% "Other Genus")])
temp = c(rct_genus_t2_levels[rct_genus_t2_levels %in% target_genus], target_genus[!(target_genus %in% rct_genus_t2_levels)])
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp, "Other Genus"))
levels(ps.final.melt$Genus)
plot_t2_c_genus <- ggplot(data = ps.final.melt,mapping = aes(x = SampleName,y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + scale_fill_manual(values = color_genus[c(temp, "Other Genus")]) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t2_c_genus
# per sample genus bar plot for treatment at T2
ps.glom <- tax_glom(physeq = ps_treatment_df,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
ps.glom.prop <- transform_sample_counts(ps.glom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(ps.glom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, ps.glom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose. I used it previously to order the taxa according to their abundance.
#temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_treatment_df)))
# plot for treatment at T2
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus = as.character(ps.final.melt$Genus)
###Again as done for the control plot, in order to pick up the same order for genera in individual plots I am picking up the levels from the group mean plot and filter/add the genera that are missing after comparing the list of genera in individual plot with the group mean plot above
rct_genus_t2_levels <- temp.levels[!(temp.levels %in% "Other Genus")]
target_genus = as.character(unique(ps.final.melt$Genus)[!(unique(ps.final.melt$Genus) %in% "Other Genus")])
temp = c(rct_genus_t2_levels[rct_genus_t2_levels %in% target_genus], target_genus[!(target_genus %in% rct_genus_t2_levels)])
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp, "Other Genus"))
levels(ps.final.melt$Genus)
plot_t2_t_genus <- ggplot(data = ps.final.melt,mapping = aes(x = SampleName,y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + scale_fill_manual(values = color_genus[c(temp, "Other Genus")]) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t2_t_genus
##For T3 by RCT groups
#####Group mean T3, Combined plot###
psef.rct.gglom <- tax_glom(physeq = ps_ef_rct_merge,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
psef.rct.gglom.prop <- transform_sample_counts(psef.rct.gglom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(psef.rct.gglom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, psef.rct.gglom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose
temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_ef_rct_merge)))
# combine plot
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp.levels,'Other Genus'))
plot_t3_rct_g_group <- ggplot(data = ps.final.melt,mapping = aes(x = Sample, y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + theme(panel.background = element_blank()) + scale_fill_manual(values = color_genus) + theme(legend.position = "right") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 0, hjust= 0.5), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t3_rct_g_group
###################
####per sample bar plot control
ps.glom <- tax_glom(physeq = ps_control_ef,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
ps.glom.prop <- transform_sample_counts(ps.glom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(ps.glom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, ps.glom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose if to be plotted separately, this oredering is according to the abundance
#temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_control_ef)))
# per sample genus bar plot for control at T3
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus = as.character(ps.final.melt$Genus)
###In order to pick up the same order for genera in individual plots I am picking up the levels from the group mean plot and filter/add the genera that are missing after comparing the list of genera in individual plot with the group mean plot above
rct_genus_t3_levels <- temp.levels[!(temp.levels %in% "Other Genus")]
target_genus = as.character(unique(ps.final.melt$Genus)[!(unique(ps.final.melt$Genus) %in% "Other Genus")])
temp = c(rct_genus_t3_levels[rct_genus_t3_levels %in% target_genus], target_genus[!(target_genus %in% rct_genus_t3_levels)])
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp, "Other Genus"))
levels(ps.final.melt$Genus)
plot_t3_c_genus <- ggplot(data = ps.final.melt,mapping = aes(x = SampleName,y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + scale_fill_manual(values = color_genus[c(temp, "Other Genus")]) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t3_c_genus
# per sample genus bar plot for treatment at T3
ps.glom <- tax_glom(physeq = ps_treatment_ef,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
ps.glom.prop <- transform_sample_counts(ps.glom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(ps.glom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, ps.glom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose. I used it previously to order the taxa according to their abundance.
#temp.levels <- as.character(tax.tab[order(rowSums(otu.tab),decreasing = T),][,'Genus'])
#insert others
otu.tab['Other',] <- as.numeric(1 - colSums(otu.tab))
tax.tab <- rbind(tax.tab,paste0('Other ',rank_names(ps.top10)))
rownames(tax.tab) <- rownames(otu.tab)
#build phyloseq
ps.final <- phyloseq(otu_table(otu.tab,taxa_are_rows = T),tax_table(tax.tab),sample_data(sample_data(ps_treatment_ef)))
# plot for treatment at T3
ps.final.melt <- psmelt(ps.final)
ps.final.melt$Genus = as.character(ps.final.melt$Genus)
###Again as done for the control plot, in order to pick up the same order for genera in individual plots I am picking up the levels from the group mean plot and filter/add the genera that are missing after comparing the list of genera in individual plot with the group mean plot above
rct_genus_t3_levels <- temp.levels[!(temp.levels %in% "Other Genus")]
target_genus = as.character(unique(ps.final.melt$Genus)[!(unique(ps.final.melt$Genus) %in% "Other Genus")])
temp = c(rct_genus_t3_levels[rct_genus_t3_levels %in% target_genus], target_genus[!(target_genus %in% rct_genus_t3_levels)])
ps.final.melt$Genus <- factor(ps.final.melt$Genus,levels = c(temp, "Other Genus"))
levels(ps.final.melt$Genus)
plot_t3_t_genus <- ggplot(data = ps.final.melt,mapping = aes(x = SampleName,y = Abundance,fill=Genus)) + geom_bar(stat = 'identity') + scale_fill_manual(values = color_genus[c(temp, "Other Genus")]) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x = element_text(angle = 90), aspect.ratio = 4/4) + labs(x="", y= "Relative abundance")
plot_t3_t_genus
##For T4 by RCT
#####Group mean T4, Combined plot###
psff.rct.gglom <- tax_glom(physeq = ps_ff_rct_merge,taxrank = 'Genus')
#convert to relative abundance and select top 10 taxa
psff.rct.gglom.prop <- transform_sample_counts(psff.rct.gglom, function(otu) otu/sum(otu))
top10 <- names(sort(taxa_sums(psff.rct.gglom.prop), decreasing=TRUE))[1:10]
ps.top10 <- prune_taxa(top10, psff.rct.gglom.prop)
otu.tab <- as.data.frame(t(otu_table(ps.top10)))
tax.tab <- as.matrix(tax_table(ps.top10))
#for ordering purpose