-
Notifications
You must be signed in to change notification settings - Fork 1
/
7.differential_abundance.Rmd
301 lines (230 loc) · 13 KB
/
7.differential_abundance.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
---
title: "aldex2_diff_abund"
author: "Erica Ryu"
date: "3/16/2023"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# 7. Differential abundance
The purpose of this script is to conduct difference abundance analysis with ALDEx2.
## load packages
```{r}
library(phyloseq)
library(data.table)
library(dplyr)
library(magrittr)
library(tidyr)
library(ALDEx2)
```
## load phyloseq object and set seed
```{r}
set.seed(123)
# oral microbiome phyloseq
phyloseq_complete <- readRDS("output/ps_complete.rds")
# subset based on extraction kit
qiagen <- subset_samples(phyloseq_complete, Condition == "Qiagen")
# gut microbiome phyloseq
gut_phyloseq <- readRDS("data/gut_phyloseq.rds")
```
## load colors
```{r}
fivecolors <- c("darkslateblue", "deepskyblue", "lightblue3", "lightsalmon" , "firebrick")
```
## prep taxa table for ALDEx2
```{r}
phy_genus <- tax_glom(qiagen, "Genus", NArm = FALSE)
otu_table <- as.data.frame(as.matrix(phy_genus@otu_table))
setDT(otu_table, keep.rownames = TRUE)[]
colnames(otu_table)[1] <- "SampleID"
otu_table2 <- otu_table %>% data.frame %>% set_rownames(.$SampleID)
otu_table2 <- otu_table2[,-1]
otu_table2 <- t(otu_table2)
tax_table <- as.data.frame(as.matrix(phy_genus@tax_table))
otu_tax <- merge(otu_table2, tax_table, by = 0, all = TRUE)
# change colnames to genus name
otu_tax$Genus <- replace_na(otu_tax$Genus, "_unclassified")
# add higher taxonomic level name to unclassified
otu_tax$Phylum <- ifelse(is.na(otu_tax$Phylum), otu_tax$Kingdom, otu_tax$Phylum)
otu_tax$Class <- ifelse(is.na(otu_tax$Class), otu_tax$Phylum, otu_tax$Class)
otu_tax$Order <- ifelse(is.na(otu_tax$Order), otu_tax$Class, otu_tax$Order)
otu_tax$Family <- ifelse(is.na(otu_tax$Family), otu_tax$Order, otu_tax$Family)
otu_tax$Genus <- ifelse(otu_tax$Genus == "_unclassified", paste(otu_tax$Family, otu_tax$Genus, sep = ""), otu_tax$Genus)
# rename completely unclassified ASV
otu_tax$Genus <- gsub("NA_unclassified", "Unclassified", otu_tax$Genus)
# set genus column as rownames
rownames(otu_tax) <- otu_tax$Genus
# remove taxonomic columns
otu_tax <- subset(otu_tax, select = -c(Row.names, Kingdom, Phylum, Class, Order, Family, Genus))
```
## ALDEx2 kw
```{r}
# set up comparison groups
conds <- ifelse(grepl("CHE",otu_table$SampleID), "Foragers",
ifelse(grepl("EUR", otu_table$SampleID), "American Industrialist",
ifelse(grepl("NEW00",otu_table$SampleID) | grepl("NEW10",otu_table$SampleID) | grepl("THA",otu_table$SampleID),
"Agriculturalists",
ifelse(grepl("NEW01",otu_table$SampleID) | grepl("NEW11",otu_table$SampleID), "Expats", "Recently Settled"))))
# transform data
set.seed(123)
transform_kw <- aldex.clr(otu_tax, conds, mc.samples=1000, denom="all")
# generate model
model_kw <- aldex.kw(transform_kw)
```
## prep gut microbiome taxa table
```{r}
phy_genus_gut <- tax_glom(gut_phyloseq, "Genus", NArm = FALSE);phy_genus_gut
otu_table_gut <- as.data.frame(as.matrix(phy_genus_gut@otu_table))
setDT(otu_table_gut, keep.rownames = TRUE)[]
colnames(otu_table_gut)[1] <- "SampleID"
otu_table2_gut <- otu_table_gut %>% data.frame %>% set_rownames(.$SampleID)
otu_table2_gut <- otu_table2_gut[,-1]
otu_table2_gut <- t(otu_table2_gut)
tax_table_gut <- as.data.frame(as.matrix(phy_genus_gut@tax_table))
otu_tax_gut <- merge(otu_table2_gut, tax_table_gut, by = 0, all = TRUE)
# change colnames to genus name
otu_tax_gut$Genus <- replace_na(otu_tax_gut$Genus, "_unclassified")
# and add higher taxonomic level to unclassified
otu_tax_gut$Phylum <- ifelse(is.na(otu_tax_gut$Phylum), otu_tax_gut$Kingdom, otu_tax_gut$Phylum)
otu_tax_gut$Class <- ifelse(is.na(otu_tax_gut$Class), otu_tax_gut$Phylum, otu_tax_gut$Class)
otu_tax_gut$Order <- ifelse(is.na(otu_tax_gut$Order), otu_tax_gut$Class, otu_tax_gut$Order)
otu_tax_gut$Family <- ifelse(is.na(otu_tax_gut$Family), otu_tax_gut$Order, otu_tax_gut$Family)
otu_tax_gut$Genus <- ifelse(otu_tax_gut$Genus == "_unclassified", paste(otu_tax_gut$Family, otu_tax_gut$Genus, sep = ""), otu_tax_gut$Genus)
# rename completely unclassified ASV
otu_tax_gut$Genus <- gsub("NA_unclassified", "Unclassified", otu_tax_gut$Genus)
# make genus rownames
rownames(otu_tax_gut) <- otu_tax_gut$Genus
# and remove taxonomic columns
otu_tax_gut <- subset(otu_tax_gut, select = -c(Row.names, Kingdom, Phylum, Class, Order, Family, Genus))
```
## aldex kw for gut microbiome
```{r}
# set up comparison groups
conds_gut <- ifelse(grepl("CHE",otu_table_gut$SampleID), "Foragers",
ifelse(grepl("EUR",otu_table_gut$SampleID), "American Industrialist",
ifelse(grepl("THA",otu_table_gut$SampleID), "Agriculturalists", "Recently Settled")))
# transform data
set.seed(100)
transform_kw_gut <- aldex.clr(otu_tax_gut, conds_gut, mc.samples=1000, denom="all")
# generate model
model_kw_gut <- aldex.kw(transform_kw_gut)
```
## only keep methods that are being analyzed
```{r}
model_kw <- subset(model_kw, select = c("kw.ep", "kw.eBH"))
model_kw_gut <- subset(model_kw_gut, select = c("kw.ep", "kw.eBH"))
```
## compare oral and gut
```{r}
model_kw_filt <- model_kw[model_kw$kw.eBH < 0.05,]
model_kw_gut_filt <- model_kw_gut[model_kw_gut$kw.eBH < 0.05,]
```
## save ALDEx2 results
```{r}
write.csv(model_kw, file = "output/model_kw.csv")
write.csv(model_kw_gut, file = "output/model_kw_gut.csv")
```
## prep for comparison with added covariates
```{r}
no_am_ps <- subset_samples(qiagen, Lifestyle != "Industrial")
no_am_genus <- tax_glom(no_am_ps, "Genus", NArm = FALSE)
otu_no_am <- as.data.frame(as.matrix(no_am_genus@otu_table))
setDT(otu_no_am, keep.rownames = TRUE)[]
colnames(otu_no_am)[1] <- "SampleID"
otu_no_am2 <- otu_no_am %>% data.frame %>% set_rownames(.$SampleID)
otu_no_am2 <- otu_no_am2[,-1]
otu_no_am2 <- t(otu_no_am2)
tax_no_am <- as.data.frame(as.matrix(no_am_genus@tax_table))
otu_tax_no_am <- merge(otu_no_am2, tax_no_am, by = 0, all = TRUE)
# change colnames to genus name
otu_tax_no_am$Genus <- replace_na(otu_tax_no_am$Genus, "_unclassified")
# add higher taxonomic level name to unclassified
otu_tax_no_am$Phylum <- ifelse(is.na(otu_tax_no_am$Phylum), otu_tax_no_am$Kingdom, otu_tax_no_am$Phylum)
otu_tax_no_am$Class <- ifelse(is.na(otu_tax_no_am$Class), otu_tax_no_am$Phylum, otu_tax_no_am$Class)
otu_tax_no_am$Order <- ifelse(is.na(otu_tax_no_am$Order), otu_tax_no_am$Class, otu_tax_no_am$Order)
otu_tax_no_am$Family <- ifelse(is.na(otu_tax_no_am$Family), otu_tax_no_am$Order, otu_tax_no_am$Family)
otu_tax_no_am$Genus <- ifelse(otu_tax_no_am$Genus == "_unclassified", paste(otu_tax_no_am$Family, otu_tax_no_am$Genus, sep = ""), otu_tax_no_am$Genus)
# rename completely unclassified ASV
otu_tax_no_am$Genus <- gsub("NA_unclassified", "Unclassified", otu_tax_no_am$Genus)
# set genus column as rownames
rownames(otu_tax_no_am) <- otu_tax_no_am$Genus
# remove taxonomic columns
otu_tax_no_am <- subset(otu_tax_no_am, select = -c(Row.names, Kingdom, Phylum, Class, Order, Family, Genus))
```
## compare with added covariates
```{r}
# all samples
## covariates
sex <- qiagen@sam_data$SEX
lifestyle <- qiagen@sam_data$Lifestyle
lifestyle <- gsub("Foragers", "1.Foragers", lifestyle)
lifestyle <- gsub("RecentlySettled", "2.Recently Settled", lifestyle)
lifestyle <- gsub("Agriculturalists", "3.Agriculturalists", lifestyle)
lifestyle <- gsub("Expats", "4.Expats", lifestyle)
lifestyle <- gsub("Industrial", "5.American Industrialist", lifestyle)
## just lifestyle
covariates_lifestyle <- data.frame(lifestyle)
mm_lifestyle <- model.matrix(~ lifestyle, covariates_lifestyle)
set.seed(100)
x.glm <- aldex.clr(otu_tax, mm_lifestyle, mc.samples=1000, denom="all", verbose=T)
glm.test <- aldex.glm(x.glm, mm_lifestyle)
## lifestyle and sex
covariates_sex <- data.frame(lifestyle, sex)
mm_sex <- model.matrix(~ lifestyle + sex, covariates_sex)
set.seed(100)
x.glm.sex <- aldex.clr(otu_tax, mm_sex, mc.samples=1000, denom="all", verbose=T)
glm.test.sex <- aldex.glm(x.glm.sex, mm_sex)
# no americans
## covariates
sex_noam <- no_am_ps@sam_data$SEX
age <- no_am_ps@sam_data$AGE
lifestyle_noam <- no_am_ps@sam_data$Lifestyle
lifestyle_noam <- gsub("Foragers", "1.Foragers", lifestyle_noam)
lifestyle_noam <- gsub("RecentlySettled", "2.Recently Settled", lifestyle_noam)
lifestyle_noam <- gsub("Agriculturalists", "3.Agriculturalists", lifestyle_noam)
lifestyle_noam <- gsub("Expats", "4.Expats", lifestyle_noam)
## no americans
covariates_noam <- data.frame(lifestyle_noam)
mm_noam <- model.matrix(~ lifestyle_noam, covariates_noam)
set.seed(100)
x.glm.noam <- aldex.clr(otu_tax_no_am, mm_noam, mc.samples=1000, denom="all", verbose=T)
glm.test.noam <- aldex.glm(x.glm.noam, mm_noam)
## all covariates
covariates_all <- data.frame(age, lifestyle_noam)
mm_all <- model.matrix(~ age + lifestyle_noam, covariates_all)
set.seed(100)
x.glm.all <- aldex.clr(otu_tax_no_am, mm_all, mc.samples=1000, denom="all", verbose=T)
glm.test.all <- aldex.glm(x.glm.all, mm_all)
```
## BH correction
```{r}
glm.test$`lifestyle2.Recently Settled:pval.BH` <- p.adjust(glm.test$`lifestyle2.Recently Settled:pval`, method = "BH", n = length(glm.test$`lifestyle2.Recently Settled:pval`))
glm.test$`lifestyle3.Agriculturalists:pval.BH` <- p.adjust(glm.test$`lifestyle3.Agriculturalists:pval`, method = "BH", n = length(glm.test$`lifestyle3.Agriculturalists:pval`))
glm.test$`lifestyle4.Expats:pval.BH` <- p.adjust(glm.test$`lifestyle4.Expats:pval`, method = "BH", n = length(glm.test$`lifestyle4.Expats:pval`))
glm.test$`lifestyle5.American Industrialist:pval.BH` <- p.adjust(glm.test$`lifestyle5.American Industrialist:pval`, method = "BH", n = length(glm.test$`lifestyle5.American Industrialist:pval`))
glm.test.sex$`lifestyle2.Recently Settled:pval.BH` <- p.adjust(glm.test.sex$`lifestyle2.Recently Settled:pval`, method = "BH", n = length(glm.test.sex$`lifestyle2.Recently Settled:pval`))
glm.test.sex$`lifestyle3.Agriculturalists:pval.BH` <- p.adjust(glm.test.sex$`lifestyle3.Agriculturalists:pval`, method = "BH", n = length(glm.test.sex$`lifestyle3.Agriculturalists:pval`))
glm.test.sex$`lifestyle4.Expats:pval.BH` <- p.adjust(glm.test.sex$`lifestyle4.Expats:pval`, method = "BH", n = length(glm.test.sex$`lifestyle4.Expats:pval`))
glm.test.sex$`lifestyle5.American Industrialist:pval.BH` <- p.adjust(glm.test.sex$`lifestyle5.American Industrialist:pval`, method = "BH", n = length(glm.test.sex$`lifestyle5.American Industrialist:pval`))
glm.test.noam$`lifestyle_noam2.Recently Settled:pval.BH` <- p.adjust(glm.test.noam$`lifestyle_noam2.Recently Settled:pval`, method = "BH", n = length(glm.test.noam$`lifestyle_noam2.Recently Settled:pval`))
glm.test.noam$`lifestyle_noam3.Agriculturalists:pval.BH` <- p.adjust(glm.test.noam$`lifestyle_noam3.Agriculturalists:pval`, method = "BH", n = length(glm.test.noam$`lifestyle_noam3.Agriculturalists:pval`))
glm.test.noam$`lifestyle_noam4.Expats:pval.BH` <- p.adjust(glm.test.noam$`lifestyle_noam4.Expats:pval`, method = "BH", n = length(glm.test.noam$`lifestyle_noam4.Expats:pval`))
glm.test.all$`lifestyle_noam2.Recently Settled:pval.BH` <- p.adjust(glm.test.all$`lifestyle_noam2.Recently Settled:pval`, method = "BH", n = length(glm.test.all$`lifestyle_noam2.Recently Settled:pval`))
glm.test.all$`lifestyle_noam3.Agriculturalists:pval.BH` <- p.adjust(glm.test.all$`lifestyle_noam3.Agriculturalists:pval`, method = "BH", n = length(glm.test.all$`lifestyle_noam3.Agriculturalists:pval`))
glm.test.all$`lifestyle_noam4.Expats:pval.BH` <- p.adjust(glm.test.all$`lifestyle_noam4.Expats:pval`, method = "BH", n = length(glm.test.all$`lifestyle_noam4.Expats:pval`))
```
## only keep pertinent columns
```{r}
glm.test.BH <- subset(glm.test, select = c("lifestyle2.Recently Settled:pval", "lifestyle3.Agriculturalists:pval", "lifestyle4.Expats:pval", "lifestyle5.American Industrialist:pval", "lifestyle2.Recently Settled:pval.BH", "lifestyle3.Agriculturalists:pval.BH", "lifestyle4.Expats:pval.BH", "lifestyle5.American Industrialist:pval.BH"))
glm.test.sex.BH <- subset(glm.test.sex, select = c("lifestyle2.Recently Settled:pval", "lifestyle3.Agriculturalists:pval", "lifestyle4.Expats:pval", "lifestyle5.American Industrialist:pval", "lifestyle2.Recently Settled:pval.BH", "lifestyle3.Agriculturalists:pval.BH", "lifestyle4.Expats:pval.BH", "lifestyle5.American Industrialist:pval.BH"))
glm.test.noam.BH <- subset(glm.test.noam, select = c("lifestyle_noam2.Recently Settled:pval", "lifestyle_noam3.Agriculturalists:pval", "lifestyle_noam4.Expats:pval", "lifestyle_noam2.Recently Settled:pval.BH", "lifestyle_noam3.Agriculturalists:pval.BH", "lifestyle_noam4.Expats:pval.BH"))
glm.test.all.BH <- subset(glm.test.all, select = c("lifestyle_noam2.Recently Settled:pval", "lifestyle_noam3.Agriculturalists:pval", "lifestyle_noam4.Expats:pval", "lifestyle_noam2.Recently Settled:pval.BH", "lifestyle_noam3.Agriculturalists:pval.BH", "lifestyle_noam4.Expats:pval.BH"))
```
## save ALDEx2 covariate comparison
```{r}
write.csv(glm.test.BH, file = "output/glm.test.BH.csv")
write.csv(glm.test.sex.BH, file = "output/glm.test.sex.BH.csv")
write.csv(glm.test.noam.BH, file = "output/glm.test.noam.BH.csv")
write.csv(glm.test.all.BH, file = "output/glm.test.all.BH.csv")
```