-
Notifications
You must be signed in to change notification settings - Fork 4
/
paf_to_barplot.py
executable file
·340 lines (273 loc) · 9.92 KB
/
paf_to_barplot.py
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
#!/usr/bin/env python3
## Pombert lab, 2024
version = '0.4b'
updated = '2024-05-28'
name = 'paf_to_barplot.py'
import sys
import os
import re
import argparse
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.patches import Rectangle
from multiprocessing import Pool, Value
import seaborn as sns
################################################################################
## README
################################################################################
usage = f"""
NAME {name}
VERSION {version}
UPDATED {updated}
SYNOPSIS Create genome alignment dotplots from PAF files with matplotlib
REQS matplotlib, seaborn
COMMAND {name} \\
-p *.paf \\
-o BARPLOTS
OPTIONS:
-p (--paf) PAF file(s) to plot
-f (--fasta) FASTA files used to generate PAF alignments
-o (--outdir) Output directory [Default: ./]
-h (--height) Figure height in inches [Default: 10.8]
-w (--width) Figure width in inches [Default: 19.2]
-a (--affix) Add affix to filename
-c (--palette) Seaborn color palette [Default: Spectral]
# See https://www.practicalpythonfordatascience.com/ap_seaborn_palette
# for a list of color palettes
-m (--mono) Use a single specified mochochrome color for all chromosomes instead
of a color palette
--clusters Color matches by clusters instead; colors are not related between contigs
--catenate Concatenate queries against the same reference in a single barplot
--fontsize Font size [Default: 8]
--threads Number of threads to use [Default: 16]
--version Show script version
"""
# Print custom message if argv is empty
if (len(sys.argv) <= 1):
print(usage)
exit(0)
################################################################################
## Create command lines switches
################################################################################
cmd = argparse.ArgumentParser(add_help=False)
cmd.add_argument("-p", "--paf", nargs='*')
cmd.add_argument("-f", "--fasta", nargs='*')
cmd.add_argument("-o", "--outdir", default='./')
cmd.add_argument("-h", "--height", default=10.8)
cmd.add_argument("-w", "--width", default=19.2)
cmd.add_argument("-a", "--affix")
cmd.add_argument("-c", "--palette", default='Spectral')
cmd.add_argument("-n", "--noticks", action='store_true')
cmd.add_argument("-m", "--mono")
cmd.add_argument("--clusters", action='store_true')
cmd.add_argument("--catenate", action='store_true')
cmd.add_argument("--fontsize", default=8)
cmd.add_argument("--threads", default=16)
cmd.add_argument("--version", action='store_true')
args = cmd.parse_args()
paf_files = args.paf
fasta_files = args.fasta
outdir = args.outdir
height = args.height
width = args.width
affix = args.affix
color_palette = args.palette
noticks = args.noticks
monochrome = args.mono
clusters = args.clusters #
catenate = args.catenate
fontsize = int(args.fontsize)
threads = int(args.threads)
scversion = args.version
#########################################################################
### Version
#########################################################################
if scversion:
print ("")
print (f"Script: {name}")
print (f"Version: {version}")
print (f"Updated: {updated}\n")
exit(0)
################################################################################
## Working on output directory
################################################################################
svgdir = outdir + '/SVG'
pngdir = outdir + '/PNG'
subdirs = [outdir,svgdir,pngdir]
for dir in subdirs:
if os.path.isdir(dir) == False:
try:
os.makedirs(dir)
except:
sys.exit(f"Can't create directory {dir}...")
################################################################################
## Parsing FASTA file(s)
################################################################################
len_dict = {}
cat_dict = {}
if fasta_files is not None:
for fasta in fasta_files:
basename = os.path.basename(fasta)
x = re.search(r'(\S+)\.fasta', basename)
if x:
basename = x.group(1)
len_dict[basename] = {}
seqname = None
with open(fasta) as f:
for line in f:
m = re.search(r'>(\S+)', line)
if m:
seqname = m.group(1)
len_dict[basename][seqname] = 0
cat_dict[seqname] = 0
else:
length = len(line.strip())
len_dict[basename][seqname] += length
cat_dict[seqname] += length
################################################################################
## Parsing and plotting PAF file(s)
################################################################################
lsize = len(paf_files) * 2
counter = Value('i', 0)
def barplot(paf):
basename = os.path.basename(paf)
qfile = None
sfile = None
global counter
m = re.search(r'^(\w+)_vs_(\w+)', basename)
if m:
sfile = m.group(1)
qfile = m.group(2)
dataframe = {}
query_len_dict = {}
subject_len_dict = {}
if fasta_files is not None:
query_len_dict = len_dict[qfile]
if catenate:
for key in len_dict:
if key != qfile:
subject_len_dict.update(len_dict[key])
else:
subject_len_dict = len_dict[sfile]
with open(paf) as file:
for line in file:
# PAF data structure:
# https://github.com/lh3/miniasm/blob/master/PAF.md
data = line.split("\t")
query = data[0]
query_len = int(data[1])
q_start = int(data[2])
q_end = int(data[3])
orientation = data[4]
subject = data[5]
subject_len = int(data[6])
s_start = int(data[7])
s_end = int(data[8])
if fasta_files is None:
query_len_dict[query] = query_len
subject_len_dict[subject] = subject_len
if query not in dataframe:
dataframe[query] = {}
if subject not in dataframe[query]:
dataframe[query][subject] = {}
y_width = s_end - s_start + 1
dataframe[query][subject][s_start] = y_width
##### Plotting data
palette = sns.color_palette(color_palette, len(query_len_dict))
# Setting default image to widescreen by default
plt.rcParams['svg.fonttype'] = 'none'
plt.rcParams["figure.figsize"] = (width,height)
plt.rcParams.update({'font.size': fontsize})
# Defining Matplotlib figure and axis
fig, ax = plt.subplots()
if noticks:
plt.setp(ax, xticks=[], yticks=[])
# creating a blank histogram
ax.hist([0, 0],[0, 0], orientation='horizontal', color='white')
## Creating bar plot edges
x = 0.5
ydata = [1]
ylabels = []
for key in reversed(sorted(subject_len_dict.keys())):
ylabels.append(key)
ax.add_patch(Rectangle((1, x), subject_len_dict[key], 1, edgecolor='black', fc='none'))
x += 2
ydata.append(x + 0.5)
## Filling bar plots
x = 0.5
cnum = 0
legend = []
dlegend = {}
for subject in reversed(sorted(subject_len_dict.keys())):
for query in sorted(query_len_dict.keys()):
if not clusters:
ccolor = palette[cnum]
xlegend = mpatches.Patch(color=ccolor, label=query)
if query not in dlegend:
dlegend[query] = {}
legend.append(xlegend)
if monochrome:
ccolor = monochrome
cnum += 1
if query in dataframe:
xnum = 0
if subject in dataframe[query]:
for start in dataframe[query][subject]:
end = dataframe[query][subject][start]
if clusters:
palette = sns.color_palette(color_palette, len(dataframe[query][subject]))
ccolor = palette[xnum]
ax.add_patch(Rectangle((start, x), end, 1, color=ccolor))
else:
ax.add_patch(Rectangle((start, x), end, 1, color=ccolor))
xnum += 1
x += 2
cnum = 0
## Adding labels to y-axis
ax.set_yticks(ydata)
ax.set_yticks(ax.get_yticks()[:-1])
ax.set_yticklabels(ylabels)
## Hiding border frame
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
# ax.spines['bottom'].set_visible(False)
## Writing to output file
basename = os.path.basename(paf)
output = basename
fig.suptitle(basename)
if monochrome or clusters:
next
else:
plt.legend(handles=legend, loc='center left', bbox_to_anchor=(1, 0.5), frameon=False)
affix_color = color_palette
if monochrome:
affix_color = monochrome
if clusters:
affix_color = 'clus'
suffix = ''
if affix is not None:
suffix = '.' + affix
png = pngdir + '/' + output.rsplit('.', 1)[0] + suffix + '.barplot.' + f"{width}x{height}." + f"{affix_color}" + '.png'
svg = svgdir + '/' + output.rsplit('.', 1)[0] + suffix + '.barplot.' + f"{width}x{height}." + f"{affix_color}" + '.svg'
with counter.get_lock():
counter.value += 1
print(f"{counter.value} / {lsize} - plotting {png}...")
if monochrome or clusters:
plt.savefig(png)
else:
plt.savefig(png, bbox_inches='tight')
with counter.get_lock():
counter.value += 1
print(f"{counter.value} / {lsize} - plotting {svg}...")
if monochrome or clusters:
plt.savefig(svg)
else:
plt.savefig(svg, bbox_inches='tight')
## Close fig
plt.clf()
plt.cla()
plt.close('all')
## Run
pool = Pool(threads)
pool.map(barplot, paf_files)