-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_Subnetwork_analysis.R
545 lines (439 loc) · 18.2 KB
/
11_Subnetwork_analysis.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
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
####################################################################################################################
# Plasmid rumen network analysis
#
# Script 11: Analysis of subnetworks
#
#
# The following outputs are used for downstream analysis:
# hgt.netdat.et.Rda, distdisp.netdat.et.Rda, recdisp.netdat.et.Rda,
# hgt.node.id.Rda, disp.node.id.Rda, recdisp.node.id.Rda, distdisp.node.id.Rda
#
# The following figures are created:
# Figure 3A, network visualization of subnetworks
# Figure 3B, histogram of edge-weights
#
# Script tested for R version 4.1.1
####################################################################################################################
####################################################################################################################
# SCRIPT SET-UP
####################################################################################################################
# Set working directory to wherever your files are located
# Load the necessary packages:
library(tidyverse)
library(igraph)
# Starting files:
# Edgelist with edgeweights created in script 03_Network_setup.R
load("net.dat2k.ew.Rda")
# Edgelist prior to edge-weight creation created in script 02_Threshold_sensitivity.R
load("thresh2000.a.Rda")
# Node id table created in script 08_Infomap_analysis_full_network.R
load("plas.2k.list.Rda")
# Infomap file created in script 08_Infomap_analysis_full_network
load("plas_mods.df.Rda")
####################################################################################################################
####################################################################################################################
# Section 1: IDENTIFY BREAK-POINTS IN EDGE-WEIGHTS FOR SUBNETWORKS
####################################################################################################################
# Label edges by subnetwork:
net.dat2k.ew.col <- net.dat2k.ew %>%
mutate(Subnetwork = case_when(
weight <= 0.5 ~ "HGT",
weight > 0.5 & weight < 0.95 ~ "Distant dispersal",
weight >= 0.95 ~ "Recent dispersal"
)) %>%
ungroup() %>%
mutate(Subnetwork = factor(Subnetwork, levels=c("HGT", "Distant dispersal","Recent dispersal")))
# Visualize the distribution of edge-weights, identify breaks
# Create Figure 3B: Histogram of edge-weights
ew.hist.col <- ggplot(net.dat2k.ew.col, aes(x=weight, fill=Subnetwork))+
geom_histogram(color="black", breaks=seq(0.2,1,0.05)) +
scale_fill_manual(values=c("indianred1", "thistle2","slategray2")) +
theme_classic() +
theme(axis.text.y = element_text(size = 20),
axis.text.x = element_text(size = 20),
axis.title.y=element_text(size=24),
axis.title.x=element_text(size=24)) +
labs(x = "Edge Weight", y="Count (Plasmids)" ) +
geom_vline(xintercept = 0.5, color = "gray59", size=2) +
geom_vline(xintercept = 0.95, color = "gray59", size=2)
# Save in desired format
ggsave(ew.hist.col, filename="ew.distr.colorbar.png", dpi = 900, width = 8, height = 6, units = "in")
ggsave(ew.hist.col, filename="ew.distr.colorbar.pdf", width = 8, height = 6, units = "in")
####################################################################################################################
####################################################################################################################
# Section 2: RANDOMIZE EDGE-WEIGHT DISTRIBUTION BY SHUFFLING ALIGNMENT LENGTH BETWEEN PLASMIDS
####################################################################################################################
set.seed(123)
# Function to shuffle length of alignment between plasmids
# NOTE: Uses thresh2000.a dataframe, NOT the network edgelist, in order to recalculate the edge-weight
# with the shuffled alignment length.
shuff.len.fun <- function(x) {
transform(thresh2000.a, length = sample(length, replace = F) )
}
# Choose how many times to shuffle the data using the function created above.
# Creates a list with each shuffling
shuff.len.list <- (lapply(seq(1000),shuff.len.fun))
# Give each shuffled list a name corresponding to the number of its shuffling
names(shuff.len.list) <- seq(1000)
# Create a new column that ID's the shuffling repetition number of each iteration
# Bind each list together into a single dataframe.
shuff.len.df <- Map(cbind, shuff.len.list, shuff.rep = names(shuff.len.list)) %>%
bind_rows()
# Recalculate edge-weights on this reshuffled dataframe
ew.len.shuff <- shuff.len.df %>%
group_by(uniq_pair, shuff.rep) %>%
mutate(align.proportion.query = length/plasmid.length.query,
align.proportion.sub = length/plasmid.length.subject ) %>%
rowwise() %>%
# Calculate the maximum proportion of plasmid length covered by the alignment
# E.g. the alignment length / length of smaller plasmid
mutate(min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
rowwise() %>%
mutate(preweight = min.align.prop*(pident/100)) %>%
group_by(uniq_pair, shuff.rep) %>%
mutate(weight = sum(preweight)) %>%
select(-min.align.prop, -max.align.prop, -qstart, -qend, -sstart, -send, -evalue,-qs.qe.diff,
-align.proportion.query,-align.proportion.sub) %>%
select(-preweight) %>%
mutate(dat.type = "Shuffled") %>%
distinct()
# Label and bind the observed data and shuffled data
plot.len.dat <- net.dat2k.ew %>%
mutate(dat.type = "Observed",
shuff.rep = "0") %>%
bind_rows(., ew.len.shuff)
# Create Figure S3, comparing the density of edge-weights for the observed data vs the shuffled data:
dens.len.plot <- ggplot(plot.len.dat, aes(x=weight, color=dat.type, fill=dat.type)) + # if you want to scale y, add argument: "y=..scaled.." to aes
geom_density(alpha=0.1, size=1)+
labs(x="Edge-weight", y = "Density", color=NULL,fill=NULL) +
theme_classic() +
scale_fill_viridis(end =0.5, discrete = T) +
scale_color_viridis(end =0.5, discrete=T) +
xlim(0,3) +
theme(legend.text=element_text(size=18),
axis.title=element_text(size=20),
axis.text = element_text(size=16))
# Save in desired format
ggsave(filename="obs.shuff.ew.png", dpi = 1800, width = 8, height = 6, units = "in")
ggsave(filename="obs.shuff.ew.pdf", dpi = 900, width = 8, height = 6, units = "in")
# Compare the distribution of edge-weights between observed and shuffled (alignment length) data
# with a statistical test (Kolmogorov-Smirnov)
shuff.len.ks <- ks.test(net.dat2k.ew$weight, ew.len.shuff$weight)
####################################################################################################################
####################################################################################################################
# Section 3: DIVIDE THE NETWORK INTO SUBNETWORKS BASED ON EDGE-WEIGHTS
####################################################################################################################
# Break up data into 3 subnetworks: HGT, recent dispersal, distant dispersal
# based on the edge-weights
# HGT network (EW < 0.5)
hgt.netdat.et <- net.dat2k.ew %>%
filter(weight <= 0.5) %>%
ungroup() %>%
select(uniq_pair,layer_from, node_from, layer_to, node_to, edge_type, weight)
# Distant dispersal (0.5 < EW < 0.95)
distdisp.netdat.et <- net.dat2k.ew %>%
filter(weight > 0.5 & weight < 0.95) %>%
ungroup() %>%
select(uniq_pair,layer_from, node_from, layer_to, node_to, edge_type, weight)
# Recent dispersal (EW > 0.95)
recdisp.netdat.et <- net.dat2k.ew %>%
filter(weight >= 0.95) %>%
ungroup() %>%
select(uniq_pair,layer_from, node_from, layer_to, node_to, edge_type, weight)
# Save each subnetwork for downstream analysis
save(hgt.netdat.et, file="hgt.netdat.et.Rda")
save(distdisp.netdat.et, file="distdisp.netdat.et.Rda")
save(recdisp.netdat.et, file="recdisp.netdat.et.Rda")
####################################################################################################################
####################################################################################################################
# Section 4: NODE ID TABLE FOR EACH SUBNETWORK
####################################################################################################################
# Node ID table for each subnetwork:
hgt.node.id <- hgt.netdat.et %>%
ungroup() %>%
select(node_from, node_to) %>%
gather(.) %>%
select(-key) %>%
mutate(node_id = as.integer(value)) %>%
select(-value) %>%
distinct() %>%
left_join(., plas.2k.list)
recdisp.node.id <- recdisp.netdat.et %>%
ungroup() %>%
select(node_from, node_to) %>%
gather(.) %>%
select(-key) %>%
mutate(node_id = as.integer(value)) %>%
select(-value) %>%
distinct() %>%
left_join(., plas.2k.list)
distdisp.node.id <- distdisp.netdat.et %>%
ungroup() %>%
select(node_from, node_to) %>%
gather(.) %>%
select(-key) %>%
mutate(node_id = as.integer(value)) %>%
select(-value) %>%
distinct() %>%
left_join(., plas.2k.list)
# Save this data:
save(hgt.node.id, file = "hgt.node.id.Rda")
save(recdisp.node.id, file = "recdisp.node.id.Rda")
save(distdisp.node.id, file = "distdisp.node.id.Rda")
####################################################################################################################
####################################################################################################################
# Section 5: FLOW PER SUBNETWORK
####################################################################################################################
# Join the node id's to the infomap data to get flow per plasmid:
plas.flow <- plas_mods.df %>%
select(node_id, flow) %>%
distinct()
# Calculate flow of plasmids in HGT subnetwork
hgt.flow <- hgt.node.id %>%
left_join(., plas.flow)
sum(hgt.flow$flow)
# Calculate flow of plasmids in recent dispersal subnetwork
rec.flow <- recdisp.node.id %>%
left_join(., plas.flow)
sum(rec.flow$flow)
# Calculate flow of plasmids in distant dispesal subnetwork
dist.flow <- distdisp.node.id %>%
left_join(., plas.flow)
sum(dist.flow$flow)
####################################################################################################################
####################################################################################################################
# Section 6: COUNTS OF INTER- TO INTRA-LAYER EDGES IN EACH SUBNETWORK
####################################################################################################################
# HGT
hgt.edg.df <- hgt.netdat.et %>%
count(uniq_pair, edge_type) %>%
spread(., edge_type, n, fill = 0,
drop = FALSE, sep = NULL)
hgt.intra.rat <- hgt.edg.df %>%
ungroup() %>%
count(Intra > 0)
hgt.intra <- 11
hgt.inter.rat <- hgt.edg.df %>%
ungroup() %>%
count(Inter > 0)
hgt.inter <- 223
hgt.rat <- hgt.inter/hgt.intra
# Recent dispersal
recdisp.edg.df <- recdisp.netdat.et %>%
count(uniq_pair, edge_type) %>%
spread(., edge_type, n, fill = 0,
drop = FALSE, sep = NULL)
recdisp.intra.rat <- recdisp.edg.df %>%
ungroup() %>%
count(Intra > 0)
recdisp.intra <- 24
recdisp.inter.rat <- recdisp.edg.df %>%
ungroup() %>%
count(Inter > 0)
recdisp.inter <- 496
recdisp.rat <- recdisp.inter/recdisp.intra
# Distant dispersal
distdisp.edg.df <- distdisp.netdat.et %>%
count(uniq_pair, edge_type) %>%
spread(., edge_type, n, fill = 0,
drop = FALSE, sep = NULL)
distdisp.intra.rat <- distdisp.edg.df %>%
ungroup() %>%
count(Intra > 0)
distdisp.intra <- 67
distdisp.inter.rat <- distdisp.edg.df %>%
ungroup() %>%
count(Inter > 0)
distdisp.inter <- 1917
####################################################################################################################
####################################################################################################################
# Section 7: IDENTIFY PLASMIDS IN MULTIPLE SUBNETWORKS
####################################################################################################################
# HGT + Recent Dispersal
hgt.recdisp <- hgt.node.id %>%
inner_join(., recdisp.node.id)
nrow(hgt.recdisp)
# 35 shared plasmids
# HGT + Distant Dispersal
hgt.distdisp <- hgt.node.id %>%
inner_join(., distdisp.node.id)
nrow(hgt.distdisp)
# 133 shared plasmids
# Recent Dispersal + Distant dispersal
rec.distdisp <- recdisp.node.id %>%
inner_join(., distdisp.node.id)
nrow(rec.distdisp)
# 293 shared plasmids
# In all three
all.three.subs <- hgt.node.id %>%
inner_join(., rec.distdisp)
nrow(all.three.subs)
# 24 shared plasmids
####################################################################################################################
####################################################################################################################
# Section 8: VISUALIZE EACH SUBNETWORK WITH IGRAPH
####################################################################################################################
# HGT network:
# Set up interlayer edges for network edge visualization
hgt.inter.vis <- hgt.netdat.et %>%
filter(edge_type=="Inter") %>%
ungroup() %>%
group_by(layer_from, layer_to) %>%
mutate(weight = n()) %>%
mutate(layer_from = as.numeric(layer_from),
layer_to = as.numeric(layer_to)) %>%
select(layer_from, layer_to, weight) %>%
distinct() %>%
ungroup
# Node list
hgt.inter.list <- hgt.inter.vis %>%
select(layer_from, layer_to) %>%
gather() %>%
select(value) %>%
distinct() %>%
dplyr::rename(layer=value)
# Intralayer edges for node size
hgt.intra.vis <- hgt.netdat.et %>%
filter(edge_type=="Intra") %>%
ungroup() %>%
group_by(layer_from, layer_to) %>%
mutate(intra.edg = n()) %>%
dplyr::rename(layer=layer_from) %>%
ungroup() %>%
select(layer, intra.edg) %>%
bind_rows(., hgt.inter.list) %>%
distinct() %>%
group_by(layer) %>%
arrange(desc(intra.edg)) %>%
slice(1) %>%
distinct() %>%
replace(is.na(.), 0)
# Create a graph
graph.hgt <- graph_from_data_frame(hgt.inter.vis, directed=F, vertices = hgt.intra.vis)
# Set edge weight attribute for visualization
set_edge_attr(graph.hgt, "weight", value= hgt.inter.vis$weight)
is.weighted(graph.hgt)
# Weight node size by number of intralayer edges per node
V(graph.hgt)$size <- 5+hgt.intra.vis$intra.edg
# Plot network
plot(graph.hgt, edge.width=(E(graph.hgt)$weight*0.1),vertex.label=NA, layout=layout_with_lgl, vertex.color="red",edge.color="black")
# Save coordinates of first (HGT) network and apply to all visualizations.
# In this way, each cow will be in the same place for each subnetwork visualization.
Coords <- layout_with_fr(graph.hgt)
# Recent dispersal
# Set up interlayer edges for network edge visualization
rec.inter.vis <- recdisp.netdat.et %>%
filter(edge_type=="Inter") %>%
ungroup() %>%
group_by(layer_from, layer_to) %>%
mutate(weight = n()) %>%
mutate(layer_from = as.numeric(layer_from),
layer_to = as.numeric(layer_to)) %>%
select(layer_from, layer_to, weight) %>%
distinct() %>%
ungroup
# Node list
rec.inter.list <- rec.inter.vis %>%
select(layer_from, layer_to) %>%
gather() %>%
select(value) %>%
distinct() %>%
dplyr::rename(layer=value)
# Intralayer edges for node size
rec.intra.vis <- recdisp.netdat.et %>%
filter(edge_type=="Intra") %>%
ungroup() %>%
group_by(layer_from, layer_to) %>%
mutate(intra.edg = n()) %>%
dplyr::rename(layer=layer_from) %>%
ungroup() %>%
select(layer, intra.edg) %>%
bind_rows(., rec.inter.list) %>%
distinct() %>%
group_by(layer) %>%
arrange(desc(intra.edg)) %>%
slice(1) %>%
distinct() %>%
replace(is.na(.), 0)
# Create a graph from dataframe
graph.rec <- graph_from_data_frame(rec.inter.vis, directed=F, vertices = rec.intra.vis)
# Set edge attributes by weight
set_edge_attr(graph.rec, "weight", value= rec.inter.vis$weight)
is.weighted(graph.rec)
# Set node attribute
V(graph.rec)$size <- 5+rec.intra.vis$intra.edg
# Plot network
plot(graph.rec, edge.width=(E(graph.rec)$weight*0.4),vertex.label=NA, layout=Coords, vertex.color="slategray2", edge.color="black")
# Distant dispersal
# Set up interlayer edges for network edge visualization
dist.inter.vis <- distdisp.netdat.et %>%
filter(edge_type=="Inter") %>%
ungroup() %>%
group_by(layer_from, layer_to) %>%
mutate(weight = n()) %>%
mutate(layer_from = as.numeric(layer_from),
layer_to = as.numeric(layer_to)) %>%
select(layer_from, layer_to, weight) %>%
distinct() %>%
ungroup
# Node list
dist.inter.list <- dist.inter.vis %>%
select(layer_from, layer_to) %>%
gather() %>%
select(value) %>%
distinct() %>%
dplyr::rename(layer=value)
# Intralayer edges for node size
dist.intra.vis <- distdisp.netdat.et %>%
filter(edge_type=="Intra") %>%
ungroup() %>%
group_by(layer_from, layer_to) %>%
mutate(intra.edg = n()) %>%
dplyr::rename(layer=layer_from) %>%
ungroup() %>%
select(layer, intra.edg) %>%
bind_rows(., dist.inter.list) %>%
distinct() %>%
group_by(layer) %>%
arrange(desc(intra.edg)) %>%
slice(1) %>%
distinct() %>%
replace(is.na(.), 0)
# Create graph from dataframe
graph.dist <- graph_from_data_frame(dist.inter.vis, directed=F, vertices = dist.intra.vis)
# Set edge weight attribute
set_edge_attr(graph.dist, "weight", value= dist.inter.vis$weight)
is.weighted(graph.dist)
# Intralayer edges for node size
V(graph.dist)$size <- 5+dist.intra.vis$intra.edg
# Plot network
plot(graph.dist, edge.width=(E(graph.dist)$weight*0.4),vertex.label=NA, layout=Coords, vertex.color="thistle2", edge.color="black")
# Create Figure 3A: Subnetwork plots
# (Each subnetwork plotted separately as a pdf)
# Save pdfs:
pdf("hgt.network.plot.pdf", width = 4, height = 4)
{
plot(graph.hgt, edge.width=(E(graph.hgt)$weight*0.2),vertex.label=NA, layout=Coords, vertex.color="indianred1",edge.color="black")
}
dev.off()
pdf("hgt.network.plot.pdf", width = 4, height = 4)
{
plot(graph.hgt, edge.width=(E(graph.hgt)$weight*0.2),vertex.label=NA, layout=Coords, vertex.color="indianred1",edge.color="black",
vertex.frame.width=0.1)
}
dev.off()
# Recent
pdf("rec.network.plot.pdf", width = 4, height = 4)
{
plot(graph.rec, edge.width=(E(graph.rec)$weight*0.2),vertex.label=NA, layout=Coords, vertex.color="slategray2", edge.color="black",
vertex.frame.width=0.1)
}
dev.off()
# Distant
pdf("dist.network.plot.pdf", width = 5, height = 5)
{
plot(graph.dist, edge.width=(E(graph.dist)$weight*0.2),vertex.label=NA, layout=Coords, vertex.color="thistle2", edge.color="black",
vertex.frame.width=0.1)
}
dev.off()
####################################################################################################################