-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path016_EnvCor_Heatmaps.R
182 lines (140 loc) · 8.07 KB
/
016_EnvCor_Heatmaps.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Script for looking at environment correlation heatmaps
# Combined Data
# Subset for samples that actually have full metadata
#Benjamini & Hochberg (1995) correction through p.adjust
set.seed(18)
library(tidyverse)
library(devtools)
#devtools::install_github("jbisanz/qiime2R")
library(qiime2R)
library(ggplot2)
library(phyloseq)
library(gridExtra)
library(vegan)
library(dplyr)
library(scales)
library(grid)
library(reshape2)
library(ggpubr)
#install_github("microbiome/microbiome")
library(microbiome)
#turn off scientific notation
options(scipen=999)
setwd("//wsl.localhost/Ubuntu-18.04/home/crschul/IndigoAgMicrobiome")
load("phy_combined.rdata")
metadata <- read.csv("Metadata_Indigo_Clean.tsv", header = TRUE, sep = "\t")
#devtools::install_github('schuyler-smith/phylosmith')
library(phylosmith)
# TSS is just relative abundance
phy_filtered = transform_sample_counts(phy_combined, function(x) (x / sum(x) * 10000))
phy_pseudo <- transform_sample_counts(phy_filtered, function(OTU) OTU +1)
#################### Change metadata columns for ploting
oldnames <- c("precip","temp","humidity",
"cloudcover","Soil.pH","Organic.Matter",
"Nitrate.N.ppm.N", "Potassium.ppm.K","Sulfate.S.ppm.S",
"Calcium.ppm.Ca","Magnesium.ppm.Mg","Sodium.ppm.Na")
# if it has Soil.pH, it has our other soil variables!
phy_pseudo <- subset_samples(phy_pseudo, !is.na(Soil.pH))
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "precip"] = "Precipitation"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "temp"] = "Temperature"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "humidity"] = "Humidity"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "cloudcover"] = "Cloud.Cover"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Soil.pH"] = "Soil.pH"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Soil.Moisture"] = "Soil.Moisture"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Soil.Temp"] = "Soil Temperature"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Organic.Matter"] = "Organic.Matter"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Nitrate.N.ppm.N"] = "Nitrate.ppm"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Potassium.ppm.K"] = "Potassium.ppm"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Sulfate.S.ppm.S"] = "Sulfate.ppm"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Calcium.ppm.Ca"] = "Calcium.ppm"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Magnesium.ppm.Mg"] = "Magnesium.ppm"
colnames(sample_data(phy_pseudo))[colnames(sample_data(phy_pseudo)) == "Sodium.ppm.Na"] = "Sodium.ppm"
# Test the whole network across everything!
combined_phylum <- conglomerate_taxa(phy_pseudo, "Phylum")
#### API doesn't have 2017 soil measures :( so we have to clean our data
# subset for just the samples that have soil data
# how many samples do we lose if we just drop NAs across the board? 734 / 1093
sample_data(combined_phylum) <- sample_data(combined_phylum)[, -which(names(sample_data(combined_phylum)) %in%
c("Soil.EC","UV.Light","Co2.ppm"))]
clean_phy <- combined_phylum
foo <- as.data.frame(sample_data(clean_phy))
#######################################################
# do bacteria and fungal graphs right next to each other!!!
Bacteria_phy <- subset_samples(clean_phy, Microbe == "bacteria")
Fungi_phy <- subset_samples(clean_phy, Microbe == "fung")
#### Fungus
fungi_corr_heat <- variable_correlation(Fungi_phy,
variables = c("Precipitation",
"Temperature",
"Humidity",
"Cloud.Cover",
"Soil.pH",
"Soil.Moisture",
"Soil.Temperature",
"Organic.Matter",
"Nitrate.ppm",
"Potassium.ppm",
"Sulfate.ppm",
"Calcium.ppm",
"Magnesium.ppm",
"Sodium.ppm"),
classification = "Phylum",
treatment = "tissue",
method = 'spearman', cores = 4)
fungi_adj <- fungi_corr_heat
fungi_adj$p <- p.adjust(fungi_adj$p, method = "BH")
fungi_adj <- filter(fungi_adj, p < .05)
corr_df_f <- fungi_adj
gf <- ggplot(corr_df_f, aes(x = Y, y = X, fill = rho)) +
geom_tile(color="black") + facet_wrap(~Treatment, nrow = 4) +
scale_fill_gradient2(low = "red", mid = "white",high = "blue") + theme_bw() +
ggtitle(("Effect of Environment on Fungal Phylum"))+
theme(panel.grid.major.x = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
xlab(element_blank()) + ylab(element_blank()) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, face = "bold")) +
theme(axis.text.y = element_text(face = "bold")) +
theme(plot.title = element_text(size = 11, face = "bold")) + scale_y_discrete(limits=rev) +
theme(strip.text.x = element_text(size = 12,face = "bold")) +
geom_hline(yintercept=seq(1.5, length(unique(corr_df_f$X)),1), colour='grey')
gf
# Bacteria
bact_corr_heat <- variable_correlation(Bacteria_phy,
variables = c("Precipitation",
"Temperature",
"Humidity",
"Cloud.Cover",
"Soil.pH",
"Soil.Moisture",
"Soil.Temperature",
"Organic.Matter",
"Nitrate.ppm",
"Potassium.ppm",
"Sulfate.ppm",
"Calcium.ppm",
"Magnesium.ppm",
"Sodium.ppm"),
classification = "Phylum",
treatment = "tissue",
method = 'spearman', cores = 4)
bact_adj <- bact_corr_heat
bact_adj$p <- p.adjust(bact_adj$p, method = "BH")
bact_adj <- filter(bact_adj, p < .05)
corr_df_b <- bact_adj
gb <- ggplot(corr_df_b, aes(x = Y, y = X, fill = rho)) +
geom_tile(color="black") + facet_wrap(~Treatment, nrow = 4) +
scale_fill_gradient2(low = "red", mid = "white",high = "blue") + theme_bw() +
ggtitle(("Effect of Environment on Bacteria Phylum")) +
theme(panel.grid.major.x = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
xlab(element_blank()) + ylab(element_blank()) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, face = "bold")) +
theme(axis.text.y = element_text(face = "bold")) +
theme(plot.title = element_text(size = 11, face = "bold")) + scale_y_discrete(limits=rev) +
theme(strip.text.x = element_text(size = 12,face = "bold")) +
geom_hline(yintercept=seq(1.5, length(unique(corr_df_b$X)),1), colour='grey')
gb
the_heatmap <- ggarrange(gb,gf, ncol = 2, common.legend = TRUE, legend = "right",
labels = c("A","B"))
ggsave("Environmental_Corr_Heatmap.png", plot = the_heatmap, path = "Results_Figs_Tables/Quick_Figures", dpi = 700,
width = 10, height = 10, units = c("in"), device = "png")