-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_Threshold_sensitivity.R
447 lines (403 loc) · 22.3 KB
/
02_Threshold_sensitivity.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
####################################################################################################################
# Plasmid rumen network analysis
#
# Script 2: Sensitivity analysis of plasmid and alignment length thresholds
#
#
# The following outputs are used for downstream analysis:
# thresh2000.a.Rda
#
# The following figures are created:
# Supplementary Figure S5A S5B, threshold effects
#
# Script tested for R version 4.1.1
####################################################################################################################
####################################################################################################################
# SCRIPT SET-UP
####################################################################################################################
# Set working directory to wherever your files are located
# Load necessary packages
library(tidyverse)
library(Hmisc)
library(ggpubr)
# Starting files:
# Network data created in script 01_Initial_Data_Processing.R
load("full.net.dat.Rda")
####################################################################################################################
####################################################################################################################
# SECTION 1: FILTER FULL NETWORK BY PLASMID LENGTH AND ALIGNMENT LENGTH
####################################################################################################################
# Filter the full.net.dat dataframe by plasmid length in intervals of 500 bp
# and alignment lengths (20% of plasmid alignment or 20% of the threshold)
# 500 bp plasmid length and alignments of 20% of the shortest plasmid in a pair
thresh500.a <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 500 & plasmid.length.subject >= 500) %>%
# Calculate the proportion of the plasmid length covered by the alignment for both
# plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(min.align.prop >= 0.2) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 28,606 alignments
# 500 bp filter but alignments must cover 20% of 500 (100 bp)
thresh500.b <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 500 & plasmid.length.subject >= 500) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(length >= 0.2*500) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 40,231 alignments
# 1000 bp plasmid length and alignments of 20% of the shortest plasmid in a pair
thresh1000.a <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 1000 & plasmid.length.subject >= 1000) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(min.align.prop >= 0.2) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 12,367 alignments
# 1000 bp filter but alignments must cover 20% of 1000 (200 bp)
thresh1000.b <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 1000 & plasmid.length.subject >= 1000) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(length >= 0.2*1000) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 14,856 alignments
# 1500 bp plasmid length and alignments of 20% of the shortest plasmid in a pair
thresh1500.a <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 1500 & plasmid.length.subject >= 1500) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(min.align.prop >= 0.2) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 7,153 alignments
# 1500 bp filter but alignments must cover 20% of 1500 (300 bp)
thresh1500.b <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 1500 & plasmid.length.subject >= 1500) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(length >= 0.2*1500) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 8,015 alignments
# 2000 bp plasmid length and alignments of 20% of the shortest plasmid in a pair
thresh2000.a <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 2000 & plasmid.length.subject >= 2000) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(min.align.prop >= 0.2) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 2,844 alignments
# 2000 bp filter but alignments must cover 20% of 2000 (400 bp)
thresh2000.b <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 2000 & plasmid.length.subject >= 2000) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(length >= 0.2*2000) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 3,255 alignments
# 2500 bp plasmid length and alignments of 20% of the shortest plasmid in a pair
thresh2500.a <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 2500 & plasmid.length.subject >= 2500) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(min.align.prop >= 0.2) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 1,446 alignments
# 2500 bp filter but alignments must cover 20% of 2500 (500 bp)
thresh2500.b <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 2500 & plasmid.length.subject >= 2500) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(length >= 0.2*2500) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 1,729 alignments
# 3000 bp plasmid length and alignments of 20% of the shortest plasmid in a pair
thresh3000.a <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 3000 & plasmid.length.subject >= 3000) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(min.align.prop >= 0.2) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 710 alignments
# 3000 bp filter but alignments must cover 20% of 3000 (600 bp)
thresh3000.b <- full.net.dat %>%
# Filter by plasmid length
filter(plasmid.length.query >= 3000 & plasmid.length.subject >= 3000) %>%
# Calculate the proportion of the plasmid length covered by the alignment
# for both plasmid subject and plasmid query:
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(max.align.prop = max(align.proportion.query, align.proportion.sub),
min.align.prop = min(align.proportion.query, align.proportion.sub)) %>%
# Filter the alignments by only including those above the defined thresholds. Basically filters
# out whatever alignments we decide are "too short"
filter(length >= 0.2*3000) %>%
# Calculate the minimum proportion that an alignment covers
# E.g. the alignment length / length of the longer plasmid
# Will be used to calculate edge-weights.
select(uniq_pair,layer_from, node_from, layer_to,node_to, max.align.prop, min.align.prop, everything()) %>%
ungroup()
# 894 alignments
####################################################################################################################
####################################################################################################################
# SECTION 2: CALCULATE THE EFFECTS OF EACH THRESHOLD ON NUMBER PLASMIDS AND ALIGNMENTS RETAINED IN DATA-SET
####################################################################################################################
# First make a list of the dataframes, using llist from Hmisc package preserving the names of each dataframe:
thresh.list <- llist(thresh500.a, thresh500.b,thresh1000.a, thresh1000.b,
thresh1500.a, thresh1500.b, thresh2000.a, thresh2000.b,
thresh2500.a, thresh2500.b, thresh3000.a, thresh3000.b)
# Specify names in list
thresh.df.names <- as.data.frame(names(thresh.list))
# Calculate the total number of alignments (nrows) per threshold
thresh.tot.align <- as.data.frame(sapply(thresh.list, nrow))
# Function to calculate the number of plasmids retained per threshold:
num.plas.fun <- function(x) {
x %>%
select(starts_with("node")) %>%
gather(.,key=node_type, value=node_id) %>%
select(-node_type) %>%
distinct() %>%
nrow()
}
# Apply the function:
thresh.num.plas <- as.data.frame(sapply(thresh.list, num.plas.fun))
# Functions to count the number of intra vs. inter-layer edges at each threshold level:
# Intra first:
intra.count <- function(x) {
x %>%
select(layer_from, node_from, layer_to, node_to, edge_type) %>%
distinct() %>%
filter(edge_type == "Intra") %>%
nrow()
}
# Apply the function:
thresh.intra <- as.data.frame(sapply(thresh.list, intra.count))
# Repeat for inter:
inter.count <- function(x) {
x %>%
select(layer_from, node_from, layer_to, node_to, edge_type) %>%
distinct() %>%
filter(edge_type == "Inter") %>%
nrow()
}
# Apply the function:
thresh.inter <- as.data.frame(sapply(thresh.list, inter.count))
# Put the information for each threshold level into a single table
thresh.df <- bind_cols(thresh.num.plas,thresh.tot.align, thresh.intra, thresh.inter)
# Set column names:
thresh.col.names <- as.vector(c("num_plasmids","total_alignments",
"intra","inter"))
# Apply column names to the dataframe:
thresh.df <- thresh.df %>%
rename_with(~thresh.col.names)
# Minor edits/format to dataframe:
thresh.df1 <- thresh.df %>%
rownames_to_column(., var = "thresh") %>%
mutate(min.leng = as.numeric(str_extract(thresh, "\\d+"))) %>%
mutate(align.type = str_sub(thresh,-1),
align.type = ifelse(align.type == "a", "20% shorter plasmid", "20% threshold"))
# Gather for plotting:
thresh.df.intra.inter <- thresh.df1 %>%
dplyr::rename(Inter = inter, Intra = intra, Total = total_alignments) %>%
pivot_longer(cols=c("Total", "Inter", "Intra"), names_to = "edge.type", values_to = "alignment.count")
####################################################################################################################
####################################################################################################################
# SECTION 3: VISUALIZATION OF THRESHOLD EFFECTS
####################################################################################################################
# Create Supplementary Figure S5:
# Panel A:
plot.thresh <- ggplot(data = thresh.df1) +
geom_line(aes(x=min.leng, y=num_plasmids, color=align.type), size=1) +
labs(x = "Minimum Plasmid Length", y="Plasmids (Count)") +
theme_classic() +
theme(legend.title = element_blank(),
legend.text=element_text(size=11),
axis.title=element_text(size=16),
axis.text = element_text(size=13)) +
theme(plot.margin = margin(1.5, 0.75, 0.5, 0.5, "cm"))
# Can save intermediate plot if desired
#ggsave(plot.thresh ,filename="thresh.plas.count.png", dpi = 900, width = 10, height = 4, units = "in")
# Panel B:
plot.thresh.intra.inter <- ggplot(data=thresh.df.intra.inter) +
geom_line(aes(x=min.leng, y=alignment.count, color=align.type, linetype=edge.type),size=1.25) +
scale_linetype_manual(values=c("solid","twodash", "dotted")) +
labs(x = "Minimum Plasmid Length", y="Alignments (Count)") +
theme_classic() +
theme(legend.title = element_blank(),
legend.text=element_text(size=11),
axis.title=element_text(size=16),
axis.text = element_text(size=13)) +
theme(plot.margin = margin(1.5, 0.75, 0.5, 0.5, "cm"))
# Can save intermediate plot if desired
#ggsave(plot.thresh.intra.inter,filename="length.thresh.intra.inter.png", dpi = 900, width = 10, height = 4, units = "in")
# Combine plots with ggarrange:
thresh.panel.plot <- ggarrange(plot.thresh, plot.thresh.intra.inter,
labels = c("A.", "B."),
ncol = 1, nrow = 2,
common.legend = FALSE)
# Save Figure S5 in desired format:
ggsave(thresh.panel.plot,filename="thresh.panel.png", dpi = 900, width = 10, height = 8, units = "in")
ggsave(thresh.panel.plot,filename="thresh.panel.pdf", dpi = 900, width = 10, height = 8, units = "in")
####################################################################################################################
####################################################################################################################
# SECTION 4: SAVE THE NETWORK DATA
####################################################################################################################
# Save the data at the chosen threshold for further research:
# 2000 bp length, alignment coverage of 20% of the shortest plasmid in the pair:
save(thresh2000.a, file="thresh2000.a.Rda")
####################################################################################################################