-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis.Rmd
1392 lines (1128 loc) · 46.2 KB
/
Analysis.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: Dynamics of woodland soil microbial communities during restoration after
industrial and agricultural land use
author: "Bana"
date: "2024-06-11"
output:
html_document:
toc: true
toc_float:
toc_collapsed: true
toc_depth: 3
number_sections: true
theme: lumen
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = "data")
```
# Install Packages
```{r, eval=FALSE}
# # CRAN
#
# install.packages("vegan")
# install.packages("Polychrome")
# install.packages("dendextend")
# install.packages("ggplotify")
# install.packages("parallelMap")
# install.packages("caret")
# install.packages("randomForest")
#
# # Bioconductor
#
# install.packages("BiocManager")
# BiocManager::install("phyloseq")
# BiocManager::install("ANCOMBC")
# BiocManager::install("mixOmics")
#
# # GitHub
#
# install.packages("devtools")
# devtools::install_github("jbisanz/qiime2R")
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
library(dplyr)
library(ggplot2)
```
# Load data to Phyloseq
Data is ASVs
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Required library
library(qiime2R)
library(phyloseq)
library(vegan)
# Source files:
feature_table_qza <- "feature_table.qza"
rooted_tree_qza <- "rooted_tree.qza"
taxonomy_qza <- "taxonomy.qza"
metadata_tsv <- "samples.txt"
# Read data
data.phy <- qza_to_phyloseq(
features = feature_table_qza,
tree = rooted_tree_qza,
taxonomy = taxonomy_qza,
metadata = metadata_tsv
)
```
```{r}
# Cleanup
# Removal of not needed objects, packages and cleaning the RAM
rm(feature_table_qza, rooted_tree_qza, taxonomy_qza, metadata_tsv) # remove unnecessary objects
detach("package:qiime2R", unload=TRUE) # unload qiime2R package
gc() # free unused R memory
```
# Filtering and merging
## Filter Industrial and Agriculture samples
```{r}
# Keep only samples with Industrial or Agricultural prior use
table(sample_data(data.phy)$Former_landuse)
```
```{r}
IndAgri <- subset_samples(data.phy,
Former_landuse %in% c("Industrial","Agriculture"))
IndAgri
```
## Filter bacterial Kingdom
```{r}
table(as.data.frame(tax_table(data.phy))$Kingdom)
```
```{r}
# Keep only Bacteria
BacIndAgri <- subset_taxa(IndAgri, Kingdom == "d__Bacteria")
BacIndAgri # The OTU count dropped from 60,355 to 60,173
```
## Merging and Rarefaction
```{r}
plot_rarefaction_curve <- function(phy.obj, taxa_level) {
# sample depth Curve before Rarefaction
# Extract the ASV table from the phyloseq object
otu_table <- otu_table(phy.obj)
# Transpose the table
if (taxa_are_rows(otu_table)) {
otu_table <- t(otu_table)
}
# Convert to a matrix
otu_matrix <- as(otu_table, "matrix")
# Generate rarefaction curve to decide sample depth
rare_curve <- rarecurve(otu_matrix, step = 100, cex = 0.5, col = "blue", label = FALSE,
xlab = "Sample Size", ylab = taxa_level)
}
```
## 1. Family level merging
```{r}
# Agglomerate Bacteria to family level
BacIndAgriFamily <- tax_glom(BacIndAgri, taxrank="Family")
BacIndAgriFamily
```
### Normalization (Rarefaction)
```{r}
# Check the sample depth for deciding the rarefaction threshold
plot_rarefaction_curve(BacIndAgriFamily, "ASVs (Family)")
```
## 2. Genera level merging
```{r}
# Agglomerate Bacteria to genus level
BacIndAgriGenera <- tax_glom(BacIndAgri, taxrank="Genus")
BacIndAgriGenera
```
### Normalization (Rarefaction)
```{r}
# Check the sample depth for deciding the rarefaction threshold
plot_rarefaction_curve(BacIndAgriGenera, "ASVs (Genus)")
```
## 3. Species level merging
### Normalization (Rarefaction)
```{r}
# Check the sample depth for deciding the rarefaction threshold
plot_rarefaction_curve(BacIndAgri, "ASVs (Species)")
```
## Applying Rarefaction on Genera merged data
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Rarefy the phyloseq object to an even depth of 10000 sequences per sample
BacIndAgriGenera.rarefied <- rarefy_even_depth(BacIndAgriGenera,
sample.size = 10000,
rngseed = 123, replace = FALSE)
```
* Rarefation done on Genera level, for multiple reasons: 1. not all ASVs are classified up to species 2. The 16srRNA sequencing achieves better genus-level resolution
* The decision of sample depth threshold (10000) to use for normalization was mainly based on visual assessment of two things: sample reaching the plateau of ASVs count and avoiding losing any samples.
## Filtering low quality ASVs (at Genera level)
```{r}
# (Function) to create and save a histogram
save_histogram <- function(data, xlab, ylab, main, filename) {
# Display the histogram
hist(data, xlab = xlab, ylab = ylab, main = main)
# Save the histogram to a file
dev.copy(png, filename)
dev.off()
}
```
```{r}
# Convert the OTU table to matrix
BacIndAgriGenera.numeric <- as.numeric(otu_table(BacIndAgriGenera))
BacIndAgriGenera.mtx <- matrix(BacIndAgriGenera.numeric, nrow=nrow(otu_table(BacIndAgriGenera)))
# Getting the ASV that has more than 0 count in each sample THRESHOLD
BacIndAgriGenera.count_filter <- BacIndAgriGenera.mtx > 0
# Sum the number of ASV for each sample that has > count threshold
# The result will show the number of ASV (frequency, y-axis) > count threshold in each sample (x-axis)
BacIndAgriGenera.count_filter.sum <- apply(BacIndAgriGenera.count_filter, 1, sum)
# Display and save the first histogram
save_histogram(BacIndAgriGenera.count_filter.sum, xlab = "Samples", ylab = "ASVs count (Genera)",
main = "Frequency of ASVs with count > 0 across samples",
filename = "results/filter_low_quality_ASVs/ASVsGenera_count_histogram.png")
# Plot histogram for sample interval (0-10)
BacIndAgriGenera.count_filter.sum.1_10.interval <- BacIndAgriGenera.count_filter.sum[BacIndAgriGenera.count_filter.sum < 10]
# Display and save the second histogram
save_histogram(BacIndAgriGenera.count_filter.sum.1_10.interval, xlab = "Samples", ylab = "ASVs count (Genera)",
main = "Frequency of ASVs with count > 0 in 10 samples or less",
filename = "results/filter_low_quality_ASVs/ASVsGenera_count_histogram_1_10_interval.png")
```
Interpretation: Let's say for the second plot we have ~ 150 ASVs that are present in two samples.
```{r}
all_taxa.num <- length(BacIndAgriGenera.count_filter.sum)
taxa_less10samples <- length(BacIndAgriGenera.count_filter.sum.1_10.interval)
lost_taxa_perc <- round((taxa_less10samples / all_taxa.num) * 100, 2)
print(paste("Out of", all_taxa.num, "the number of lost ASVs (Genera) after filtering taxa with abundance > 0 counts in less than 10 samples", taxa_less10samples, "Which is", lost_taxa_perc, "%"))
```
From count filtering histogram and stats, half of ASVs (Genera) will be lost. Therefore, for further analysis the data will be kept without filtering unless it’s required for a statistical method to be used.
# Visualizing Microbial diversity
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
library(ggplot2)
library(plotly)
library(RColorBrewer)
```
## Bar plot
### Former land use
```{r}
# Aggregate data at Phylum level
BacIndAgriPhylum.rarefied <- tax_glom(BacIndAgriGenera.rarefied, taxrank = "Phylum")
```
## Check data types
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Make sure pH / Conductivity / Age are numerical
sample_data(BacIndAgriPhylum.rarefied)$pH <- as.numeric(as.character(sample_data(BacIndAgriPhylum.rarefied)$pH))
sample_data(BacIndAgriPhylum.rarefied)$Conductivity <- as.numeric(as.character(sample_data(BacIndAgriPhylum.rarefied)$Conductivity))
sample_data(BacIndAgriPhylum.rarefied)$Woodland_age <- as.numeric(as.character(sample_data(BacIndAgriPhylum.rarefied)$Woodland_age))
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide',fig.keep='all'}
env_factor_bar_plot <- function(phyloseq.obj, env_factor, save_path, levels=c()) {
# Merge samples by environmental factor (categorical one)
phyloseq.obj.merged <- merge_samples(phyloseq.obj, env_factor)
# Transform counts to relative abundances
phyloseq.obj.merged <- transform_sample_counts(phyloseq.obj.merged, function(x) x / sum(x))
# Extract the data for plotting
phyloseq.obj.merged.df <- psmelt(phyloseq.obj.merged)
# Get counts per phylum
Phyla.df <- phyloseq.obj.merged.df %>%
group_by(Phylum) %>%
summarise(Count = sum(Abundance))
# Select the cut-off for the Phylum taxa (e.g. 1% of total count)
cutoff <- 0.01 * sum(phyloseq.obj.merged.df$Abundance)
# Select low-abundant Phyla (with total counts below the cutoff)
lowAbundant <- Phyla.df[Phyla.df$Count <= cutoff,]$Phylum
# Substitute Phylum names to "<1%" for the low-abundant phyla
phyloseq.obj.merged.df[phyloseq.obj.merged.df$Phylum %in% lowAbundant,]$Phylum <- '<1%'
# Ensure env_factor levels are ordered correctly in the melted data frame
if (length(levels) > 0){
phyloseq.obj.merged.df$Sample <- factor(phyloseq.obj.merged.df$Sample, levels = levels)
}
# Define high contrast palette for bar plot
n_colors <- length(unique(phyloseq.obj.merged.df$Phylum))
high_contrast_palette <- c(brewer.pal(min(9, n_colors), "Set1"), brewer.pal(max(0, n_colors - 9), "Dark2"))
# Create bar plot
barplot <- ggplot(phyloseq.obj.merged.df, aes(x = Sample, y = Abundance, fill = Phylum, text = paste("Phylum:", Phylum, "<br>", env_factor, ":", Sample, "<br>Relative Abundance:", Abundance))) +
geom_bar(stat = "identity", position = "stack") +
scale_fill_manual(values = high_contrast_palette) +
theme(axis.text.x = element_text(angle = 0, hjust = 1)) +
labs(title = paste("Taxa Abundance by", env_factor), x = env_factor, y = "Relative Abundance")
# Convert to plotly
barplot.plotly <- ggplotly(barplot, tooltip = "text")
barplot.plotly
# Save the plot as an HTML file
htmlwidgets::saveWidget(barplot.plotly, save_path)
return(barplot.plotly)
}
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
barplot.FLU <- env_factor_bar_plot(phyloseq.obj = BacIndAgriPhylum.rarefied, env_factor = "Former_landuse",
save_path = "results/plotly/Taxonomy_Phyla_landuse_normalized.html")
barplot.FLU
```
### pH
```{r}
pHValues <- as.numeric(sample_data(BacIndAgriGenera)$pH)
pHValues.removed_nan <- na.omit(pHValues)
print("")
print("max pH")
max(pHValues.removed_nan)
print("Min pH")
min(pHValues.removed_nan)
print("Median pH")
median(pHValues.removed_nan)
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Make sure pH column is numeric
sample_data(BacIndAgriPhylum.rarefied)$pH <- as.numeric(as.character(sample_data(BacIndAgriPhylum.rarefied)$pH))
# Categorizing pH
sample_data(BacIndAgriPhylum.rarefied)$pH_category <- factor(
cut(sample_data(BacIndAgriPhylum.rarefied)$pH,
breaks = c(3.78, 5, 6, 7.98),
labels = c("<5", "5-6", ">6")),
levels = c("<5", "5-6", ">6")
)
```
```{r}
pH_less_5 <- as.numeric(sample_data(BacIndAgriPhylum.rarefied)$pH_category == "<5")
pH_less_5 <- na.omit(pH_less_5)
print(paste("Number of samples with to pH < 5 =", sum(pH_less_5)))
pH_5_6 <- as.numeric(sample_data(BacIndAgriPhylum.rarefied)$pH_category == "5-6")
pH_5_6 <- na.omit(pH_5_6)
print(paste("Number of samples with to pH 5-6 =", sum(pH_5_6)))
pH_more_6 <- as.numeric(sample_data(BacIndAgriPhylum.rarefied)$pH_category == ">6")
pH_more_6 <- na.omit(pH_more_6)
print(paste("Number of samples with pH >6 =", sum(pH_more_6)))
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
barplot.pH <- env_factor_bar_plot(phyloseq.obj = BacIndAgriPhylum.rarefied, env_factor = "pH_category",
save_path = "results/plotly/Taxonomy_Phyla_pH_normalized.html",
levels = c("<5", "5-6", ">6"))
barplot.pH
```
### Conductivity
```{r}
conductivityValues <- as.numeric(sample_data(BacIndAgriGenera)$Conductivity)
# Calculate the quartiles
quartiles <- quantile(conductivityValues, probs = c(0.25, 0.5, 0.75), na.rm = TRUE)
quartiles
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Make sure conductivity column is numeric
sample_data(BacIndAgriPhylum.rarefied)$Conductivity <- as.numeric(as.character(sample_data(BacIndAgriPhylum.rarefied)$Conductivity))
# Calculate the quartiles for conductivity
quartiles <- quantile(sample_data(BacIndAgriPhylum.rarefied)$Conductivity, probs = c(0.25, 0.5, 0.75), na.rm = TRUE)
# Categories based on quartiles
sample_data(BacIndAgriPhylum.rarefied)$Conductivity_category <-
cut(sample_data(BacIndAgriPhylum.rarefied)$Conductivity,
breaks = c(-Inf, quartiles, Inf),
labels = c("Q1: <0.25", "Q2: 0.25-0.5", "Q3: 0.5-0.75", "Q4: 0.75<"),
include.lowest = TRUE)
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
barplot.conductivity <- env_factor_bar_plot(phyloseq.obj = BacIndAgriPhylum.rarefied, env_factor = "Conductivity_category",
save_path = "results/plotly/Taxonomy_Phyla_Conductivity_normalized.html")
barplot.conductivity
```
## Line Plot
### FLU vs pH
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
# Make sure pH column is numeric
sample_data(BacIndAgri)$pH <- as.numeric(as.character(sample_data(BacIndAgri)$pH))
# Make Former_landuse column as factor
sample_data(BacIndAgri)$Former_landuse <- as.factor(sample_data(BacIndAgri)$Former_landuse)
# Extract sample data into a df
FLU_pH.df <- data.frame(sample_data(BacIndAgri))
FLU_pH.plot <- plot_ly()
# Compute density estimates for each Former_landuse level
for(level in levels(FLU_pH.df$Former_landuse)) {
data_subset <- FLU_pH.df[FLU_pH.df$Former_landuse == level, ]
dens <- density(data_subset$pH, na.rm = TRUE) # Compute density
# Add density trace
FLU_pH.plot <- FLU_pH.plot %>% add_trace(
x = dens$x,
y = dens$y,
type = 'scatter',
mode = 'lines',
name = level,
opacity = 0.6
)
}
# Customize the layout
FLU_pH.plot <- FLU_pH.plot %>% layout(
title = "Kernel Density Estimate of pH by Former Landuse",
xaxis = list(title = "pH"),
yaxis = list(title = "Density")
)
FLU_pH.plot
# Save the plot as an HTML file
htmlwidgets::saveWidget(FLU_pH.plot, "results/plotly/pH_vs_FLU.html")
```
## Correlation
Scatter Plot
### pH vs Conductivity
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
library(broom)
```
```{r}
# Make sure conductivity column is numeric
sample_data(BacIndAgri)$Conductivity <- as.numeric(as.character(sample_data(BacIndAgri)$Conductivity))
# Extract sample data
Cond_pH.df <- data.frame(sample_data(BacIndAgri))
# linear regression
model <- lm(Conductivity ~ pH, data = Cond_pH.df)
# Extract the coefficients
summary_lm <- summary(model) # Benjamini-Hochberg adjusted p-value by default
print(summary_lm)
# Extract R-squared and slope
r_squared <- summary_lm$r.squared
slope <- summary_lm$coefficients[2, 1]
# Create the regression plot
Cond_pH.plot <- ggplot(Cond_pH.df, aes(x = pH, y = Conductivity, color = Former_landuse)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE) +
labs(title = "Regression of Conductivity on pH by Former Land Use",
subtitle = paste("R-squared =", round(r_squared, 2),
"Slope =", round(slope, 2)),
x = "pH",
y = "Conductivity") +
theme_minimal()
print(Cond_pH.plot)
# Convert to plotly
Cond_pH.plot_plotly <- ggplotly(Cond_pH.plot)
print(Cond_pH.plot_plotly)
# Save the plot as an HTML file
htmlwidgets::saveWidget(Cond_pH.plot_plotly, "results/plotly/Regression_pH_Conductivity_by_LandUse.html")
```
Adjusted R-squared 0.2218 (very low) shows that the model failed to explain the real data points and the results are not reliable. For the results it tells that for one unit increase of pH the conductivity increases by 31.986, and theoretically at pH 0 the conductivity is -96.885
### Woodland age vs taxa abundance
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
library(dplyr)
```
```{r}
# Function to compute the correlation between microbial abundance and environmental factor
abundance_env_factor_corr <- function(phyloseq.obj, env_factor){
# Create abundance df
phyloseq.obj.abund <- psmelt(phyloseq.obj)
# Ensure environmental factors are numerical
phyloseq.obj.abund$Woodland_age <- as.numeric(as.character(phyloseq.obj.abund$Woodland_age))
phyloseq.obj.abund$Conductivity <- as.numeric(as.character(phyloseq.obj.abund$Conductivity))
phyloseq.obj.abund$pH <- as.numeric(as.character(phyloseq.obj.abund$pH))
# Aggregate abundance by Site_code (as samples of each site share the same value of pH, Conductivity, age) and Phylum, summing ASVs
phyloseq.obj.abund.summary <- phyloseq.obj.abund %>%
group_by(Site_code, Phylum) %>%
summarise(Abundance = mean(Abundance), .groups = 'drop')
# Align Sample IDs between phyloseq sample data and the summary data
sample_data_df <- as.data.frame(sample_data(phyloseq.obj))
sample_data_df$Sample <- rownames(sample_data_df)
# Join the summary data with the sample metadata
phyloseq.obj.abund.summary <- left_join(phyloseq.obj.abund.summary, sample_data_df, by = "Site_code")
# Correlation function
compute_correlation <- function(df) {
cor_result <- cor.test(df[[env_factor]], df$Abundance, method = "spearman")
data.frame(
corr = cor_result$estimate,
p_value = cor_result$p.value
)
}
# Compute correlation and p-value for each Phylum
correlations <- phyloseq.obj.abund.summary %>%
group_by(Phylum) %>%
summarise(correlation = list(compute_correlation(cur_data())), .groups = 'drop')
# Flatten the list column to extract correlation and p-value
correlations <- correlations %>%
mutate(
correlation = sapply(correlations$correlation, function(x) x$corr),
p_value = sapply(correlations$correlation, function(x) x$p_value)
)
# Calculate the adjusted p-value
correlations$pAdjust <- p.adjust(correlations$p_value)
# Filter taxa based on adjusted p-value <= 0.05
significant_taxa <- correlations %>%
filter(pAdjust <= 0.05)
# Get top positively and negatively correlated taxa
top_positive <- significant_taxa %>%
arrange(desc(correlation)) %>%
slice_head(n = 4)
top_negative <- significant_taxa %>%
arrange(correlation) %>%
slice_head(n = 4)
print("Top positively correlated taxa with Woodland age:")
print(top_positive)
print("Top negatively correlated taxa with Woodland age:")
print(top_negative)
return(list("abund.summary"=phyloseq.obj.abund.summary,
"top_positive"=top_positive, "top_negative"=top_negative))
}
```
```{r}
age_abund_corr <- abundance_env_factor_corr(phyloseq.obj = BacIndAgriPhylum.rarefied,
env_factor = "Woodland_age")
```
```{r, fig.keep='all', fig.show='hold'}
top_pos_neg_plot <- function(top_positive, top_negative, abund.summary, env_factor) {
# Select the top positively and negatively correlated taxa
top_taxa <- rbind(top_positive[1, ], top_negative[1, ])
# Extract data for these taxa
top_taxa.abund.summary <- abund.summary %>%
filter(Phylum %in% top_taxa$Phylum)
# Scatter plots
for (phylum in unique(top_taxa$Phylum)) {
# Filter data for the current Phylum
phylum_data <- top_taxa.abund.summary %>% filter(Phylum == phylum)
# Fit a linear model
lm_formula <- paste("Abundance", "~", env_factor)
lm_formula <- as.formula(lm_formula)
lm_model <- lm(lm_formula, data = phylum_data)
summary_lm <- summary(lm_model)
print("--------------------------------------")
print(summary_lm)
# Extract R-squared and slope
r_squared <- summary_lm$r.squared
slope <- summary_lm$coefficients[2, 1]
# Create scatter plot
p <- ggplot(phylum_data, aes_string(x = env_factor, y = "Abundance")) +
geom_point(color = "blue") +
geom_smooth(method = "lm", se = TRUE, color = "red") +
labs(title = paste("Abundance of", phylum, "vs", env_factor),
subtitle = paste("R-squared =", round(r_squared, 2),
"Slope =", round(slope, 2)),
x = env_factor,
y = "Abundance") +
theme_minimal()
# Print the plot
print(p)
}
}
```
```{r, fig.keep='all', fig.show='hold'}
top_pos_neg_plot(top_positive = age_abund_corr$top_positive, top_negative = age_abund_corr$top_negative,
abund.summary = age_abund_corr$abund.summary, env_factor = "Woodland_age")
```
### pH vs taxa abundance
```{r}
pH_abund_corr <- abundance_env_factor_corr(phyloseq.obj = BacIndAgriPhylum.rarefied,
env_factor = "pH")
```
```{r, fig.keep='all', fig.show='hold'}
top_pos_neg_plot(top_positive = pH_abund_corr$top_positive, top_negative = pH_abund_corr$top_negative,
abund.summary = pH_abund_corr$abund.summary, env_factor = "pH")
```
### Conductivity vs taxa abundance
```{r}
conductivity_abund_corr <- abundance_env_factor_corr(phyloseq.obj = BacIndAgriPhylum.rarefied,
env_factor = "Conductivity")
```
```{r, fig.keep='all', fig.show='hold'}
top_pos_neg_plot(top_positive = conductivity_abund_corr$top_positive,
top_negative = conductivity_abund_corr$top_negative,
abund.summary = conductivity_abund_corr$abund.summary, env_factor = "Conductivity")
```
# Alpha Diversity
## plot
```{r}
alpha_diversity.plot <- function(alpha_richness, phyloseq_obj, env_factor){
# Apply Wilcoxon test
chao1_res <- wilcox.test(alpha_richness$Chao1~
sample_data(phyloseq_obj)[[env_factor]])
chao1.p_value <- chao1_res$p.value
shannon_res <- wilcox.test(alpha_richness$Shannon~
sample_data(phyloseq_obj)[[env_factor]])
shannon.p_value <- shannon_res$p.value
simpson_res <- wilcox.test(alpha_richness$Simpson~
sample_data(phyloseq_obj)[[env_factor]])
simpson.p_value <- simpson_res$p.value
plot_richness(phyloseq_obj,
measures=c("Chao1", "Shannon", "Simpson"),
color=env_factor, x=env_factor) +
geom_boxplot() + ggtitle(paste("Alpha diversity vs", env_factor),
subtitle=paste("Chao1 p-value:", chao1.p_value,
"\nShannon p-value:", shannon.p_value,
"\nSimpson p-value:", simpson.p_value))
}
```
### by FLU
```{r}
# Calculate Alpha diversity indices
alpha_richness = estimate_richness(
BacIndAgriGenera.rarefied, measures = c("Chao1", "Shannon", "Simpson"))
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
alpha_diversity.plot(alpha_richness = alpha_richness, phyloseq_obj = BacIndAgriGenera.rarefied,
env_factor = "Former_landuse")
```
### by Region
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
alpha_diversity.plot(alpha_richness = alpha_richness, phyloseq_obj = BacIndAgriGenera.rarefied,
env_factor = "Region")
```
## Variance
### Shannon
```{r}
shannon_values <- alpha_richness$Shannon
site_codes <- sample_data(BacIndAgriGenera.rarefied)$Site_code
FLU_values <- sample_data(BacIndAgriGenera.rarefied)$Former_landuse
shannon_site.df <- data.frame(Site_code = site_codes, Shannon = shannon_values,
Former_landuse = FLU_values)
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
ggplot(shannon_site.df, aes(x = Site_code, y = Shannon, fill = Former_landuse)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Shannon Diversity by Site",
x = "Site",
y = "Shannon Index") +
theme(axis.text.x = element_text(angle = 90, size=4))
```
```{r}
# Calculate variance for sites replicas
variance_data.shannon <- shannon_site.df %>%
group_by(Site_code) %>%
summarise(CV = (sd(Shannon, na.rm = TRUE)/mean(Shannon, na.rm = TRUE))*100, Former_landuse = unique(Former_landuse))
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
ggplot(variance_data.shannon, aes(x = Site_code, y = CV, fill = Former_landuse)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Coefficient of Variation of Shannon Diversity by Site",
x = "Site",
y = "Coefficient of Variation") +
theme(axis.text.x = element_text(angle = 90, size=4))
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
library(readxl)
```
```{r}
sites_pH_EC <- read_excel("pH_EC_RestREco_8th_July_2024.xlsx")
```
### pH
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
ggplot(sites_pH_EC, aes(x = Site_code, y = pH, fill = Former_landuse)) +
geom_boxplot() +
theme_minimal() +
labs(title = "pH Diversity by Site",
x = "Site",
y = "pH") +
theme(axis.text.x = element_text(angle = 90, size = 6))
```
```{r}
# Calculate variance for sites replicas
variance_data.pH <- sites_pH_EC %>%
group_by(Site_code) %>%
summarise(CV = (sd(pH, na.rm = TRUE)/mean(pH, na.rm = TRUE))*100, Former_landuse = unique(Former_landuse))
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
ggplot(variance_data.pH, aes(x = Site_code, y = CV, fill = Former_landuse)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Coefficient of Variation of pH by Site",
x = "Site",
y = "Coefficient of Variation") +
theme(axis.text.x = element_text(angle = 90, size = 6))
```
### Conductivity
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
ggplot(sites_pH_EC, aes(x = Site_code, y = Conductivity, fill = Former_landuse)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Conductivity Diversity by Site",
x = "Site",
y = "Conductivity") +
theme(axis.text.x = element_text(angle = 90, size = 6))
```
```{r}
# Calculate variance for sites replicas
variance_data.conductivity <- sites_pH_EC %>%
group_by(Site_code) %>%
summarise(CV = (sd(Conductivity, na.rm = TRUE)/mean(Conductivity, na.rm = TRUE))*100,
Former_landuse = unique(Former_landuse))
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
ggplot(variance_data.conductivity, aes(x = Site_code, y = CV, fill = Former_landuse)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Coefficient of Variation of Conductivity by Site",
x = "Site",
y = "Coefficient of Variation") +
theme(axis.text.x = element_text(angle = 90, size = 6))
```
## Modelling (nested design)
```{r, eval=FALSE}
#install.packages("remotes")
#remotes::install_github("mikemc/speedyseq")
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
library(speedyseq)
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Merge replicas for each site by median (non-parametric)
BacIndAgriGenera.rarefied.merged_replica <- merge_samples2(x = BacIndAgriGenera.rarefied, group = "Site_code", fun_otu = median)
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
# Sanity check of counts per sample (the sum should be same across all samples)
plot(colSums(otu_table(BacIndAgriGenera.rarefied.merged_replica)),
ylab="Count", xlab="Samples", xaxt="n",
main="Counts per sample after merging")
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
# Plot the ASVs count per site after median merging to decide a threshold for normalization
plot_rarefaction_curve(BacIndAgriGenera.rarefied.merged_replica, "ASVs (Genera)")
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Rarefy (normalize) to an even depth of 8000 sequences per sample
BacIndAgriGenera.rarefied2.merged_replica <- rarefy_even_depth(BacIndAgriGenera.rarefied.merged_replica,
sample.size = 8000,
rngseed = 123, replace = FALSE)
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
# Sanity check
plot(colSums(otu_table(BacIndAgriGenera.rarefied2.merged_replica)),
ylab="Count", xlab="Samples", xaxt="n",
main="Counts per sample after merging and rarefaction")
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Calculate Shannon
richness.shannon = estimate_richness(
BacIndAgriGenera.rarefied2.merged_replica, measures = c("Shannon"))
# Add shannon to the phyloseq obj
sample_data(BacIndAgriGenera.rarefied2.merged_replica)$Shannon <- richness.shannon$Shannon
```
```{r}
# Get sample data into dataframe
BacIndAgriGenera.rarefied2.merged_replica.df <-
as(sample_data(BacIndAgriGenera.rarefied2.merged_replica), "data.frame")
```
```{r}
reg_model.alphaDiv <- lm(Shannon~Region/Former_landuse, data=BacIndAgriGenera.rarefied2.merged_replica.df)
summary(reg_model.alphaDiv)
```
# Differential Abundance
On merged sites replicas
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
library(ANCOMBC)
```
Differential Abundance Tests:
pH, conductivity, Region, age, FLU
pH + conductivity + age --> Trend test
Region --> Global test / pairwise
FLU --> pairwise
## FLU
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# Estimate differential abundances
# 1. FLU (Pairwise) bonferroni correction is often applied to control for the increased risk of Type I errors
ancom.FLU = ancombc2(data = BacIndAgriGenera.rarefied2.merged_replica, tax_level = "Genus",
fix_formula = "Former_landuse",
group = "Former_landuse",
p_adj_method = "bonferroni", alpha = 0.05, pairwise = TRUE,
prv_cut = 0.10, lib_cut = 0,
n_cl = 4, verbose = TRUE)
# alpha: significance cut-off
# prv_cut: filter taxa not present in prv_cut * samples_number
# lib_cut: filter samples with library size threshold
```
```{r}
# Extract the results table
ancom.FLU.df <- ancom.FLU$res
colnames(ancom.FLU.df)
```
```{r}
# Select only columns that we need
ancom.FLU.df <- ancom.FLU.df %>%
select(taxon, contains("Former_landuse"))
```
```{r}
# Select differentially abundant taxa
dif.FLU.df <- ancom.FLU.df %>%
filter(diff_Former_landuseIndustrial & passed_ss_Former_landuseIndustrial) %>%
arrange(desc(lfc_Former_landuseIndustrial))
```
```{r}
# Filter out values that are not 2 fold change in both positive and negative direction
dif.FLU.df <- dif.FLU.df %>%
filter(lfc_Former_landuseIndustrial >= 1 | lfc_Former_landuseIndustrial <= -1)
# Create the 'direct' column to categorize LFC
dif.FLU.df <- dif.FLU.df %>%
mutate(
direct = ifelse(lfc_Former_landuseIndustrial >= 1, "Positive LFC", "Negative LFC")
)
# Ensure direct factorized
dif.FLU.df$direct =
factor(dif.FLU.df$direct, levels = c("Positive LFC", "Negative LFC"))
dif.FLU.df$taxon =
factor(dif.FLU.df$taxon, levels = dif.FLU.df$taxon)
```
```{r}
dif.FLU.df
```
```{r, warning=FALSE,message=FALSE,error=FALSE, fig.keep='all'}
# Make bar plot
dif.FLU.plot <- dif.FLU.df %>%
ggplot(aes(x = taxon, y = lfc_Former_landuseIndustrial, fill = direct)) +
geom_bar(stat = "identity", width = 0.7, color = "black",
position = position_dodge(width = 0.4)) +
geom_errorbar(aes(ymin = lfc_Former_landuseIndustrial - se_Former_landuseIndustrial,
ymax = lfc_Former_landuseIndustrial + se_Former_landuseIndustrial),
width = 0.2, position = position_dodge(0.05),
color = "black") +
labs(x = "Genus", y = "Log fold change") +
ggtitle(label = "Differentially abundant taxa",
subtitle="Prior land use: Industrial vs Agricultural") +
scale_fill_discrete(name = NULL) +
scale_color_discrete(name = NULL) +
theme_bw() +
theme(panel.grid.minor.y = element_blank(),
axis.text.x = element_text(size = 6, angle = 60, hjust = 1))
# Convert to plotly
dif.FLU.plot_plotly <- ggplotly(dif.FLU.plot, tooltip = "text")
dif.FLU.plot_plotly
# Save the plot as an HTML file
htmlwidgets::saveWidget(dif.FLU.plot_plotly, "results/plotly/differential_abundant_taxa_FLU.html")
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide', fig.keep='all', fig.show='hold'}
# box plot function
diff_abund.box_plot <- function(taxon, env_factor, title) {
taxon_data <- psmelt(BacIndAgriGenera.rarefied) %>%
filter(Genus == taxon)
plot <- ggplot(taxon_data, aes_string(x = env_factor, y = "Abundance", fill = env_factor)) +
geom_violin() +
geom_jitter(width = 0.2, alpha = 0.5) +
labs(title = title, x = env_factor, y = "Abundance") +
theme_minimal() +
theme(legend.position = "none")
return(plot)
}
```
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide', fig.keep='all', fig.show='hold'}
# Extract the first positive and negative taxa
first_positive_taxa.FLU <- dif.FLU.df %>%
filter(direct == "Positive LFC") %>%
slice(1) %>%
pull(taxon)
first_negative_taxa.FLU <- dif.FLU.df %>%
filter(direct == "Negative LFC") %>%
slice(1) %>%
pull(taxon)
# Generate and plot the box plots
positive_plot.FLU <- diff_abund.box_plot(first_positive_taxa.FLU, "Former_landuse",
paste("Abundance of", first_positive_taxa.FLU, "by Former Land Use"))
negative_plot.FLU <- diff_abund.box_plot(first_negative_taxa.FLU, "Former_landuse",
paste("Abundance of", first_negative_taxa.FLU, "by Former Land Use"))
print(positive_plot.FLU)
print(negative_plot.FLU)
```
## Region
```{r, warning=FALSE,message=FALSE,error=FALSE, results='hide'}
# 2. Region
ancom.region = ancombc2(data = BacIndAgriGenera.rarefied2.merged_replica, tax_level = "Genus",
fix_formula = "Region",
group = "Region",
p_adj_method = "bonferroni", alpha = 0.05, pairwise = TRUE,
prv_cut = 0.10, lib_cut = 0,
n_cl = 4, verbose = TRUE)
```
```{r}
# Extract the results table
ancom.region.df <- ancom.region$res
```
```{r}
# Select only columns that we need
ancom.region.df <- ancom.region.df %>%
select(taxon, contains("Region"))
```
```{r}
# Select differentially abundant taxa
dif.region.df <- ancom.region.df %>%