forked from charlottewright/lep_busco_painter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_buscopainter.R
More file actions
executable file
·569 lines (500 loc) · 16.1 KB
/
Copy pathplot_buscopainter.R
File metadata and controls
executable file
·569 lines (500 loc) · 16.1 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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env Rscript
### Original script by : Charlotte Wright
### Original Repo : https://github.com/charlottewright/lep_busco_painter/
### Modified by : Damon-Lee Pointon
### Date of modification:
### 2025-04-24
### Converting double space to tabs for linting and added
### version information
### 2025-08-26
### Conform script to linting standards
###
### Description :
### This script generates a plot showing locations of putative
### merian elements in a genomic assembly
### Load packages
library(optparse)
suppressMessages(library(tidyverse))
suppressMessages(library(scales))
version <- "1.0.0"
### Funcions for making busco paints in R ####
prepare_data <- function(args1) {
locations <- read_tsv(args1, col_types = cols())
# format location data
locations <- locations |> filter(!grepl(":", query_chr))
# format location data
locations <- locations |> filter(!grepl(":", assigned_chr))
locations <- locations |>
group_by(query_chr) |>
mutate(length = max(position)) |>
ungroup()
locations$start <- 0
locations
}
prepare_data_with_index <- function(args1, args2) {
locations <- read_tsv(args1, col_types = cols())
contig_lengths <- read_tsv(args2, col_names = FALSE, col_types = cols())
colnames(contig_lengths) <- c("Seq", "length", "offset",
"linebases", "linewidth"
)
# format location data
locations <- locations |> filter(!grepl(":", query_chr))
# format location data
locations <- locations |> filter(!grepl(":", assigned_chr))
locations <- merge(
locations, contig_lengths, by.x = "query_chr", by.y = "Seq"
)
locations$start <- 0
locations
}
# minimum of buscos to be present
filter_buscos <- function(locations, minimum) {
locations_filt <- locations |>
# filter df to only keep query_chr with >=3 buscos to remove shrapnel
group_by(query_chr) |>
# make a new column reporting number buscos per query_chr
mutate(n_busco = n()) |>
ungroup() |>
filter(n_busco >= minimum)
locations_filt
}
# Set mapping of Merian element to colour when only plot
set_merian_colour_mapping <- function(location_set) {
merian_order <- c(
"MZ", "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10",
"M11", "M12", "M13", "M14", "M15", "M16", "M17", "M18", "M19", "M20",
"M21", "M22", "M23", "M24", "M25", "M26", "M27", "M28", "M29", "M30",
"M31", "self"
)
colour_palette <- append(hue_pal()(32), "grey")
status_merians <- unique(location_set$status)
subset_merians <- subset(colour_palette, merian_order %in% status_merians)
subset_merians
}
busco_paint_theme <- theme(
legend.position = "right",
strip.text.x = element_text(
margin = margin(0, 0, 0, 0, "cm")
),
panel.background = element_rect(fill = "white", colour = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.x = element_line(color = "black", linewidth = 0.5),
axis.text.x = element_text(size = 15),
axis.title.x = element_text(size = 15),
strip.text.y = element_text(angle = 0),
strip.background = element_blank(),
plot.title = element_text(hjust = 0.5, face = "italic", size = 20),
plot.subtitle = element_text(hjust = 0.5, size = 20)
)
bp_no_facet_labels_theme <- theme(
legend.position = "right",
strip.text.x = element_blank(),
panel.background = element_rect(fill = "white", colour = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.x = element_line(color = "black", linewidth = 0.5),
axis.text.x = element_text(size = 15),
axis.title.x = element_text(size = 15),
strip.text.y = element_text(angle = 0),
strip.background = element_blank(),
plot.title = element_text(hjust = 0.5, face = "italic", size = 20),
plot.subtitle = element_text(hjust = 0.5, size = 20)
)
# plot only buscos that have moved - paint by Merians
paint_merians_differences_only <- function(
spp_df, subset_merians, num_col, title, karyotype
) {
merian_order <- c(
"MZ", "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10",
"M11", "M12", "M13", "M14", "M15", "M16", "M17", "M18", "M19", "M20",
"M21", "M22", "M23", "M24", "M25", "M26", "M27", "M28", "M29", "M30",
"M31", "self"
)
spp_df$status_f <- factor(spp_df$status, levels = merian_order)
chr_levels <- subset(
spp_df,
select = c(query_chr, length)
) |>
unique() |>
arrange(length, decreasing = TRUE)
chr_levels <- chr_levels$query_chr
# set chr order as order for plotting
spp_df$query_chr_f <- factor(spp_df$query_chr, levels = chr_levels)
sub_title <- paste("n contigs =", karyotype)
the_plot <- ggplot(data = spp_df) +
scale_colour_manual(
values = subset_merians,
aesthetics = c("colour", "fill")
) +
geom_rect(
aes(xmin = start, xmax = length, ymax = 0, ymin = 12),
colour = "black",
fill = "white"
) +
geom_rect(
aes(
xmin = position - 2e4,
xmax = position + 2e4,
ymax = 0,
ymin = 12,
fill = status_f
)
) +
facet_wrap(query_chr_f ~ ., ncol = num_col) +
guides(scale = "none") +
xlab("Position (Mb)") +
scale_x_continuous(labels = function(x) x / 1e6, expand = c(0.005, 1)) +
scale_y_continuous(breaks = NULL) +
ggtitle(label = title, subtitle = sub_title) +
guides(fill = guide_legend("Merian element"), color = "none")
# busco_paint_theme
the_plot
}
# plot only buscos that have moved - paint by species
paint_species_differences_only <- function(spp_df, num_col, title, karyotype) {
chr_levels <- subset(
spp_df,
select = c(query_chr, length)
) |>
unique() |>
arrange(length, decreasing = TRUE)
chr_levels <- chr_levels$query_chr
chr_levels <- chr_levels [! chr_levels %in% "self"]
# set chr order as order for plotting query chr
spp_df$query_chr_f <- factor(spp_df$query_chr, levels = chr_levels)
legend_levels <- unique(spp_df$status)
# remove "self" from list
legend_levels <- legend_levels[legend_levels != "self"]
# then put "self" back in to have it in first position as want "self"
# to always be painted grey.
legend_levels <- c("self", legend_levels)
num_colours <- length(legend_levels)
col_palette <- hue_pal()(num_colours)
col_palette[1] <- "grey"
# set chr order as order for plotting
spp_df$status_f <- factor(spp_df$status, levels = legend_levels)
sub_title <- paste("n contigs =", karyotype)
the_plot <- ggplot(data = spp_df) +
scale_colour_manual(
values = col_palette,
aesthetics = c("fill"),
breaks = legend_levels
) +
geom_rect(
aes(xmin = start, xmax = length, ymax = 0, ymin = 12),
colour = "black",
fill = "white"
) +
geom_rect(
aes(
xmin = position - 2e4,
xmax = position + 2e4,
ymax = 0,
ymin = 12,
fill = status_f
)
) +
facet_wrap(
query_chr_f ~ ., ncol = num_col, strip.position = "right"
) +
guides(scale = "none") +
xlab("Position (Mb)") +
scale_x_continuous(labels = function(x) x / 1e6, expand = c(0.005, 1)) +
scale_y_continuous(breaks = NULL) +
ggtitle(label = title, subtitle = sub_title) +
guides(fill = guide_legend("Query chromosome"), color = "none") +
busco_paint_theme
the_plot
}
paint_merians_all <- function(spp_df, num_col, title, karyotype) {
colour_palette <- append(hue_pal()(32), "grey")
merian_order <- c(
"MZ", "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10",
"M11", "M12", "M13", "M14", "M15", "M16", "M17", "M18", "M19", "M20",
"M21", "M22", "M23", "M24", "M25", "M26", "M27", "M28", "M29", "M30",
"M31", "self"
)
spp_df$assigned_chr_f <- factor(spp_df$assigned_chr, levels = merian_order)
chr_levels <- subset(
spp_df, select = c(query_chr, length)
) |>
unique() |>
arrange(length, decreasing = TRUE)
chr_levels <- chr_levels$query_chr
# set chr order as order for plotting
spp_df$query_chr_f <- factor(spp_df$query_chr, levels = chr_levels)
sub_title <- paste("n contigs =", karyotype)
the_plot <- ggplot(data = spp_df) +
scale_colour_manual(
values = colour_palette,
aesthetics = c("colour", "fill")
) +
geom_rect(
aes(xmin = start, xmax = length, ymax = 0, ymin = 12),
colour = "black",
fill = "white"
) +
geom_rect(
aes(
xmin = position - 2e4,
xmax = position + 2e4,
ymax = 0,
ymin = 12,
fill = assigned_chr_f
)
) +
facet_wrap(
query_chr_f ~ ., ncol = num_col, strip.position = "right"
) +
guides(scale = "none") +
xlab("Position (Mb)") +
scale_x_continuous(labels = function(x) x / 1e6, expand = c(0.005, 1)) +
scale_y_continuous(breaks = NULL) +
ggtitle(label = title, subtitle = sub_title) +
guides(fill = guide_legend("Merian element"), color = "none") +
busco_paint_theme
the_plot
}
# paint all buscos by species
paint_species_all <- function(spp_df, num_col, title, karyotype) {
chr_levels <- subset(
spp_df, select = c(query_chr, length)
) |>
unique() |>
arrange(length, decreasing = TRUE)
chr_levels <- chr_levels$query_chr
chr_levels <- chr_levels [! chr_levels %in% "self"]
# set chr order as order for plotting
spp_df$query_chr_f <- factor(spp_df$query_chr, levels = chr_levels)
legend_levels <- subset(spp_df, select = c(assigned_chr)) |> unique()
legend_levels <- legend_levels$assigned_chr
num_colours <- length(legend_levels)
col_palette <- hue_pal()(num_colours)
# set chr order as order for plotting
spp_df$assigned_chr_f <- factor(spp_df$assigned_chr, levels = legend_levels)
sub_title <- paste("n contigs =", karyotype)
the_plot <- ggplot(data = spp_df) +
scale_colour_manual(
values = col_palette,
aesthetics = c("fill"),
breaks = legend_levels
) +
geom_rect(
aes(xmin = start, xmax = length, ymax = 0, ymin = 12),
colour = "black",
fill = "white"
) +
geom_rect(
aes(
xmin = position - 2e4,
xmax = position + 2e4,
ymax = 0,
ymin = 12,
fill = assigned_chr_f
)
) +
facet_wrap(
query_chr_f ~ .,
ncol = num_col,
strip.position = "right"
) +
guides(scale = "none") +
xlab("Position (Mb)") +
scale_x_continuous(labels = function(x) x / 1e6, expand = c(0.005, 1)) +
scale_y_continuous(breaks = NULL) +
ggtitle(label = title, subtitle = sub_title) +
guides(fill = guide_legend("Query chromosome"), color = "none") +
busco_paint_theme
the_plot
}
### get args
option_list <- list(
make_option(
c("-f", "--file"),
type = "character",
default = NULL,
help = "location.tsv file",
metavar = "character"
),
make_option(
c("-p", "--prefix"),
type = "character",
default = "Query species",
help = "prefix for plot title",
metavar = "character"
),
make_option(
c("-i", "--index"),
type = "character",
default = "False",
help = "genome index file",
metavar = "character"
),
make_option(
c("-m", "--merians"),
type = "character",
default = "False",
help = "Are you comparing a genome to Merian elements",
metavar = "character"
),
make_option(
c("-d", "--differences"),
type = "character",
default = "False",
help = "Colour buscos moved from the dominant chromosome",
metavar = "character"
),
make_option(
c("-n", "--minimum"),
type = "integer",
default = 3,
help = "minimum number of buscos ",
metavar = "number"
),
make_option(
c("-v", "--version"),
action = "store_true",
default = FALSE,
help = "show version and exit"
)
)
opt_parser <- OptionParser(
usage = "%prog [options] input",
option_list = option_list
)
opt <- parse_args(opt_parser)
if (isTRUE(opt$version)) {
cat(paste(version, "\n"))
quit(status = 0)
}
locations <- opt$file
prefix <- opt$prefix
index <- opt$index
merians <- opt$merians
differences_only <- opt$differences
minimum <- opt$minimum
# if no index supplied
if (index == "False") {
location_set <- prepare_data(locations)
locations_filt <- filter_buscos(location_set, minimum)
# if index supplied
} else {
location_set <- prepare_data_with_index(locations, index)
locations_filt <- filter_buscos(location_set, minimum)
}
# total number of query_chr before filtering
total_contigs <- length(unique(location_set$query_chr))
# number of query_chr after filtering
num_contigs <- as.character(length(unique(locations_filt$query_chr)))
num_removed_contigs <- length(
unique(location_set$query_chr)
) -
length(
unique(locations_filt$query_chr)
)
print(
paste(
"Number of contigs before filtering by number of BUSCOs:",
total_contigs
)
)
print(
paste(
"Number of contigs removed by filtering :",
num_removed_contigs
)
)
print(
paste(
"Number of contigs post-filtering:",
num_contigs
)
)
# if Merian elements are being used as the comparator
if (merians != "False") {
subset_merians <- set_merian_colour_mapping(locations_filt)
}
# generate the plot - four possible options based on given arguments to script
# plot only buscos that have moved - paint by Merians
# if comparing two species
if (merians == "False") {
# if colouring all orthologs
if (differences_only == "False") {
p <- paint_species_all(locations_filt, 1, prefix, num_contigs)
# if only colouring orthologs that have moved
} else {
p <- paint_species_differences_only(
locations_filt, 1, prefix, num_contigs
)
}
# comparing one species to Merian elements
} else {
# if colouring all orthologs
if (differences_only == "False") {
p <- paint_merians_all(locations_filt, 1, prefix, num_contigs)
# if only colouring orthologs that have moved
} else {
if (length(locations_filt$query_chr) < 100) {
p <- paint_merians_differences_only(
locations_filt, subset_merians, 1, prefix, num_contigs
)
p <- p + busco_paint_theme
} else {
p <- paint_merians_differences_only(
locations_filt, subset_merians, 3, prefix, num_contigs
)
p <- p + bp_no_facet_labels_theme
}
}
}
ggsave(
paste(
as.character(opt$file),
"_buscopainter.png",
sep = ""
),
plot = p,
width = 15,
height = 30,
units = "cm",
device = "png"
)
pdf(NULL)
ggsave(
paste(
as.character(opt$file),
"_buscopainter.pdf",
sep = ""
),
plot = p,
width = 15,
height = 30,
units = "cm",
device = "pdf"
)
ggsave(
paste(
as.character(opt$file),
"_buscopainter.png",
sep = ""
),
plot = p,
width = 15,
height = 30,
units = "cm",
device = "png"
)
pdf(NULL)
ggsave(
paste(
as.character(opt$file),
"_buscopainter.pdf",
sep = ""
),
plot = p,
width = 15,
height = 30,
units = "cm",
device = "pdf"
)