3
3
4
4
"""
5
5
File : BtPlot.py
6
- Version : 0.1
7
6
Author : Dominik R. Laetsch, dominik.laetsch at gmail dot com
8
- Bugs : ?
9
- To do : ?
10
7
"""
8
+
11
9
from __future__ import division
12
10
from numpy import array , arange , logspace , mean , std
13
11
import math
14
- import lib .BtLog as BtLog
15
- import lib .BtIO as BtIO
16
- import lib .BtTax as BtTax
12
+ import bloblib .BtLog as BtLog
13
+ import bloblib .BtIO as BtIO
14
+ import bloblib .BtTax as BtTax
17
15
import matplotlib as mat
18
16
from matplotlib import cm
19
17
from matplotlib .ticker import NullFormatter , MultipleLocator , AutoMinorLocator
@@ -91,17 +89,13 @@ def set_format_scatterplot(axScatter, **kwargs):
91
89
min_y , max_y = 0.005 , kwargs ['max_cov' ]+ 1000
92
90
axScatter .set_yscale ('log' )
93
91
axScatter .set_xscale ('linear' )
94
- axScatter .set_xlabel ("GC proportion" )
95
- axScatter .set_ylabel ("Coverage" )
96
92
axScatter .xaxis .set_major_locator (major_xticks )
97
93
axScatter .xaxis .set_minor_locator (minor_xticks )
98
94
elif kwargs ['plot' ] == 'covplot' :
99
95
min_x , max_x = 0.005 , kwargs ['max_cov' ]+ 1000
100
96
min_y , max_y = 0.005 , kwargs ['max_cov' ]+ 1000
101
97
axScatter .set_yscale ('log' )
102
98
axScatter .set_xscale ('log' )
103
- axScatter .set_xlabel (kwargs ['xlabel' ])
104
- axScatter .set_ylabel (kwargs ['ylabel' ])
105
99
else :
106
100
BtLog .error ('34' % kwargs ['plot' ])
107
101
axScatter .set_xlim ( (min_x , max_x ) )
@@ -211,12 +205,14 @@ def __init__(self, data_dict, cov_lib_dict, cov_lib_selection, plot_type):
211
205
self .labels = {'all' }
212
206
self .plot = plot_type # type of plot
213
207
self .group_labels = {}
208
+ self .cov_lib_dict = cov_lib_dict
214
209
self .cov_libs = self .subselect_cov_libs (cov_lib_dict , cov_lib_selection )
215
210
self .cov_libs_total_reads_dict = self .get_cov_libs_total_reads_dict (cov_lib_dict )
216
211
self .cov_libs_mapped_reads_dict = self .get_cov_libs_mapped_reads_dict (cov_lib_dict )
217
212
self .data_dict = data_dict
218
213
self .stats = {}
219
214
self .exclude_groups = []
215
+ self .version = None
220
216
self .colours = {}
221
217
self .group_order = []
222
218
self .plot_order = []
@@ -230,10 +226,8 @@ def __init__(self, data_dict, cov_lib_dict, cov_lib_selection, plot_type):
230
226
self .cumulative_flag = ''
231
227
232
228
self .cov_y_dict = {}
233
- self .xlabel = ''
234
- self .ylabel = ''
235
- self .xmax = ''
236
- self .ymax = ''
229
+ self .xlabel = None
230
+ self .ylabel = None
237
231
238
232
self .refcov_dict = {}
239
233
@@ -298,18 +292,33 @@ def write_stats(self, out_f):
298
292
for g , labels in self .group_labels .items ():
299
293
if label in labels :
300
294
stats .append (self .get_stats_for_group (g ))
301
-
295
+ output = []
296
+ output .append ('## %s' % self .version )
297
+ for cov_lib , cov_lib_dict in self .cov_lib_dict .items ():
298
+ if cov_lib in self .cov_libs :
299
+ output .append ("## %s=%s" % (cov_lib , cov_lib_dict ['f' ]))
300
+ fields = ['name' , 'colour' , 'count_visible' , 'count_visible_perc' , 'span_visible' ,'span_visible_perc' , 'n50' , 'gc_mean' , 'gc_std' ]
301
+ header = [field for field in fields ]
302
+ for cov_lib in sorted (self .cov_libs ):
303
+ header .append ('%s_mean' % cov_lib )
304
+ header .append ('%s_std' % cov_lib )
305
+ header .append ('%s_read_map' % cov_lib )
306
+ header .append ('%s_read_map_p' % cov_lib )
307
+ output .append ('# %s' % "\t " .join (header ))
308
+ for stat in stats :
309
+ line = []
310
+ for field in fields :
311
+ line .append (stat [field ])
312
+ for cov_lib in sorted (self .cov_libs ):
313
+ line .append (stat ['cov_mean' ][cov_lib ])
314
+ line .append (stat ['cov_std' ][cov_lib ])
315
+ line .append (stat ['reads_mapped' ][cov_lib ])
316
+ line .append (stat ['reads_mapped_perc' ][cov_lib ])
317
+ output .append ("%s" % "\t " .join (line ))
302
318
out_f = "%s.stats.txt" % out_f
303
319
with open (out_f , 'w' ) as fh :
304
- for cov_lib in sorted (self .cov_libs ):
305
- fh .write ("# %s - %s\n " % (self .out_f , cov_lib ))
306
- fh .write ("%s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\n " % ('Group' , 'colour' , 'count' , 'visible-%' , 'span' , 'visible-%' , 'n50' , 'GC-mean' , 'GC-std' , 'Cov-mean' , 'Cov-std' , 'Mapped-reads' , 'Mapped-reads-%' ))
307
- for stat in stats :
308
- fh .write ("%s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\n " % (\
309
- stat ['name' ], stat ['colour' ], stat ['count_visible' ], stat ['count_visible_perc' ], stat ['span_visible' ], \
310
- stat ['span_visible_perc' ], stat ['n50' ], stat ['gc_mean' ], stat ['gc_std' ], stat ['cov_mean' ][cov_lib ], \
311
- stat ['cov_std' ][cov_lib ], stat ['reads_mapped' ][cov_lib ], stat ['reads_mapped_perc' ][cov_lib ]))
312
-
320
+ print BtLog .status_d ['24' ] % ("%s" % out_f )
321
+ fh .write ("\n " .join (output ))
313
322
314
323
def compute_stats (self ):
315
324
stats = {}
@@ -334,6 +343,7 @@ def compute_stats(self):
334
343
'count_hidden' : 0 ,
335
344
'span_hidden' : 0
336
345
}
346
+
337
347
for group , labels in self .group_labels .items ():
338
348
for label in labels :
339
349
stats [label ]['name' ] = stats [label ]['name' ] + self .data_dict [group ]['name' ]
@@ -372,10 +382,8 @@ def relabel_and_colour(self, colour_dict, user_labels):
372
382
colour_groups = [group if not (group in user_labels ) else user_labels [group ] for group in groups ]
373
383
colour_dict = generateColourDict (colour_groups )
374
384
for idx , group in enumerate (self .group_order ):
375
- if (self .exclude_groups ):
376
- if group in self .exclude_groups :
377
- self .group_labels [group ].add ('other' )
378
- self .colours [group ] = WHITE
385
+ if group in self .exclude_groups :
386
+ pass
379
387
elif group in user_labels :
380
388
label = user_labels [group ]
381
389
self .group_labels [group ].add (label )
@@ -408,7 +416,9 @@ def setupPlot(self, plot):
408
416
# Setting up plots and axes
409
417
fig = plt .figure (1 , figsize = (35 ,35 ), dpi = 400 )
410
418
axScatter = plt .axes (rect_scatter , axisbg = BGGREY )
411
- axScatter = set_format_scatterplot (axScatter , max_cov = self .max_cov , plot = plot , xlabel = self .xlabel , ylabel = self .ylabel )
419
+ axScatter = set_format_scatterplot (axScatter , max_cov = self .max_cov , plot = plot )
420
+ axScatter .set_xlabel (self .xlabel )
421
+ axScatter .set_ylabel (self .ylabel )
412
422
axHistx = plt .axes (rect_histx , axisbg = BGGREY )
413
423
axHistx = set_format_hist_x (axHistx , axScatter )
414
424
axHisty = plt .axes (rect_histy , axisbg = BGGREY )
@@ -475,16 +485,20 @@ def plotBar(self, cov_lib, out_f):
475
485
ax_main_data ['labels' ].append ('Mapped (assembly)' )
476
486
ax_main_data ['values' ].append (reads_mapped / reads_total )
477
487
ax_main_data ['colours' ].append (DGREY )
478
- if cov_lib in self .refcov_dict :
479
- reads_total_ref = refcov_dict [cov_lib ]['reads_total' ]
480
- reads_mapped_ref = refcov_dict [cov_lib ]['reads_mapped' ]
481
- reads_unmapped_ref = reads_total_ref - reads_mapped_ref
482
- ax_main_data ['labels' ].append ('Unmapped (ref)' )
483
- ax_main_data ['values' ].append (reads_unmapped_ref / reads_total_ref )
484
- ax_main_data ['colours' ].append (DGREY )
485
- ax_main_data ['labels' ].append ('Mapped (ref)' )
486
- ax_main_data ['values' ].append (reads_mapped_ref / reads_total_ref )
487
- ax_main_data ['colours' ].append (DGREY )
488
+ if (self .refcov_dict ):
489
+ if cov_lib in self .refcov_dict :
490
+ reads_total_ref = self .refcov_dict [cov_lib ]['reads_total' ]
491
+ reads_mapped_ref = self .refcov_dict [cov_lib ]['reads_mapped' ]
492
+ reads_unmapped_ref = reads_total_ref - reads_mapped_ref
493
+ ax_main_data ['labels' ].append ('Unmapped (ref)' )
494
+ ax_main_data ['values' ].append (reads_unmapped_ref / reads_total_ref )
495
+ ax_main_data ['colours' ].append (DGREY )
496
+ ax_main_data ['labels' ].append ('Mapped (ref)' )
497
+ ax_main_data ['values' ].append (reads_mapped_ref / reads_total_ref )
498
+ ax_main_data ['colours' ].append (DGREY )
499
+ else :
500
+ BtLog .error ('40' , cov_lib )
501
+
488
502
# mapped plotted groups
489
503
for group in self .plot_order :
490
504
ax_group_data ['labels' ].append (group )
@@ -524,7 +538,7 @@ def plotScatter(self, cov_lib, info_flag, out_f):
524
538
if group == 'no-hit' :
525
539
alpha = 0.5
526
540
group_length_array = array (self .stats [group ]['length' ])
527
- if len (group_length_array ) > 0 :
541
+ if len (group_length_array ) > 0 and group not in self . exclude_groups :
528
542
colour = self .colours [group ]
529
543
group_x_array = ''
530
544
group_y_array = ''
@@ -567,7 +581,7 @@ def plotScatter(self, cov_lib, info_flag, out_f):
567
581
axScatter .scatter (group_x_array , group_y_array , color = colour , s = marker_size_array , lw = lw , alpha = alpha , edgecolor = BLACK , label = label )
568
582
axLegend .axis ('off' )
569
583
if (self .multiplot ):
570
- fig_m , axScatter_m , axHistx_m , axHisty_m , axLegend_m = self .setupPlot ()
584
+ fig_m , axScatter_m , axHistx_m , axHisty_m , axLegend_m , top_bins , right_bins = self .setupPlot (self . plot )
571
585
legend_handles_m = []
572
586
legend_labels_m = []
573
587
legend_handles_m .append (Line2D ([0 ], [0 ], linewidth = 0.5 , linestyle = "none" , marker = "o" , alpha = 1 , markersize = 24 , markerfacecolor = colour ))
0 commit comments