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)) 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 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_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_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)) 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 test_get_table_by_id_with_bad_id(self): r = Report('redfang') t1 = Table('tabid1') t1.add_column(Column('c1')) r.add_table(t1) bad_t = r.get_table_by_id('id_that_does_not_exist') self.assertIsNone(bad_t)
def test_get_table_by_id(self): r = Report('redfang') t1 = Table('tabid1') t1.add_column(Column('c1')) r.add_table(t1) t = r.get_table_by_id('tabid1') self.assertEqual(t, t1)
def test_get_column_by_id(self): r = Report('redfang') t1 = Table('tabid1') c1 = Column('c1') t1.add_column(c1) r.add_table(t1) c = r.get_table_by_id('tabid1').get_column_by_id('c1') self.assertEqual(c, c1)
def test_get_column_by_id_with_bad_id(self): r = Report('redfang') t1 = Table('tabid1') c1 = Column('c1') t1.add_column(c1) r.add_table(t1) bad_c = r.get_table_by_id('tabid1').get_column_by_id( 'id_that_does_not_exist') assert bad_c is None
def test_get_table_by_id(self): r = Report('redfang') t1 = Table('tabid1') t1.add_column(Column('c1')) r.add_table(t1) t = r.get_table_by_id('tabid1') assert t == t1 columns_d = t.to_columns_d() assert len(columns_d) == 0
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_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_consensus_table_and_attributes(ref_data, reference_entry): """ Get a tuple: Table and list of Attributes :param ref_data: (dict) dict of data pulled from alignment_summary.gff :param reference_entry: (pbsystem.io.reference_entry) reference entry :return: tuple (pbreports.io.model.Table, [pbreports.io.model.Attributes]) """ table = Table('consensus_table', 'Consensus Calling Results') table.add_column(Column('contig_name', 'Reference')) table.add_column(Column('contig_len', 'Reference Length')) table.add_column(Column('bases_called', 'Bases Called')) table.add_column(Column('concordance', 'Consensus Accuracy')) table.add_column(Column('coverage', 'Base Coverage')) ordered_ids = _ref_ids_ordered_by_len(ref_data) sum_lengths = 0.0 mean_bases_called = 0 mean_concord = 'NA' mean_coverage = 0 for seqid in ordered_ids: contig = reference_entry.get_contig(seqid) length = float(ref_data[seqid][LENGTH]) gaps = float(ref_data[seqid][GAPS]) errors = float(ref_data[seqid][ERR]) cov = float(ref_data[seqid][COV]) sum_lengths += length bases_called = 1.0 - gaps / length mean_bases_called += bases_called * length concord = 'NA' if length != gaps: log.info('length {f}'.format(f=length)) log.info('gaps {f}'.format(f=gaps)) log.info('errors {f}'.format(f=errors)) concord = 1.0 - errors / (length - gaps) if mean_concord is 'NA': mean_concord = concord * length else: mean_concord += concord * length coverage = cov / length mean_coverage += coverage * length # table shows values for each contig table.add_data_by_column_id('contig_name', contig.name) table.add_data_by_column_id('contig_len', length) table.add_data_by_column_id('bases_called', bases_called) table.add_data_by_column_id('concordance', concord) table.add_data_by_column_id('coverage', coverage) mean_contig_length = sum_lengths / len(ordered_ids) mean_bases_called = mean_bases_called / sum_lengths if mean_concord is not 'NA': mean_concord = mean_concord / sum_lengths mean_coverage = mean_coverage / sum_lengths attributes = [Attribute(id_, val, Constants.ATTR_LABELS[id_]) for id_, val in [ (Constants.MEAN_CONTIG_LENGTH, mean_contig_length), (Constants.MEAN_BASES_CALLED, mean_bases_called), (Constants.MEAN_CONCORDANCE, mean_concord), (Constants.MEAN_COVERAGE, mean_coverage), (Constants.LONGEST_CONTIG, ordered_ids[0])]] return table, attributes