-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.Rhistory
More file actions
512 lines (512 loc) · 26.3 KB
/
Copy path.Rhistory
File metadata and controls
512 lines (512 loc) · 26.3 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
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
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
tibble::add_column(., timepoint = stringr::str_sub(.$SampleID, 12, 13)) %>%
dplyr::group_by(timepoint, OTU) %>%
dplyr::select(-SampleID)%>%
dplyr::summarise(Abundance=mean(Abundance)) %>%
tidyr::spread(timepoint, Abundance) %>%
tibble::column_to_rownames(var = 'OTU') %>%
data.frame()
gg_topi_meta_samples <- phyloseq(otu_table(meta_samples, taxa_are_rows = TRUE), tax_table(gg_topi), phy_tree(gg_topi))
otus <- data.frame(otu_table(gg_topi.genus.relab), check.names = FALSE) %>%
tibble::rownames_to_column('OTU') %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
left_join(data.frame(tax_table(gg_topi.genus.relab)[,-7], check.names = FALSE) %>% tibble::rownames_to_column('OTU')) %>%
tibble::add_column(., timepoint = stringr::str_sub(.$SampleID, 12, 13)) %>%
tibble::add_column(., mouseID = stringr::str_sub(.$SampleID, 8, 10))
otus %>%
# filter(mouseID==865) %>%
# head %>%
reshape2::dcast(OTU ~ timepoint, value.var = 'Abundance')
otus %>%
# filter(mouseID==865) %>%
# head %>%
reshape2::dcast(OTU ~ timepoint + mouseID, value.var = 'Abundance')
otus %>%
# filter(mouseID==865) %>%
# head %>%
reshape2::dcast(OTU + ...~ timepoint + mouseID, value.var = 'Abundance')
otus %>%
# filter(mouseID==865) %>%
# head %>%
reshape2::dcast(OTU ~ timepoint + mouseID + ... , value.var = 'Abundance')
otus %>%
# filter(mouseID==865) %>%
# head %>%
reshape2::dcast(OTU ~ timepoint + mouseID , value.var = 'Abundance')
otus <- data.frame(otu_table(gg_topi.genus.relab), check.names = FALSE) %>%
tibble::rownames_to_column('OTU') %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
tibble::add_column(., timepoint = stringr::str_sub(.$SampleID, 12, 13)) %>%
tibble::add_column(., mouseID = stringr::str_sub(.$SampleID, 8, 10))
otus %>%
# filter(mouseID==865) %>%
# head %>%
reshape2::dcast(OTU ~ timepoint + mouseID , value.var = 'Abundance')
otus
otus %>%
# filter(mouseID==865) %>%
# head %>%
reshape2::dcast(OTU ~ timepoint + mouseID , value.var = 'Abundance') %>%
left_join(data.frame(tax_table(gg_topi.genus.relab)[,-7], check.names = FALSE) %>% tibble::rownames_to_column('OTU'))
otus %>%
filter(mouseID==865) %>%
reshape2::dcast(OTU ~ timepoint + mouseID , value.var = 'Abundance') %>%
left_join(data.frame(tax_table(gg_topi.genus.relab)[,-7], check.names = FALSE) %>% tibble::rownames_to_column('OTU'))
otus %>%
filter(mouseID==865) %>%
reshape2::dcast(OTU ~ timepoint, value.var = 'Abundance') %>%
left_join(data.frame(tax_table(gg_topi.genus.relab)[,-7], check.names = FALSE) %>% tibble::rownames_to_column('OTU'))
libray
library(xlsx)
install.packages('xlsx')
library(xlsx)
library(xlsx)
library(xlsx)
library(xlsx)
library(xlsx)
Sys.getenv(c("JAVA_HOME", "LD_LIBRARY_PATH"))
install.packages('rJava')
library(xlsx)
remove.packages(c('rJava', 'xlsx'))
remove.packages(c('rJava', 'xlsx'))
library(xlsx)
gg_topi <- import_biom(BIOMfilename = '/shares/CIBIO-Storage/CM/mir/projects/fbeghini_16s/20180731_run186ms_segata/grandi/otus/otu_table_mc10_w_tax_rdp.biom',
treefilename = '/shares/CIBIO-Storage/CM/mir/projects/fbeghini_16s/20180731_run186ms_segata/grandi/otus/rep_set.tre',
refseqfilename = '/shares/CIBIO-Storage/CM/mir/projects/fbeghini_16s/20180731_run186ms_segata/grandi/otus/rep_set.fna',
refseqFunction = parse_taxonomy_default
)
knitr::opts_chunk$set(message = FALSE, warning = FALSE, paged.print=FALSE, echo=FALSE)
knitr::opts_chunk$set(fig.width=12, fig.height=8)
library(phyloseq)
library(magrittr)
library(tidyverse)
library(reshape2)
library(ggsignif)
library(DESeq2)
gg_topi <- import_biom(BIOMfilename = '/shares/CIBIO-Storage/CM/mir/projects/fbeghini_16s/20180731_run186ms_segata/grandi/otus/otu_table_mc10_w_tax_rdp.biom',
treefilename = '/shares/CIBIO-Storage/CM/mir/projects/fbeghini_16s/20180731_run186ms_segata/grandi/otus/rep_set.tre',
refseqfilename = '/shares/CIBIO-Storage/CM/mir/projects/fbeghini_16s/20180731_run186ms_segata/grandi/otus/rep_set.fna',
refseqFunction = parse_taxonomy_default
)
mappingfile <- read.delim('/shares/CIBIO-Storage/CM/mir/projects/fbeghini_16s/20180731_run186ms_segata/grandi/mappingfile', row.names = 1, check.names = FALSE)
mappingfile$Description %<>% str_replace_all(.,'_','-')
mappingfile$condizione <- factor(mappingfile$condizione, labels = c('Pentatome + Bifido', 'Pentatome + PBS', 'Empty + Bifido', 'Empty + PBS'))
sample_names(gg_topi) <- mappingfile[sample_names(gg_topi), 'Description']
rownames(mappingfile) <- mappingfile$Description
sample_data(gg_topi) <- mappingfile
sample_data(gg_topi)$id.topo <- factor(sample_data(gg_topi)$id.topo)
sample_data(gg_topi)$pentatome <- factor(stringr::str_split(sample_data(gg_topi)$condizione, ' \\+ ', simplify = TRUE)[,1])
sample_data(gg_topi)$bifido <- factor(stringr::str_split(sample_data(gg_topi)$condizione, ' \\+ ', simplify = TRUE)[,2])
sample_data(gg_topi)$cond_all <- factor(with(sample_data(gg_topi), paste(timepoint,real_vaccine, real_bifido, sep = '_')))
sample_data(gg_topi)$cond_vac <- factor(with(sample_data(gg_topi), paste(merge_timepoint,real_vaccine, sep = '_')))
sample_data(gg_topi)$cond_bif <- factor(with(sample_data(gg_topi), paste(merge_timepoint,real_bifido, sep = '_')))
sample_data(gg_topi)$vacc_bif <- strtoi(paste(ifelse(sample_data(gg_topi)$real_vaccine=='YES','1','0'),ifelse(sample_data(gg_topi)$real_bifido=='YES','1','0'), sep = ''), 2)
colnames(tax_table(gg_topi)) <- c("Domain", "Phylum", "Class", "Order", "Family", "Genus", "Species")
tax_table_filtered <- tax_table(gg_topi) %>%
as.data.frame %>%
mutate_all( .funs = function(x) stringr:::str_replace_all(x,"D_.__",""))
rownames(tax_table_filtered) <- rownames(tax_table(gg_topi))
tax_table(gg_topi) <- as.matrix(tax_table_filtered)
unass_otus <- tax_table(gg_topi) %>%
data.frame %>%
tibble::rownames_to_column('OTU') %>%
filter(Domain == 'Unassigned') %>%
select(OTU)
unknown_otus <- tax_table(gg_topi) %>%
data.frame %>%
tibble::rownames_to_column('OTU') %>%
filter(is.na(Genus)) %>%
select(OTU)
unknown_family <- tax_table(gg_topi) %>%
data.frame %>%
tibble::rownames_to_column('OTU') %>%
filter(is.na(Family)) %>%
select(OTU)
tax_table(gg_topi)[unass_otus$OTU, ] <- rep.int('Unassigned', 7)
tax_table(gg_topi)[unknown_otus$OTU, 'Genus'] <- 'Unknown'
tax_table(gg_topi)[unknown_family$OTU, 'Family'] <- 'Unknown'
gg_topi.relab <- transform_sample_counts(gg_topi, function(x) x/sum(x))
gg_topi.genus <- tax_glom(gg_topi,taxrank = "Genus")
gg_topi.genus.relab <- transform_sample_counts(gg_topi.genus, function(x) x/sum(x))
gg_topi.family <- tax_glom(gg_topi,taxrank = "Genus")
gg_topi.family.relab <- transform_sample_counts(gg_topi.family, function(x) x/sum(x))
ggtopi_subs <-subset_samples(gg_topi, timepoint %in% c('T3','T7'))
ggtopi_subs.genus <- tax_glom(ggtopi_subs,taxrank = "Genus")
ggtopi_subs.genus.relab <- transform_sample_counts(ggtopi_subs.genus, function(x) x/sum(x))
mmtx <- model.matrix(object = ~timepoint + timepoint:real_vaccine + timepoint:real_bifido + timepoint:real_vaccine:real_bifido, data = data.frame(ggtopi_subs@sam_data))
idx <- which(apply(mmtx, 2, function(x) all(x==0)))
mmtx <- mmtx[,-idx]
pds_37 <- phyloseq_to_deseq2(ggtopi_subs, design = ~ timepoint + timepoint:real_vaccine + timepoint:real_bifido + timepoint:real_vaccine:real_bifido, ignoreRank = TRUE)
dds_37 <- DESeq2::DESeq(pds_37, parallel = TRUE, quiet = TRUE, full = mmtx)
pds_genus <- phyloseq_to_deseq2(ggtopi_subs.genus, design = ~ timepoint + timepoint:real_vaccine + timepoint:real_bifido + timepoint:real_vaccine:real_bifido, ignoreRank = TRUE)
dds_genus <- DESeq2::DESeq(pds_genus, parallel = TRUE, quiet = TRUE, full = mmtx)
res37 <- DESeq2::results(dds_37, name = "timepointT7", alpha = 0.05)
res37.filt <- res37[!is.na(res37$padj),]
res37.filt <- res37.filt[res37.filt$padj<0.05,]
res37.filt
res37.filt %>% as.data.frame %>%
cbind(as.matrix(tax_table(ggtopi_subs)[rownames(res37.filt),])) %>%
ggplot(aes(log2FoldChange, interaction(Family, Genus))) +
geom_vline(xintercept = 0.0, color = "gray", size = 0.5) +
geom_point(size=3, aes(color = baseMean)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y",switch = "y",as.table = T)+
theme_bw() +
scale_y_discrete(position = "right") +
theme(axis.text = element_text(hjust = 0, vjust=0.5, size = 11),
strip.text = element_text(size = 9),
axis.title = element_text(size = 9),
axis.title.y = element_blank(),
strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 180)
)
t37_samples_idx <- grep('T7|T3', sample_names(ggtopi_subs), perl = TRUE)
otu_ab_t37 <- otu_table(ggtopi_subs)[rownames(res37.filt), t37_samples_idx] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13))
otu_ab_t37
otu_ab_t37 %>% bind_cols(data.frame(tax_table(ggtopi_filt)[otu_ab_t37$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
otu_ab_t37 %>% bind_cols(data.frame(tax_table(ggtopi_subs)[otu_ab_t37$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
res37.filt %>% as.data.frame %>%
cbind(as.matrix(tax_table(gg_topi.relab)[rownames(res37.filt),])) %>%
ggplot(aes(log2FoldChange, interaction(Family, Genus))) +
geom_vline(xintercept = 0.0, color = "gray", size = 0.5) +
geom_point(size=3, aes(color = baseMean)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y",switch = "y",as.table = T)+
theme_bw() +
scale_y_discrete(position = "right") +
theme(axis.text = element_text(hjust = 0, vjust=0.5, size = 11),
strip.text = element_text(size = 9),
axis.title = element_text(size = 9),
axis.title.y = element_blank(),
strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 180)
)
otu_ab_t37 <- otu_table(gg_topi.relab)[rownames(res37.filt), t37_samples_idx] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13))
otu_ab_t37 %>% bind_cols(data.frame(tax_table(gg_topi.relab)[otu_ab_t37$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
t37_samples_idx <- grep('T7|T3', sample_names(ggtopi_subs), perl = TRUE)
otu_ab_t37 <- otu_table(gg_topi.relab)[rownames(res37.filt), t37_samples_idx] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13))
otu_ab_t37
otu_ab_t37 %>% bind_cols(data.frame(tax_table(gg_topi.relab)[otu_ab_t37$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
otu_ab_t37
otu_ab_t37 %>% bind_cols(data.frame(tax_table(ggtopi_subs)[otu_ab_t37$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
t37_samples_idx <- grep('T7|T3', sample_names(ggtopi_subs), perl = TRUE)
ggtopi_subs.relab <- transform_sample_counts(ggtopi_subs, function(x) x/sum(x))
otu_ab_t37 <- otu_table(ggtopi_subs.relab)[rownames(res37.filt), t37_samples_idx] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13))
otu_ab_t37 %>% bind_cols(data.frame(tax_table(ggtopi_subs.relab)[otu_ab_t37$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
res37.filt %>% as.data.frame %>%
cbind(as.matrix(tax_table(ggtopi_subs)[rownames(res37.filt),]))
res37.filt %>% as.data.frame %>%
cbind(as.matrix(tax_table(ggtopi_subs)[rownames(res37.filt),])) %>% filter(Genus %in% c('Escherichia'))
res37.filt %>% as.data.frame %>%
cbind(as.matrix(tax_table(ggtopi_subs)[rownames(res37.filt),])) %>% filter(Genus %in% c('Escherichia-Shigella'))
res37 <- DESeq2::results(dds_genus, name = "timepointT7", alpha = 0.05)
res37.filt <- res37[!is.na(res37$padj),]
res37.filt <- res37.filt[res37.filt$padj<0.05,]
res37.filt %>% as.data.frame %>%
cbind(as.matrix(tax_table(ggtopi_subs.genus)[rownames(res37.filt),])) %>%
ggplot(aes(log2FoldChange, interaction(Family, Genus))) +
geom_vline(xintercept = 0.0, color = "gray", size = 0.5) +
geom_point(size=3, aes(color = baseMean)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y",switch = "y",as.table = T)+
theme_bw() +
scale_y_discrete(position = "right") +
theme(axis.text = element_text(hjust = 0, vjust=0.5, size = 11),
strip.text = element_text(size = 9),
axis.title = element_text(size = 9),
axis.title.y = element_blank(),
strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 180)
)
res37.filt %>% as.data.frame %>%
cbind(as.matrix(tax_table(ggtopi_subs.genus)[rownames(res37.filt),-7])) %>%
ggplot(aes(log2FoldChange, interaction(Family, Genus))) +
geom_vline(xintercept = 0.0, color = "gray", size = 0.5) +
geom_point(size=3, aes(color = baseMean)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y",switch = "y",as.table = T)+
theme_bw() +
scale_y_discrete(position = "right") +
theme(axis.text = element_text(hjust = 0, vjust=0.5, size = 11),
strip.text = element_text(size = 9),
axis.title = element_text(size = 9),
axis.title.y = element_blank(),
strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 180)
)
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13))
genus_ab_t %>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
gg_topi_no247 <- subset_taxa(gg_topi, !Family %in% c("Bacteroidales S24-7 group"))
ggtopi_subs <-subset_samples(gg_topi_no247, timepoint %in% c('T3','T7'))
ggtopi_subs.genus <- tax_glom(ggtopi_subs,taxrank = "Genus")
ggtopi_subs.genus.relab <- transform_sample_counts(ggtopi_subs.genus, function(x) x/sum(x))
mmtx <- model.matrix(object = ~timepoint + timepoint:real_vaccine + timepoint:real_bifido + timepoint:real_vaccine:real_bifido, data = data.frame(ggtopi_subs@sam_data))
idx <- which(apply(mmtx, 2, function(x) all(x==0)))
mmtx <- mmtx[,-idx]
pds_37 <- phyloseq_to_deseq2(ggtopi_subs, design = ~ timepoint + timepoint:real_vaccine + timepoint:real_bifido + timepoint:real_vaccine:real_bifido, ignoreRank = TRUE)
dds_37 <- DESeq2::DESeq(pds_37, parallel = TRUE, quiet = TRUE, full = mmtx)
pds_genus <- phyloseq_to_deseq2(ggtopi_subs.genus, design = ~ timepoint + timepoint:real_vaccine + timepoint:real_bifido + timepoint:real_vaccine:real_bifido, ignoreRank = TRUE)
dds_genus <- DESeq2::DESeq(pds_genus, parallel = TRUE, quiet = TRUE, full = mmtx)
res37 <- DESeq2::results(dds_genus, name = "timepointT7", alpha = 0.05)
res37.filt <- res37[!is.na(res37$padj),]
res37.filt <- res37.filt[res37.filt$padj<0.05,]
res37.filt
res37.filt %>% as.data.frame %>%
cbind(as.matrix(tax_table(ggtopi_subs.genus)[rownames(res37.filt),-7])) %>%
ggplot(aes(log2FoldChange, interaction(Family, Genus))) +
geom_vline(xintercept = 0.0, color = "gray", size = 0.5) +
geom_point(size=3, aes(color = baseMean)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y",switch = "y",as.table = T)+
theme_bw() +
scale_y_discrete(position = "right") +
theme(axis.text = element_text(hjust = 0, vjust=0.5, size = 11),
strip.text = element_text(size = 9),
axis.title = element_text(size = 9),
axis.title.y = element_blank(),
strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 180)
)
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13))
genus_ab_t %>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
genus_ab_t
genus_ab_t %>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')]))
order(genus_ab_t[['Genus']], genus_ab_t$logFC, decreasing=TRUE)
order(genus_ab_t[[Genus]], genus_ab_t$logFC, decreasing=TRUE)
order(genus_ab_t[[Genus]], genus_ab_t$Abundance, decreasing=TRUE)
order(genus_ab_t[['Genus']], genus_ab_t$Abundance, decreasing=TRUE)
order(genus_ab_t$Abundance, decreasing=TRUE)
order(genus_ab_t[['Genus']], genus_ab_t$Abundance, decreasing=TRUE)
genus_ab_t
genus_ab_t %<>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')]))
order(genus_ab_t[['Genus']], genus_ab_t$Abundance, decreasing=TRUE)
ord <- order(genus_ab_t[['Genus']], genus_ab_t$Abundance, decreasing=TRUE)
genus_ab_t$Genus <- factor(genus_ab_t$Genus, levels = unique(genus_ab_t$Genus[ord]))
genus_ab_t$Genus <- factor(genus_ab_t$Genus, levels = unique(genus_ab_t$Genus[ord]))
genus_ab_t$Genus
ggplot(genus_ab_t) +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0)) +
theme_bw()
ggplot(genus_ab_t) +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme_bw() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
genus_ab_t
genus_ab_t %>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=timepoint)) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme_bw() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
genus_ab_t
genus_ab_t %>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')]))
stringr::str_sub(.$SampleID,12, 13)
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition=stringr::str_sub(.$SampleID,4, 7))
genus_ab_t
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition=stringr::str_sub(.$SampleID,4, 6))
genus_ab_t
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=interaction(timepoint, condition)) +
genus_ab_t %>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')])) %>%
genus_ab_t %>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=interaction(timepoint, condition))) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme_bw() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
?mutate_if
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition=stringr::str_sub(.$SampleID,4, 6)) %>%
mutate_if(timepoint == 'T3', condition = '')
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition=stringr::str_sub(.$SampleID,4, 6)) %>%
mutate_if('timepoint' == 'T3', condition = '')
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition=stringr::str_sub(.$SampleID,4, 6)) %>%
mutate_if(.$timepoint == 'T3', condition = '')
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition=stringr::str_sub(.$SampleID,4, 6)) %>%
mutate_if(.$timepoint == 'T3', .$condition = '')
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition=stringr::str_sub(.$SampleID,4, 6)) %>%
mutate_if(equals(timepoint, 'T3'), condition = '')
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition=stringr::str_sub(.$SampleID,4, 6)) %>%
mutate_if(equals(.$timepoint, 'T3'), condition = '')
genus_ab_t <- otu_table(ggtopi_subs.genus.relab)[rownames(res37.filt), ] %>%
data.frame %>%
tibble::rownames_to_column("OTU") %>%
tidyr::gather_("SampleID", "Abundance", setdiff(colnames(.), "OTU")) %>%
mutate(timepoint=stringr::str_sub(.$SampleID,12, 13)) %>%
mutate(condition= if_else(timepoint=='T3','',stringr::str_sub(.$SampleID,4, 6)))
genus_ab_t
genus_ab_t %>% bind_cols(data.frame(tax_table(ggtopi_subs.genus.relab)[genus_ab_t$OTU,c('Phylum','Family','Genus')])) %>%
ggplot() +
geom_boxplot(aes(interaction(Family, Genus), Abundance, fill=interaction(timepoint, condition))) +
facet_grid(Phylum ~ ., scales = "free", space = "free_y", as.table= T)+
coord_flip() +
theme_bw() +
theme(strip.background = element_rect(colour = "transparent", fill = "transparent"),
strip.text.y = element_text(angle = 0))
ggplot(x) +
geom_boxplot(aes(timepoint, value)) +
facet_grid(variable~condizione,scales = 'free_y')
knitr::opts_chunk$set(message = FALSE, warning = FALSE, paged.print=FALSE, echo=FALSE)
knitr::opts_chunk$set(fig.width=12, fig.height=8)
library(phyloseq)
library(magrittr)
library(tidyverse)
library(reshape2)
library(ggsignif)
library(DESeq2)
ggplot(x) +
geom_boxplot(aes(timepoint, value)) +
facet_grid(variable~condizione,scales = 'free_y')
x <- melt(alpha_div, id.vars = c('Description','condizione','timepoint'), measure.vars = c('Observed', 'se.chao1', 'Simpson', 'Shannon'))
alpha_div <- estimate_richness(gg_topi)
alpha_div %<>% bind_cols(data.frame(sample_data(gg_topi)))
x <- melt(alpha_div, id.vars = c('Description','condizione','timepoint'), measure.vars = c('Observed', 'se.chao1', 'Simpson', 'Shannon'))
x$value <- as.numeric(x$value)
ggplot(x) +
geom_boxplot(aes(timepoint, value)) +
facet_grid(variable~condizione,scales = 'free_y')
filt_alpha <- alpha_div %>%
filter(timepoint %in% c('T2','T3')) %>%
select('Description','condizione','timepoint','Observed', 'se.chao1', 'Simpson', 'Shannon') %>%
mutate(t=as.numeric(stringr::str_sub(timepoint,2,2)))
ttest_res <- data.frame()
for(measure in c('Observed', 'se.chao1', 'Simpson', 'Shannon')){
ttest_res %<>% bind_rows(data.frame(Measure=measure, pvalue=with(filt_alpha, t.test(as.formula(paste(measure, 't', sep = '~'))))$p.value))
}
filt_alpha %>%
melt(id.vars = c('Description','condizione','timepoint'), measure.vars = c('Observed', 'se.chao1', 'Simpson', 'Shannon')) %>%
ggplot() +
geom_boxplot(aes(timepoint, value)) +
facet_grid(variable~.,scales = 'free_y')
knitr::kable(ttest_res)
source('R/loadQiimeData.R')
# Chunk 1: setup
knitr::opts_chunk$set(message = FALSE, warning = FALSE, paged.print=FALSE, echo=FALSE)
knitr::opts_chunk$set(fig.width=12, fig.height=8)
library(phyloseq)
library(magrittr)
library(tidyverse)
library(reshape2)
library(ggsignif)
library(DESeq2)
source('R/loadQiimeData.R')
gg_topi <- loadQiimeData()
gg_topi
devtools::create('ggtopi')