Skip to content

Commit b0c5416

Browse files
Dom LaetschDom Laetsch
authored andcommitted
...
1 parent be8c463 commit b0c5416

File tree

3 files changed

+64
-91
lines changed

3 files changed

+64
-91
lines changed

lib/BtLog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def progress(iteration, steps, max_value):
4949
'17' : '[ERROR:17]\t: Label could not be parsed from "%s".',
5050
'18' : '[ERROR:18]\t: Please provide a tax file in BLAST format.',
5151
'19' : '[ERROR:19]\t: Sequence %s in file %s is not part of the assembly.',
52-
'20' : '[ERROR:20]\t: Please add "clc_mapping_info" to you PATH variable.'
52+
'20' : '[ERROR:20]\t: Please add "clc_mapping_info" to you PATH variable.',
53+
'21' : '[ERROR:21]\t: refcov FILE does not seem to have the right format.'
5354

5455
}
5556

lib/BtPlot.py

Lines changed: 53 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
FONTSIZE = 24
3131
COLOURMAP = "rainbow" # "Set1", "Paired", "Set2", "Spectral"
32-
BLACK, GREY, BGGREY, WHITE, DGREY = unicode('#262626'), unicode('#d3d3d3'), unicode('#F0F0F5'), unicode('#ffffff'), unicode('#3B3B3B')
32+
BLACK, GREY, BGGREY, WHITE, DGREY = unicode('#262626'), unicode('#d3d3d3'), unicode('#F0F0F5'), unicode('#ffffff'), unicode('#4d4d4d')
3333
nullfmt = NullFormatter()
3434

3535
def n50(list_of_lengths):
@@ -47,6 +47,20 @@ def n50(list_of_lengths):
4747
break
4848
return N50
4949

50+
def parseRefCov(refcov_f):
51+
refcov_dict = {}
52+
with open(refcov_f) as fh:
53+
for l in fh:
54+
try:
55+
cov_lib, reads_mapped_ref, reads_unmapped_ref = l.split(",")
56+
refcov_dict[cov_lib] = {
57+
'reads_mapped' : int(reads_mapped_ref),
58+
'reads_unmapped' : int(reads_unmapped_ref)
59+
}
60+
except:
61+
BtLog.error('21')
62+
return refcov_dict
63+
5064
def getSortedGroups(data_dict, sort_order):
5165
""" Returns list of sorted groups based on span or count. """
5266
sorted_groups = []
@@ -285,24 +299,14 @@ def relabel_and_colour(self, colour_f, user_labels):
285299
if 'other' in self.labels:
286300
self.plot_order.append('other')
287301

288-
def plotReadCov(self):
289-
FONTSIZE = 12
290-
291-
# plot ReadCov by tax for each cov_lib
292-
#plot_data = {'by_group' : {} }
302+
def plotReadCov(self, refcov_dict):
303+
mat.rcParams.update({'font.size': 14})
304+
plot_data = {}
293305

294-
# plot ReadCov by cov_lib for each tax
295-
#if len(self.cov_libs) > 1:
296-
reference_read_cov = {}
297-
298306
main_columns = 2
299-
if (reference_read_cov):
307+
if (refcov_dict):
300308
main_columns += 2
301309
group_columns = len(self.plot_order)
302-
303-
plot_data = {}
304-
305-
# plot cov_per_tax
306310

307311
for cov_lib in self.cov_libs:
308312
if not self.cov_libs_total_reads_dict[cov_lib] == 0:
@@ -311,100 +315,61 @@ def plotReadCov(self):
311315
# unmapped (assembly)
312316
reads_total = self.cov_libs_total_reads_dict[cov_lib]
313317
reads_unmapped = reads_total - self.stats['all']['reads_mapped'][cov_lib]
314-
main_plot.labels.append('Unmapped')
318+
if cov_lib in refcov_dict:
319+
reads_mapped_ref = refcov_dict[cov_lib]['reads_mapped']
320+
reads_unmapped_ref = refcov_dict[cov_lib]['reads_unmapped']
321+
reads_total_ref = reads_mapped_ref + reads_unmapped_ref
322+
main_plot.labels.append('Unmapped (ref)')
323+
main_plot.values.append(reads_unmapped_ref/reads_total_ref)
324+
main_plot.colours.append(DGREY)
325+
main_plot.labels.append('Mapped (ref)')
326+
main_plot.values.append(reads_mapped_ref/reads_total_ref)
327+
main_plot.colours.append(DGREY)
328+
329+
main_plot.labels.append('Unmapped (assembly)')
315330
main_plot.values.append(reads_unmapped/reads_total)
316-
main_plot.colours.append(GREY)
331+
main_plot.colours.append(DGREY)
317332
# mapped (assembly)
318-
main_plot.labels.append('Mapped')
333+
main_plot.labels.append('Mapped (assembly)')
319334
main_plot.values.append(self.stats['all']['reads_mapped_perc'][cov_lib])
320-
main_plot.colours.append(BLACK)
335+
main_plot.colours.append(DGREY)
321336
# mapped plotted groups
322337
for group in self.plot_order:
323338
group_plot.labels.append(group)
324339
group_plot.values.append(self.stats[group]['reads_mapped_perc'][cov_lib])
325340
group_plot.colours.append(self.colours[group])
341+
326342
plot_data[cov_lib] = {'main' : main_plot, 'group' : group_plot}
327343

328-
x_pos_main = arange(main_columns)
329-
x_pos_group = arange(len(self.plot_order))
344+
x_pos_main = arange(main_columns)
345+
x_pos_group = arange(len(self.plot_order))
330346

331-
for cov_lib in sorted(self.cov_libs):
332-
if not self.cov_libs_total_reads_dict[cov_lib] == 0:
333-
print cov_lib
334-
print plot_data[cov_lib]['main'].values
335-
print plot_data[cov_lib]['main'].labels
336-
print plot_data[cov_lib]['group'].values
337-
print plot_data[cov_lib]['group'].labels
338347
fig = plt.figure(1, figsize=(30, 10), dpi=200)
339348
gs = mat.gridspec.GridSpec(1, 2, width_ratios=[main_columns, len(self.plot_order)])
340349
ax_main = plt.subplot(gs[0])
341350
ax_main.set_axis_bgcolor(BGGREY)
342351
ax_group = plt.subplot(gs[1])
343352
ax_group.set_axis_bgcolor(BGGREY)
344-
ax_main.grid(True, axis='y', which="major", lw=2., color=WHITE, linestyle='-')
345-
ax_group.grid(True, axis='y', which="major", lw=2., color=WHITE, linestyle='-')
346-
rect_main = ax_main.bar(x_pos_main, plot_data[cov_lib]['main'].values, tick_label=plot_data[cov_lib]['main'].labels, align='center', color = plot_data[cov_lib]['main'].colours)
347-
for rect_m in rect_main:
348-
height_m = rect_m.get_height()
349-
ax_main.text(rect_m.get_x() + rect_m.get_width()/2., 1.05*height_m, '{:.1f}%'.format(int(height_m)*100), ha='center', va='bottom')
350-
rect_group = ax_group.bar(x_pos_group, plot_data[cov_lib]['group'].values, tick_label=plot_data[cov_lib]['group'].labels, align='center', color = plot_data[cov_lib]['group'].colours)
353+
rect_group = ax_group.bar(x_pos_group, plot_data[cov_lib]['group'].values, width = 0.5, tick_label=plot_data[cov_lib]['group'].labels, align='center', color = plot_data[cov_lib]['group'].colours)
351354
for rect_g in rect_group:
352-
height_g = rect_g.get_height()
353-
ax_main.text(rect_g.get_x() + rect_g.get_width()/2., 1.05*height_g, '{:.1f}%'.format(int(height_g)*100), ha='center', va='bottom')
354-
ax_main = label_barchart(rect_main, ax_main)
355-
ax_group = label_barchart(rect_group, ax_group)
356-
ax_main_y_values = ax_main.get_yticks()
357-
ax_group_y_values = ax_group.get_yticks()
358-
ax_main.set_yticklabels(['{:.1f}%'.format(x*100) for x in ax_main_y_values])
359-
ax_main.set_yticklabels(['{:.1f}%'.format(x*100) for x in ax_group_y_values])
355+
height_g = float(rect_g.get_height())
356+
ax_group.text(rect_g.get_x() + rect_g.get_width()/2., 0.005 + height_g, '{:.1f}%'.format(height_g*100), ha='center', va='bottom')
357+
rect_main = ax_main.bar(x_pos_main, plot_data[cov_lib]['main'].values, width = 0.5, tick_label=plot_data[cov_lib]['main'].labels, align='center', color = plot_data[cov_lib]['main'].colours)
358+
for rect_m in rect_main:
359+
height_m = float(rect_m.get_height())
360+
ax_main.text(rect_m.get_x() + rect_m.get_width()/2., 0.005 + height_m, '{:.1f}%'.format(height_m*100), ha='center', va='bottom')
361+
ax_main.set_ylim(0, 1.1)
362+
ax_group.set_ylim(0, 1.1)
363+
ax_main.set_yticklabels(['{:.0f}%'.format(x*100) for x in ax_main.get_yticks()])
364+
ax_main.set_xticklabels(plot_data[cov_lib]['main'].labels, rotation=45)
365+
ax_group.set_yticklabels(['{:.0f}%'.format(x*100) for x in ax_group.get_yticks()])
366+
ax_group.set_xticklabels(plot_data[cov_lib]['group'].labels, rotation=45)
367+
ax_main.grid(True, axis='y', which="major", lw=2., color=WHITE, linestyle='--')
368+
ax_group.grid(True, axis='y', which="major", lw=2., color=WHITE, linestyle='--')
360369
out_f = "%s.%s.read_cov.%s" % (self.out_f, cov_lib, self.format)
361370
print BtLog.status_d['8'] % out_f
362-
plt.xticks(fontsize=FONTSIZE)
363-
plt.yticks(fontsize=FONTSIZE)
364371
plt.tight_layout()
365-
plt.savefig(out_f, format=self.format)
366-
367-
#top_y_pos = arange(len(top_labels))
368-
#bottom_y_pos = arange(len(bottom_labels))
369-
#fig = plt.figure(1, figsize=(30,10), dpi=200)
370-
#gs = mat.gridspec.GridSpec(2, 1, height_ratios=[len(top_labels), len(bottom_labels)])
371-
#axarr[0] = plt.subplot(gs[0], sharex=True)
372-
#axarr = []
373-
#axarr.append(plt.subplot(gs[0]))
374-
#axarr.append(plt.subplot(gs[1]))
375-
#axarr[0].set_axis_bgcolor(BGGREY)
376-
#axarr[1].set_axis_bgcolor(BGGREY)
377-
#
378-
#axarr[0].grid(True, axis='x', which="major", lw=2., color=WHITE, linestyle='-')
379-
#axarr[1].grid(True, axis='x', which="major", lw=2., color=WHITE, linestyle='-')
380-
381-
#rects_0 = axarr[0].barh(top_y_pos, top_perc_mapped, tick_label=top_labels, align='center', height = 0.75, color = top_colours)
382-
#rects_1 = axarr[1].barh(bottom_y_pos, bottom_perc_mapped, tick_label=bottom_labels, align='center', height = 0.75, color = bottom_colours)
383-
#
384-
# #axarr[1].set_xlabel("Percent of reads")
385-
#axarr[0].set_title(self.title)
386-
#ax_right_0 = axarr[0].twinx()
387-
#ax_right_1 = axarr[1].twinx()
388-
#ax_right_0.set_yticks(top_y_pos)
389-
#ax_right_1.set_yticks(bottom_y_pos)
390-
#axarr[0].set_xticklabels([])
391-
#axarr[1].set_xticklabels([])
392-
#ax_right_0.set_xlim(0, 1)
393-
#ax_right_1.set_xlim(0, 1)
394-
#ax_right_0_labels = ['{0:.2%}'.format(value) for value in top_perc_mapped]
395-
#ax_right_1_labels = ['{0:.2%}'.format(value) for value in bottom_perc_mapped]
396-
#
397-
#ax_right_0.set_yticklabels(ax_right_0_labels)
398-
#ax_right_1.set_yticklabels(ax_right_1_labels)
399-
#
400-
#ax_right_0.set_ylim(axarr[0].get_ylim())
401-
#ax_right_1.set_ylim(axarr[1].get_ylim())
402-
#
403-
#out_f = "%s.%s.read_cov.%s" % (self.out_f, cov_lib, self.format)
404-
#print BtLog.status_d['8'] % out_f
405-
#plt.tight_layout()
406-
#plt.savefig(out_f, format=self.format)
407-
372+
plt.savefig(out_f, format=self.format)
408373

409374
def plotBlobs(self, cov_lib, info_flag):
410375
rect_scatter, rect_histx, rect_histy, rect_legend = set_canvas()

plot.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[-r RANK] [-x TAXRULE] [--label GROUPS...]
66
[-o PREFIX] [-m] [--sort ORDER] [--hist HIST] [--title]
77
[--colours FILE] [--include FILE] [--exclude FILE]
8-
[--format FORMAT] [--noblobs] [--noreads]
8+
[--format FORMAT] [--noblobs] [--noreads] [--refcov FILE]
99
[-h|--help]
1010
1111
Options:
@@ -40,6 +40,9 @@
4040
ps, svg, svgz, tiff) [default: png]
4141
--noblobs Omit blobplot [default: False]
4242
--noreads Omit plot of reads mapping [default: False]
43+
--refcov FILE File containing number of "mapped" and "unmapped" reads
44+
per coverage file. (e.g.: bam0,900,100). If provided, info
45+
will be used in read coverage plot(s).
4346
"""
4447

4548
from __future__ import division
@@ -75,6 +78,7 @@
7578
format = args['--format']
7679
no_plot_blobs = args['--noblobs']
7780
no_plot_reads = args['--noreads']
81+
refcov_f = args['--refcov']
7882

7983
# Does blobdb_f exist ?
8084
if not isfile(blobdb_f):
@@ -103,6 +107,9 @@
103107
exclude_groups = exclude_groups.rsplit(",")
104108
else:
105109
exclude_groups = exclude_groups
110+
111+
if (refcov_f):
112+
refcov_dict = BtPlot.parseRefCov(revcov)
106113

107114
# Load BlobDb
108115
print BtLog.status_d['9'] % blobdb_f
@@ -163,4 +170,4 @@
163170
plotObj.write_stats()
164171

165172
if not (no_plot_reads):
166-
plotObj.plotReadCov()
173+
plotObj.plotReadCov(refcov_dict)

0 commit comments

Comments
 (0)