def test_to_dict_multi(self): """ Multiple complex elements. The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ tags = ["alpha", "beta", "gamma"] r = Report('redfang', tags=tags) a = Attribute('a', 'b') a2 = Attribute('a2', 'b2') r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup('pgid') pg.add_plot(Plot('pid', 'anImg')) pg.add_plot(Plot('pid2', 'anImg2')) r.add_plotgroup(pg) pg = PlotGroup('pgid2') pg.add_plot(Plot('pid2', 'anImg2')) pg.add_plot(Plot('pid22', 'anImg22')) r.add_plotgroup(pg) t = Table('tabid') t.add_column(Column('c1')) r.add_table(t) t = Table('tabid2') t.add_column(Column('c2')) r.add_table(t) d = r.to_dict() log.debug(str(d)) assert 'redfang' == d['id'] assert 'redfang.a' == d['attributes'][0]['id'] assert 'redfang.a2' == d['attributes'][1]['id'] assert 'redfang.pgid' == d['plotGroups'][0]['id'] assert 'redfang.pgid.pid' == d['plotGroups'][0]['plots'][0]['id'] assert 'redfang.pgid.pid2' == d['plotGroups'][0]['plots'][1]['id'] assert 'redfang.pgid2' == d['plotGroups'][1]['id'] assert 'redfang.pgid2.pid2' == d['plotGroups'][1]['plots'][0]['id'] assert 'redfang.pgid2.pid22' == d['plotGroups'][1]['plots'][1]['id'] assert 'redfang.tabid' == d['tables'][0]['id'] assert 'redfang.tabid.c1' == d['tables'][0]['columns'][0]['id'] assert 'redfang.tabid2' == d['tables'][1]['id'] assert 'redfang.tabid2.c2' == d['tables'][1]['columns'][0]['id'] assert list(sorted(d['tags'])) == list(sorted(tags)) loaded_report = load_report_from(d) assert list(sorted(loaded_report.tags)) == list(sorted(tags)) log.info(repr(r)) assert repr(r) is not None
def test_to_dict_multi(self): """ Multiple complex elements. The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ r = Report('redfang') a = Attribute('a', 'b') a2 = Attribute('a2', 'b2') r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup('pgid') pg.add_plot(Plot('pid', 'anImg')) pg.add_plot(Plot('pid2', 'anImg2')) r.add_plotgroup(pg) pg = PlotGroup('pgid2') pg.add_plot(Plot('pid2', 'anImg2')) pg.add_plot(Plot('pid22', 'anImg22')) r.add_plotgroup(pg) t = Table('tabid') t.add_column(Column('c1')) r.add_table(t) t = Table('tabid2') t.add_column(Column('c2')) r.add_table(t) d = r.to_dict() log.debug(str(d)) self.assertEqual('redfang', d['id']) self.assertEqual('redfang.a', d['attributes'][0]['id']) self.assertEqual('redfang.a2', d['attributes'][1]['id']) self.assertEqual('redfang.pgid', d['plotGroups'][0]['id']) self.assertEqual('redfang.pgid.pid', d['plotGroups'][0]['plots'][0]['id']) self.assertEqual('redfang.pgid.pid2', d['plotGroups'][0]['plots'][1]['id']) self.assertEqual('redfang.pgid2', d['plotGroups'][1]['id']) self.assertEqual('redfang.pgid2.pid2', d['plotGroups'][1]['plots'][0]['id']) self.assertEqual('redfang.pgid2.pid22', d['plotGroups'][1]['plots'][1]['id']) self.assertEqual('redfang.tabid', d['tables'][0]['id']) self.assertEqual('redfang.tabid.c1', d['tables'][0]['columns'][0]['id']) self.assertEqual('redfang.tabid2', d['tables'][1]['id']) self.assertEqual('redfang.tabid2.c2', d['tables'][1]['columns'][0]['id']) log.info(repr(r)) self.assertIsNotNone(repr(r))
def test_init_with_thumbnail(self): """Initial with thumbnail""" image = "my_image.png" thumbnail = "my_image_thumb.png" p = Plot('plot_1', image, thumbnail=thumbnail, caption="Awesome image") self.assertEqual(p.thumbnail, thumbnail) log.info(pformat(p.to_dict())) self.assertTrue(isinstance(p.to_dict(), dict))
def test_to_dict(self): """Test plot to dictionary method""" a = Plot('123', 'foo', caption='foo is the caption') d = a.to_dict() self.assertEquals('123', d['id']) self.assertEquals('foo', d['image']) self.assertEquals('foo is the caption', d['caption']) log.info(pformat(d, indent=4)) log.info(repr(a)) self.assertIsNotNone(repr(a))
def test_to_dict(self): """Test plot to dictionary method""" a = Plot('123', 'foo', caption='foo is the caption') d = a.to_dict() assert '123' == d['id'] assert 'foo' == d['image'] assert 'foo is the caption' == d['caption'] assert Plot.PLOT_TYPE == d['plotType'] log.info(pformat(d, indent=4)) log.info(repr(a)) assert repr(a) is not None
def to_readlen_plotgroup(readlen_dist, output_dir): plot_name = get_plot_title( spec, Constants.PG_READLENGTH, Constants.P_READLENGTH) x_label = get_plot_xlabel( spec, Constants.PG_READLENGTH, Constants.P_READLENGTH) y_label = get_plot_ylabel( spec, Constants.PG_READLENGTH, Constants.P_READLENGTH) nbins = readlen_dist.numBins heights = readlen_dist.bins bin_width = readlen_dist.binWidth edges = [float(bn) * bin_width for bn in xrange(nbins)] edges, heights, bin_width = reshape(readlen_dist, edges, heights) fig, ax = get_fig_axes_lpr() if sum(readlen_dist.bins) > 0: ax.bar(edges, heights, color=get_green(0), edgecolor=get_green(0), width=(bin_width * 0.75)) ax.set_xlabel(x_label) ax.set_ylabel(y_label) ax.yaxis.set_major_locator(MaxNLocator(integer=True)) png_fn = os.path.join( output_dir, "{p}.png".format(p=Constants.P_READLENGTH)) png_base, thumbnail_base = save_figure_with_thumbnail(fig, png_fn, dpi=DEFAULT_DPI) readlen_plot = Plot(Constants.P_READLENGTH, os.path.relpath(png_base, output_dir), title=plot_name, caption=plot_name, thumbnail=os.path.relpath(thumbnail_base, output_dir)) plot_groups = [PlotGroup(Constants.PG_READLENGTH, plots=[readlen_plot])] return plot_groups
def _to_plot(d): id_ = _to_id(d['id']) caption = d.get('caption', None) image = d['image'] thumbnail = d.get('thumbnail', None) p = Plot(id_, image, caption=caption, thumbnail=thumbnail) return p
def test_basic(self): p1 = Plot('p1', 'image.png', thumbnail='thumb.png') p2 = Plot('p2', 'image2.png', thumbnail='thumb2.png') plots = [p1, p2] title = "My Plots" legend = "Legend" thumbnail = p1.thumbnail pg = PlotGroup('my_id', title=title, legend=legend, thumbnail=thumbnail, plots=plots) d = pg.to_dict() validate_plot_group(d) self.assertIsNotNone(d)
def fasta_to_plot_group(fasta_file, output_dir): lengths = [] with FastaReader(fasta_file) as f: for record in f: lengths.append(len(record.sequence)) from pbreports.plot.helper import get_fig_axes #pylint: disable=import-error from pbcommand.models.report import PlotGroup, Plot fig, ax = get_fig_axes() if len(lengths) == 1: v = lengths[0] hrange = (v - 1, v + 1) ax.hist(lengths, range=hrange) else: ax.hist(lengths) ax.set_title("Sequence Length Histogram") ax.set_xlabel("Sequence Length") name = "sequence_length_hist.png" png_path = os.path.join(output_dir, name) fig.savefig(png_path) plots = [Plot("sequence_lengths", name)] pg = PlotGroup("reference_hist", "Sequence Lengths", plots=plots) return pg
def make_report(in_fn, out_dir='.', bounds=None, nolegend=False, reference=None, dpi=60, name=None): """AlignmentToPng Report Convert an input bam or DataSet XML file to a figure of Concordance vs. Subread Length. Args: in_fn: the bam, DataSet XML or cmp.h5 file to turn into a length vs concordance plot out_dir: the output directory to be used with the file name or default name: the file name to be used with the outdir or default (no full path filenames!) bounds: the figure limits (in xmin:xmax:ymin:ymax) nolegend: exclude the figure legend reference: the reference to use in the figure. Default of all references dpi: the dots per inch (resolution) of the figure """ data = _read_in_file(in_fn, reference) report = Report('alignment_to_png_report') if not name: name = '%s.png' % os.path.splitext(os.path.basename(in_fn))[0] png_fn = os.path.join(out_dir, name) _make_plot(data, png_fn, bounds, dpi, nolegend) plot_group = PlotGroup(Constants.PLOT_GROUP_ID, plots=[Plot('alignment_to_png_plot', os.path.basename(png_fn))]) report.add_plotgroup(plot_group) return report
def addQmodMotifHist(csvFile, kinData, outputFolder, dpi=DEFAULT_DPI, max_motifs=10): # Apart from passing in motif_summary.csv file name, nearly identical to # addQmodHist image_name = os.path.join(outputFolder, Constants.I_MOTIFS_QMOD) # Generate modification detection plot fig, ax = plotMotifHist(csvFile, kinData, max_motifs=max_motifs) png, thumbpng = PH.save_figure_with_thumbnail(fig, image_name, dpi=dpi) log.info((png, thumbpng)) plot = Plot(Constants.P_MOD_QV, image=os.path.basename(png), thumbnail=os.path.basename(thumbpng)) pg = PlotGroup(Constants.PG_MOD_QV, title=get_plotgroup_title(spec, Constants.PG_MOD_QV), plots=[plot], thumbnail=os.path.basename(thumbpng)) return pg
def _coverage_vs_quality_plot(contigs, output_dir): """ Creates a scatter plot coverage vs quality plot for each contig in the polished assembly. Each point represents one contig. :param contigs: (dict) contig id -> ContigInfo object :param output_dir: (str) path to output directory :return: (Plot) object that has already been saved as a PNG to output_dir """ import pbreports.plot.helper as PH fig, axes = PH.get_fig_axes_lpr() axes = fig.add_subplot(111) axes.set_axisbelow(True) axes.set_ylabel( get_plot_ylabel(spec, Constants.PG_COVERAGE, Constants.P_COVERAGE)) axes.set_xlabel( get_plot_xlabel(spec, Constants.PG_COVERAGE, Constants.P_COVERAGE)) PH.set_tick_label_font_size(axes, 12, 12) PH.set_axis_label_font_size(axes, 16) x_vals = [x.mean_coverage for x in contigs.values()] y_vals = [x.mean_qv for x in contigs.values()] axes.set_xlim(0, max(x_vals) * 1.2) axes.set_ylim(0, max(y_vals) * 1.2) axes.scatter(x_vals, y_vals, s=12) png_path = os.path.join(output_dir, "polished_coverage_vs_quality.png") png, thumbpng = PH.save_figure_with_thumbnail(fig, png_path) return Plot(Constants.P_COVERAGE, os.path.basename(png), thumbnail=os.path.basename(thumbpng))
def _create_coverage_histo_plot_grp(self, stats, output_dir): """ Returns io.model.PlotGroup object Create the plotGroup element that contains the coverage plot histogram :param stats: (ReferenceStats) see _get_reference_coverage_stats :param output_dir: (string) where to write images """ fig, ax = self._create_histogram(stats) fname, thumb = [ os.path.basename(f) for f in save_figure_with_thumbnail( fig, os.path.join(output_dir, 'coverage_histogram.png')) ] plot = Plot(Constants.P_COVERAGE_HIST, fname, caption=get_plot_caption(self.spec, Constants.PG_COVERAGE_HIST, Constants.P_COVERAGE_HIST), title=get_plot_title(self.spec, Constants.PG_COVERAGE_HIST, Constants.P_COVERAGE_HIST)) plot_group = PlotGroup(Constants.PG_COVERAGE_HIST, thumbnail=thumb, plots=[plot], title=get_plotgroup_title( self.spec, Constants.PG_COVERAGE_HIST)) return plot_group
def create_plot(_make_plot_func, plot_id, axis_labels, nbins, plot_name, barcolor, data, output_dir, dpi=DEFAULT_DPI): """Internal function used to create Plot instances. This should probably have a special container class to capture all the plot config options. """ fig, ax = _make_plot_func(data, axis_labels, nbins, barcolor) path = os.path.join(output_dir, plot_name) try: fig.tight_layout() except AttributeError as e: # FIXME bug 25872 log.warn("figure.tight_layout() not available") log.warn(str(e)) except ValueError as e: log.error(str(e)) fig.savefig(path, dpi=dpi) log.debug("Saved plot with id {i} to {p}".format(p=path, i=plot_id)) thumbnail = plot_name.replace(".png", "_thumb.png") to_b = lambda x: os.path.basename(x) fig.savefig(os.path.join(output_dir, thumbnail), dpi=DEFAULT_THUMB_DPI) plt.close(fig) log.debug("Saved plot to {p}".format(p=thumbnail)) plot = Plot(plot_id, to_b(plot_name), thumbnail=to_b(thumbnail)) return plot
def to_hq_hist_plot(hqbasefraction_dist, output_dir): plot_name = get_plot_title(spec, Constants.PG_HQ, Constants.P_HQ) x_label = get_plot_xlabel(spec, Constants.PG_HQ, Constants.P_HQ) y_label = get_plot_ylabel(spec, Constants.PG_HQ, Constants.P_HQ) nbins = int(hqbasefraction_dist['NumBins'].metavalue) bin_counts = hqbasefraction_dist['BinCounts'] heights = [int(bc.metavalue) for bc in bin_counts] edges = [float(bn) / float(nbins) for bn in xrange(nbins)] bin_width = float(hqbasefraction_dist['BinWidth'].metavalue) fig, ax = get_fig_axes_lpr() ax.bar(edges, heights, color=get_green(0), edgecolor=get_green(0), width=(bin_width * 0.75)) ax.set_xlabel(x_label) ax.set_ylabel(y_label) png_fn = os.path.join(output_dir, "{p}.png".format(p=Constants.P_HQ)) png_base, thumbnail_base = save_figure_with_thumbnail(fig, png_fn, dpi=DEFAULT_DPI) hq_plot = Plot(Constants.P_HQ, os.path.relpath(png_base, output_dir), title=plot_name, caption=plot_name, thumbnail=os.path.relpath(thumbnail_base, output_dir)) plot_groups = [PlotGroup(Constants.PG_HQ, plots=[hq_plot])] return plot_groups
def test_get_plotgroup_by_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') pg1.add_plot(Plot('pid1', 'anImg')) r.add_plotgroup(pg1) pg = r.get_plotgroup_by_id('pgid1') self.assertEqual(pg, pg1)
def test_get_plotgroup_by_id_with_bad_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') pg1.add_plot(Plot('pid1', 'anImg')) r.add_plotgroup(pg1) bad_pg = r.get_plotgroup_by_id('id_that_does_not_exist') self.assertIsNone(bad_pg)
def test_get_plot_by_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') p1 = Plot('pid1', 'anImg') pg1.add_plot(p1) r.add_plotgroup(pg1) p = r.get_plotgroup_by_id('pgid1').get_plot_by_id('pid1') assert p == p1
def test_get_plot_by_id_with_bad_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') p1 = Plot('pid1', 'anImg') pg1.add_plot(p1) r.add_plotgroup(pg1) bad_p = r.get_plotgroup_by_id('pgid1').get_plot_by_id( 'id_that_does_not_exist') assert bad_p is None
def _get_plot_group_length(control_data, sample_data, output_dir): """ Create the quality plot group and return it. """ fig = _create_length_figure(control_data, sample_data) fname = 'control_non-control_readlength.png' thumb = save_figure_with_thumbnail(fig, os.path.join(output_dir, fname))[1] plots = [Plot(Constants.P_LENGTH, fname)] pg = PlotGroup(Constants.PG_LENGTH, thumbnail=os.path.basename(thumb), plots=plots) return pg
def apply_plot_view(self, plot): caption = plot.caption title = plot.title if caption is None: caption = self.caption if title is None: title = self.title return Plot(self.id, image=plot.image, caption=caption, title=title, thumbnail=plot.thumbnail)
def test_to_dict(self): """ The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ r = Report('redfang') a = Attribute('a', 'b') a2 = Attribute('a2', 'b2') r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup('pgid') pg.add_plot(Plot('pid', 'anImg')) pg.add_plot(Plot('pid2', 'anImg2')) r.add_plotgroup(pg) t = Table('tabid') t.add_column(Column('c1')) r.add_table(t) d = r.to_dict() log.debug("\n" + pformat(d)) r2 = load_report_from(d) self.assertEqual(r.uuid, r2.uuid) self.assertEqual('redfang', d['id']) self.assertEqual('redfang.a', d['attributes'][0]['id']) self.assertEqual('redfang.a2', d['attributes'][1]['id']) self.assertEqual('redfang.pgid', d['plotGroups'][0]['id']) self.assertEqual('redfang.pgid.pid', d['plotGroups'][0]['plots'][0]['id']) self.assertEqual('redfang.pgid.pid2', d['plotGroups'][0]['plots'][1]['id']) self.assertEqual('redfang.tabid', d['tables'][0]['id']) self.assertEqual('redfang.tabid.c1', d['tables'][0]['columns'][0]['id'])
def get_qmod_hist(basemods_h5, output_dir, dpi): """ Return a plot object """ fig, ax = _create_fig_template() plot_kinetics_hist(basemods_h5, ax) png_path = os.path.join(output_dir, "kinetic_histogram.png") png, thumbpng = PH.save_figure_with_thumbnail(fig, png_path, dpi=dpi) return Plot(Constants.P_HIST, os.path.basename(png), thumbnail=os.path.basename(thumbpng))
def _to_plot(d): id_ = _to_id(d['id']) caption = d.get('caption', None) image = d['image'] thumbnail = d.get('thumbnail', None) title = d.get('title', None) plot_type = d.get("plotType", Plot.PLOT_TYPE) plotly_version = d.get("plotlyVersion", None) if plot_type == Plot.PLOT_TYPE: return Plot(id_, image, caption=caption, thumbnail=thumbnail, title=title) elif plot_type == PlotlyPlot.PLOT_TYPE: return PlotlyPlot(id_, image, caption=caption, thumbnail=thumbnail, title=title, plotly_version=plotly_version) else: raise ValueError("Unrecognized plotType '{t}'".format(t=plot_type))
def addQmodPlot(kinData, outputFolder): #chartPng = "kineticDetections.png" chartPng = Constants.I_KINETICS_SCATTER # Generate modification detection plot plotKineticsScatter(kinData, os.path.join(outputFolder, chartPng)) # Put plot into report p = Plot(Constants.P_MOD_COV, image=chartPng) #graph = GraphItem() #graph.title = 'Modification QV vs. Coverage' # graph.addImage(chartPng) return p
def addQmodHist(kinData, outputFolder): #chartPng = "kineticHistogram.png" chartPng = Constants.I_KINETICS_HIST image_name = os.path.join(outputFolder, chartPng) # Generate modification detection plot plotKineticsHist(kinData, image_name) # Put plot into report #graph = GraphItem() p = Plot(Coverage.P_MOD_HIST, image=chartPng) #graph.title = 'Modification QVs' # graph.addImage(chartPng) return p
def _create_coverage_plot_grp(self, top_contigs, cov_map, output_dir): """ Returns io.model.PlotGroup object Create the plotGroup element that contains the coverage plots of the top contigs. :param top_contigs: (list of Contig objects) sorted by contig size :param cov_map: (dict string:ContigCoverage) mapping of contig.id to ContigCoverage object :param output_dir: (string) where to write images """ plots = [] thumbnail = None idx = 0 log.debug('Creating plots for {n} top contig(s)'.format( n=str(len(top_contigs)))) for tc in top_contigs: if not tc.id in cov_map: # no coverage of this contig log.debug('contig {c} has no coverage info '.format(c=tc.id)) continue ctg_cov = cov_map[tc.id] fig, ax = self._create_contig_plot(ctg_cov) fname = os.path.join(output_dir, ctg_cov.file_name) if thumbnail is None: imgfiles = save_figure_with_thumbnail(fig, fname) thumbnail = os.path.basename(imgfiles[1]) else: fig.savefig(fname) plt.close(fig) id_ = "coverage_contig_{i}".format(i=str(idx)) caption = self.spec.get_plotgroup_spec( Constants.PG_COVERAGE).get_plot_spec( Constants.P_COVERAGE).caption + " {c}." plot = Plot(id_, os.path.basename(fname), caption.format(c=ctg_cov.name), title=caption.format(c=ctg_cov.name)) plots.append(plot) idx += 1 plot_group = PlotGroup(Constants.PG_COVERAGE, title=get_plotgroup_title( self.spec, Constants.PG_COVERAGE), thumbnail=thumbnail, plots=plots) return plot_group
def _create_variants_plot_grp(top_contigs, var_map, output_dir): """ Returns io.model.PlotGroup object Create the plotGroup element that contains variants plots of the top contigs. :param top_contigs: (list of Contig objects) sorted by contig size :param var_map: (dict string:ContigVariants) mapping of contig.header to ContigVariants object :param output_dir: (string) where to write images """ plots = [] thumbnail = None legend = None idx = 0 for tc in top_contigs: if not tc.header in var_map: # no coverage of this contig continue ctg_var = var_map[tc.header] bars = _create_bars(ctg_var) if legend is None: legend = _get_legend_file(bars, output_dir) fig, ax = _create_contig_fig_ax(bars, _get_x_labels(ctg_var)) fname = os.path.join(output_dir, ctg_var.file_name) if thumbnail is None: imgfiles = PH.save_figure_with_thumbnail(fig, fname) thumbnail = os.path.basename(imgfiles[1]) else: fig.savefig(fname, dpi=DEFAULT_DPI) id_ = 'coverage_variants_{i}'.format(i=str(idx)) caption = "Observed variants across {c}".format(c=ctg_var.name) plot = Plot(id_, os.path.basename(fname), title=caption, caption=caption) plots.append(plot) idx += 1 plt.close(fig) plot_group = PlotGroup(Constants.PG_VARIANTS, thumbnail=thumbnail, legend=legend, plots=plots) return plot_group
def test_to_dict(self): """Test plotGroup to_dict function.""" a = PlotGroup('123', title='foo title', legend='foo legend', thumbnail='foo thumbnail') a.add_plot(Plot('id', 'i1', caption='a caption')) d = a.to_dict() log.debug(pformat(d)) self.assertEquals('123', d['id']) self.assertEquals('foo title', d['title']) self.assertEquals('foo legend', d['legend']) self.assertEquals('foo thumbnail', d['thumbnail']) self.assertEquals(1, len(d['plots'])) log.info(a) self.assertIsNotNone(repr(a))
def test_to_dict(self): """Test plotGroup to_dict function.""" a = PlotGroup('123', title='foo title', legend='foo legend', thumbnail='foo thumbnail') a.add_plot(Plot('id', 'i1', caption='a caption')) d = a.to_dict() log.debug(pformat(d)) assert '123' == d['id'] assert 'foo title' == d['title'] assert 'foo legend' == d['legend'] assert 'foo thumbnail' == d['thumbnail'] assert 1 == len(d['plots']) log.info(a) assert repr(a) is not None
def test_basic(self): image = "image.png" p = Plot("my_id", image, caption="Caption", thumbnail=image) d = p.to_dict() validate_plot(d)
def test_basic(self): image = 'image.png' p = Plot('my_id', image, caption="Caption", thumbnail=image) d = p.to_dict() validate_plot(d)