-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb3_exp_refseq_bac_fun
225 lines (205 loc) · 10.5 KB
/
fb3_exp_refseq_bac_fun
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
###############################################################
# Explanatory analysis of metatranscriptomic data #
# Data: Hiseq - RefSeq Bacteria #
# Mona Parizadeh - 2020-2021 #
###############################################################
# Load libraries
library(phyloseq); packageVersion("phyloseq") #‘1.34.0’
library(vegan); packageVersion("vegan") #‘2.5.7’
library(ggplot2); packageVersion("ggplot2") #‘3.3.3’
library(tidyverse)
# Import data ####
setwd("~/Documents/article3/metatranscriptomics_dbCor/")
ps = readRDS("ps_ref_bac_dbCor_clean_noHP_rare.rds")
ps
fun = read.delim("function_names_bac_dbCor_clean_noHP_rare.tsv")
dim(fun)
#Richness ####
gene.rich = estimate_richness(ps, measures = "Observed") #gene per sample (richness)
summary(gene.rich)
sd(gene.rich$Observed, na.rm=TRUE) /
sqrt(length(gene.rich$Observed[!is.na(gene.rich$Observed)])) #SE
#Top 10 ####
#relative abundance
ps.ra = transform_sample_counts(ps, function(otu) otu/sum(otu))
#melt
ps.tab = psmelt(ps.ra); dim(ps.tab)
#merge
ps.mrg = merge(ps.tab, fun, by.x = "OTU", by.y = "id", all = TRUE)
head(ps.mrg); dim(ps.mrg)
ps.mrg.ra.ord = ps.mrg %>%
group_by(function.) %>%
# Add abundances within each genus
summarize_at("Abundance", sum) %>%
arrange(dplyr::desc(Abundance)) %>%
mutate(rel_abund = Abundance/sum(Abundance)*100)
ps.mrg.ra.ord[1:10,]
sum(ps.mrg.ra.ord[1:10,3]) #sum(ps.mrg.ra.ord[1:10,]$rel_abund)
top = ps.mrg.ra.ord[1:10,1]
#top = names(sort(taxa_sums(ps), decreasing=TRUE))[1:10]
fun.top = fun[which(fun$function. %in% top$function.),]
dim(fun.top)
ps.top = prune_taxa(fun.top$id, ps)
sum(taxa_sums(ps.top))/sum(taxa_sums(ps)) * 100
ps.top.ra = transform_sample_counts(ps.top, function(otu) otu/sum(otu))
ps.top.tab = psmelt(ps.top.ra)
ps.top.mrg = merge(ps.top.tab, fun.top, by.x = "OTU", by.y = "id", all = TRUE,
no.dups = TRUE)
dim(ps.top.mrg)
#correct function names (Capital first letter)
ps.top.mrg$function.[which(ps.top.mrg$function. == "elongation factor G")] = "Elongation factor G"
ps.top.mrg$function.[which(ps.top.mrg$function. == "elongation factor Tu")] = "Elongation factor Tu"
ps.top.mrg$function.[which(ps.top.mrg$function. == "endopeptidase La")] = "Endopeptidase La"
ps.top.mrg$function.[which(ps.top.mrg$function. == "molecular chaperone DnaK")] = "Molecular chaperone DnaK"
ps.top.mrg$function.[which(ps.top.mrg$function. == "molecular chaperone GroEL")] = "Molecular chaperone GroEL"
#Fig.S1B####
top = ggplot(ps.top.mrg, aes(x = sampleid, y = Abundance)) +
theme_bw() +
geom_bar(aes(fill = function.), stat = "identity") +
facet_wrap(~year+neonic, scales = "free_x", ncol = 4,
labeller=labeller(neonic = c(N="Control",Y="Neonicotinoid-treated"))) +
scale_x_discrete("Sample ID") +
scale_y_continuous("Total Relative Abundance") +
scale_fill_manual(name = "Ten Most Abundant Soil RefSeq\n Bacterial Functional Categories",
values = c("darkorchid4","chocolate3","tan2","darkgoldenrod1","tan4",
"bisque4","burlywood3","darkolivegreen4","blue2","brown3")) +
#scale_fill_viridis_d(name = "Ten Most Abundant Bacterial Functional Activities",option = "D") +
theme(axis.text.x = element_text(size = 10, angle = 90), #axis
axis.text.y = element_text(size = 10),
axis.title.x = element_text(size = 12, face = "bold"), #main x-axis label
axis.title.y = element_text(size = 12, face = "bold"), #main y-axis label
strip.text.x = element_text(size=9, face="bold"),
legend.title = element_text(size = 12, face="bold"),
legend.text=element_text(size=10),
legend.position = 'right') +
labs(tag = "B)") + theme(plot.tag = element_text(size = 20, face = "bold"))
top
library(cowplot); packageVersion("cowplot") #"‘1.1.1’"
save_plot("Documents/article3/metatranscriptomics_dbCor/graphs/ParizadehM_Fig.S2B_bac_fun_top.pdf",top, ncol = 2, nrow = 2)
#PERMANOVA ####
#make dataframe
df = as(sample_data(ps), "data.frame")
#bray-curtis distance
dis = phyloseq::distance(ps, method = "bray")
#permanova
set.seed(411)
adns = adonis2(dis ~ year/month*neonic, df)
adns
# Homogeneity of dispersion test ####
beta = betadisper(dis, df$neonic)
beta
#ANOVA
set.seed(1233)
permutest(beta)
#Ordination ####
pcoa = ordinate(ps, method = "MDS", k = 2, try = 100, distance = "bray")
plot_ordination(ps, pcoa, color = "neonic", shape = "year") + geom_point(size=5) + ggtitle("PCoA") +
geom_text(aes(label = sampleid), check_overlap = TRUE, size = 3, nudge_y = -0.01) + #nudge_x to seperate id from point
scale_color_manual(name = "Treatment", values = c("chartreuse4", "darkred")) +
facet_wrap(~month)
plot_ordination(ps, pcoa, color = "month", shape = "neonic") + geom_point(size=5) + ggtitle("PCoA") +
geom_text(aes(label = sampleid), check_overlap = TRUE, size = 3, nudge_y = -0.01)+ #nudge_x to seperate id from point
scale_color_manual(name = "Month", values = c("chartreuse4", "darkred")) +
facet_wrap(~year)
pcoa1 = plot_ordination(ps,pcoa)
#define variables as factors
yr = get_variable(ps, "year")
sample_data(ps)$year = factor(yr)
mnt = get_variable(ps, "month")
sample_data(ps)$month = factor(mnt)
neo = get_variable(ps, "neonic")
sample_data(ps)$neonic = factor(neo)
# #% variables
# group.neo.yr.mnt = paste(neo,mnt,yr, sep = "")
# group.yr.mnt = paste(mnt,yr, sep = "")
group.neo.mnt = paste(neo,mnt, sep = "")
#define as factor
group.neo.mnt.fac = factor(group.neo.mnt, levels = c("NJune","YJune",
"NSeptember","YSeptember"))
#Empty points of the PCoA replace them with the desired shapes
pcoa1$layers
pcoa1$layers = pcoa1$layers[-1] #remove the original points to add the desired colors and shapes
#Fig.1B####
p.pcoa = pcoa1 +
theme_bw() +
#group by year and month
stat_ellipse(aes(fill = year, group = year), geom = "polygon",
level = 0.99, linetype = 0, alpha = 0.2, show.legend=TRUE) +
scale_fill_manual(name="Year ellipses",values = c("cornflowerblue","darkgoldenrod3")) +
geom_point(size = 5, alpha = 0.9, aes(color = group.neo.mnt.fac, shape = group.neo.mnt.fac)) +
#geom_text(aes(label=sampleid)) +
scale_shape_manual(name = "Treatment & Month",
labels = c("June - Control","June - Neonicotinoid-treated",
"September - Control","September - Neonicotinoid-treated"),
values = c(19,17, 19,17)) +
scale_x_reverse() + #reverse the pcoa (mirror)
scale_colour_manual(name = "Treatment & Month",
labels = c("June - Control","June - Neonicotinoid-treated",
"September - Control","September - Neonicotinoid-treated"),
values = c("cyan4","cyan4","darkgoldenrod3","darkgoldenrod3")) +
theme(axis.text.x = element_text(size = 12), #axis
axis.text.y = element_text(size = 12),
axis.title = element_text(size = 12, face = "bold"), #main axis label
legend.title = element_text(size=12, face="bold"),legend.text=element_text(size=12),
legend.position = "none") +
guides(fill = guide_legend(override.aes=list(shape=15))) +
labs(tag = "B)") + theme(plot.tag = element_text(size = 20, face = "bold"))
p.pcoa
library(cowplot); packageVersion("cowplot") #"‘1.1.1’"
save_plot("Documents/article3/metatranscriptomics_dbCor/graphs/ParizadehM_Fig.1B_bac_fun_pcoa.pdf",p.pcoa, ncol = 2, nrow = 2)
#Richness ####
shn.rich = cbind(estimate_richness(ps,measures = 'shannon'),
sample_data(ps))
library(ggpubr); packageVersion("ggpubr") #‘0.3.0’
compare_means(Shannon ~ neonic, data = shn.rich, method = "wilcox.test", p.adjust.method = "holm",
group.by = c("month","year"))
compare_means(Shannon ~ year, data = shn.rich, method = "wilcox.test", p.adjust.method = "holm",
group.by = c("month","neonic"))
compare_means(Shannon ~ month, data = shn.rich, method = "wilcox.test", p.adjust.method = "holm")
compare_means(Shannon ~ month, data = shn.rich, method = "wilcox.test", p.adjust.method = "holm",
group.by = "neonic")
compare_means(Shannon ~ year, data = shn.rich, method = "wilcox.test", p.adjust.method = "holm")
compare_means(Shannon ~ year, data = shn.rich, method = "wilcox.test", p.adjust.method = "holm",
group.by = "neonic")
ggplot(shn.rich, aes(x = month, y = Shannon, color=month)) +
theme_bw() +
geom_boxplot() +
stat_compare_means(comparisons = list(c("June","September")), size = 5,
method = "wilcox.test", label = "p.signif", label.y.npc = 0.5, label.y = c(7.1)) # Add global p-value; #holm
#Richness / month ####
#June
ps.jn = subset_samples(ps, sample_data(ps)$month == "June")
ps.jn = prune_taxa(taxa_sums(ps.jn)>0,ps.jn); ps.jn
shn.rich.jn = cbind(estimate_richness(ps.jn,measures = c('shannon')),
sample_data(ps.jn))
summary(shn.rich.jn$Shannon)
sd(shn.rich.jn$Shannon, na.rm=TRUE) /
sqrt(length(shn.rich.jn$Shannon[!is.na(shn.rich.jn$Shannon)])) #SE
#September
ps.sp = subset_samples(ps, sample_data(ps)$month == "September")
ps.sp = prune_taxa(taxa_sums(ps.sp)>0,ps.sp); ps.sp
shn.rich.sp = cbind(estimate_richness(ps.sp,measures = c('shannon')),
sample_data(ps.sp))
summary(shn.rich.sp$Shannon)
sd(shn.rich.sp$Shannon, na.rm=TRUE) /
sqrt(length(shn.rich.sp$Shannon[!is.na(shn.rich.sp$Shannon)])) #SE
#neonic-September - YEAR
ps.sp.neo = subset_samples(ps.sp, sample_data(ps.sp)$neonic == "Y")
ps.sp.neo = prune_taxa(taxa_sums(ps.sp.neo)>0,ps.sp.neo); ps.sp.neo
#year 2016
ps.sp.neo.16 = subset_samples(ps.sp.neo, sample_data(ps.sp.neo)$year == "2016")
ps.sp.neo.16 = prune_taxa(taxa_sums(ps.sp.neo.16)>0,ps.sp.neo.16); ps.sp.neo.16
shn.rich.sp.neo.16 = cbind(estimate_richness(ps.sp.neo.16,measures = c('shannon')),
sample_data(ps.sp.neo.16))
summary(shn.rich.sp.neo.16$Shannon)
sd(shn.rich.sp.neo.16$Shannon, na.rm=TRUE) /
sqrt(length(shn.rich.sp.neo.16$Shannon[!is.na(shn.rich.sp.neo.16$Shannon)])) #SE
#year 2017
ps.sp.neo.17 = subset_samples(ps.sp.neo, sample_data(ps.sp.neo)$year == "2017")
ps.sp.neo.17 = prune_taxa(taxa_sums(ps.sp.neo.17)>0,ps.sp.neo.17); ps.sp.neo.17
shn.rich.sp.neo.17 = cbind(estimate_richness(ps.sp.neo.17,measures = c('shannon')),
sample_data(ps.sp.neo.17))
summary(shn.rich.sp.neo.17$Shannon)
sd(shn.rich.sp.neo.17$Shannon, na.rm=TRUE) /
sqrt(length(shn.rich.sp.neo.17$Shannon[!is.na(shn.rich.sp.neo.17$Shannon)])) #SE
save.image("~/Documents/article3/metatranscriptomics_dbCor/a3_fb3_exp_refseq_bac_fun.Rdata")