-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMP comparison methods code clean.R
321 lines (215 loc) · 9.7 KB
/
HMP comparison methods code clean.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
#HMP paper - Comparison Methods
#####Installing and loading packages#######
#Don't actually need all of these packages for this code, but I always install them anyway.
BiocManager::install(c("phyloseq", "DESeq2"))
install.packages('pacman')
pacman::p_load("devtools","here", "BiocManager", "MASS", "lattice", "lmtest", "plyr" , "pscl", "assertr", "tidyverse", "reshape2", "janitor", "dada2", "phyloseq", "qiime2R", "ggplot2", "DESeq2", "GUniFrac", "vegan", "LDM", "scales", "grid", "ggsci", "ggpubr", "viridis", "picante", "knitr", "gridExtra")
library(pacman)
pacman::p_loaded()
install.packages('rgr')
library(rgr)
install.packages(ape)
library(ape)
install.packages(dplyr)
library(dplyr)
install.packages(ggplot2)
library(ggplot2)
install.packages(gplots)
library(gplots)
install.packages(lme4)
library(lme4)
install.packages(phangorn)
library(phangorn)
install.packages(plotly)
library(plotly)
install.packages(tidyr)
library(tidyr)
install.packages(magrittr)
library(magrittr)
install.packages(reshape2)
library(reshape2)
install.packages(randomForest)
library(randomForest)
######Data Processing######
#loading the OTU file
OTU = read.csv("HMP_RELAbun.csv")
#loading the taxonomu file
tax = read.csv('HMP_Tax.csv')
#loading the simulated vars file
sim = read.csv('forShannah070821.csv')
#OTU file also has a bunch of meta vars, so need to make those into their own data set
# selecting meta variables
myvars <- c("PSN", "RSID", "VISITNO", "SEX", "RUN_CENTER", "HMP_BODY_SITE", "HMP_BODY_SUBSITE", "SRS_SAMPLE_ID")
meta <- OTU[myvars]
#need to filter the sim file to only include sim_exp = 1
sim_1 <- sim %>%
# keep obs if sim_exp = 1
filter(sim_exp == 1)
#now need to merge the meta file with the sim file
meta.sim <- merge(meta,sim_1,by="RSID")
#setting the row names to match in meta and otu files
row.names(OTU) = OTU$RSID
row.names(meta.sim) = meta.sim$RSID
#making a sim factor var
meta.sim$sim_fac <- factor(meta.sim$sim_smk, labels = c("No", "Yes"))
#making a random factor var
meta.sim$rv_fac <- factor(meta.sim$rv0, labels = c("No", "Yes"))
#now removing the non-otu vars from the otu file
myvars <- names(OTU) %in% c("PSN", "RSID", "VISITNO", "SEX", "RUN_CENTER", "HMP_BODY_SITE", "HMP_BODY_SUBSITE", "SRS_SAMPLE_ID")
OTU.clean <- OTU[!myvars]
#setting the row names in the tax file by the OTU number
row.names(tax) = tax$OTU
#removing all the OTUs that aren't in our OTU file
tax.clean = tax[row.names(tax) %in% colnames(OTU.clean),]
#separate the taxonomy into seperate columns
tax.clean = separate(tax.clean, taxonomy, into = c("Root","Domain", "Phylum", "Class", "Order", "Family", "Genus", "Species", "Strain"), sep=";")
#removing unnecessary columns
tax.clean = tax.clean[,-which(names(tax.clean) %in% c("OTU", "Root", "Species", "Strain"))]
#setting a random number seed in case we need it later
set.seed(2020)
#
OTU.physeq = otu_table(as.matrix(OTU.clean), taxa_are_rows=FALSE)
tax.physeq = tax_table(as.matrix(tax.clean))
meta.physeq = sample_data(meta.sim)
physeq.alpha = phyloseq(OTU.physeq, tax.physeq, meta.physeq)
########ALPHA DIVERSITY###############
#generate alpha diversity columns in phyloseq object
sample_data(physeq.alpha)$shannon.physeq <- estimate_richness(physeq.alpha, measures="Shannon")
sample_data(physeq.alpha)$InvSimpson.physeq <- estimate_richness(physeq.alpha, measures="InvSimpson")
plot_richness(physeq.alpha, measures="Shannon")
plot_richness(physeq.alpha, measures="InvSimpson")
#plotting shannon by sim_fac
plot_richness(physeq.alpha, "sim_fac", measures="Shannon")
#plotting shannon by rv_fac
plot_richness(physeq.alpha, "rv_fac", measures="Shannon")
# #calculating with vegan adds the column to meta file
meta.sim$shannon.vegan <- diversity(OTU.clean, "shannon")
meta.sim$invsimpson.vegan <- diversity(OTU.clean, index="invsimpson")
meta.sim <- as.data.frame(sample_data(physeq.alpha))
#finding the max, min, mean of alpha diversity
mean(meta.sim$shannon)
min(meta.sim$shannon)
max(meta.sim$shannon)
#running a linear regression with the alpha diversity calculated in VEGAN
lmshannon = lm(shannon.vegan ~ sim_smk + SEX, data = meta.sim)
summary(lmshannon)
#running a linear regression comparison with the random variable
lmshannon = lm(shannon.vegan ~ rv0 + SEX, data = meta.sim)
summary(lmshannon)
######Beta Diversity#######
#Here we're looking at all the obs, colored by simulation variable
bray_dist <- phyloseq::distance(physeq.alpha, method="bray")
bray_ord <- ordinate(physeq.alpha, method="MDS", distance=bray_dist)
p1_bray <- plot_ordination(physeq.alpha, bray_ord, color="sim_fac") + theme(aspect.ratio=1) +
theme_bw()+
scale_color_viridis(discrete=TRUE)+
labs(color="Simulated Variable")+
stat_ellipse(size=1)
p1_bray
#Plotting the random var
p2_bray <- plot_ordination(physeq.alpha, bray_ord, color="rv_fac") + theme(aspect.ratio=1) +
theme_bw()+
scale_color_viridis(discrete=TRUE)+
labs(color="Random Variable")+
stat_ellipse(size=1)
p2_bray
#PERMANOVA
#This tells us if the beta diversity is significantly different between groups
#Calculate bray curtis distance and save as a matrix
BC.dist=vegdist(OTU.clean, distance="bray")
#Run PERMANOVA on distances by the simulated variable.
adonis2(BC.dist ~ sim_smk + SEX , data = meta.sim, by='margin', permutations = 9999)
#Run PERMANOVA on distances by the random variable.
adonis2(BC.dist ~ rv0 + SEX , data = meta.sim, by='margin', permutations = 9999)
#Running the same with Aitchison distance.
#Looks like I have to CLR transform the OTU table first, and then run the Euclidian distance.
OTU.CLR <- clr(OTU.clean)
A.dist=vegdist(OTU.clean, method="euclidean")
#Run PERMANOVA on distances by the simulated variable.
adonis2(A.dist ~ sim_smk + SEX , data = meta.sim, by='margin', permutations = 9999)
#Run PERMANOVA on distances by the random variable.
adonis2(A.dist ~ rv0 + SEX , data = meta.sim, by='margin', permutations = 9999)
#####SIMPER#####
#This tells us which OTUs contributed the most variance to the differences in beta diversity between the two comparison groups.
#running simper by the simulated var
simper(OTU.clean, meta.sim$sim_smk, permutations=999)
#running simper by the random var
simper(OTU.clean, meta.sim$rv0, permutations=999)
#####RANDOM FOREST - Sim#####
# Make a dataframe of training data with OTUs as column and samples as rows
predictors <- data.frame(otu_table(physeq.alpha))
dim(predictors)
predictors$SEX <- as.factor(meta.sim$SEX)
# Make one column for our outcome/response variable
response <- as.factor(sample_data(physeq.alpha)$sim_smk)
# Combine them into 1 data frame
rf.data <- data.frame(response, predictors)
# Random Forest Model
rf.model <- randomForest(response~., data = rf.data, ntree = 100)
print(rf.model)
# What variables are stored in the output?
names(rf.model)
# Make a data frame with predictor names and their importance
imp <- importance(rf.model)
imp <- data.frame(predictors = rownames(imp), imp)
# Order the predictor levels by importance
imp.sort <- arrange(imp, desc(MeanDecreaseGini))
imp.sort$predictors <- factor(imp.sort$predictors, levels = imp.sort$predictors)
# Select the top 20 predictors
imp.20 <- imp.sort[1:20, ]
# here's the whole list
imp.869 <- imp.sort[1:869, ]
# I want to export this data file;
write.csv(imp.869, file = 'RandomForestOTUImportance.csv')
# ggplot
ggplot(imp.20, aes(x = predictors, y = MeanDecreaseGini)) +
geom_bar(stat = "identity", fill = "indianred") +
coord_flip() +
ggtitle("Most important OTUs for classifying Simulated Variable Groups")
ggplot(imp.869, aes(x = predictors, y = MeanDecreaseGini)) +
geom_bar(stat = "identity", fill = "indianred") +
coord_flip() +
ggtitle("Most important OTUs for classifying Simulated Variable Groups")
# What are those OTUs?
otunames <- imp.20$predictors
r <- rownames(tax_table(physeq.alpha)) %in% otunames
kable(tax_table(physeq.alpha)[r, ])
#####RANDOM FOREST - rand#####
# Make a dataframe of training data with OTUs as column and samples as rows
predictors <- data.frame(otu_table(physeq.alpha))
dim(predictors)
predictors$SEX <- as.factor(meta.sim$SEX)
# Make one column for our outcome/response variable
response <- as.factor(sample_data(physeq.alpha)$rv0)
# Combine them into 1 data frame
rf.data <- data.frame(response, predictors)
# Random Forest Model
rf.model <- randomForest(response~., data = rf.data, ntree = 100)
print(rf.model)
# What variables are stored in the output?
names(rf.model)
# Make a data frame with predictor names and their importance
imp <- importance(rf.model)
imp <- data.frame(predictors = rownames(imp), imp)
# Order the predictor levels by importance
imp.sort <- arrange(imp, desc(MeanDecreaseGini))
imp.sort$predictors <- factor(imp.sort$predictors, levels = imp.sort$predictors)
# Select the top 20 predictors
imp.r.20 <- imp.sort[1:20, ]
# here's the whole list
imp.r.869 <- imp.sort[1:869, ]
# I want to export this data file;
write.csv(imp.r.869, file = 'RandomForestOTUImportance_random.csv')
# ggplot
ggplot(imp.r.20, aes(x = predictors, y = MeanDecreaseGini)) +
geom_bar(stat = "identity", fill = "indianred") +
coord_flip() +
ggtitle("Most important OTUs for classifying Simulated Variable Groups")
ggplot(imp.r.869, aes(x = predictors, y = MeanDecreaseGini)) +
geom_bar(stat = "identity", fill = "indianred") +
coord_flip() +
ggtitle("Most important OTUs for classifying Simulated Variable Groups")
# What are those OTUs?
otunames <- imp.r.20$predictors
r <- rownames(tax_table(physeq.alpha)) %in% otunames
kable(tax_table(physeq.alpha)[r, ])