def main(): args = docopt(__doc__) args = BtPlot.check_input(args) blobdb_f = args['--infile'] cov_f = args['--cov'] rank = args['--rank'] min_length = int(args['--length']) max_group_plot = int(args['--plotgroups']) hide_nohits = args['--nohit'] taxrule = args['--taxrule'] c_index = args['--cindex'] exclude_groups = args['--exclude'] labels = args['--label'] colour_f = args['--colours'] refcov_f = args['--refcov'] catcolour_f = args['--catcolour'] sort_order = args['--sort'] sort_first = args['--sort_first'] multiplot = args['--multiplot'] out_prefix = args['--out'] sort_order = args['--sort'] hist_type = args['--hist'] no_title = args['--notitle'] ignore_contig_length = args['--noscale'] format_plot = args['--format'] no_plot_blobs = args['--noblobs'] #no_plot_reads = args['--noreads'] legend_flag = args['--legend'] cumulative_flag = args['--cumulative'] cov_lib_selection = args['--lib'] xlabel = args['--xlabel'] ylabel = args['--ylabel'] axis_max = float(args['--max']) exclude_groups = BtIO.parseCmdlist(exclude_groups) refcov_dict = BtIO.parseReferenceCov(refcov_f) user_labels = BtIO.parseCmdLabels(labels) catcolour_dict = BtIO.parseCatColour(catcolour_f) colour_dict = BtIO.parseColours(colour_f) # Load BlobDb print(BtLog.status_d['9'] % blobdb_f) blobDb = Bt.BlobDb('blobplot') blobDb.version = interface.__version__ blobDb.load(blobdb_f) # Generate plot data print(BtLog.status_d['1'] % ('cov_y_axis', cov_f)) cov_y_dict, reads_total, reads_mapped, reads_unmapped, read_cov_dict = BtIO.parseCov( cov_f, set(blobDb.dict_of_blobs)) print(BtLog.status_d['18']) data_dict, min_cov, max_cov, cov_lib_dict = blobDb.getPlotData( rank, min_length, hide_nohits, taxrule, c_index, catcolour_dict) plotObj = BtPlot.PlotObj(data_dict, cov_lib_dict, cov_lib_selection, 'covplot', sort_first) # set lowest coverage to 0.01 for contig in cov_y_dict: if cov_y_dict[contig] < 0.1: cov_y_dict[contig] = 0.1 plotObj.cov_y_dict = cov_y_dict plotObj.exclude_groups = exclude_groups plotObj.version = blobDb.version plotObj.format = format_plot plotObj.max_cov = axis_max plotObj.no_title = no_title plotObj.multiplot = multiplot plotObj.hist_type = hist_type plotObj.ignore_contig_length = ignore_contig_length plotObj.max_group_plot = max_group_plot plotObj.legend_flag = legend_flag plotObj.cumulative_flag = cumulative_flag # order by which to plot (should know about user label) plotObj.group_order = BtPlot.getSortedGroups(data_dict, sort_order) # labels for each level of stats plotObj.labels.update(plotObj.group_order) # plotObj.group_labels is dict that contains labels for each group : all/other/user_label if (user_labels): for group, label in user_labels.items(): plotObj.labels.add(label) plotObj.group_labels = {group: set() for group in plotObj.group_order} plotObj.relabel_and_colour(colour_dict, user_labels) plotObj.compute_stats() plotObj.refcov_dict = refcov_dict # Plotting info_flag = 1 out_f = '' for cov_lib in plotObj.cov_libs: plotObj.xlabel = basename(cov_lib_dict[cov_lib]['f']) plotObj.ylabel = cov_f if (ylabel): plotObj.ylabel = ylabel if (xlabel): plotObj.xlabel = xlabel out_f = "%s.%s.%s.p%s.%s.%s" % (blobDb.title, taxrule, rank, max_group_plot, hist_type, min_length) if catcolour_dict: out_f = "%s.%s" % (out_f, "catcolour") if ignore_contig_length: out_f = "%s.%s" % (out_f, "noscale") if c_index: out_f = "%s.%s" % (out_f, "c_index") if exclude_groups: out_f = "%s.%s" % (out_f, "exclude_" + "_".join(exclude_groups)) if labels: out_f = "%s.%s" % (out_f, "userlabel_" + "_".join( set([name for name in user_labels.values()]))) out_f = "%s.%s" % (out_f, "covplot") if (plotObj.cumulative_flag): out_f = "%s.%s" % (out_f, "cumulative") if (plotObj.multiplot): out_f = "%s.%s" % (out_f, "multiplot") out_f = BtIO.getOutFile(out_f, out_prefix, None) if not (no_plot_blobs): plotObj.plotScatter(cov_lib, info_flag, out_f) info_flag = 0 plotObj.write_stats(out_f)
def parseCoverage(self, **kwargs): # arguments covLibObjs = kwargs['covLibObjs'] no_base_cov = kwargs['no_base_cov'] for covLib in covLibObjs: self.addCovLib(covLib) print BtLog.status_d['1'] % (covLib.name, covLib.f) if covLib.fmt == 'bam' or covLib.fmt == 'sam': base_cov_dict = {} if covLib.fmt == 'bam': base_cov_dict, covLib.reads_total, covLib.reads_mapped, read_cov_dict = BtIO.parseBam( covLib.f, set(self.dict_of_blobs), no_base_cov) else: base_cov_dict, covLib.reads_total, covLib.reads_mapped, read_cov_dict = BtIO.parseSam( covLib.f, set(self.dict_of_blobs), no_base_cov) if covLib.reads_total == 0: print BtLog.warn_d['4'] % covLib.f for name, base_cov in base_cov_dict.items(): cov = base_cov / self.dict_of_blobs[name].agct_count covLib.cov_sum += cov self.dict_of_blobs[name].addCov(covLib.name, cov) self.dict_of_blobs[name].addReadCov( covLib.name, read_cov_dict[name]) # Create COV file for future use out_f = BtIO.getOutFile(covLib.f, kwargs.get('prefix', None), None) covView = ViewObj(name="covlib", out_f=out_f, suffix="cov", header="", body=[]) self.view(viewObjs=[covView], ranks=None, taxrule=None, hits_flag=None, seqs=None, cov_libs=[covLib.name], progressbar=False) elif covLib.fmt == 'cas': cov_dict, covLib.reads_total, covLib.reads_mapped, read_cov_dict = BtIO.parseCas( covLib.f, self.order_of_blobs) if covLib.reads_total == 0: print BtLog.warn_d['4'] % covLib.f for name, cov in cov_dict.items(): covLib.cov_sum += cov self.dict_of_blobs[name].addCov(covLib.name, cov) self.dict_of_blobs[name].addReadCov( covLib.name, read_cov_dict[name]) out_f = BtIO.getOutFile(covLib.f, kwargs.get('prefix', None), None) covView = ViewObj(name="covlib", out_f=out_f, suffix="cov", header="", body=[]) self.view(viewObjs=[covView], ranks=None, taxrule=None, hits_flag=None, seqs=None, cov_libs=[covLib.name], progressbar=False) elif covLib.fmt == 'cov': base_cov_dict, covLib.reads_total, covLib.reads_mapped, covLib.reads_unmapped, read_cov_dict = BtIO.parseCov( covLib.f, set(self.dict_of_blobs)) #cov_dict = BtIO.readCov(covLib.f, set(self.dict_of_blobs)) if not len(base_cov_dict) == self.seqs: print BtLog.warn_d['4'] % covLib.f for name, cov in base_cov_dict.items(): covLib.cov_sum += cov self.dict_of_blobs[name].addCov(covLib.name, cov) if name in read_cov_dict: self.dict_of_blobs[name].addReadCov( covLib.name, read_cov_dict[name]) else: pass covLib.mean_cov = covLib.cov_sum / self.seqs if covLib.cov_sum == 0.0: print BtLog.warn_d['6'] % (covLib.name) self.covLibs[covLib.name] = covLib