-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrobiome_beta_diversity.R
More file actions
387 lines (315 loc) · 19.5 KB
/
Copy pathmicrobiome_beta_diversity.R
File metadata and controls
387 lines (315 loc) · 19.5 KB
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
# Microbiome Analysis - Beta Diversity
# load libraries
library(ggplot2)
library(tidyverse)
library(phyloseq)
library(vegan)
library(microbiome)
library(DivNet)
library(breakaway)
library(ape)
# setwd
setwd("/Users/kristinvandenham/kmvanden/RStudio/")
### load data
# metadata
meta <- read.table("metadata.txt", header = TRUE)
rownames(meta) <- meta$sample_name
# feature table
feat <- read.table("feature_table.txt", header = TRUE)
#########################################################################
##### BETA DIVERSITY ANALYSIS - NO PREVALENCE FILTERING OF TAXA #####
#########################################################################
### beta diversity analysis with phyloseq (mainly)
# format feature table and metadata
feat_otu <- as.matrix(feat) # convert feature table to matrix
feat_otu <- otu_table(feat_otu, taxa_are_rows = TRUE) # convert to otu_table
all(colnames(feat_otu) == rownames(meta)) # ensure sample names are the same
sampledata <- sample_data(meta) # convert to sample_data
# create phyloseq object
ps <- phyloseq(feat_otu, sampledata)
ps_rel <- transform_sample_counts(ps, function(x) x / sum(x)) # relative abundance for bray-curtis and canberra
ps_pa <- transform_sample_counts(ps, function(x) as.numeric(x > 0)) # presence/absence for jaccard
ps_log <- transform_sample_counts(ps, function(x) log1p(x)) # log-transformation for euclidean
ps_clr <- transform(ps, "clr") # clr transformation (microbiome package adds pseudocount) for aitchison
# compute distance matrices
bray_dist <- distance(ps_rel, method = "bray") # bray-curtis
jaccard_dist <- distance(ps_pa, method = "jaccard", binary = TRUE) # jaccard
euc_dist <- distance(ps_log, method = "euclidean") # euclidean
canberra_dist <- distance(ps_rel, method = "canberra") # canberra
aitchison_dist <- distance(ps_clr, method = "euclidean") # aitchison (euclidean in clr space)
# statistical testing with PERMANOVA and betadispar
adonis2(bray_dist ~ condition, data = meta) # bray-curtis
betadisper(bray_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(bray_dist, group = meta$condition))
adonis2(jaccard_dist ~ condition, data = meta) # jaccard
betadisper(jaccard_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(jaccard_dist, group = meta$condition))
adonis2(euc_dist ~ condition, data = meta) # euclidean
betadisper(euc_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(euc_dist, group = meta$condition))
adonis2(canberra_dist ~ condition, data = meta) # canberra
betadisper(canberra_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(canberra_dist, group = meta$condition))
adonis2(aitchison_dist ~ condition, data = meta) # aitchison
betadisper(aitchison_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(canberra_dist, group = meta$condition))
# PCoA ordination
ordination_pcoa_bray <- ordinate(ps_rel, method = "PCoA", distance = bray_dist) # bray-curtis
ordination_pcoa_jaccard <- ordinate(ps_pa, method = "PCoA", distance = jaccard_dist) # jaccard
ordination_pcoa_euc <- ordinate(ps_log, method = "PCoA", distance = euc_dist) # euclidean
ordination_pcoa_canberra <- ordinate(ps_rel, method = "PCoA", distance = canberra_dist) # canberra
ordination_pcoa_aitchison <- ordinate(ps_clr, method = "PCoA", distance = aitchison_dist) # aitchison
# PCoA plots
plot_ordination(ps_rel, ordination_pcoa_bray, color = "condition") +
geom_point(size = 2) + ggtitle("PCoA - Bray-Curtis") + theme_minimal() # bray-curtis
plot_ordination(ps_pa, ordination_pcoa_jaccard, color = "condition") +
geom_point(size = 2) + ggtitle("PCoA - Jaccard") + theme_minimal() # jaccard
plot_ordination(ps_log, ordination_pcoa_euc, color = "condition") +
geom_point(size = 2) + ggtitle("PCoA - Euclidean") + theme_minimal() # euclidean
plot_ordination(ps_rel, ordination_pcoa_canberra, color = "condition") +
geom_point(size = 2) + ggtitle("PCoA - Canberra") + theme_minimal() # canberra
plot_ordination(ps_clr, ordination_pcoa_aitchison, color = "condition") +
geom_point(size = 2) + ggtitle("PCoA - Aitchison") + theme_minimal() # aitchison
# NMDS ordination
ordination_nmds_bray <- ordinate(ps_rel, method = "NMDS", distance = bray_dist) # bray-curtis
ordination_nmds_jaccard <- ordinate(ps_pa, method = "NMDS", distance = jaccard_dist) # jaccard
ordination_nmds_canberra <- ordinate(ps_rel, method = "NMDS", distance = canberra_dist) # canberra
# NMDS plots
plot_ordination(ps_rel, ordination_nmds_bray, color = "condition") +
geom_point(size = 2) + ggtitle("NMDS - Bray-Curtis") + theme_minimal() # bray-curtis
plot_ordination(ps_pa, ordination_nmds_jaccard, color = "condition") +
geom_point(size = 2) + ggtitle("NMDS - Jaccard") + theme_minimal() # jaccard
plot_ordination(ps_rel, ordination_nmds_canberra, color = "condition") +
geom_point(size = 2) + ggtitle("NMDS - Canberra") + theme_minimal() # canberra
### extract PCA data
# euclidean
log_otu <- t(otu_table(ps_log)) # extract CLR-transformed data and transpose
pca_euc <- prcomp(log_otu, center = TRUE, scale. = FALSE) # run PCA
pca_euc_df <- as.data.frame(pca_euc$x) # extract PCA scores (coordinates)
pca_euc_df$sample_name <- rownames(pca_euc_df)
pca_euc_df <- left_join(pca_euc_df, meta, by = "sample_name") # join with metadata
euc_var_explained <- round(100 * summary(pca_euc)$importance[2, 1:2], 1) # extract percentage of variance explained
# aitchison
clr_otu <- t(otu_table(ps_clr)) # extract CLR-transformed data and transpose
pca_ait <- prcomp(clr_otu, center = TRUE, scale. = FALSE) # run PCA
pca_ait_df <- as.data.frame(pca_ait$x) # extract PCA scores (coordinates)
pca_ait_df$sample_name <- rownames(pca_ait_df)
pca_ait_df <- left_join(pca_ait_df, meta, by = "sample_name") # join with metadata
ait_var_explained <- round(100 * summary(pca_ait)$importance[2, 1:2], 1) # extract percentage of variance explained
### PCA plots
# euclidean
ggplot(pca_euc_df, aes(x = PC1, y = PC2, color = condition)) +
geom_point(size = 2) + theme_minimal() + stat_ellipse(type = "norm", level = 0.95) +
labs(title = "PCA of log-transformed data (euclidean)",
x = paste0("PC1 (", euc_var_explained[1], "% variance)"),
y = paste0("PC2 (", euc_var_explained[2], "% variance)"))
# aitchison
ggplot(pca_ait_df, aes(x = PC1, y = PC2, color = condition)) +
geom_point(size = 2) + theme_minimal() + stat_ellipse(type = "norm", level = 0.95) +
labs(title = "PCA of CLR-transformed data (aitchison)",
x = paste0("PC1 (", ait_var_explained[1], "% variance)"),
y = paste0("PC2 (", ait_var_explained[2], "% variance)"))
######################################################################
##### BETA DIVERSITY ANALYSIS - PREVALENCE FILTERING OF TAXA #####
######################################################################
### beta diversity analysis with phyloseq (mainly)
# format feature table and metadata
feat_otu <- as.matrix(feat) # convert feature table to matrix
feat_otu <- otu_table(feat_otu, taxa_are_rows = TRUE) # convert to otu_table
all(colnames(feat_otu) == rownames(meta)) # ensure sample names are the same
sampledata <- sample_data(meta) # convert to sample_data
# create phyloseq object and filter low prevalence taxa (present in less than 10% of samples)
ps <- phyloseq(feat_otu, sampledata)
ps_filt <- filter_taxa(ps, function(x) sum(x > 0) >= 0.1 * nsamples(ps), prune = TRUE)
ps_rel <- transform_sample_counts(ps_filt, function(x) x / sum(x)) # relative abundance for bray-curtis and canberra
ps_pa <- transform_sample_counts(ps_filt, function(x) as.numeric(x > 0)) # presence/absence for jaccard
ps_log <- transform_sample_counts(ps_filt, function(x) log1p(x)) # log-transformation for euclidean
ps_clr <- transform(ps_filt, "clr") # clr transformation (microbiome package adds pseudocount) for aitchison
# compute distance matrices
bray_dist <- distance(ps_rel, method = "bray") # bray-curtis
jaccard_dist <- distance(ps_pa, method = "jaccard", binary = TRUE) # jaccard
euc_dist <- distance(ps_log, method = "euclidean") # euclidean
canberra_dist <- distance(ps_rel, method = "canberra") # canberra
aitchison_dist <- distance(ps_clr, method = "euclidean") # aitchison (euclidean in clr space)
# statistical testing with PERMANOVA and betadisper
adonis2(bray_dist ~ condition, data = meta) # bray-curtis
betadisper(bray_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(bray_dist, group = meta$condition))
adonis2(jaccard_dist ~ condition, data = meta) # jaccard
betadisper(jaccard_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(jaccard_dist, group = meta$condition))
adonis2(euc_dist ~ condition, data = meta) # euclidean
betadisper(euc_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(euc_dist, group = meta$condition))
adonis2(canberra_dist ~ condition, data = meta) # canberra
betadisper(canberra_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(canberra_dist, group = meta$condition))
adonis2(aitchison_dist ~ condition, data = meta) # aitchison
betadisper(aitchison_dist, group = meta$condition) %>% permutest()
boxplot(betadisper(aitchison_dist, group = meta$condition))
# PCoA ordination
ordination_pcoa_bray <- ordinate(ps_rel, method = "PCoA", distance = bray_dist) # bray-curtis
ordination_pcoa_jaccard <- ordinate(ps_pa, method = "PCoA", distance = jaccard_dist) # jaccard
ordination_pcoa_euc <- ordinate(ps_log, method = "PCoA", distance = euc_dist) # euclidean
ordination_pcoa_canberra <- ordinate(ps_rel, method = "PCoA", distance = canberra_dist) # canberra
ordination_pcoa_aitchison <- ordinate(ps_clr, method = "PCoA", distance = aitchison_dist) # aitchison
# PCoA plots
plot_ordination(ps_rel, ordination_pcoa_bray, color = "condition") + stat_ellipse(type = "norm", level = 0.95) +
geom_point(size = 2) + ggtitle("PCoA - Bray-Curtis") + theme_minimal() # bray-curtis
plot_ordination(ps_pa, ordination_pcoa_jaccard, color = "condition") + stat_ellipse(type = "norm", level = 0.95) +
geom_point(size = 2) + ggtitle("PCoA - Jaccard") + theme_minimal() # jaccard
plot_ordination(ps_log, ordination_pcoa_euc, color = "condition") + stat_ellipse(type = "norm", level = 0.95) +
geom_point(size = 2) + ggtitle("PCoA - Euclidean") + theme_minimal() # euclidean
plot_ordination(ps_rel, ordination_pcoa_canberra, color = "condition") + stat_ellipse(type = "norm", level = 0.95) +
geom_point(size = 2) + ggtitle("PCoA - Canberra") + theme_minimal() # canberra
plot_ordination(ps_clr, ordination_pcoa_aitchison, color = "condition") + stat_ellipse(type = "norm", level = 0.95) +
geom_point(size = 2) + ggtitle("PCoA - Aitchison") + theme_minimal() # aitchison
# NMDS ordination
ordination_nmds_bray <- ordinate(ps_rel, method = "NMDS", distance = bray_dist) # bray-curtis
ordination_nmds_jaccard <- ordinate(ps_pa, method = "NMDS", distance = jaccard_dist) # jaccard
ordination_nmds_canberra <- ordinate(ps_rel, method = "NMDS", distance = canberra_dist) # canberra
# NMDS plots
plot_ordination(ps_rel, ordination_nmds_bray, color = "condition") + stat_ellipse(type = "norm", level = 0.95) +
geom_point(size = 2) + ggtitle("NMDS - Bray-Curtis") + theme_minimal() # bray-curtis
plot_ordination(ps_pa, ordination_nmds_jaccard, color = "condition") + stat_ellipse(type = "norm", level = 0.95) +
geom_point(size = 2) + ggtitle("NMDS - Jaccard") + theme_minimal() # jaccard
plot_ordination(ps_rel, ordination_nmds_canberra, color = "condition") + stat_ellipse(type = "norm", level = 0.95) +
geom_point(size = 2) + ggtitle("NMDS - Canberra") + theme_minimal() # canberra
### extract PCA data
# euclidean
log_otu <- t(otu_table(ps_log)) # extract CLR-transformed data and transpose
pca_euc <- prcomp(log_otu, center = TRUE, scale. = FALSE) # run PCA
pca_euc_df <- as.data.frame(pca_euc$x) # extract PCA scores (coordinates)
pca_euc_df$sample_name <- rownames(pca_euc_df)
pca_euc_df <- left_join(pca_euc_df, meta, by = "sample_name") # join with metadata
euc_var_explained <- round(100 * summary(pca_euc)$importance[2, 1:2], 1) # extract percentage of variance explained
# aitchison
clr_otu <- t(otu_table(ps_clr)) # extract CLR-transformed data and transpose
pca_ait <- prcomp(clr_otu, center = TRUE, scale. = FALSE) # run PCA
pca_ait_df <- as.data.frame(pca_ait$x) # extract PCA scores (coordinates)
pca_ait_df$sample_name <- rownames(pca_ait_df)
pca_ait_df <- left_join(pca_ait_df, meta, by = "sample_name") # join with metadata
ait_var_explained <- round(100 * summary(pca_ait)$importance[2, 1:2], 1) # extract percentage of variance explained
### PCA plots
# euclidean
ggplot(pca_euc_df, aes(x = PC1, y = PC2, color = condition)) +
geom_point(size = 2) + theme_minimal() + stat_ellipse(type = "norm", level = 0.95) +
labs(title = "PCA of log-transformed data (euclidean)",
x = paste0("PC1 (", euc_var_explained[1], "% variance)"),
y = paste0("PC2 (", euc_var_explained[2], "% variance)"))
# aitchison
ggplot(pca_ait_df, aes(x = PC1, y = PC2, color = condition)) +
geom_point(size = 2) + theme_minimal() + stat_ellipse(type = "norm", level = 0.95) +
labs(title = "PCA of CLR-transformed data (aitchison)",
x = paste0("PC1 (", ait_var_explained[1], "% variance)"),
y = paste0("PC2 (", ait_var_explained[2], "% variance)"))
###################################################################################
##### BETA DIVERSITY ANALYSIS - STATISTICAL MODEL-BASED APPROACH - DIVNET #####
###################################################################################
### create a phyloseq object
# format feature table and metadata
feat_otu <- as.matrix(feat) # convert feature table to matrix
feat_otu <- otu_table(feat_otu, taxa_are_rows = TRUE) # convert to otu_table
all(colnames(feat_otu) == rownames(meta)) # ensure sample names are the same
sampledata <- sample_data(meta) # convert to sample_data
# create phyloseq object
ps <- phyloseq(feat_otu, sampledata)
### beta diversity analysis with DivNet
# most abundant feature in each sample
most_abundant_taxa <- apply(feat, 2, function(x) {
feature_index <- which.max(x) # get index of max value
feature_name <- rownames(feat)[feature_index] # get feature name
return(feature_name)
})
# run DivNet
divnet_results <- divnet(ps, B = 5, base = NULL)
# saveRDS(divnet_results, file = "divnet_results.rds")
# divnet_results <- readRDS("divnet_results.rds")
# create sample specimen matrix
sample_specimen_matrix <- diag(nrow(sample_data(ps)))
rownames(sample_specimen_matrix) <- rownames(meta)
colnames(sample_specimen_matrix) <- rownames(meta)
test_bray <- testBetaDiversity(dv = divnet_results,
h0 = "bray-curtis",
groups = as.numeric(factor(meta$condition)),
sample_specimen_matrix = sample_specimen_matrix,
n_boot = 1000)
test_bray$p_value
test_euc <- testBetaDiversity(dv = divnet_results,
h0 = "euclidean",
groups = as.numeric(factor(meta$condition)),
sample_specimen_matrix = sample_specimen_matrix,
n_boot = 1000)
test_euc$p_value
# extract the bray-curtis and euclidean matrices
bray_divnet <- divnet_results$`bray-curtis`
euc_divnet <- divnet_results$euclidean
# isSymmetric(bray_divnet)
# all(diag(bray_divnet) == 0)
# PCoA ordination
# use pcoa() from ape which works on generic, externally computed distance matrices (not on phyloseq objects)
bray_pcoa <- pcoa(bray_divnet)
ordination_coords_bray <- bray_pcoa$vectors[, 1:2] # extract first two PCoA axes
var_explained_bray <- 100 * bray_pcoa$values$Relative_eig[1:2] # extract percentage of variance explained
euc_pcoa <- pcoa(euc_divnet)
ordination_coords_euc <- euc_pcoa$vectors[, 1:2] # extract first two PCoA axes
var_explained_euc <- 100 * euc_pcoa$values$Relative_eig[1:2] # extract percentage of variance explained
# PCoA plot of bray-curtis (divnet)
ordination_df_bray <- as.data.frame(ordination_coords_bray)
ordination_df_bray$condition <- meta$condition # add metadata to data.frame
ggplot(ordination_df_bray, aes(x = Axis.1, y = Axis.2, color = condition)) +
geom_point(size = 2) + theme_minimal() + stat_ellipse(type = "norm", level = 0.95) +
labs(title = "PCoA - Bray-Curtis (DivNet)",
x = paste0("PCoA 1 (", round(var_explained_bray[1], 1), "%)"),
y = paste0("PCoA 2 (", round(var_explained_bray[2], 1), "%)"))
# PCoA plot of euclidean (divnet)
ordination_df_euc <- as.data.frame(ordination_coords_euc)
ordination_df_euc$condition <- meta$condition # add metadata to data.frame
ggplot(ordination_df_euc, aes(x = Axis.1, y = Axis.2, color = condition)) +
geom_point(size = 2) + theme_minimal() + stat_ellipse(type = "norm", level = 0.95) +
labs(title = "PCoA - Euclidean (DivNet)",
x = paste0("PCoA 1 (", round(var_explained_euc[1], 1), "%)"),
y = paste0("PCoA 2 (", round(var_explained_euc[2], 1), "%)"))
sessionInfo()
# R version 4.5.0 (2025-04-11)
# Platform: aarch64-apple-darwin20
# Running under: macOS Sequoia 15.6
#
# Matrix products: default
# BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
#
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#
# time zone: America/Edmonton
# tzcode source: internal
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] ape_5.8-1 microbiome_1.30.0 DivNet_0.4.1 breakaway_4.8.4 vegan_2.7-1 permute_0.9-7
# [7] phyloseq_1.52.0 lubridate_1.9.4 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.4
# [13] readr_2.1.5 tidyr_1.3.1 tibble_3.3.0 tidyverse_2.0.0 ggplot2_3.5.2
#
# loaded via a namespace (and not attached):
# [1] ade4_1.7-23 tidyselect_1.2.1 farver_2.1.2 Biostrings_2.76.0
# [5] digest_0.6.37 timechange_0.3.0 lifecycle_1.0.4 cluster_2.1.8.1
# [9] multcompView_0.1-10 survival_3.8-3 magrittr_2.0.3 compiler_4.5.0
# [13] rlang_1.1.6 tools_4.5.0 igraph_2.1.4 data.table_1.17.4
# [17] labeling_0.4.3 plyr_1.8.9 RColorBrewer_1.1-3 abind_1.4-8
# [21] Rtsne_0.17 withr_3.0.2 BiocGenerics_0.54.0 grid_4.5.0
# [25] stats4_4.5.0 multtest_2.64.0 biomformat_1.36.0 Rhdf5lib_1.30.0
# [29] scales_1.4.0 iterators_1.0.14 MASS_7.3-65 dichromat_2.0-0.1
# [33] cli_3.6.5 crayon_1.5.3 reformulas_0.4.1 generics_0.1.4
# [37] rstudioapi_0.17.1 httr_1.4.7 reshape2_1.4.4 tzdb_0.5.0
# [41] minqa_1.2.8 rhdf5_2.52.1 splines_4.5.0 parallel_4.5.0
# [45] XVector_0.48.0 vctrs_0.6.5 boot_1.3-31 Matrix_1.7-3
# [49] jsonlite_2.0.0 IRanges_2.42.0 hms_1.1.3 S4Vectors_0.46.0
# [53] foreach_1.5.2 glue_1.8.0 nloptr_2.2.1 codetools_0.2-20
# [57] mvnfast_0.2.8 stringi_1.8.7 gtable_0.3.6 GenomeInfoDb_1.44.0
# [61] UCSC.utils_1.4.0 lme4_1.1-37 pillar_1.10.2 rhdf5filters_1.20.0
# [65] GenomeInfoDbData_1.2.14 R6_2.6.1 Rdpack_2.6.4 doParallel_1.0.17
# [69] lattice_0.22-7 Biobase_2.68.0 rbibutils_2.3 Rcpp_1.0.14
# [73] nlme_3.1-168 mgcv_1.9-3 pkgconfig_2.0.3