def test_top_contigs(self):
     """
     Test top contigs from ref
     """
     #ref_entry = ReferenceEntry.from_ref_dir(ref)
     ref_entry = openReference(self.REFERENCE)
     self.assertEqual(1, len(get_top_contigs_from_ref_entry(ref_entry, 2)))
示例#2
0
 def test_top_contigs(self):
     """
     Test top contigs from ref
     """
     #ref_entry = ReferenceEntry.from_ref_dir(ref)
     ref_entry = openReference(self.REFERENCE)
     self.assertEqual(1, len(get_top_contigs_from_ref_entry(ref_entry, 2)))
    def test_get_consensus_table(self):
        """
        Test the data in the table object and the attributes
        """
        top_contigs = get_top_contigs_from_ref_entry(self._get_reference_entry(), 2)

        ref_data = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY, top_contigs)[0]
        self.assertEqual(1, len(ref_data))

        results = ref_data[self.CONTIG_ID]
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(0, results[ERR])

        _append_variants_gff_data(ref_data, self.VARIANTS_GFF)

        table, attributes = _get_consensus_table_and_attributes(ref_data, self._get_reference_entry())
        self.assertEqual(5, len(table.columns))
        self.assertEqual(self.CONTIG_ID, table.columns[0].values[0])
        self.assertEqual(self.TEST_VALUES["contig_len"],
                         table.columns[1].values[0])
        self.assertEqual(1, table.columns[2].values[0])
        self.assertAlmostEqual(self.TEST_VALUES["concordance"],
                               table.columns[3].values[0], places=4)
        self.assertAlmostEqual(self.TEST_VALUES["coverage"],
                               table.columns[4].values[0], places=2)
示例#4
0
def make_variants_report(aln_summ_gff, variants_gff, reference, max_contigs_to_plot, report, output_dir, dpi=72, dumpdata=True):
    """
    Entry to report.
    :param aln_summ_gff: (str) path to alignment_summary.gff
    :param variants_gff: (str) path to variants_gff
    :param reference: (str) path to reference_dir
    :param max_contigs_to_plot: (int) max number of contigs to plot
    """
    _validate_inputs([('aln_summ_gff', aln_summ_gff),
                      ('variants_gff', variants_gff),
                      ('reference', reference)])

    # reference entry & top contings
    ref = openReference(reference)
    top_contigs = get_top_contigs_from_ref_entry(ref, max_contigs_to_plot)

    # extract gff data from files
    ref_data, contig_variants = _extract_alignment_summ_data(
        aln_summ_gff, top_contigs)
    _append_variants_gff_data(ref_data, variants_gff)

    # make report objects
    table, atts = _get_consensus_table_and_attributes(ref_data, ref)
    plotgroup = _create_variants_plot_grp(
        top_contigs, contig_variants, output_dir)

    rpt = Report(Constants.R_ID,
                 plotgroups=[plotgroup],
                 attributes=atts,
                 tables=[table],
                 dataset_uuids=(ReferenceSet(reference).uuid,))

    rpt = spec.apply_view(rpt)
    rpt.write_json(os.path.join(output_dir, report))
    return rpt
示例#5
0
    def test_get_consensus_table(self):
        """
        Test the data in the table object and the attributes
        """
        top_contigs = get_top_contigs_from_ref_entry(
            self._get_reference_entry(), 2)

        ref_data = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY,
                                                top_contigs)[0]
        self.assertEqual(1, len(ref_data))

        results = ref_data[self.CONTIG_ID]
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(0, results[ERR])

        _append_variants_gff_data(ref_data, self.VARIANTS_GFF)

        table, attributes = _get_consensus_table_and_attributes(
            ref_data, self._get_reference_entry())
        self.assertEqual(5, len(table.columns))
        self.assertEqual(self.CONTIG_ID, table.columns[0].values[0])
        self.assertEqual(self.TEST_VALUES["contig_len"],
                         table.columns[1].values[0])
        self.assertEqual(1, table.columns[2].values[0])
        self.assertAlmostEqual(self.TEST_VALUES["concordance"],
                               table.columns[3].values[0],
                               places=4)
        self.assertAlmostEqual(self.TEST_VALUES["coverage"],
                               table.columns[4].values[0],
                               places=2)
    def test_get_consensus_attributes(self):
        """
        Test the data in the table object and the attributes
        """
        top_contigs = get_top_contigs_from_ref_entry(self._get_reference_entry(), 2)

        ref_data = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY, top_contigs)[0]
        self.assertEqual(1, len(ref_data))

        results = ref_data[self.CONTIG_ID]
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(0, results[ERR])

        _append_variants_gff_data(ref_data, self.VARIANTS_GFF)

        table, attributes = _get_consensus_table_and_attributes(ref_data, self._get_reference_entry())
        self.assertEqual(5, len(attributes))

        def __get_att_val(_id, atts):
            for a in atts:
                if a.id == _id:
                    return a.value

        self.assertEqual(self.TEST_VALUES["contig_len"], __get_att_val("mean_contig_length", attributes))
        self.assertAlmostEqual(
            self.TEST_VALUES["bases_called"], __get_att_val("weighted_mean_bases_called", attributes), places=4
        )
        self.assertAlmostEqual(
            self.TEST_VALUES["concordance"], __get_att_val("weighted_mean_concordance", attributes), places=4
        )
        self.assertAlmostEqual(
            self.TEST_VALUES["coverage"], __get_att_val("weighted_mean_coverage", attributes), places=2
        )
        self.assertEqual(self.CONTIG_ID, __get_att_val("longest_contig_name", attributes))
    def test_create_variants_plot_grp(self):
        """
        Test that plotGroup of variants plots is created as expected
        """
        top_contigs = get_top_contigs_from_ref_entry(self._get_reference_entry(), 25)
        ref_data, contig_variants = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY, top_contigs)

        plot_group = _create_variants_plot_grp(top_contigs, contig_variants, self._output_dir)
        self.assertEqual(1, len(plot_group.plots))

        self._assert_image_file(plot_group.thumbnail)

        for plot in plot_group.plots:
            self._assert_image_file(plot.image)
        self._assert_image_file(plot_group.legend)
    def test_extract_alignment_summ_data(self):
        """
        Test the dicts returned from parsing the alignment_summary gff
        """
#        top_contigs = get_top_contigs(ref, 2)

        top_contigs = get_top_contigs_from_ref_entry(self._get_reference_entry(), 2)

        ref_data, contig_variants = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY, top_contigs)
        self.assertEqual(1, len(ref_data))

        results = ref_data[self.CONTIG_ID]
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(0, results[ERR])

        self.assertEqual(1, len(contig_variants))
    def test_legend(self):
        """
        Create the legend png
        """
        top_contigs = get_top_contigs_from_ref_entry(self._get_reference_entry(), 25)
        contig_variants = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY, top_contigs)[1]
        ctgvar = None
        for t in top_contigs:
            ctgvar = contig_variants[t.header]

        bars = _create_bars(ctgvar)
        self.assertEqual(3, len(bars))

        leg = _get_legend_file(bars, self._output_dir)
        leg_path = op.join(self._output_dir, leg)

        log.info('legend path: {l}'.format(l=leg_path))
        self.assertTrue(op.exists(leg_path))
    def test_append_variants_gff_data(self):
        """
        Test that ERR information gets added to the ref_data dict
        """
#        top_contigs = get_top_contigs(ref, 2)

        top_contigs = get_top_contigs_from_ref_entry(self._get_reference_entry(), 2)

        ref_data = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY, top_contigs)[0]
        self.assertEqual(1, len(ref_data))

        results = ref_data[self.CONTIG_ID]
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(self.TEST_VALUES["n_gff_err_1"], results[ERR])

        _append_variants_gff_data(ref_data, self.VARIANTS_GFF)
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(self.TEST_VALUES["n_gff_err_2"], results[ERR])
示例#11
0
    def test_extract_alignment_summ_data(self):
        """
        Test the dicts returned from parsing the alignment_summary gff
        """
        #        top_contigs = get_top_contigs(ref, 2)

        top_contigs = get_top_contigs_from_ref_entry(
            self._get_reference_entry(), 2)

        ref_data, contig_variants = _extract_alignment_summ_data(
            self.ALIGNMENT_SUMMARY, top_contigs)
        self.assertEqual(1, len(ref_data))

        results = ref_data[self.CONTIG_ID]
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(0, results[ERR])

        self.assertEqual(1, len(contig_variants))
示例#12
0
    def test_create_variants_plot_grp(self):
        """
        Test that plotGroup of variants plots is created as expected
        """
        top_contigs = get_top_contigs_from_ref_entry(
            self._get_reference_entry(), 25)
        ref_data, contig_variants = _extract_alignment_summ_data(
            self.ALIGNMENT_SUMMARY, top_contigs)

        plot_group = _create_variants_plot_grp(top_contigs, contig_variants,
                                               self._output_dir)
        self.assertEqual(1, len(plot_group.plots))

        self._assert_image_file(plot_group.thumbnail)

        for plot in plot_group.plots:
            self._assert_image_file(plot.image)
        self._assert_image_file(plot_group.legend)
示例#13
0
    def test_get_consensus_attributes(self):
        """
        Test the data in the table object and the attributes
        """
        top_contigs = get_top_contigs_from_ref_entry(
            self._get_reference_entry(), 2)

        ref_data = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY,
                                                top_contigs)[0]
        self.assertEqual(1, len(ref_data))

        results = ref_data[self.CONTIG_ID]
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(0, results[ERR])

        _append_variants_gff_data(ref_data, self.VARIANTS_GFF)

        table, attributes = _get_consensus_table_and_attributes(
            ref_data, self._get_reference_entry())
        self.assertEqual(5, len(attributes))

        def __get_att_val(_id, atts):
            for a in atts:
                if a.id == _id:
                    return a.value

        self.assertEqual(self.TEST_VALUES["contig_len"],
                         __get_att_val('mean_contig_length', attributes))
        self.assertAlmostEqual(self.TEST_VALUES["bases_called"],
                               __get_att_val('weighted_mean_bases_called',
                                             attributes),
                               places=4)
        self.assertAlmostEqual(self.TEST_VALUES["concordance"],
                               __get_att_val('weighted_mean_concordance',
                                             attributes),
                               places=4)
        self.assertAlmostEqual(self.TEST_VALUES["coverage"],
                               __get_att_val('weighted_mean_coverage',
                                             attributes),
                               places=2)
        self.assertEqual(self.CONTIG_ID,
                         __get_att_val('longest_contig_name', attributes))
示例#14
0
    def test_legend(self):
        """
        Create the legend png
        """
        top_contigs = get_top_contigs_from_ref_entry(
            self._get_reference_entry(), 25)
        contig_variants = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY,
                                                       top_contigs)[1]
        ctgvar = None
        for t in top_contigs:
            ctgvar = contig_variants[t.header]

        bars = _create_bars(ctgvar)
        self.assertEqual(3, len(bars))

        leg = _get_legend_file(bars, self._output_dir)
        leg_path = op.join(self._output_dir, leg)

        log.info('legend path: {l}'.format(l=leg_path))
        self.assertTrue(op.exists(leg_path))
示例#15
0
    def test_append_variants_gff_data(self):
        """
        Test that ERR information gets added to the ref_data dict
        """
        #        top_contigs = get_top_contigs(ref, 2)

        top_contigs = get_top_contigs_from_ref_entry(
            self._get_reference_entry(), 2)

        ref_data = _extract_alignment_summ_data(self.ALIGNMENT_SUMMARY,
                                                top_contigs)[0]
        self.assertEqual(1, len(ref_data))

        results = ref_data[self.CONTIG_ID]
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(self.TEST_VALUES["n_gff_err_1"], results[ERR])

        _append_variants_gff_data(ref_data, self.VARIANTS_GFF)
        self.assertEqual(self.TEST_VALUES["contig_len"], results[LENGTH])
        self.assertEqual(self.TEST_VALUES["n_gff_err_2"], results[ERR])