-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsa-vis.R
executable file
·133 lines (121 loc) · 4.45 KB
/
msa-vis.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
#!/usr/bin/env Rscript
# load pkgs
suppressPackageStartupMessages(library(ggmsa))
suppressPackageStartupMessages(library(scales))
suppressPackageStartupMessages(library(ggpubr))
suppressPackageStartupMessages(library(argparse))
suppressPackageStartupMessages(library(furrr))
# add parser
parser <- ArgumentParser()
# add cli options
parser$add_argument("-i", "--input", type="character", required=TRUE,
help = "Path to MSA in FASTA format")
parser$add_argument("-o", "--output", type="character", default="./msa-vis.pdf",
help="Path to output visualization [default %(default)s]")
parser$add_argument("--type", type="character", required=TRUE,
choices = c("dna", "aa"),
help="Specify input MSA is DNA or amino acid alignment")
parser$add_argument("--include_legend", action="store_true",
help="Add colour legend")
parser$add_argument("--hide_letters", action="store_true",
help="Do not display nucleotide/amino acid letters")
parser$add_argument("--chunk_len", type="integer", default=50,
help="Number of base pairs to show per alignment chunk [default \"%(default)s\"]")
parser$add_argument("--font", type="character", default='helvetical',
choices = c('helvetical', 'mono', 'DroidSansMono', 'TimesNewRoman'),
help="Font style [default \"%(default)s\"]")
parser$add_argument("--fontsize", type="integer", default=9,
help="Font size [default \"%(default)s\"]")
parser$add_argument("-t", "--threads", type="double", default=1,
help="Number of threads to use [default \"%(default)s\"]")
# parse args
args <- parser$parse_args()
# hide warning msgs in ggplot
# get executing script name
src_name <- gsub("--file=", "", commandArgs()[4])
# params check
check_params <- function(args) {
# check if input file exists
if ( !file.exists(args$input) ) {
message(paste0(src_name, ": Input MSA [ ", args$input, " ] does not exist, exiting."))
quit(status=1)
}
}
# plot msa
plot_msa <- function(
msa, start, end,
scheme="Chemistry_NT",
font_size=10,
font = 'helvetical',
name = T,
char_width = 0.5,
show_legend = F,
show_letter = T
) {
suppressMessages(
p <- msa %>%
ggmsa(
start, end,
seq_name = name,
color = scheme,
char_width = char_width,
show.legend = show_legend,
font = font
) +
theme_minimal(font_size) +
geom_seqlogo(color = scheme) +
theme(
panel.grid = element_blank(),
# plot.margin = margin(t = 0, # Top margin
# r = 0, # Right margin
# b = 0, # Bottom margin
# l = 0), # Left margin
#axis.text.y = element_text(colour = cols),
legend.position = "top"
) +
scale_x_continuous(
labels = comma,
expand = c(0,0)
) +
labs(fill = "")
)
p
}
# main
main <- function() {
# validate args
check_params(args)
# set up parellel processing
if ( args$threads > 1) { plan(future::cluster, workers = args$threads) }
# read msa
msa <- Biostrings::readDNAStringSet(args$input)
# revise fasta header names; only keep strings before the first blank space
names(msa) <- gsub(" .*", "", names(msa))
# create msa viz for each subregion with length of chunk_len
plots <- future_map(
seq(1, msa@ranges@width[1], args$chunk_len),
~plot_msa(
msa,
start=.,
end=. + args$chunk_len - 1,
scheme=ifelse(args$type == "dna", "Chemistry_NT", "Chemistry_AA"),
show_legend=args$include_legend,
font=unlist(ifelse(args$hide_letters, list(NULL), list(args$font))),
font_size=args$fontsize
),
.progress = T
)
# combine all subregion plots into one figure
fig <- ggarrange(
plotlist = plots,
ncol = 1,
nrow = ifelse(round(35/length(msa@ranges)) < 1, 1, round(35/length(msa@ranges))),
common.legend = T
)
# export figure
ggexport(fig, filename = args$output)
unlink("Rplots.pdf")
# Shut down parallel sessions
if (!inherits(plan(), "sequential")) plan(sequential)
}
main()