-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMARS-Keen_phyloseq.Rmd
2987 lines (2398 loc) · 121 KB
/
MARS-Keen_phyloseq.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
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
---
title: "MARS-Keen_phyloseq"
author: "Shawn Higdon"
date: "6/24/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Libraries
```{r Libraries, message=FALSE}
library(rhdf5)
library(ggplot2)
library(ggrepel)
library(tidyverse)
library(phyloseq)
library(biomformat)
library(ComplexHeatmap)
library(RColorBrewer)
library(viridis)
library(circlize)
library(DESeq2)
library(reshape2)
theme_set(theme_bw())
```
## Data Import
### Kraken-Biom
### Sample Map
Kraken Reports were used as input for Bayesian Re-estimation of Taxa Abundance using [**Bracken v2**](https://github.com/jenniferlu717/Bracken). Bracken Report files were converted to biom hdf5 format using the python package [**kraken-biom**](https://github.com/smdabdoub/kraken-biom).
> Use `biomformat` to read in bracken-biom file in hdf5 format
```{r}
# read in hdf5 biom table from kraken-biom output of bracken reports
bracken_biom_file <- read_hdf5_biom("./MARS-Keen_bracken_biom_table-seqids.hdf5")
# create biom object
bracken_biom <- biom(bracken_biom_file)
bracken_physeq <- import_biom(bracken_biom, parseFunction = parse_taxonomy_greengenes)
# summary of bracken biom
bracken_physeq
head(tax_table(bracken_physeq))
# Read in the sample map file (list has strictly static order, numeric ascending)
sample_map <- read.csv("./../meta_files/MARS-Keen_metadata.csv", header = T, row.names = 1)
# create phyloseq sample map from dataframe
bracken_sam <- sample_data(sample_map, errorIfNULL = TRUE)
bracken_physeq <- merge_phyloseq(bracken_physeq, bracken_sam)
# add tax rank Species2
tax_table(bracken_physeq) <- cbind(tax_table(bracken_physeq), OTU=taxa_names(bracken_physeq))
## Define Ranks to include
label_ranks <- c("Genus", "Species", "OTU")
labels <- apply(tax_table(bracken_physeq)[, label_ranks], 1, paste, sep="", collapse=" ")
## add concatenated labels as a new rank after Species2
tax_table(bracken_physeq) <- cbind(tax_table(bracken_physeq), Species2=labels)
head(tax_table(bracken_physeq))
ntaxa(bracken_physeq)
nsamples(bracken_physeq)
sample_names(bracken_physeq)
rank_names(bracken_physeq)
sample_variables(bracken_physeq)
```
### Processed Data Analysis
#### Normalization
> The data must be normalized to account for variation among samples in sequencing depth. This is achieved by converting abundances to counts per million.
##### Counts per Million (CPM)
> Using the Phyloseq suggested method of normalization
```{r Preprocessing bp transform}
# transform to even sampling depth with Phyloseq CPM method
bp_norm <- transform_sample_counts(bracken_physeq, function(x) 1E6 * x/sum(x))
bp_norm_beta <- prune_taxa(taxa_sums(bp_norm) > 100, bp_norm)
bp_norm_beta
```
##### BCW Normalization method
> Calculating relative abundance, followed by division by total reads to account for difference in read population size (sampling depth) and multiplying by a scaling factor
```{r}
# normalize the count data
bp_norm_bcw <- transform_sample_counts(bracken_physeq, function(x) (x/sum(x))/sum(x) * 1E6)
# create matrices for otu table and taxonomy table
bp_norm_bcw_otu_table <- as(otu_table(bp_norm_bcw), "matrix")
bp_norm_bcw_taxa_table <- as(tax_table(bp_norm_bcw), "matrix")
# convert matrices to data.frame
bp_norm_bcw_otu_table_df <- as.data.frame(bp_norm_bcw_otu_table)
bp_norm_bcw_taxa_table_df <- as.data.frame(bp_norm_bcw_taxa_table)
# inspect elements
#head(bp_norm_bcw_otu_table_df)
#head(bp_norm_bcw_taxa_table_df)
# mutate otu table to add Taxonomy ID variable
bp_norm_bcw_otu_table_df <- mutate(bp_norm_bcw_otu_table_df, OTU= rownames(bp_norm_bcw_otu_table_df))
# Merge OTU and Taxonomy dataframes by OTU variable
bp_bcw_norm_master_df <- inner_join(bp_norm_bcw_otu_table_df,
bp_norm_bcw_taxa_table_df,
by = "OTU")
# Inspect non sample variable names
colnames(bp_bcw_norm_master_df[153:161])
# clone for rownames as taxa names
bp_bcw_norm_master_df2 <- bp_bcw_norm_master_df
# set rownames to full taxonomic id (genus + species + OTU tax_id)
rownames(bp_bcw_norm_master_df2) <- bp_bcw_norm_master_df$Species2
# subset dataframe to grab only sample abundance data
bp_bcw_norm_abund_df <- bp_bcw_norm_master_df2[,1:152]
# save normalized OTU abundance matrix
write.csv(bp_bcw_norm_abund_df, "./R_output_files/bracken_bcw-normalized-relabund_tax-names.csv")
```
### Diff. Time Abund Matrix
> Using relative abundance values per taxon normalized for seq. depth:
* Subset data by Timepoint
* Compute differences in abundance
* Arrange in Matrix of differential abundance values by sampling timepoint(rows as taxa, columns as sample)
```{r}
# Convert master df to narrow format, Keep Taxon Name
bp_bcw_norm_abund_narrow_df <- bp_bcw_norm_master_df %>% gather(key = "Sample", value = "norm_rel_abund", -Species2)
# Inspect Element
head(bp_bcw_norm_abund_narrow_df)
# add time variable to bp_bcw_norm_master_df
bp_bcw_norm_abund_narrow_df$Timepoint <- sample_map$Timepoint[match(bp_bcw_norm_abund_narrow_df$Sample, rownames(sample_map))]
# add Patient_ID Variable
bp_bcw_norm_abund_narrow_df$Patient_ID <- sample_map$Sample_ID[match(bp_bcw_norm_abund_narrow_df$Sample, rownames(sample_map))]
# add Sample_Type Variable
bp_bcw_norm_abund_narrow_df$Sample_Type <- sample_map$Sample_Type[match(bp_bcw_norm_abund_narrow_df$Sample, rownames(sample_map))]
# subset by Sample_Type to create two dataframes
## Saliva
bp_bcw_norm_abund_narrow_saliva_df <- filter(bp_bcw_norm_abund_narrow_df, Sample_Type == "Saliva")
## Fecal
bp_bcw_norm_abund_narrow_fecal_df <- filter(bp_bcw_norm_abund_narrow_df, Sample_Type == "Fecal")
# Check that the levels of patient ID are equal between sample types
unique(bp_bcw_norm_abund_narrow_saliva_df$Patient_ID)
nlevels(bp_bcw_norm_abund_narrow_fecal_df$Patient_ID)
# Subset based on Timepoint
## Saliva
### Time 1 (t1)
bp_bcw_norm_abund_sal_t1 <- bp_bcw_norm_abund_narrow_saliva_df %>% filter(Timepoint == "Initial") %>% select("Species2", "norm_rel_abund", "Patient_ID")
### Time 2 (t2)
bp_bcw_norm_abund_sal_t2 <- bp_bcw_norm_abund_narrow_saliva_df %>% filter(Timepoint == "Final")
colnames(bp_bcw_norm_abund_sal_t1)
head(bp_bcw_norm_abund_sal_t1)
### Spread
bp_bcw_norm_abund_sal_t1_wide <- dcast(bp_bcw_norm_abund_sal_t1, Species2 ~ Patient_ID, value.var="norm_rel_abund")
#?dcast()
#head(bp_bcw_norm_abund_sal_t1_wide)
## Fecal
unique(bp_bcw_norm_abund_narrow_fecal_df$Patient_ID)
unique(bp_bcw_norm_abund_narrow_saliva_df$Patient_ID)
# Compute difference between matrices
head(bp_bcw_norm_abund_narrow_saliva_df)
```
## Phyloseq Plots
### Alpha Diversity: Sample Richness
```{r ggplot2 color theming}
pal = "Dark2"
scale_colour_discrete <- function(palname=pal, ...){
scale_colour_brewer(palette=palname, ...)
}
scale_fill_discrete <- function(palname=pal, ...){
scale_fill_brewer(palette=palname, ...)
}
```
#### Prune Taxa
> Remove taxa for alpha diversity plots that are not in any samples
```{r}
bp_alpha_pruned <- prune_taxa(taxa_sums(bracken_physeq) > 0, bracken_physeq)
```
#### Subset by Sample Type
```{r}
bp_alpha_saliva <- subset_samples(bp_alpha_pruned, Sample_Type == "Saliva")
bp_alpha_fecal <- subset_samples(bp_alpha_pruned, Sample_Type == "Fecal")
```
#### Subset by Time
```{r}
bp_alpha_saliva_initial <- subset_samples(bp_alpha_saliva, Timepoint == "Initial")
bp_alpha_saliva_final <- subset_samples(bp_alpha_saliva, Timepoint == "Final")
bp_alpha_fecal_initial <- subset_samples(bp_alpha_fecal, Timepoint == "Initial")
bp_alpha_fecal_final <- subset_samples(bp_alpha_fecal, Timepoint == "Final")
```
#### Plot Richness
### Overall by Sample Type
```{r}
# Shannon Diversity Index by sample_type
bp_alpha_plot_type <- plot_richness(bracken_physeq, x = "Sample_Type", color = "Sample_Type", measures = c("Observed", "Chao1", "Shannon"), title = "Microbiome Sample Alpha Diversity: Sample Type")
bp_alpha_plot_type <- bp_alpha_plot_type + labs(color="Sample_Type") +
xlab("Sample Type") +
labs(color = "Sample Type") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
bp_alpha_plot_type
```
##### Box Plot: Sample Type
```{r}
bp_alpha_boxplot_type <- plot_richness(bracken_physeq, x = "Sample_Type", color = "Sample_Type", measures = c("Observed", "Chao1", "Shannon"), title = "Microbiome Alpha Diversity: All Samples by Type")
bp_alpha_boxplot_type <- bp_alpha_boxplot_type + geom_boxplot() +
labs(color="Sample Type") +
xlab("Sample Type") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
bp_alpha_boxplot_type
```
##### Merged Plot: By Sample Type
> All samples were merged based on Sample Type of either Fecal or Saliva
```{r}
# merge by sample type
bp_merged_sample_type <- merge_samples(bracken_physeq, "Sample_Type")
# repair variable that was damaged during merge (coerced to numeric)
sample_data(bp_merged_sample_type)$Sample_Type <- factor(sample_names(bp_merged_sample_type))
# plot merged version of data
bp_merged_alpha_plot <- plot_richness(bp_merged_sample_type, x = "Sample_Type",
color = "Sample_Type", measures = c("Observed", "Chao1", "Shannon"), title = "Microbiome Alpha Diversity: Merged by Sample Type")
bp_merged_alpha_plot <- bp_merged_alpha_plot + geom_point(size=5, alpha=0.7) +
labs(color="Sample Type") +
xlab("Sample Type") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
bp_merged_alpha_plot
```
#### Saliva
##### Individual Sample Alpha Index
```{r}
# Alpha Diversity Index
alpha_bp_saliva_all_plot <- plot_richness(bp_alpha_saliva, x = "Product", color = "Product", measures = c("Observed", "Chao1", "Shannon"), title = "Saliva Microbiome Sample Alpha Diversity")
alpha_bp_saliva_all_plot <- alpha_bp_saliva_all_plot + labs(color="Product") +
xlab("Product") +
ylab("Diversity Index") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
alpha_bp_saliva_all_plot
# save plot
ggsave("./alpha_plots/saliva_bp_alpha_all.pdf", alpha_bp_saliva_all_plot, width = 22, height = 12)
```
#### Merged Plot: By Product Type
> All samples were merged based on Product Type: A, B, C, D
```{r}
# merge by sample type
alpha_bp_saliva_merged_product_type <- merge_samples(bp_alpha_saliva, "Product")
# repair variable that was damaged during merge (coerced to numeric)
sample_data(alpha_bp_saliva_merged_product_type)$Product <- factor(sample_names(alpha_bp_saliva_merged_product_type))
# plot merged version of data
alpha_bp_saliva_merged_product_plot <- plot_richness(alpha_bp_saliva_merged_product_type, x = "Product",
color = "Product", measures = c("Observed", "Chao1", "Shannon"), title = "Saliva Microbiome Alpha Diversity: Merged by Product Treatment")
alpha_bp_saliva_merged_product_plot <- alpha_bp_saliva_merged_product_plot + geom_point(size=5, alpha=0.7) +
labs(color="Product") +
xlab("Product") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
alpha_bp_saliva_merged_product_plot
# save plot
ggsave("./alpha_plots/saliva_bp_alpha_merged_product.pdf", alpha_bp_saliva_merged_product_plot, width = 22, height = 12)
```
#### Time 1 (Initial)
```{r}
# merge by sample type
alpha_bp_saliva_merged_product_initial <- merge_samples(bp_alpha_saliva_initial, "Product")
# repair variable that was damaged during merge (coerced to numeric)
sample_data(alpha_bp_saliva_merged_product_initial)$Product <- factor(sample_names(alpha_bp_saliva_merged_product_initial))
# plot merged version of data
alpha_bp_saliva_merged_initial_plot <- plot_richness(alpha_bp_saliva_merged_product_initial, x = "Product",
color = "Product", measures = c("Observed", "Chao1", "Shannon"), title = "Saliva Microbiome Alpha Diversity: Merged by Product; Initial Time")
alpha_bp_saliva_merged_initial_plot <- alpha_bp_saliva_merged_initial_plot + geom_point(size=5, alpha=0.7) +
labs(color="Product") +
xlab("Product") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
alpha_bp_saliva_merged_initial_plot
# save plot
ggsave("./alpha_plots/saliva_bp_alpha_merged_initial.pdf", alpha_bp_saliva_merged_initial_plot, width = 22, height = 12)
```
#### Time 2 (Final)
```{r}
# merge by sample type
alpha_bp_saliva_merged_product_final <- merge_samples(bp_alpha_saliva_final, "Product")
# repair variable that was damaged during merge (coerced to numeric)
sample_data(alpha_bp_saliva_merged_product_final)$Product <- factor(sample_names(alpha_bp_saliva_merged_product_final))
# plot merged version of data
alpha_bp_saliva_merged_final_plot <- plot_richness(alpha_bp_saliva_merged_product_final, x = "Product",
color = "Product", measures = c("Observed", "Chao1", "Shannon"), title = "Saliva Microbiome Alpha Diversity: Merged by Product; Final Time")
alpha_bp_saliva_merged_final_plot <- alpha_bp_saliva_merged_final_plot + geom_point(size=5, alpha=0.7) +
labs(color="Product") +
xlab("Product") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
alpha_bp_saliva_merged_final_plot
# save plot
ggsave("./alpha_plots/saliva_bp_alpha_merged_final.pdf", alpha_bp_saliva_merged_final_plot, width = 22, height = 12)
```
#### Fecal
#### Time 1 (Initial)
```{r}
# merge by sample type
alpha_bp_fecal_merged_product_initial <- merge_samples(bp_alpha_fecal_initial, "Product")
# repair variable that was damaged during merge (coerced to numeric)
sample_data(alpha_bp_fecal_merged_product_initial)$Product <- factor(sample_names(alpha_bp_fecal_merged_product_initial))
# plot merged version of data
alpha_bp_fecal_merged_initial_plot <- plot_richness(alpha_bp_fecal_merged_product_initial, x = "Product",
color = "Product", measures = c("Observed", "Chao1", "Shannon"), title = "Fecal Microbiome Alpha Diversity: Merged by Product; Initial Time")
alpha_bp_fecal_merged_initial_plot <- alpha_bp_fecal_merged_initial_plot + geom_point(size=5, alpha=0.7) +
labs(color="Product") +
xlab("Product") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
alpha_bp_fecal_merged_initial_plot
# save plot
ggsave("./alpha_plots/fecal_bp_alpha_merged_initial.pdf", alpha_bp_fecal_merged_initial_plot, width = 22, height = 12)
```
#### Time 2 (Final)
```{r}
# merge by sample type
alpha_bp_fecal_merged_product_final <- merge_samples(bp_alpha_fecal_final, "Product")
# repair variable that was damaged during merge (coerced to numeric)
sample_data(alpha_bp_fecal_merged_product_final)$Product <- factor(sample_names(alpha_bp_fecal_merged_product_final))
# plot merged version of data
alpha_bp_fecal_merged_final_plot <- plot_richness(alpha_bp_fecal_merged_product_final, x = "Product",
color = "Product", measures = c("Observed", "Chao1", "Shannon"), title = "Gut Microbiome Alpha Diversity: Merged by Product; Final Time")
alpha_bp_fecal_merged_final_plot <- alpha_bp_fecal_merged_final_plot + geom_point(size=5, alpha=0.7) +
labs(color="Product") +
xlab("Product") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
alpha_bp_fecal_merged_final_plot
# save plot
ggsave("./alpha_plots/fecal_bp_alpha_merged_final.pdf", alpha_bp_fecal_merged_final_plot, width = 22, height = 12)
```
##### Individual Sample Alpha Index
```{r}
# Alpha Diversity Index
alpha_bp_fecal_all_plot <- plot_richness(bp_alpha_fecal, x = "Product", color = "Product", measures = c("Observed", "Chao1", "Shannon"), title = "Fecal Microbiome Sample Alpha Diversity")
alpha_bp_fecal_all_plot <- alpha_bp_fecal_all_plot + labs(color="Product") +
xlab("Product") +
ylab("Diversity Index") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
alpha_bp_fecal_all_plot
# save plot
ggsave("./alpha_plots/fecal_bp_alpha_all.pdf", alpha_bp_fecal_all_plot, width = 22, height = 12)
```
#### Merged Plot: By Product Type
> All samples were merged based on Product Type: A, B, C, D
```{r}
# merge by sample type
alpha_bp_fecal_merged_product_type <- merge_samples(bp_alpha_fecal, "Product")
# repair variable that was damaged during merge (coerced to numeric)
sample_data(alpha_bp_fecal_merged_product_type)$Product <- factor(sample_names(alpha_bp_fecal_merged_product_type))
# plot merged version of data
alpha_bp_fecal_merged_product_plot <- plot_richness(alpha_bp_fecal_merged_product_type, x = "Product",
color = "Product", measures = c("Observed", "Chao1", "Shannon"), title = "Fecal Microbiome Alpha Diversity: Merged by Product Treatment")
alpha_bp_fecal_merged_product_plot <- alpha_bp_fecal_merged_product_plot + geom_point(size=5, alpha=0.7) +
labs(color="Product") +
xlab("Product") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.y = element_text(size = 10))
alpha_bp_fecal_merged_product_plot
# save plot
ggsave("./alpha_plots/fecal_bp_alpha_merged_product.pdf", alpha_bp_fecal_merged_product_plot, width = 22, height = 12)
```
### Beta Diversity:
##### Raw Data Analysis
> Beta Diversity analysis with untransformed read data
##### Bray-Curtis Dissimilarity
```{r Ordination Plots PCoA Bray-Curtis}
# set orination parameters
bp_ord1 <- ordinate(bracken_physeq, "PCoA", "bray")
# plot ordination
bp_bray.curtis_pcoa <- plot_ordination(bracken_physeq, bp_ord1, type="samples", color="Product", shape = "Sample_Type", title="Microbiome PCoA: Bray-Curtis Dissimilarity")
# aesthetics of ordination plot
bp_bray.curtis_pcoa <- bp_bray.curtis_pcoa +
labs(color="Product", shape="Sample Type")
# geom_text_repel(label = sample_names(bracken_physeq), show.legend = FALSE) +
# theme(legend.title = element_text(size = 14),
# legend.text = element_text(face = "bold", size = 10),
# title = element_text(face = "bold", size = 14),
# axis.title.x = element_text(face = "bold", size = 13),
# axis.title.y = element_text(face = "bold", size = 13),
# axis.text.x = element_text(size = 12),
# axis.text.y = element_text(size = 12))
# print plot
print(bp_bray.curtis_pcoa)
```
##### Jaccard Distance
```{r Ordination Plots PCoA Jaccard}
# set ordination method
bp_ord2 <- ordinate(bracken_physeq, "PCoA", "jaccard")
#plot ordination
bp_jaccard_pcoa <- plot_ordination(bracken_physeq, bp_ord2, type="samples", color="Product", shape = "Sample_Type", title="Microbiome PCoA: Jaccard Distance")
# plot aesthetics
bp_jaccard_pcoa <- bp_jaccard_pcoa +
labs(color="Product", shape="Sample Type")
# geom_text_repel(label = sample_names(bracken_physeq), show.legend = FALSE) +
# theme(legend.title = element_text(size = 14),
# legend.text = element_text(face = "bold", size = 10),
# title = element_text(face = "bold", size = 14),
# axis.title.x = element_text(face = "bold", size = 13),
# axis.title.y = element_text(face = "bold", size = 13),
# axis.text.x = element_text(size = 12),
# axis.text.y = element_text(size = 12))
# print plot
print(bp_jaccard_pcoa)
```
#### Beta Diversity: Distances
##### Bray-Curtis PCoA
```{r Normalized Counts PCoA Bray-Curtis}
# set orination parameters
bp_norm_ord1 <- ordinate(bp_norm_beta, "PCoA", "bray")
# plot ordination
bp_norm_pcoa_bc <- plot_ordination(bp_norm, bp_norm_ord1, type="samples", color="Product", shape = "Sample_Type", title="Microbiome PCoA: Bray-Curtis; Normalized")
# aesthetics of ordination plot
bp_norm_pcoa_bc <- bp_norm_pcoa_bc +
labs(color="Product", shape="Sample Type") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
# print plot
print(bp_norm_pcoa_bc)
# save plot
ggsave("./beta_plots/beta_pca_bray_norm.pdf", bp_norm_pcoa_bc, width = 22, height = 12)
```
#### Subset by Sample Type
```{r}
bp_beta_norm_saliva <- subset_samples(bp_norm_beta, Sample_Type == "Saliva")
bp_beta_norm_fecal <- subset_samples(bp_norm_beta, Sample_Type == "Fecal")
bp_beta_norm_saliva
```
##### Saliva; normalized PCoA - Bray
```{r Normalized Counts PCoA Bray-Curtis}
# set orination parameters
beta_norm_saliva_ord1 <- ordinate(bp_beta_norm_saliva, "PCoA", "bray")
# plot ordination
beta_norm_bray_pca_saliva <- plot_ordination(bp_beta_norm_saliva, beta_norm_saliva_ord1, type="samples", color="Product", title="Saliva Microbiome PCoA: Bray-Curtis; Normalized")
# aesthetics of ordination plot
beta_norm_bray_pca_saliva <- beta_norm_bray_pca_saliva +
labs(color="Product") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
# print plot
print(beta_norm_bray_pca_saliva)
# save plot
ggsave("./beta_plots/saliva_beta_pca_bray_norm.pdf", beta_norm_bray_pca_saliva, width = 22, height = 12)
```
##### Fecal; normalized PCoA - Bray
```{r Normalized Counts PCoA Bray-Curtis}
# set orination parameters
beta_norm_fecal_ord1 <- ordinate(bp_beta_norm_fecal, "PCoA", "bray")
# plot ordination
beta_norm_bray_pca_fecal <- plot_ordination(bp_beta_norm_fecal, beta_norm_fecal_ord1, type="samples", color="Product", title="Fecal Microbiome PCoA: Bray-Curtis; Normalized")
# aesthetics of ordination plot
beta_norm_bray_pca_fecal <- beta_norm_bray_pca_fecal +
labs(color="Product") +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
# print plot
print(beta_norm_bray_pca_fecal)
# save plot
ggsave("./beta_plots/fecal_beta_pca_bray_norm.pdf", beta_norm_bray_pca_fecal, width = 22, height = 12)
```
##### Jaccard - PCoA
```{r Normalized Counts PCoA Bray-Curtis}
# set orination parameters
bp_norm_ord2 <- ordinate(bp_norm, "PCoA", "jaccard")
# plot ordination
bp_norm_pcoa_jac <- plot_ordination(bp_norm, bp_norm_ord2, type="samples", color="Product", shape = "Sample_Type", title="Microbiome PCoA: Jaccard; Normalized")
# aesthetics of ordination plot
bp_norm_pcoa_jac <- bp_norm_pcoa_jac +
labs(color="Patient Type")
# geom_text_repel(label = sample_names(bp_norm), show.legend = FALSE) +
# theme(legend.title = element_text(size = 14),
# legend.text = element_text(face = "bold", size = 10),
# title = element_text(face = "bold", size = 14),
# axis.title.x = element_text(face = "bold", size = 13),
# axis.title.y = element_text(face = "bold", size = 13),
# axis.text.x = element_text(size = 12),
# axis.text.y = element_text(size = 12))
# print plot
print(bp_norm_pcoa_jac)
```
##### Bray - NMDS
```{r Normalized Counts PCoA Bray-Curtis}
# set orination parameters
bp_norm_ord3 <- ordinate(bp_norm, "NMDS", "bray")
# plot ordination
bp_norm_nmds_bray <- plot_ordination(bp_norm, bp_norm_ord3, type="samples", color="Product", shape = "Sample_Type", title="Microbiome NMDS: Bray; Normalized")
# aesthetics of ordination plot
bp_norm_nmds_bray <- bp_norm_nmds_bray +
labs(color="Product")
# geom_text_repel(label = sample_names(bp_norm), show.legend = FALSE) +
# theme(legend.title = element_text(size = 14),
# legend.text = element_text(face = "bold", size = 10),
# title = element_text(face = "bold", size = 14),
# axis.title.x = element_text(face = "bold", size = 13),
# axis.title.y = element_text(face = "bold", size = 13),
# axis.text.x = element_text(size = 12),
# axis.text.y = element_text(size = 12))
# print plot
print(bp_norm_nmds_bray)
```
##### Jaccard - NMDS
```{r Normalized Counts PCoA Bray-Curtis}
# set orination parameters
bp_norm_ord4 <- ordinate(bp_norm, "NMDS", "jaccard")
# plot ordination
bp_norm_nmds_jac <- plot_ordination(bp_norm, bp_norm_ord4, type="samples", color="Product", shape = "Sample_Type", title="Microbiome NMDS: Jaccard; Normalized")
# aesthetics of ordination plot
bp_norm_nmds_jac <- bp_norm_nmds_jac +
labs(color="Product")
# geom_text_repel(label = sample_names(bp_norm), show.legend = FALSE) +
# theme(legend.title = element_text(size = 14),
# legend.text = element_text(face = "bold", size = 10),
# title = element_text(face = "bold", size = 14),
# axis.title.x = element_text(face = "bold", size = 13),
# axis.title.y = element_text(face = "bold", size = 13),
# axis.text.x = element_text(size = 12),
# axis.text.y = element_text(size = 12))
# print plot
print(bp_norm_nmds_jac)
```
#### Bacteria Only
```{r bp bacteria only}
# subset to keep only bacteria
bp_norm_bac <- subset_taxa(bp_norm, Kingdom =="Bacteria")
# number of taxa surviving
ntaxa(bp_norm_bac)
```
##### Beta Diversity: Ordination Plot of Bacteria Subset
```{r Beta Diversity Bacteria Only 1}
# Keep only most abundant 10 Phyla
phylum.sum <- tapply(taxa_sums(bp_norm_bac), tax_table(bp_norm_bac)[, "Phylum"], sum, na.rm=TRUE)
top10phyla <- names(sort(phylum.sum, TRUE))[1:10]
bp_norm_bac <- prune_taxa((tax_table(bp_norm_bac)[, "Phylum"] %in% top10phyla), bp_norm_bac)
# n taxa surviving
ntaxa(bp_norm_bac)
```
##### NMDS: Bray-Curtis Dissimilarity
```{r Beta Diversity Bacteria Only 2}
bp_norm_bac.ord1 <- ordinate(bp_norm_bac, "NMDS", "bray")
bp_norm_bac_p1 <- plot_ordination(bp_norm_bac, bp_norm_bac.ord1, type = "sample", color = "Patient_Type",
title = "OVF Microbiome: NMDS Bray-Curtis; Bacteria only") +
labs(color="Patient Type") +
geom_text_repel(label = sample_names(bp_norm_bac), show.legend = FALSE) +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
bp_norm_bac_p1
```
##### PCoA/MDS: Jaccard Distance
```{r Beta Diversity Bacteria Only 3}
bp_norm_bac.ord2 <- ordinate(bp_norm_bac, "PCoA", "jaccard")
bp_norm_bac_p2 <- plot_ordination(bp_norm_bac, bp_norm_bac.ord2, type = "sample", color = "Patient_Type",
title = "OVF Microbiome: PCoA Jaccard; Bacteria only") +
labs(color="Patient Type") +
geom_text_repel(label = sample_names(bp_norm_bac), show.legend = FALSE) +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
bp_norm_bac_p2
```
#### Viruses Only
```{r bp viruses only}
# subset to keep only bacteria
bp_norm_vir <- subset_taxa(bp_norm, Kingdom =="Viruses")
# number of taxa surviving
ntaxa(bp_norm_vir)
```
##### NMDS: Bray-Curtis Dissimilarity
```{r Beta Diversity Viruses Only 2}
bp_norm_vir.ord1 <- ordinate(bp_norm_vir, "NMDS", "bray")
bp_norm_vir_p1 <- plot_ordination(bp_norm_vir, bp_norm_vir.ord1, type = "sample", color = "Patient_Type",
title = "OVF Microbiome: NMDS Bray-Curtis; Viruses only") +
labs(color="Patient Type") +
geom_text_repel(label = sample_names(bp_norm_vir), show.legend = FALSE) +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
bp_norm_vir_p1
```
##### PCoA/MDS: Jaccard Distance
```{r Beta Diversity Viruses Only 3}
bp_norm_vir.ord2 <- ordinate(bp_norm_vir, "PCoA", "bray")
bp_norm_vir_p2 <- plot_ordination(bp_norm_vir, bp_norm_vir.ord2, type = "sample", color = "Patient_Type",
title = "OVF Microbiome: PCoA Bray; Viruses only") +
labs(color="Patient Type") +
geom_text_repel(label = sample_names(bp_norm_vir), show.legend = FALSE) +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
bp_norm_vir_p2
```
#### Archaea Only
```{r bp archaea only}
# subset to keep only bacteria
bp_norm_arc <- subset_taxa(bp_norm, Kingdom =="Archaea")
# number of taxa surviving
ntaxa(bp_norm_arc)
```
##### NMDS: Bray-Curtis Dissimilarity
```{r Beta Diversity Viruses Only 2}
bp_norm_arc.ord1 <- ordinate(bp_norm_arc, "NMDS", "bray")
bp_norm_arc_p1 <- plot_ordination(bp_norm_arc, bp_norm_arc.ord1, type = "sample", color = "Patient_Type",
title = "OVF Microbiome: NMDS Bray-Curtis; Archaea only") +
labs(color="Patient Type") +
geom_text_repel(label = sample_names(bp_norm_arc), show.legend = FALSE) +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
bp_norm_arc_p1
```
##### PCoA/MDS: Jaccard Distance
```{r Beta Diversity Viruses Only 3}
bp_norm_arc.ord2 <- ordinate(bp_norm_arc, "PCoA", "bray")
bp_norm_arc_p2 <- plot_ordination(bp_norm_arc, bp_norm_arc.ord2, type = "sample", color = "Patient_Type",
title = "OVF Microbiome: PCoA Bray; Archaea only") +
labs(color="Patient Type") +
geom_text_repel(label = sample_names(bp_norm_arc), show.legend = FALSE) +
theme(legend.title = element_text(size = 14),
legend.text = element_text(face = "bold", size = 10),
title = element_text(face = "bold", size = 14),
axis.title.x = element_text(face = "bold", size = 13),
axis.title.y = element_text(face = "bold", size = 13),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12))
bp_norm_arc_p2
```
## Differential Abundance Testing
> Using Negative Binomial in Microbiome Differential Abundance Testing
#### Convert Phyloseq object to DESeq2 object
```{r Phyloseq to DESeq2}
# Summarize bracken_physeq
bracken_physeq
# Check Product Factor
head(sample_data(bracken_physeq)$Product, n=4)
# Convert Phyloseq Object to DESeq2 Class Object by Factor Patient Type
bp_prod_dds <- phyloseq_to_deseq2(bracken_physeq, ~Product)
# calculate geometric means prior to estimate size factors
gm_mean = function(x, na.rm=TRUE){
exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
}
bp_prod_geoMeans = apply(counts(bp_prod_dds), 1, gm_mean)
bp_prod_dds = estimateSizeFactors(bp_prod_dds, geoMeans=bp_prod_geoMeans)
bp_prod_dds = DESeq(bp_prod_dds, fitType="local")
```
## Subset by Product
### Product A
```{r}
# Subset Phyloseq Dataset samples for "Product A"
bp_norm_A <- subset_samples(bp_norm, Product == "A")
# remove low count taxa from data
bp_norm_A <- prune_taxa(taxa_sums(bp_norm_A) > 5000, bp_norm_A)
```
##### Select Top 9 Phyla
```{r}
phylum.sum_A = tapply(taxa_sums(bp_norm_A), tax_table(bp_norm_A)[, "Phylum"], sum, na.rm=TRUE)
top5phyla_A = names(sort(phylum.sum_A, TRUE))[1:8]
bp_norm_A = prune_taxa((tax_table(bp_norm_A)[, "Phylum"] %in% top5phyla_A), bp_norm_A)
```
##### Convert Phyloseq to DESeq2
```{r Phyloseq to DESeq2}
# Summarize bracken_physeq
bp_norm_A
# Check Sample_Type Factor
head(sample_data(bp_norm_A)$Sample_Type, n=2)
# Convert Phyloseq Object to DESeq2 Class Object by Factor Patient Type
bp_A_dds <- phyloseq_to_deseq2(bp_norm_A, ~Sample_Type)
# calculate geometric means prior to estimate size factors
gm_mean = function(x, na.rm=TRUE){
exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
}
bp_A_geoMeans = apply(counts(bp_A_dds), 1, gm_mean)
bp_A_dds = estimateSizeFactors(bp_A_dds, geoMeans=bp_A_geoMeans)
bp_A_dds = DESeq(bp_A_dds, test = "Wald", fitType="parametric")
```
##### Investigate Test Table Results
##### Oral vs Fecal
```{r}
bp_results_A <- results(bp_A_dds, contrast = c("Sample_Type", "Saliva", "Fecal"))
mcols(bp_results_A)$description
bp_results_A = bp_results_A[order(bp_results_A$padj, na.last=NA), ]
head(bp_results_A)
alpha = 0.001
bp_A_sigtab = bp_results_A[(bp_results_A$padj < alpha), ]
bp_A_sigtab = cbind(as(bp_A_sigtab, "data.frame"), as(tax_table(bp_norm_A)[rownames(bp_A_sigtab), ], "matrix"))
head(bp_A_sigtab)
write.csv(bp_A_sigtab, "./deseq2_output/MARS_bracken_diff_abund_deseq2_product_A.csv")
```
##### Interpretation
> For a particular taxa, a log2 fold change of -1 for condition **Saliva** vs **Fecal** means that the **Saliva** Sample Type had a multiplicative change in observed read abundance of 2^−1=0.5 compared to the **Fecal** Sample Type. Because the variable of interest represents estimated absolute abundance within the sample, the reported log2 fold change for a given taxa represents log2 units of change in absolute abundance (fraction of total reads).
##### Plot
```{r}
library("ggplot2")
theme_set(theme_gray())