-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoss_Network.R
270 lines (183 loc) · 7.24 KB
/
Moss_Network.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
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
################################################################################
### Peatland microbes network analysis
### Authors: Rachel Korn
### [email protected] University of Fribourg 2021 - 2024
################################################################################
library("phyloseq")
library("reshape2")
library("igraph")
library("Hmisc")
library("indicspecies")
rm(list = ls())
gc()
set.seed(7113)
################################################################################
### ASV data ###################################################################
moss <- readRDS("rds/Moss_moss.RDS")
moss
## Extract ASVs, taxonomy and merge
otus <- data.frame(t(otu_table(moss)))
tax <- data.frame(tax_table(moss))
otus.tax <- merge(tax, otus, by = 0)
names(otus.tax)[1] <- "ASV"
head(otus.tax)
## Fill unannotated genera with higher-level taxa
otus.tax$Genus <- ifelse(is.na(otus.tax$Genus), otus.tax$Family, otus.tax$Genus)
otus.tax$Genus <- ifelse(is.na(otus.tax$Genus), otus.tax$Order, otus.tax$Genus)
otus.tax$Genus <- ifelse(is.na(otus.tax$Genus), otus.tax$Class, otus.tax$Genus)
otus.tax$Genus <- ifelse(is.na(otus.tax$Genus), otus.tax$Phylum, otus.tax$Genus)
## Abbreviate Incertae_Sedis to IS and merge it with its higher taxonomy
otus.tax$Genus[otus.tax$Genus == "Incertae_Sedis"] <- "IS"
otus.tax[4:6][otus.tax[4:6] == "Incertae_Sedis"] <- NA
otus.tax$Genus[!is.na(otus.tax$Family) & otus.tax$Genus == "IS"] <- paste(
otus.tax$Family[!is.na(otus.tax$Family) & otus.tax$Genus == "IS"],
otus.tax$Genus[!is.na(otus.tax$Family) & otus.tax$Genus == "IS"],
sep = ":"
)
otus.tax$Genus[!is.na(otus.tax$Order) & otus.tax$Genus == "IS"] <- paste(
otus.tax$Order[!is.na(otus.tax$Order) & otus.tax$Genus == "IS"],
otus.tax$Genus[!is.na(otus.tax$Order) & otus.tax$Genus == "IS"],
sep = ":"
)
otus.tax$Genus[!is.na(otus.tax$Class) & otus.tax$Genus == "IS"] <- paste(
otus.tax$Class[!is.na(otus.tax$Class) & otus.tax$Genus == "IS"],
otus.tax$Genus[!is.na(otus.tax$Class) & otus.tax$Genus == "IS"],
sep = ":"
)
otus.tax$Genus[!is.na(otus.tax$Phylum) & otus.tax$Genus == "IS"] <- paste(
otus.tax$Phylum[!is.na(otus.tax$Phylum) & otus.tax$Genus == "IS"],
otus.tax$Genus[!is.na(otus.tax$Phylum) & otus.tax$Genus == "IS"],
sep = ":"
)
grep("IS", otus.tax$Genus, value = TRUE)
otus.tax$Genus <- make.unique(otus.tax$Genus)
rownames(otus.tax) <- otus.tax$ASV
## Make a copy for indicator species analysis
otus.tax.is <- otus.tax
## Subset to taxa occuring in at least 8 samples (= at least 20 %)
otus.pa <- otus
otus.pa[otus.pa > 0] <- 1
otus.pa <- otus.pa[rowSums(otus.pa) > 7, ]
summary(rowSums(otus.pa))
otus.tax.is <- otus.tax[otus.tax.is$ASV %in% rownames(otus.pa), ]
summary(rowSums(otus.pa))
## Reformat
otus.tax.is <- otus.tax.is[, -c(1:7)]
otus.tax.is <- data.frame(t(otus.tax.is))
## Metadata
mossMeta <- read.table("csv/Mosses_Metadata_unscaled.csv", sep = "\t",
header = TRUE)
summary(rownames(otus.tax.is) == mossMeta$FullID)
### Indicator species for soil reaction
range(mossMeta$Soil.reaction)
mossMeta$Soil.reactionC <- mossMeta$Soil.reaction
mossMeta$Soil.reactionC[mossMeta$Soil.reactionC < 2] <- "Strong acidic"
mossMeta$Soil.reactionC[mossMeta$Soil.reactionC < 4] <- "Acidic"
mossMeta$Soil.reactionC[mossMeta$Soil.reactionC < 5] <- "Moderate acidic"
mossMeta$Soil.reactionC
table(mossMeta$Soil.reactionC)
indval <- multipatt(otus.tax.is, mossMeta$Soil.reactionC,
control = how(nperm = 999), duleg = TRUE)
summary(indval)
sr <- data.frame(indval$str)
### Export indicator species table
sr.otus <- sr
sr.otus$ASV <- rownames(sr.otus)
sr.otus <- merge(sr.otus, otus.tax[, c(1:7)], by = "ASV")
sr.otus[, 2:4] <- format(round(sr.otus[, 2:4], 2), nsmall = 3)
# write.table(sr.otus, "csv/Mosses_IndicatorSpecies.csv", sep = "\t",
# row.names = FALSE)
## Arrange for network analysis
sr[sr < 0.7] <- NA
sr$Acidic[!is.na(sr$Acidic)] <- "Acidic"
sr$Moderate.acidic[!is.na(sr$Moderate.acidic)] <- "Moderate acidic"
sr$Strong.acidic[!is.na(sr$Strong.acidic)] <- "Strong acidic"
sr$SoilReaction <- apply(sr, 1, function(x) x[!is.na(x)][1])
sr <- sr["SoilReaction"]
sr$ASV <- rownames(sr)
sr <- sr[complete.cases(sr), ]
table(sr)
table(sr$SoilReaction)
################################################################################
# Co-occurrence network with rank correlation ##################################
## Presence/absence and taxa that occur at least in 8 (so that they can all
## occur within a site) samples and then, subset
## Total abundance
otus.tax$Abundance <- rowSums(otus.tax[, 8:47])
## Correlation analysis based on Spearman's coefficient
otus.dist <- rcorr(t(otus), type = "spearman")
otus.cor <- otus.dist$r
otus.cor.p <- otus.dist$P
## Multiple testing correction using Benjamini-Hochberg standard FDR correction
otus.cor.p <- p.adjust(otus.cor.p, method = "BH")
## Positive and netagive cooccurence at given coefficient and p-value cutoff
cor.cutoff <- 0.7
p.cutoff <- 0.005
otus.cor[which(otus.cor >= (- cor.cutoff) & otus.cor <= cor.cutoff)] <- 0
otus.cor[which(otus.cor.p > p.cutoff)] <- 0
diag(otus.cor) <- 0
## Delete rows and columns with sum = 0
otus.cor <- otus.cor[which(rowSums(otus.cor) != 0), ]
otus.cor <- otus.cor[, which(colSums(otus.cor) != 0)]
dim(otus.cor)
### Create a graph
g3 <- graph.adjacency(otus.cor,
weight = TRUE, # weight = correlation
mode = "undirected")
g3 <- simplify(g3) # remove duplicate and loop edges
### Add taxonomy
## Get taxa in network and merge with taxonomy
target <- data.frame(ASV = V(g3)$name,
to_sort = seq(1:length(V(g3)$name)))
otu.target <- merge(target, otus.tax, by = "ASV")
otu.target <- otu.target[order(otu.target$to_sort), ]
table(sr$SoilReaction)
intersect(otu.target$ASV, sr$ASV)
length(intersect(otu.target$ASV, sr$ASV))
## Add indicator species
otu.target <- merge(otu.target, sr, by = "ASV", all.x = TRUE)
# table(otu.target$SoilReaction)
otu.target <- otu.target[order(otu.target$to_sort), ]
E(g3)
E(g3)$weight
V(g3)
V(g3)$name
### Add edge attributes
E(g3)$scale <- ifelse(E(g3)$weight < 0,
E(g3)$weight * -1,
E(g3)$weight)
E(g3)$sign <- ifelse(E(g3)$weight < 0,
"Negative",
"Positive")
### Add vertex attributes
g3 <- set_vertex_attr(g3, "degree", value = degree(g3))
g3 <- set_vertex_attr(g3, "ASV", value = otu.target$ASV)
V(g3)$ASV
g3 <- set_vertex_attr(g3, "Domain", value = otu.target$Domain)
V(g3)$Domain
g3 <- set_vertex_attr(g3, "Phylum", value = otu.target$Phylum)
V(g3)$Phylum
g3 <- set_vertex_attr(g3, "Genus", value = otu.target$Genus)
V(g3)$Genus
g3 <- set_vertex_attr(g3, "Abundance", value = sqrt(otu.target$Abundance))
V(g3)$Abundance
### Indicator species
V(g3)$SoilReaction <- otu.target$SoilReaction
## Export graph
write_graph(g3,
paste("graphml/Co-occurrence", cor.cutoff, p.cutoff, ".graphml",
sep = "_"),
format = "graphml")
## Network statistics
gsize(g3) # = ecount(g3)
vcount(g3)
vertex_connectivity(g3)
table(E(g3)$sign)
table(V(g3)$Domain)
table(V(g3)$SoilReaction)
table(degree(g3))
g3.degree <- as.data.frame(table(degree(g3)))
summary(degree(g3))
barplot(sort(degree_distribution(g3)))
## Subnetworks
components(g3)