-
Notifications
You must be signed in to change notification settings - Fork 1
/
BRC_growth_rate_microbiome.RMD
1883 lines (1696 loc) · 74.6 KB
/
BRC_growth_rate_microbiome.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: "How fast does biological rock crust grow?"
subtitle: "Microbial community analysis"
date: "`r format(Sys.time(), '%d %B, %Y')`"
bibliography: references.bib
link-citations: yes
always_allow_html: true
output:
rmarkdown::github_document:
toc: true
toc_depth: 5
df_print: "kable"
keep_html: TRUE
---
```{r libraries, include=F}
library(extrafont) # Tools for using fonts, CRAN v0.17
library(svglite) # An 'SVG' Graphics Device, CRAN v1.2.3.2
library(ragg) # Graphic Devices Based on AGG, CRAN v0.3.1
library(tidyverse) # Easily Install and Load the 'Tidyverse', CRAN v1.3.0
library(magrittr) # A Forward-Pipe Operator for R, CRAN v1.5
library(scales) # Scale Functions for Visualization, CRAN v1.1.1
library(ggpomological) # Pomological plot themes for ggplot2, [github::gadenbuie/ggpomological] v0.1.2
library(cowplot) # Streamlined Plot Theme and Plot Annotations for 'ggplot2', CRAN v1.1.0
library(patchwork) # The Composer of Plots, CRAN v1.0.1 # The Composer of Plots, CRAN v1.0.1
library(kableExtra) # Construct Complex Table with 'kable' and Pipe Syntax, CRAN v1.2.1
library(see) # Visualisation Toolbox for 'easystats' and Extra Geoms, Themes and Color Palettes for 'ggplot2', CRAN v0.6.0
library(phyloseq) # Handling and analysis of high-throughput microbiome census data, Bioconductor v1.32.0
library(speedyseq) # psmelt
library(vegan) # Community Ecology Package, CRAN v2.5-6
library(BiodiversityR) # Package for Community Ecology and Suitability Analysis, CRAN v2.12-1
# library(agricolae)
library(car) # Companion to Applied Regression, CRAN v3.0-9
library(userfriendlyscience) # Quantitative Analysis Made Accessible, CRAN v0.7.2
library(emmeans) # Estimated Marginal Means, aka Least-Squares Means, CRAN v1.5.1
library(multcomp) # Simultaneous Inference in General Parametric Models, CRAN v1.4-14
library(ALDEx2) # Analysis Of Differential Abundance Taking Sample Variation Into Account, Bioconductor v1.20.0
library(corncob) # Count Regression for Correlated Observations with the Beta-Binomial, [github::bryandmartin/corncob] v0.1.0
library(ggrepel) # Automatically Position Non-Overlapping Text Labels with 'ggplot2', CRAN v0.8.2
```
```{r style settings, include=F}
options(width = 90, knitr.table.format = "html")
knitr::opts_chunk$set(
warning = FALSE,
message = FALSE,
cache = TRUE,
dev = c("svglite", "png"),
dev.args = list(svglite = list(bg = 'white'), png = list(bg = 'white')),
dpi = 300,
# fig.width = 12,
# fig.height = 8,
cache.path = "BRC_growth_rate_cache/",
fig.path = "BRC_growth_rate_figures/"
)
f_name <- "DejaVu Sans" #sub("\\s//", "", f_name)
f_size <- 12
font_import(pattern = "DejaVuSans\\.", prompt = FALSE)
loadfonts() # registers fonts
theme_set(theme_bw(base_size = f_size,
base_family = f_name
))
```
```{r functions, include=FALSE}
PlotLibDist <- function(Ps_obj, x = "Replicate", fill = "Rock.type", facet = "Location") {
Library.size <- rowSums(otu_table(Ps_obj))
ggplot(sample_data(Ps_obj),
aes(x = !!sym(x), y = Library.size, fill = !!sym(fill))) +
geom_bar(stat = "identity",
position = "dodge",
color = "black") +
scale_y_log10(
breaks = trans_breaks("log10", function(x)
10 ^ x),
labels = trans_format("log10", math_format(10 ^ .x))
) +
background_grid(major = "xy", minor = "none") +
scale_fill_pomological() +
facet_grid(get(facet) ~ .)
}
PlotReadHist <- function(OTUmat, b.width = 10) {
OTUmat %>%
t() %>%
as.tibble() %>%
gather(key = sample, value = abundance) %>%
ggplot(aes(abundance)) +
# geom_histogram(binwidth = 1000) +
geom_freqpoly(binwidth = b.width) +
scale_y_log10()
}
GMPR <- function (comm,
intersect.no = 4,
ct.min = 4) {
require(matrixStats)
# Computes the GMPR size factor
#
# Args:
# comm: a matrix of counts, row - features (OTUs, genes, etc) , column - sample
# intersect.no: the minimum number of shared features between sample pair, where the ratio is calculated
# ct.min: the minimum number of counts required to calculate ratios (Empirical study found ct.min=4 is suitable)
#
# Returns:
# a list that contains:
# gmpr: the GMPR size factors for all samples; Samples with distinct sets of features will be output as NA.
# nss: number of samples with significant sharing (> intersect.no) including itself
# mask counts < ct.min
comm[comm < ct.min] <- 0
if (is.null(colnames(comm))) {
colnames(comm) <- paste0('S', 1:ncol(comm))
}
cat('Begin GMPR size factor calculation ...\n')
comm.no <- numeric(ncol(comm))
gmpr <- sapply(1:ncol(comm), function(i) {
if (i %% 50 == 0) {
cat(i, '\n')
}
x <- comm[, i]
# Compute the pairwise ratio
pr <- x / comm
# Handling of the NA, NaN, Inf
pr[is.nan(pr) | !is.finite(pr) | pr == 0] <- NA
# Counting the number of non-NA, NaN, Inf
incl.no <- colSums(!is.na(pr))
# Calculate the median of PR
pr.median <- colMedians(pr, na.rm = TRUE)
# Record the number of samples used for calculating the GMPR
comm.no[i] <<- sum(incl.no >= intersect.no)
# Geometric mean of PR median
if (comm.no[i] > 1) {
return(exp(mean(log(pr.median[incl.no >= intersect.no]))))
} else {
return(NA)
}
})
if (sum(is.na(gmpr))) {
warning(
paste0(
'The following samples\n ',
paste(colnames(comm)[is.na(gmpr)], collapse = '\n'),
'\ndo not share at least ',
intersect.no,
' common taxa with the rest samples! ',
'For these samples, their size factors are set to be NA! \n',
'You may consider removing these samples since they are potentially outliers or negative controls!\n',
'You may also consider decreasing the minimum number of intersecting taxa and rerun the procedure!\n'
)
)
}
cat('Completed!\n')
cat(
'Please watch for the samples with limited sharing with other samples based on NSS! They may be outliers! \n'
)
names(gmpr) <- names(comm.no) <- colnames(comm)
return(list(gmpr = gmpr, nss = comm.no))
}
PlotLmResid <- function(lm.df, which = c(1:6), mfrow = c(3, 2)){
require(grid)
if (length(levels(as.factor(lm.df$.fitted))) < 10) {# if number of unique x values is <10 just draw a line through the means
smoother <- stat_summary(fun = mean, colour = "red", geom = "line")
} else smoother <- stat_smooth(method = "loess", geom = "smooth", se = FALSE, colour = "firebrick", size = 1)
# residuals vs fitted
g1 <- ggplot(lm.df, aes(.fitted, .resid)) +
geom_point2() +
smoother +
geom_hline(yintercept = 0, linetype = 2, size = .2) +
scale_x_continuous("Fitted Values") +
scale_y_continuous("Residual") +
labs(title = "Residuals vs Fitted")
# normal qq
a <- quantile(lm.df$.stdresid, c(0.25, 0.75), na.rm = TRUE)
b <- qnorm(c(0.25, 0.75))
slope <- diff(a)/diff(b)
int <- a[1] - slope * b[1]
g2 <- ggplot(lm.df, aes(sample = .stdresid)) +
stat_qq() +
geom_abline(slope = slope, intercept = int, colour = "firebrick", size = 1) +
scale_x_continuous("Theoretical Quantiles") +
scale_y_continuous("Standardized Quantiles") +
labs(title = "Normal Q-Q")
# scale-location
g3 <- ggplot(lm.df, aes(.fitted, sqrt(abs(.stdresid)))) +
geom_point2() +
smoother +
scale_x_continuous("Fitted Values") +
scale_y_continuous("Root of Standardized Residuals") +
labs(title = "Scale-Location")
# residuals vs leverage
g4 <- ggplot(lm.df, aes(factors, .stdresid)) +
geom_point2() +
smoother +
geom_hline(yintercept = 0, linetype = 2, size = .2) +
scale_x_continuous("Factor Level Combinations") +
scale_y_continuous("Standardized Residuals") +
labs(title = "Residuals vs Factor Levels")
# # cook's distance
# g5 <- ggplot(lm.df, aes(rows, .cooksd, ymin=0, ymax=.cooksd)) +
# geom_point2() + geom_linerange() +
# scale_x_continuous("Observation Number") +
# scale_y_continuous("Cook's distance") +
# labs(title="Cook's Distance")
# cooksd vs leverage
g5 <- ggplot(lm.df, aes(factors, .cooksd)) +
geom_point2() +
smoother +
scale_x_continuous("Factor Level Combinations") +
scale_y_continuous("Cook's distance") +
labs(title = "Cook's dist vs Leverage")
bw <- diff(range(lm.df$.resid)) / (2 * IQR(lm.df$.resid) / length(lm.df$.resid) ^ (1/3))
sshist <- function(x){ # optimise bins
# 2006 Author Hideaki Shimazaki
# Department of Physics, Kyoto University
# shimazaki at ton.scphys.kyoto-u.ac.jp
N <- 2 : 100
C <- numeric(length(N))
D <- C
for (i in 1:length(N)) {
D[i] <- diff(range(x)) / N[i]
edges = seq(min(x), max(x), length=N[i])
hp <- hist(x, breaks = edges, plot=FALSE)
ki <- hp$counts
k <- mean(ki)
v <- sum((ki-k) ^ 2) / N[i]
C[i] <- (2 * k-v) / D[i] ^ 2 #Cost Function
}
idx <- which.min(C)
optD <- D[idx]
bins <- seq(min(x), max(x), length=N[idx])
# h = hist(x, breaks = bins)
# rug(x)
return(bins)
}
bins <- sshist(lm.df$.resid)
g6 <- ggplot(lm.df, aes(.resid)) +
geom_histogram(breaks = bins)
plots <- list(g1, g2, g3, g4, g5, g6)
# making the plots
grid.newpage()
if (prod(mfrow) > 1) {
mypos <- expand.grid(1:mfrow[1], 1:mfrow[2])
mypos <- mypos[with(mypos, order(Var1)), ]
pushViewport(viewport(layout = grid.layout(mfrow[1], mfrow[2])))
formatter <- function(.){}
} else {
mypos <- data.frame(matrix(1, length(which), 2))
pushViewport(viewport(layout = grid.layout(1, 1)))
formatter <- function(.) {
.dontcare <- readline("Hit <Return> to see next plot: ")
grid.newpage()
}
}
j <- 1
for (i in which) {
formatter()
print(plots[[i]], vp = viewport(layout.pos.row = mypos[j, ][1], layout.pos.col = mypos[j, ][2]))
j <- j + 1
}
}
TestAlphaV3 <- function(data2test = Richness_Diversity_long_sub,
response_name = "Estimate",
factor_names = c("Location", "Rock.type"),
boxcox.trans = FALSE) {
require(dplyr)
mod_lm <-
aov(as.formula(paste(
response_name,
paste(factor_names[1], factor_names[2], sep = " * "),
sep = " ~ "
)), data2test)
# }
if (boxcox.trans) {
# employ boxcox transformation then recalculate model
print("Performing Box-Cox transformation of the data")
lambdas <- boxcox(as.formula(paste(
response_name,
paste(factor_names[1], factor_names[2], sep = " * "),
sep = " ~ "
)),
data = data2test,
lambda = seq(0, 1.0, 0.01))
print(range(lambdas$x[lambdas$y > max(lambdas$y) - qchisq(0.95, 1) /
2]))
print(l.max <- lambdas$x[which.max(lambdas$y)])
if (l.max == 0)
l.max <- 1
data2test$Estimate.box <-
(data2test$Estimate ^ l.max - 1) / l.max
mod_lm <-
aov(as.formula(paste(
"Estimate.box",
paste(factor_names[1], factor_names[2], sep = " * "),
sep = " ~ "
)), data2test)
}
if (exists("mod_lm")) {
print(mod_lm)
mod_df <- fortify(mod_lm)
factor.combinations <-
as.numeric(factor(paste(mod_df[, factor_names[1]], mod_df[, factor_names[2]]),
levels = unique(as.character(
paste(mod_df[, factor_names[1]], mod_df[, factor_names[2]])
)))) # needed for "residuals vs leverage
mod_df <-
cbind(mod_df,
rows = 1:nrow(mod_df),
factors = factor.combinations)
PlotLmResid(mod_df)
if ((data2test %>% group_by(!!sym(factor_names[1]),!! sym(factor_names[2])) %>% dplyr::count() %>% pull(n) %>% n_distinct() == 1)) {
print("Equal group sizes - showing SS type I")
print(summary(mod_lm)) # display Type I ANOVA table
} else {
print("Unequal group sizes - showing SS type III")
options(contrasts = c("contr.sum", "contr.poly"))
print(Anova(mod_lm, type = "III")) # type III SS
}
print(model.tables(mod_lm, "means"), digits = 3) # Show the means
return(mod_lm)
}
}
PairwiseAdonis <- function(x, factors, sim.function = "vegdist", sim.method = "horn",
p.adjust.m = "BH", reduce = NULL)
{
# Taken from: https://github.com/pmartinezarbizu/pairwiseAdonis
co <- combn(unique(as.character(factors)), 2)
pairs <- c()
total.DF <- c()
F.Model <- c()
R2 <- c()
p.value <- c()
for (elem in 1:ncol(co)) {
if (sim.function == "daisy") {
x1 = cluster::daisy(x[factors %in% c(co[1, elem], co[2, elem]),
], metric = sim.method)
}
else {
x1 = vegdist(x[factors %in% c(co[1, elem], co[2,
elem]), ], method = sim.method)
}
ad <- adonis(x1 ~ factors[factors %in% c(co[1, elem],
co[2, elem])], permutations = 999)
pairs <- c(pairs, paste(co[1, elem], "-", co[2, elem]))
total.DF <- c(total.DF, ad$aov.tab["Total", 1])
F.Model <- c(F.Model, ad$aov.tab[1, 4])
R2 <- c(R2, ad$aov.tab[1, 5])
p.value <- c(p.value, ad$aov.tab[1, 6])
}
p.adjusted <- p.adjust(p.value, method = p.adjust.m)
sig = c(rep("", length(p.adjusted)))
sig[p.adjusted <= 0.05] <- "."
sig[p.adjusted <= 0.01] <- "*"
sig[p.adjusted <= 0.001] <- "**"
sig[p.adjusted <= 1e-04] <- "***"
pairw.res <- data.frame(pairs, total.DF, F.Model, R2, p.value,
p.adjusted, sig)
if (!is.null(reduce)) {
pairw.res <- subset(pairw.res, grepl(reduce, pairs))
pairw.res$p.adjusted <- p.adjust(pairw.res$p.value, method = p.adjust.m)
sig = c(rep("", length(pairw.res$p.adjusted)))
sig[pairw.res$p.adjusted <= 0.05] <- "."
sig[pairw.res$p.adjusted <= 0.01] <- "*"
sig[pairw.res$p.adjusted <= 0.001] <- "**"
sig[pairw.res$p.adjusted <= 1e-04] <- "***"
pairw.res <- data.frame(pairw.res[, 1:5], sig)
}
class(pairw.res) <- c("pwadonis", "data.frame")
return(pairw.res)
}
STAMPR2 <- function(physeq_obj,
tax_rank = "Phylum",
vars2test = c("Location", "Rock.type"),
threshold = 0.005,
outputfile = "STAMPR_output") {
# run a STAMP-like analysis: compare relative abundance differences using two-way analysis, then run a post-hoc test and correct for multiple comparison. This is an extended and updated version for Parks, D. H., Tyson, G. W., Hugenholtz, P., and Beiko, R. G. (2014). STAMP: statistical analysis of taxonomic and functional profiles. Bioinformatics 30, 3123–3124. doi:10.1093/bioinformatics/btu494.)
# The test is meant as a follow-up test for a pair-wise ADONIS and use the significant test pairs
# The global analysis is done using the Aligned Rank Transformed ANOVA test. This is a "modern" non-parametric test that should be more robust than the Kruskal-Wallis Rank Sum Test or the Scheirer Ray Hare.
# The post-hoc test is an Estimated marginal means test on each significant pair.
# If two variables are provided only the interactions are computed (for the main effects run it separately on each variable)
# TODO:
require("emmeans") # Estimated marginal means (Least-squares means))
require("ARTool") # Aligned Rank Transform
require("multcomp") # Simultaneous Inference in General Parametric Models
if (length(vars2test) != 1 &
length(vars2test) != 2)
{stop('This function only suppurts 1 or two independent variables')}
test_expression <-
as.formula(paste("Abundance", paste(vars2test, collapse = " * "), sep = " ~ "))
physeq_rel <-
transform_sample_counts(physeq_obj, function(x)
x / sum(x)) # convert to relative abundance
physeq_glom_rel <- tax_glom(physeq_rel,
tax_rank,
NArm = TRUE) # agglomerate taxa using taxonomy
# Test only abundant taxa: group dataframe by rank, calculate total rel. abundance per phylum and keep only taxa above threshold
physeq_glom_rel %>%
psmelt(.) %>%
group_by(!!sym(tax_rank)) %>%
summarise(tot_abundance = sum(Abundance)) %>%
filter(tot_abundance >= threshold) %>%
pull(1) %>%
as.character() ->
Taxa2test
physeq_glom_rel_abund <- # remove rare taxa
prune_taxa(tax_table(physeq_glom_rel)[, tax_rank] %in% Taxa2test, physeq_glom_rel)
if (length(vars2test) == 1) {# One-way test
cmb <- combn(unique(as.character(get_variable(physeq_glom_rel_abund, vars2test))), 2)
as_tibble(matrix( # prepare results table (P and Eta)
NA,
nrow = ntaxa(physeq_glom_rel_abund),
ncol = ncol(cmb) + 2,
), .name_repair = "minimal") %>%
bind_cols(Phylum = as(tax_table(physeq_glom_rel_abund)[, tax_rank], "vector"), .) %>%
set_names(c(tax_rank,
paste(vars2test, "- P"),
paste(vars2test, "- EtaSq"),
seq(ncol(cmb)))) %>%
mutate_if(., is.logical, as.numeric) ->
taxa_test_results
as_tibble(matrix( # prepare estimates table
NA,
nrow = ntaxa(physeq_glom_rel_abund),
ncol = (ncol(cmb) * 5)
),
.name_repair = "minimal") %>%
bind_cols(Phylum = as(tax_table(physeq_glom_rel_abund)[, tax_rank], "vector"), .) %>%
mutate_if(., is.logical, as.numeric) ->
taxa_test_estimates
colnames(taxa_test_estimates) <- # cannot use set_names with non-unique names
c(tax_rank, c(rbind(
cmb, matrix(rep(
c("Estimate diff.", "low CI", "high CI"), ncol(cmb)
), ncol = ncol(cmb))
))) # this is ugly but it works well, basically c() flattens a matrix
for (phy_id in seq(ntaxa(physeq_glom_rel_abund))) {
data2test <-
bind_cols(Abundance = as.numeric(otu_table(physeq_glom_rel_abund)[, phy_id] * 100),
as(sample_data(physeq_glom_rel_abund), "data.frame"))
print(tax_table(physeq_glom_rel_abund)[phy_id, tax_rank])
art_mod <- art(test_expression, data = data2test)
print(mod_summary <- anova(art_mod))
taxa_test_results[phy_id, c(2)] <- mod_summary$`Pr(>F)` # p values
mod_summary %>%
transmute(`Part Eta Sq` = `Sum Sq`/(`Sum Sq` + `Sum Sq.res`)) ->
taxa_test_results[phy_id, c(3)] # EtaSq (effect size)
art_mod_lm <- artlm(art_mod, vars2test) # build a linear model for ART data
# Compute estimated marginal means (EMMs)
(emm_mod <- emmeans(art_mod_lm, as.formula(paste("", vars2test, sep = " ~ ")), weights = "equal"))
# Contrasts, pairwise comparisons, tests, and confidence intervals.
(emm_mod_cons <- contrast(emm_mod, method = "pairwise", adjust = "tukey"))
# (emm_mod_CI <- confint(emm_mod_cons, adjust = "fdr", level = 0.95, type = "response"))
# (emm_mod_CLD <- multcomp::cld(emm_mod, alpha = 0.05, Letters = letters, adjust = "tukey"))
print("Note that the estimates aren’t as in a linear model because they are on the scale of ranks and not the data, but the p-values are useful")
# assign pairwise results to table
colnames(taxa_test_results)[seq(4, 3 + ncol(cmb))] <- summary(emm_mod_cons)$contrast
taxa_test_results[phy_id, seq(4, 3 + ncol(cmb))] <-
as.list(summary(emm_mod_cons)$p.value)
# assign estimates to table
mod_lm <- lm(test_expression, data = data2test) # because we want the real means not the rank means!
emm_mod_lm <- emmeans(mod_lm, as.formula(paste("", vars2test, sep = " ~ ")), weights = "equal")
emm_mod_lm_cons <- contrast(emm_mod, method = "pairwise", adjust = "tukey")
emm_mod_lm_CI <- confint(emm_mod_lm_cons, adjust = "fdr", level = 0.95, type = "response")
taxa_test_estimates[phy_id, c(2,3)] <- as.list(summary(emm_mod_lm)$emmean)
taxa_test_estimates[phy_id, 4] <- emm_mod_lm_CI$estimate
taxa_test_estimates[phy_id, 5] <- as.list(emm_mod_lm_CI$lower.CL)
taxa_test_estimates[phy_id, 6] <- as.list(emm_mod_lm_CI$upper.CL)
# # assign pairwise means and CI to table
# # I use wilcox.test here to get estimates on the moments and CI of the data not the ranks and p-value
# for (pair in seq(length(sig_pairs[[1]]))) {
# pair2test <- unlist(str_split(sig_pairs[[1]][pair], " - "))
# possibleError <- tryCatch(
# wilcox_mod <- wilcox.test(
# test_expression,
# data = data2test,
# subset = do.call("%in%", list(get(vars2test), pair2test)), # grab only pair2test from vars2test
# conf.int = TRUE,
# exact = FALSE
# ),
# error = function(e)
# e
# ) # AKA Mann Whitney
# if (inherits(possibleError, 'error')) {
# print(possibleError)
# taxa_test_results[phy_id, pair + 3] <- NA
# } else {
# print(wilcox_mod)
# taxa_test_estimates[phy_id, (pair - 1) * 5 + 2] <-
# mean(data2test[unlist(data2test[vars2test]) %in% pair2test[1],]$Abundance)
# taxa_test_estimates[phy_id, (pair - 1) * 5 + 3] <-
# mean(data2test[unlist(data2test[vars2test]) %in% pair2test[2],]$Abundance)
# taxa_test_estimates[phy_id, (pair - 1) * 5 + 4] <-
# wilcox_mod$estimate
# taxa_test_estimates[phy_id, (pair - 1) * 5 + c(5, 6)] <-
# as.list(wilcox_mod$conf.int[c(1, 2)])
# }
# }
}
} else {# Two-way test
cmb <- combn(unique(c(as.character(get_variable(physeq_glom_rel_abund, vars2test[1])), as.character(get_variable(physeq_glom_rel_abund, vars2test[2])))), 2) # just for setting the right number of columns
sample_data(physeq_glom_rel_abund)$Vars_combination <- with(sample_data(physeq_glom_rel_abund), get(vars2test[1]):get(vars2test[2]))
as_tibble(matrix(
NA,
nrow = ntaxa(physeq_glom_rel_abund),
ncol = sum(ncol(cmb)) + 6, # sig_pairs is taken from pairwise adonis. In addition the factors are tested alone.
),
.name_repair = "minimal") %>%
bind_cols(Phylum = as(tax_table(physeq_glom_rel_abund)[, tax_rank], "vector"), .) %>%
set_names(c(tax_rank,
paste(vars2test[1], "- P"),
paste(vars2test[1], "- EtaSq"),
paste(vars2test[2], "- P"),
paste(vars2test[2], "- EtaSq"),
paste(paste(vars2test, collapse = " X "), "- P"),
paste(paste(vars2test, collapse = " X "), "- EtaSq"),
seq(ncol(cmb)))) %>%
mutate_if(., is.logical, as.numeric) ->
taxa_test_results # prepare results table (P and Eta)
as_tibble(matrix(
NA,
nrow = ntaxa(physeq_glom_rel_abund),
ncol = (ncol(cmb) * 5)
),
.name_repair = "minimal") %>%
bind_cols(Phylum = as(tax_table(physeq_glom_rel_abund)[, tax_rank], "vector"), .) %>%
mutate_if(., is.logical, as.numeric) ->
taxa_test_estimates # prepare estimates table
colnames(taxa_test_estimates) <- # cannot use set_names with non-unique names
c(tax_rank, c(rbind(
cmb, matrix(rep(
c("Estimate diff.", "low CI", "high CI"), ncol(cmb)
), ncol = ncol(cmb))
))) # this is ugly but it works well, basically c() flattens a matrix
for (phy_id in seq(ntaxa(physeq_glom_rel_abund))) {
bind_cols(Abundance = as.numeric(otu_table(physeq_glom_rel_abund)[, phy_id] * 100), as(sample_data(physeq_glom_rel_abund), "data.frame")) %>%
mutate_at(., vars2test[1],as.factor) %>%
mutate_at(., vars2test[2],as.factor) %>%
mutate_at(., "Vars_combination", as.factor) ->
data2test
print(tax_table(physeq_glom_rel_abund)[phy_id, tax_rank])
art_mod <- art(test_expression, data = data2test)
print(mod_summary <- anova(art_mod))
taxa_test_results[phy_id, c(2, 4, 6)] <-
as.list(mod_summary$`Pr(>F)`) # p values
mod_summary %>%
transmute(`Part Eta Sq` = `Sum Sq`/(`Sum Sq` + `Sum Sq.res`)) %>%
t() %>%
as.list() ->
taxa_test_results[phy_id, c(3, 5, 7)] # EtaSq (effect size)
# Post hoc of the main effects
posthoc_expression <- as.formula(paste("Abundance", "Vars_combination", sep = " ~ "))
art_mod_lm <- artlm(art(posthoc_expression, data = data2test), "Vars_combination") # build a linear model for ART data (This only works on combined factors; see: https://cran.r-project.org/web/packages/ARTool/vignettes/art-contrasts.html). Also, this might not be generalisable for more factors than two!!
# Compute estimated marginal means (EMMs)
(emm_mod <- emmeans(art_mod_lm, as.formula(paste("~", "Vars_combination"))))
# Contrasts, pairwise comparisons, tests, and confidence intervals.
(emm_mod_cons <- contrast(emm_mod, method = "pairwise", adjust = "tukey"))
# (emm_mod_CI <- confint(emm_mod_cons, adjust = "tukey", level = 0.95, type = "response"))
# (emm_mod_CLD <- cld(emm_mod, alpha = 0.05, Letters = letters, adjust = "tukey"))
print("Note that the estimates aren’t as in a linear model because they are on the scale of ranks and not the data, but the p-values are useful")
# assign pairwise results to table
colnames(taxa_test_results)[seq(8, 7 + ncol(cmb))] <- summary(emm_mod_cons)$contrast
taxa_test_results[phy_id, seq(8, 7 + length(summary(emm_mod_cons)$p.value))] <-
as.list(summary(emm_mod_cons)$p.value) # need to check!! (trouble is it outputs everything and not just significant ones)
# assign estimates to table
mod_lm <- lm(test_expression, data = data2test) # because we want the real means not the rank means!
emm_mod_lm <- emmeans(mod_lm, as.formula(paste("~", vars2test[1], "*",vars2test[2])), weights = "equal")
# emm_mod_lm <- emmeans(mod_lm, as.formula(paste("~", "Vars_combination")), weights = "equal")
emm_mod_lm_cons <- contrast(emm_mod, method = "pairwise", adjust = "tukey")
emm_mod_lm_CI <- confint(emm_mod_lm_cons, adjust = "fdr", level = 0.95, type = "response")
colnames(taxa_test_estimates)[seq(2, ncol(taxa_test_estimates), by = 5)] <-
str_split(emm_mod_lm_CI$contrast, " - ", simplify = TRUE)[, 1]
colnames(taxa_test_estimates)[seq(3, ncol(taxa_test_estimates), by = 5)] <-
str_split(emm_mod_lm_CI$contrast, " - ", simplify = TRUE)[, 2]
taxa_test_estimates[phy_id, -1] <- as.list(summary(emm_mod_lm)$emmean[match(colnames(taxa_test_estimates), with(summary(emm_mod_lm), get(vars2test[1]):get(vars2test[2])))])[-1]
taxa_test_estimates[phy_id, seq(4, ncol(taxa_test_estimates), by = 5)] <- as.list(emm_mod_lm_CI$estimate)
taxa_test_estimates[phy_id, seq(5, ncol(taxa_test_estimates), by = 5)] <- as.list(emm_mod_lm_CI$lower.CL)
taxa_test_estimates[phy_id, seq(6, ncol(taxa_test_estimates), by = 5)] <- as.list(emm_mod_lm_CI$upper.CL)
}
}
# Correct for FDR for each comparison pair (not needed if adjust has been used in contrast())
# for (ps2correct in seq(2, ncol(taxa_test_results), by = 2)) {
# # print(pair)
# taxa_test_results[, ps2correct] <-
# p.adjust(pull(taxa_test_results[, ps2correct]) , method = "BY") # Benjamini, Y., and Yekutieli, D. (2001). The control of the false discovery rate in multiple testing under dependency. Annals of Statistics 29, 1165–1188.
# # calculated by replacing the alpha of the Benjamini-Hochberg procedure by alpha/sum(1/1:m)).
# # we use this and not BH because of dependency in the tests (compositional data)
# }
#
write.csv(taxa_test_results, file = paste0("./Results/", outputfile, "_", tax_rank, "_Pvals.csv"))
write.csv(taxa_test_estimates, file = paste0("./Results/", outputfile, "_", tax_rank, "_CI.csv"))
Taxa_tests <- list(taxa_test_results, taxa_test_estimates)
return(Taxa_tests)
}
plotSTAMPR <- function(STAMPR_output = Taxa_tests_phylum1, pair = "City - Slope", tax_level = "Phylum", f_size = 14){
require(ggthemes)
require(ggpomological)
if (!is.list(STAMPR_output) | length(STAMPR_output) != 2) {print("The function accepts only lists with two elements")}
pair_number <- which(colnames(STAMPR_output[[1]]) == pair) - 3
pvals_col <- pair_number + 3
stats_col <- seq(from = 2, to = (ncol(STAMPR_output[[1]]) - 3) * 5, by = 5)[pair_number - 4]
bind_cols(STAMPR_output[[1]][, 1],
STAMPR_output[[1]][, pair] ,
STAMPR_output[[2]][, stats_col:(stats_col + 4)]
) %>%
mutate(Higher = if_else(.[3] > .[4], colnames(.)[3], colnames(.)[4])) %>%
gather("Factor", `Mean abundance (%)`, 3:4) %>%
mutate_at(c(tax_level, "Higher", "Factor"), ~fct_rev(.)) -> # fct_rev because coord_flip() reverses the order
STAMPR_df
tibble(
min = seq(
from = 0.5,
to = max(as.numeric(pull(STAMPR_df, tax_level))),
by = 1
),
max = seq(
from = 1.5,
to = max(as.numeric(pull(STAMPR_df, tax_level))) + 0.5,
by = 1
)) %>%
add_column(Shade = rep(c(0, 1), length.out = nrow(.))) %>%
mutate_at("Shade", ~as.factor(.)) ->
# %>% slice(rep(1:n(), each = 2))
# mutate(col = ifelse(allyrs == TRUE, 1, 0))
#
# mutate_at("min", ~case_when(col == 1 ~ . - 0.3,
# TRUE ~ .)) %>%
# mutate_at("max", ~case_when(col == 1 ~ . - 0.3,
# TRUE ~ .)) ->
shading
# ggthemr("grape")
p1 <- ggplot() +
geom_rect(data = shading,
aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
fill = Shade, alpha = 0.1), fill = rep(c("white", "#E9EDED"), length.out = nrow(shading)), show.legend = F) +
geom_col(data = STAMPR_df,
mapping = aes(!! sym(tax_level), y = `Mean abundance (%)`, fill = Factor),
width = 0.8,
position = position_dodge(),
alpha = 2/3) +
scale_y_continuous(expand = c(0, 0)) +
scale_x_discrete(expand = c(0, 0)) +
coord_flip() +
geom_rangeframe(data = STAMPR_df, aes(!! sym(tax_level), y = `Mean abundance (%)`), sides = "b") +
scale_fill_manual(values = Gradient.colours[c(6, 5)]) +
theme_tufte(base_size = f_size, base_family = "sans") +
theme(legend.position = "top",
legend.justification = 'left',
legend.title = element_blank()) +
guides(fill = guide_legend(reverse = TRUE))
# panel.grid.major.x = element_line(colour = "white"),
# panel.ontop = TRUE)
p2 <- ggplot() +
geom_rect(
data = shading,
aes(
xmin = min,
xmax = max,
ymin = -Inf,
ymax = Inf,
fill = Shade,
alpha = 0.1
),
fill = rep(c("white", "#E9EDED"), length.out = nrow(shading)),
show.legend = F
) +
geom_hline(yintercept = 0, linetype = "dashed", color = "slategray", size = 1, alpha = 2/3) +
geom_errorbar(data = STAMPR_df,
aes(
!! sym(tax_level),
ymin = `low CI`,
ymax = `high CI`,
colour = Higher),
width = 0.3,
alpha = 2/3
) +
geom_point2(data = STAMPR_df,
aes(!! sym(tax_level),
y = `Estimate diff.`,
colour = Higher),
size = 4,
alpha = 2/3) +
scale_y_continuous(expand = c(0, 0)) +
scale_x_discrete(
expand = c(0, 0),
breaks = STAMPR_df[[tax_level]],
labels = formatC(deframe(STAMPR_df[, 2]), format = "e", digits = 2),
position = "top"
) +
geom_rangeframe(data = STAMPR_df, aes(!! sym(tax_level), y = seq(
min(`low CI`),
max(`high CI`),
length.out = nrow(STAMPR_df)
)), sides = "b") +
scale_colour_manual(values = Gradient.colours[c(6, 5)]) +
theme_tufte(base_size = f_size, base_family = "sans") +
theme(legend.position = "none",
plot.title = element_text(margin = margin(10, 0, 18, 0), hjust = 0.5),
axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 20, l = 0), angle = 180)) + # BUG: no response
labs(title = "95% confidence intervals",
y = "Difference in mean abundance (%)",
x = "p-value (corrected)") +
coord_flip()
# p1 + p2 + plot_layout(widths = c(1, 2)) # doesn't look as good
plot_grid(p1, p2, rel_widths = c(2, 3)) + theme(plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"))
# ggthemr_reset()
}
GGPlotCorncob <- function(da_analysis_df, OTU_labels = FALSE, Taxa = "Phylum", Y_val = "Differential abundance", sig_level = 0.05, Rank = Taxa_rank) {
# Plot differential abundance model results
require(magrittr)
require(ggplot2)
require(ggrepel)
require(see)
pos <- position_jitter(width = 0.1, seed = 1)
da_analysis_df %<>%
mutate_at(vars(matches(Taxa)), as_factor) %>%
mutate(!!Taxa := fct_relevel(get(Taxa), pull(Rank, !!Taxa))) %>% # Taxa_rank is calcuted for the taxa box plots
mutate(!!Taxa := fct_relevel(get(Taxa), "Rare", after = Inf))
corncob_summary <- tibble(Label = c(paste0("⬆", sum(da_analysis_df$`Differential abundance` > 0 & da_analysis_df$Significance == "Pass"), " ⬇", sum(da_analysis_df$`Differential abundance` < 0 & da_analysis_df$Significance == "Pass"), " (", nrow(da_analysis_df), ")")))
p <-
ggplot(da_analysis_df) +
geom_point2(aes(
x = !!sym(Taxa),
y = !!sym(Y_val),
colour = !!sym("Significance"),
size = !!sym("Mean abundance (%)")),
position = pos,
alpha = 2 / 3,
stroke = 0) +
geom_linerange(aes(x = !!sym(Taxa),
y = !!sym(Y_val),
ymin = `ymin`,
ymax = `ymax`,
colour = !!sym("Significance")),
position = pos,
alpha = 1/5,
show.legend = FALSE) +
geom_text(
data = corncob_summary,
mapping = aes(x = Inf, y = Inf, label = Label),
hjust = 1.1,
vjust = 1.6
) +
xlab("") +
ylab("Differential abundance") +
labs(colour = paste("Significance at \n p <", sig_level), size = "Mean abundance (%)") +
theme_grey(base_size = 18) +
theme(axis.text.x = element_text(angle = 45.0, vjust = 1, hjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()) +
guides(colour = guide_legend(override.aes = list(size = 5))) +
scale_colour_manual(values = c(ggpomological:::pomological_base[[7]], ggpomological:::pomological_palette[[1]])) +
scale_size_continuous(name = "Mean abundance (%)",
range = c(2, 8),
breaks = c(round(seq(min(da_analysis_df$`Mean abundance (%)`), max(da_analysis_df$`Mean abundance (%)`), length.out = 5), 1)))
if (OTU_labels) {
p <- p + geom_label_repel(
aes(x = !!sym(Taxa), y = !!sym(Y_val)),
size = 6,
label = sub("Seq_([0-9]+)", "\\1", pull(da_analysis_df[da_analysis_df$Significance == "Pass", ], "OTU")),
position = pos,
data = da_analysis_df[da_analysis_df$Significance == "Pass", ],
# nudge_x = 0.4,
colour = "#4a4a4a",
label.size = NA,
alpha = 0.75,
# fontface = 'bold',
box.padding = 0.80,
point.padding = 0.5
)
}
return(p)
}
gz <- function (in_path, out_path = tempfile())
{
# Compress a file using gz and delete the uncompressed file
out <- gzfile(out_path, "w")
writeLines(readLines(in_path), out)
close(out)
file.remove(in_path)
invisible(out_path)
}
```
### Setting general parameters:
```{r general parameters}
set.seed(15102020)
bootstraps <- 1000
data_path <- "./Data/"
Proj_name <- "BRC_growth_rate"
Browns <- RColorBrewer::brewer.pal(n = 9, "YlOrBr")[9:6]
Greens <- RColorBrewer::brewer.pal(n = 9, "YlGn")[9:6]
Blues <- RColorBrewer::brewer.pal(n = 9, "YlGnBu")[9:6]
Gradient.colours <- c(Browns[1], Greens[1], Browns[2], Greens[2], Browns[3], Greens[3], Browns[4], Greens[4], Blues)
```
## Description
This script reproduces all sequence analysis steps and plots included in the paper plus some additional exploratory analyses.
### Load data
```{r load data, cache=TRUE}
OTUmat <- t(read.csv(paste0(data_path, "Shivta_site_otuTab2.txt"), header = TRUE, row.names = 1))
sort.order <- as.numeric(gsub("OTU([0-9]+)", "\\1", colnames( OTUmat )))
OTUmat <- OTUmat[, order(sort.order )]
Metadata <- read.csv(paste0(data_path, "Shivta_metadata.csv"), row.names = 1, header = TRUE)
read_csv(paste0(data_path, "Shivta_metadata.csv"),
trim_ws = TRUE) %>%
mutate_at(
c(
"Rock.type",
"Location"
),
~(factor(.))
) %>%
column_to_rownames("Sample.code") ->
Metadata
row.names(OTUmat) <- gsub("(.*)Nimrod[0-9]+|Osnat[0-9]+", "\\1", row.names( OTUmat))
Metadata <- Metadata[order(row.names(Metadata)), ]
OTUmat <- OTUmat[order(row.names(OTUmat)), ]
# calculate sample size
Metadata$Library.size = rowSums(OTUmat)
Metadata$Location.rock <- with(Metadata, Location:Rock.type)
# Load taxonomy data
tax.file <- "Shivta_site_silva.nrv119.taxonomy"
Taxonomy <- read.table(paste0(data_path, tax.file), stringsAsFactors = FALSE) # read taxonomy file
# count how many ';' in each cell and add up to 6
for (i in 1:nrow(Taxonomy)){
semicolons <- length(gregexpr(";", Taxonomy$V2[i] )[[1]])
if (semicolons < 6){
x <- paste0( rep("Unclassified;", 6 - semicolons ), collapse = "")
Taxonomy$V2[i] <- paste0( Taxonomy$V2[i], x, sep = "")
}
}
# split taxonomy to columns
do.call( "rbind", strsplit( Taxonomy$V1, ";", fixed = TRUE)) %>%
gsub( "size=([0-9]+)", "\\1", .) %>%
data.frame( ., do.call( "rbind", strsplit( Taxonomy$V2, ";", fixed = TRUE)), stringsAsFactors = F) %>%
apply(., 2, function(x) gsub( "\\(.*\\)", "", x)) %>%
replace(., . == "unclassified", "Unclassified") ->
Taxonomy
colnames( Taxonomy ) <- c( "OTU", "Frequency", "Domain", "Phylum", "Class", "Order", "Family", "Genus" )
# rownames(Taxonomy) <- colnames(Rock_weathering_OTUmat)
rownames(Taxonomy) <- Taxonomy[, 1]
Tree_IQ <- read_tree(paste0(data_path, "Shivta_site_otuReps.filtered.align.treefile"))
# generate phyloseq object
Ps_obj <- phyloseq(otu_table(OTUmat, taxa_are_rows = FALSE),
tax_table(Taxonomy[, -c(1, 2)]),
sample_data(Metadata),
phy_tree(Tree_IQ)
)
# Reorder factors for plotting
sample_data(Ps_obj)$Location %<>% fct_relevel("Slope", "City")
```
Remove un- and mis-classified sequences, chloroplasts and mitochondria
```{r remove samples, cache=T}
domains2remove <- c("", "Archaea", "Eukaryota", "Unclassified")
classes2remove <- c("Chloroplast")
families2remove <- c("Mitochondria")
Ps_obj_filt <- subset_taxa(Ps_obj, !is.na(Phylum) &
!Domain %in% domains2remove &
!Class %in% classes2remove &
!Family %in% families2remove)
```
### Inspect library size and number of OTU
```{r Library Sizes, cache=T}
Ps_obj_df <-
as.data.frame(sample_data(Ps_obj_filt)) # Put sample_data into a ggplot-friendly data.frame
Ps_obj_df <- Ps_obj_df[order(Ps_obj_df$Library.size), ]
Ps_obj_df$Index <- seq(nrow(Ps_obj_df))
ggplot(data = Ps_obj_df,
aes(x = Index, y = Library.size, color = Location.rock)) +
geom_point2(size = 4) +
scale_colour_manual(values = ggpomological:::pomological_palette[c(2, 1, 9, 3)], name = "Location.rock")
summary(sample_sums(Ps_obj_filt))
summary(taxa_sums(Ps_obj_filt))
```
### Explore the prevalence of different taxa in the database
```{r explore prevalence, cache=T}
prevdf <- apply(X = otu_table(Ps_obj_filt),
MARGIN = ifelse(taxa_are_rows(Ps_obj_filt), yes = 1, no = 2),
FUN = function(x){sum(x > 0)})
# Add taxonomy and total read counts to this data.frame
prevdf <- data.frame(Prevalence = prevdf,
TotalAbundance = taxa_sums(Ps_obj_filt),
tax_table(Ps_obj_filt))
prevdf %>%
group_by(Phylum) %>%
summarise(`Mean prevalence` = mean(Prevalence),
`Sum prevalence` = sum(Prevalence)) ->
Prevalence_phylum_summary
Prevalence_phylum_summary %>%
kable(., digits = c(0, 1, 0)) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), full_width = F)
prevdf %>%
group_by(Order) %>%
summarise(`Mean prevalence` = mean(Prevalence),
`Sum prevalence` = sum(Prevalence)) ->
Prevalence_Order_summary
Prevalence_Order_summary %>%
kable(., digits = c(0, 1, 0)) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), full_width = F)
```
Based on that we will remove all phyla with a prevalence of under 8
```{r remove rare taxa, cache=T}
Prevalence_phylum_summary %>%
filter(`Sum prevalence` < 8) %>%
dplyr::select(Phylum) %>%
map(as.character) %>%
unlist() ->
filterPhyla
Ps_obj_filt %<>% subset_taxa(!Phylum %in% filterPhyla)
sample_data(Ps_obj_filt)$Library.size <- rowSums(otu_table(Ps_obj_filt))
print(Ps_obj)
print(Ps_obj_filt)
```
#### Plot general prevalence features of the phyla
```{r prevalence phylum, cahce=T, fig.height=12, fig.width=10}
# Subset to the remaining phyla