-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.exploratory_analysis.r
More file actions
277 lines (237 loc) · 8.71 KB
/
3.exploratory_analysis.r
File metadata and controls
277 lines (237 loc) · 8.71 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
#------ exploratory_analysis.r -------------------------------------
# This script explores the filtered data in broad strokes -
# It finds kingdom distribution, quantifies abundance and richness,
# and calculates alpha and beta diversity.
#------------------------------------------------------------
## @knitr include
# Includes ------------
library(tidyverse)
library(magrittr)
library(cowplot)
library(reshape2)
library(vegan)
library(igraph)
library(GUniFrac)
library(infomapecology)
check_infomap()
source('functions.R')
# Load data --------------------------------
ASV_Core_30 <- read_csv('local_output/core_ASV_30.csv') %>%
mutate(Farm=factor(Farm, levels = c("UK1","UK2","IT1","IT2","IT3","FI1",'SE1')))
## @knitr END
# ASV analysis --------------------------------
## @knitr ASV_analysis
## Richness per cow --------------------------------
plt_richness_per_cow <-
ASV_Core_30 %>%
group_by(Cow_Code) %>%
summarise(Richness=n_distinct(ASV_ID)) %>%
arrange(desc(Richness)) %>%
ggplot(aes(Richness))+
geom_histogram(fill='dark green', color='white')+
labs(x='ASVs per cow', y='Count') +
html_figs_theme_no_legend
## Richness per farm --------------------------------
richness_per_farm <- ASV_Core_30 %>%
group_by(Farm) %>%
summarise(ASV_Richness=n_distinct(ASV_ID))
plt_richness_per_farm <-
richness_per_farm %>%
arrange(desc(ASV_Richness)) %>%
ggplot(aes(Farm,ASV_Richness))+
geom_col(fill='dark red', color='white')+
theme_classic() +
labs(y='Number of ASVs') +
html_figs_theme_no_legend
png(filename = 'local_output/figures/ASV_richness.png', width = 2300, height = 1000, res = 300)
plot_grid(plt_richness_per_cow, plt_richness_per_farm,
nrow = 1, ncol = 2, labels = c('(A)','(B)'),vjust = 1.1)
dev.off()
## Richness per cow per farm --------------------------------
Richness_per_cow_farm <-
ASV_Core_30 %>%
group_by(Farm,Cow_Code) %>%
summarise(S_cow=n_distinct(ASV_ID)) %>%
group_by(Farm) %>%
summarise(S_mean=round(mean(S_cow),1),
S_median=round(median(S_cow),1),
S_sd=round(sd(S_cow),1),
S_min=min(S_cow),
S_max=max(S_cow)) %>%
mutate(ASV_summary=paste(S_mean,' [',S_min,'-',S_max,']',sep=''))
# cow stats per all cows
ASV_Core_30 %>%
group_by(Farm,Cow_Code) %>%
summarise(S_cow=n_distinct(ASV_ID)) %>%
group_by(Farm) %>%
summarise(S_mean=round(mean(S_cow),1),
S_median=round(median(S_cow),1),
S_sd=round(sd(S_cow),1),
S_min=min(S_cow),
S_max=max(S_cow)) %>%
mutate(ASV_summary=paste(S_mean,' [',S_min,'-',S_max,']',sep=''))
plt_richness_per_cow_farm <-
ASV_Core_30 %>%
group_by(Country,Farm,Cow_Code) %>%
summarise(richness=n_distinct(ASV_ID)) %>%
arrange(desc(richness)) %>%
ggplot(aes(x=Farm, y=richness, Country))+
geom_boxplot(aes(color=Country))+
theme_bw() +
labs(y = 'ASV richness per cow per farm') +
paper_figs_theme_no_legend
## @knitr END
## Number of cows in which microbes occur -----------
p1=ASV_Core_30 %>%
group_by(ASV_ID) %>%
summarise(habitats=n_distinct(Cow_Code)) %>%
ggplot(aes(habitats))+
geom_histogram(fill='brown', color='white')+
labs(x='Number of cows', y='Count')+
paper_figs_theme_no_legend
## Number of farms in which microbes occur -----------
p2=ASV_Core_30 %>%
group_by(ASV_ID) %>%
summarise(habitats=n_distinct(Farm)) %>%
arrange(desc(habitats)) %>%
group_by(habitats) %>%
summarise(n=n_distinct(ASV_ID)) %>%
ggplot(aes(habitats, n))+
geom_col(fill='dark green')+
labs(x='Number of farms', y='Count')+
scale_x_continuous(breaks = seq(0,7,1))+
paper_figs_theme_no_legend
pdf(paste(paper_output_path, "microbe_occurrence.pdf", sep = ""), 10, 6)
plot_grid(p1,p2,nrow = 1, ncol = 2, labels = c('(A)','(B)'))
dev.off()
# Per-farm network stats --------------
## Number of cows per farm -------
cows_per_farm <- ASV_Core_30 %>%
group_by(Farm) %>%
summarise(cow_num=n_distinct(Cow_Code))
cows_per_farm %>%
ggplot(aes(Farm,cow_num))+
geom_col(fill='dark green', color='white')+
theme_bw() +
labs(y = 'Number of cows per farm') +
theme(axis.text = element_text(size=10, color='black'),
title = element_text(size=14), axis.text.x = element_text(angle = 90))
# Save ASV Summary table for the farms ----
# This it part of Table 1 in the paper
# the rest will be completed after constructing the networks on the HPC
# on scripts "cooccurrence_network_analysis.r"
cows_per_farm %>%
left_join(richness_per_farm) %>%
left_join(Richness_per_cow_farm %>% select(Farm, ASV_summary)) %>%
write_csv('local_output/summary_table_pre_network.csv')
# Cows summary across farms ------
ASV_Core_30 %>%
group_by(Cow_Code) %>%
summarise(S_cow=n_distinct(ASV_ID)) %>%
summarise(S_mean=mean(S_cow),
S_median=median(S_cow),
S_sd=sd(S_cow),
S_min=min(S_cow),
S_max=max(S_cow)) %>%
mutate(ASV_summary=paste(S_mean,' [',S_min,'-',S_max,']',sep=''))
## @knitr betadiv
# ASV Beta diversity between farms ----------------
ASV_occurrence_farm <-
ASV_Core_30 %>%
select(-c(Cow_Code)) %>%
distinct(Farm, ASV_ID) %>%
mutate(present=1) %>%
spread(ASV_ID, present, fill = 0) %>%
column_to_rownames("Farm")
## Using Jaccard ----
beta_diver_farms <- as.matrix(1-vegdist(ASV_occurrence_farm, "jaccard"))
diag(beta_diver_farms) <- 1
# Heatmap
beta_diver_farms[lower.tri(beta_diver_farms, diag = F)] <- NA
beta_diver_farms_m <- reshape2::melt(beta_diver_farms)
plt_J <- ggplot(beta_diver_farms_m, aes(x = Var1, y = Var2, fill = value, label=round(value,2))) +
geom_tile() +
geom_text(color = "black", size = 4)+
scale_y_discrete(limits=rev)+
scale_fill_gradient(high = "blue", low = "light blue", na.value = 'white') +
html_figs_theme_no_legend+theme(axis.title = element_blank())
## Using UniFrac ----
phylo_tree <- readRDS("local_output/fitted_asvs_phylo_tree.rds")
tree <- phylo_tree$tree
# prune the tree
included_asvs <- unique(ASV_Core_30$ASV_ID)
unincluded <- tree$tip.label[!tree$tip.label %in% included_asvs]
pruned <- dendextend::prune(tree, unincluded)
unifracs <- GUniFrac(ASV_occurrence_farm, pruned, alpha=c(0, 0.5, 1))$unifracs
d_UW <- 1-(unifracs[, , "d_UW"])
# Heatmap
d_UW[lower.tri(d_UW)] <- NA
beta_diver_farms_UF <- reshape2::melt(d_UW)
plt_U <-
ggplot(beta_diver_farms_UF, aes(x = Var1, y = Var2, fill = value, label=round(value,2))) +
geom_tile() +
geom_text(color = "black", size = 4)+
scale_y_discrete(limits=rev)+
scale_fill_gradient(high = "#ff8c00", low = "#fff494", na.value = 'white') +
html_figs_theme_no_legend+theme(axis.title = element_blank())
plt_beta_div <- plot_grid(plt_J,plt_U, labels = c('(A)','(B)'))
## @knitr END
# Taxonomic analysis ------------------
ASV_taxa <- read_csv('local_output/ASV_full_taxa.csv') %>%
select(ASV_ID, everything(), -seq16S)
n_distinct(ASV_Core_30$ASV_ID)
## Phylum-level composition --------------------------------------------
ASV_Core_30 %>%
left_join(ASV_taxa) %>%
group_by(Farm, Phylum) %>%
summarise(ASV_num=n_distinct(ASV_ID)) %>%
drop_na() %>%
mutate(relative_richness=ASV_num/sum(ASV_num)) %>%
mutate(ypos = cumsum(relative_richness)- 0.5*relative_richness) %>%
ggplot(aes(x="", y=relative_richness, fill=Phylum))+
facet_wrap(~Farm)+
geom_bar(stat="identity", width=1) +
# scale_fill_manual(values = signif_colors)+
# geom_text(aes(y = ypos, label = round(prop,2)), color = "white", size=3) +
coord_polar("y", start=0)+
paper_figs_theme+
theme(axis.text = element_blank(),
axis.title = element_blank())
## composition in all ASVs --------------------------------------------
### Phylum-level
percents_ph <- ASV_Core_30 %>%
left_join(ASV_taxa) %>%
group_by(Phylum) %>%
summarise(ASV_num=n_distinct(ASV_ID)) %>%
drop_na() %>%
mutate(relative_richness=ASV_num/sum(ASV_num)) %>%
mutate(percnt = 100*ASV_num/sum(ASV_num))
### Family-level
percents_fa <- ASV_Core_30 %>%
left_join(ASV_taxa) %>%
group_by(Family) %>%
summarise(ASV_num=n_distinct(ASV_ID)) %>%
drop_na() %>%
mutate(relative_richness=ASV_num/sum(ASV_num)) %>%
mutate(percnt = 100*ASV_num/sum(ASV_num))
# For paper----
## Number of cows in which microbes occur -----------
ASV_Core_30 %>%
group_by(ASV_ID) %>%
summarise(habitats=n_distinct(Cow_Code)) %>%
ggplot(aes(habitats))+
geom_histogram(fill='brown', color='white')+
labs(x='Number of cows', y='Count')+
html_figs_theme_no_legend
## Number of farms in which microbes occur -----------
ASV_Core_30 %>%
group_by(ASV_ID) %>%
summarise(habitats=n_distinct(Farm)) %>%
arrange(desc(habitats)) %>%
group_by(habitats) %>%
summarise(n=n_distinct(ASV_ID)) %>%
ggplot(aes(habitats, n))+
geom_col(fill='dark green')+
labs(x='Number of farms', y='Count')+
scale_x_continuous(breaks = seq(0,7,1))+
paper_figs_theme_no_legend