def test_calculate_dimensions_rectangle(self):
        """Checcks calculate_dimensions_rectangle is sane"""
        # Sets up known values
        known_figure_dimensions_def = (5.2, 4.45)
        known_axis_dimensions_def = Bbox(array([[0.01923077, 0.02247191],
                                                [0.78846154, 0.92134831]]))

        known_figure_dims_in = (4.2, 3.45)
        known_axis_dims_in = Bbox(array([[0.26190476, 0.31884058],
                                         [0.73809524, 0.89855072]]))

        known_figure_dims_cm = (1.6535433, 1.3582677)
        known_axis_dims_cm = Bbox(array([[0.26190476, 0.31884058],
                                         [0.73809524, 0.89855072]]))
        # Sets up test values
        test_axis_side = 2
        test_border = 0.1
        test_xlab = 1
        test_ylab = 1

        # Tests that an error is raised if the units are not sane
        with self.assertRaises(ValueError):
            calculate_dimensions_rectangle(unit='Demons')

        # Calculates the test values
        (test_axis_df, test_fig_df) = calculate_dimensions_rectangle()

        (test_axis_in, test_fig_in) = \
            calculate_dimensions_rectangle(axis_width=test_axis_side,
                                           axis_height=test_axis_side,
                                           border=test_border,
                                           xlab=test_xlab,
                                           ylab=test_ylab)

        (test_axis_cm, test_fig_cm) = \
            calculate_dimensions_rectangle(axis_width=test_axis_side,
                                           axis_height=test_axis_side,
                                           border=test_border,
                                           xlab=test_xlab,
                                           ylab=test_ylab,
                                           unit='cm')

        assert_almost_equal(test_fig_df, known_figure_dimensions_def,
                            decimal=5)
        assert_almost_equal(test_axis_df, known_axis_dimensions_def,
                            decimal=5)

        assert_almost_equal(test_fig_in, known_figure_dims_in, decimal=5)
        assert_almost_equal(test_axis_in, known_axis_dims_in, decimal=5)

        assert_almost_equal(test_fig_cm, known_figure_dims_cm, decimal=5)
        assert_almost_equal(test_axis_cm, known_axis_dims_cm, decimal=5)
Example #2
0
    def test_calculate_dimensions_rectangle(self):
        """Checcks calculate_dimensions_rectangle is sane"""
        # Sets up known values
        known_figure_dimensions_def = (5.2, 4.45)
        known_axis_dimensions_def = Bbox(
            array([[0.01923077, 0.02247191], [0.78846154, 0.92134831]]))

        known_figure_dims_in = (4.2, 3.45)
        known_axis_dims_in = Bbox(
            array([[0.26190476, 0.31884058], [0.73809524, 0.89855072]]))

        known_figure_dims_cm = (1.6535433, 1.3582677)
        known_axis_dims_cm = Bbox(
            array([[0.26190476, 0.31884058], [0.73809524, 0.89855072]]))
        # Sets up test values
        test_axis_side = 2
        test_border = 0.1
        test_xlab = 1
        test_ylab = 1

        # Tests that an error is raised if the units are not sane
        with self.assertRaises(ValueError):
            calculate_dimensions_rectangle(unit='Demons')

        # Calculates the test values
        (test_axis_df, test_fig_df) = calculate_dimensions_rectangle()

        (test_axis_in, test_fig_in) = \
            calculate_dimensions_rectangle(axis_width=test_axis_side,
                                           axis_height=test_axis_side,
                                           border=test_border,
                                           xlab=test_xlab,
                                           ylab=test_ylab)

        (test_axis_cm, test_fig_cm) = \
            calculate_dimensions_rectangle(axis_width=test_axis_side,
                                           axis_height=test_axis_side,
                                           border=test_border,
                                           xlab=test_xlab,
                                           ylab=test_ylab,
                                           unit='cm')

        assert_almost_equal(test_fig_df,
                            known_figure_dimensions_def,
                            decimal=5)
        assert_almost_equal(test_axis_df, known_axis_dimensions_def, decimal=5)

        assert_almost_equal(test_fig_in, known_figure_dims_in, decimal=5)
        assert_almost_equal(test_axis_in, known_axis_dims_in, decimal=5)

        assert_almost_equal(test_fig_cm, known_figure_dims_cm, decimal=5)
        assert_almost_equal(test_axis_cm, known_axis_dims_cm, decimal=5)
def main(tax_table, output_dir, samples_to_analyze=None):
    """Generates pie chart of the most abundant twelve taxa in the sample
    INPUTS:
        otu_table -- a biom formatted taxonomy table at the desired level of
        resolution

        output_dir -- the location of the directory where output files should
                    be stored.

        samples_to_analyze -- a list of sample ids to plot. If no value is
                    passed, then all samples in the biom table are analyzed.

    OUTPUTS:
        A pdf of the piechart summarizing the most abundant taxa will be
        generated and saved to the output directory. These will follow the
        naming convention PIECHART_<SAMPLEID>.pdf.
    """

    # Creates the text around hte file name
    FILENAME_BEFORE = 'piechart_'
    FILENAME_AFTER = '.pdf'

    # Handles string cleaning
    RENDER = 'LATEX'
    UNCLASSIFIED = False

    # Sets up the rare threshhold for
    RARE_THRESH = 0.0
    SUM_MIN = 1

    # Sets up axis parameters
    AXIS_LENGTH = 7.25
    AXIS_BORDER = 0.01
    AXIS_TITLE = 0
    AXIS_LEGEND = 7
    # Modifies the axis limits
    AX_LIMS = [-1.05, 1.05]
    # Sets up constants for getting the colormap and plotting
    MAP_NAME = 'BrBG'
    NUM_SHOW = 12
    OTHER_COLOR = array([[85/255, 85/255, 85/255]])
    # Sets up plotting parameters
    FIG_LEGEND = True
    FIG_COLOR_EDGE = False
    FIG_LEG_FRAME = False
    FIG_LEG_OFFSET = [0.95, 0.025, 1.0, 0.95]
    # Sets up the the legend font
    LEG_FONT = FontProperties()
    LEG_FONT.set_size(28)
    LEG_FONT.set_family('sans-serif')
    # Sets the general font properties
    use_latex = True
    rc_font_family = 'sans-serif'
    rc_font = ['Helvetica', 'Arial']

    # Sets up the colormap
    colormap = translate_colors((NUM_SHOW-1), MAP_NAME)
    colormap = vstack((colormap, OTHER_COLOR))

     # Sets up plotting constants
    (axis_dims, fig_dims) = calculate_dimensions_rectangle(
        axis_width=AXIS_LENGTH, axis_height=AXIS_LENGTH, border=AXIS_BORDER,
        title=AXIS_TITLE, legend=AXIS_LEGEND)

    # Walks over a taxa tree and prioritizes based on taxonomy
    (tree, all_taxa) = build_tree_from_taxontable(tax_table)

    # Sets up samples for which tables are being generated
    if samples_to_analyze is not None:
        samples_to_test = samples_to_analyze
    else:
        samples_to_test = all_taxa.keys()

    # Checks the samples exist
    if samples_to_test:
        samples_to_test = set(samples_to_test)
        tmp = {k: v for k, v in all_taxa.items() if k in samples_to_test}
        all_taxa = tmp
        if not samples_to_test:
            raise ValueError("No samples!")

    # Walks over the table
    filt_fun = lambda v, i, md: v.sum() > 0
    for samp, filtered_table, rare, unique in sample_rare_unique(tree,
                                                                 tax_table,
                                                                 all_taxa,
                                                                 RARE_THRESH):
        # abund_fun = lambda v, i, md: i in all_taxa[samp]
        filtered_table = tax_table.filterObservations(filt_fun)
        sample_data = filtered_table.sampleData(samp)
        taxa = filtered_table.ObservationIds

        # Calculates abundance and limits to the top n samples.
        abund_rank = calculate_abundance(sample=sample_data,
                                         taxa=taxa,
                                         sum_min=SUM_MIN)

        abund_rank = abund_rank[:(NUM_SHOW-1)]

        # Cleans the greengenes strings and adds an "Other" Category for
        # missing taxa
        [sample_tax, sample_freq] = [list(a) for a in zip(*abund_rank)]
        clean_tax = [clean_greengenes_string(tax, RENDER,
                                             unclassified=UNCLASSIFIED)
                     for tax in sample_tax]
        clean_tax.append('Other')
        sample_freq.append(1-sum(sample_freq))

        # Sets up the sample filename
        filename = pjoin(output_dir, '%s%s%s' % (FILENAME_BEFORE, samp,
                                                 FILENAME_AFTER))


        # Creates the pie chart
        render_single_pie(data_vec=sample_freq,
                          group_names=clean_tax,
                          axis_dims=axis_dims,
                          fig_dims=fig_dims,
                          file_out=filename,
                          legend=FIG_LEGEND,
                          colors=colormap,
                          show_edge=FIG_COLOR_EDGE,
                          legend_frame=FIG_LEG_FRAME,
                          rc_font=rc_font,
                          legend_offset=FIG_LEG_OFFSET,
                          rc_fam=rc_font_family,
                          legend_font=LEG_FONT,
                          use_latex=use_latex,
                          x_lims=AX_LIMS,
                          y_lims=AX_LIMS)
Example #4
0
def main(tax_table, output_dir, samples_to_analyze=None):
    """Generates pie chart of the most abundant twelve taxa in the sample
    INPUTS:
        otu_table -- a biom formatted taxonomy table at the desired level of
        resolution

        output_dir -- the location of the directory where output files should
                    be stored.

        samples_to_analyze -- a list of sample ids to plot. If no value is
                    passed, then all samples in the biom table are analyzed.

    OUTPUTS:
        A pdf of the piechart summarizing the most abundant taxa will be
        generated and saved to the output directory. These will follow the
        naming convention PIECHART_<SAMPLEID>.pdf.
    """

    # Creates the text around hte file name
    FILENAME_BEFORE = 'piechart_'
    FILENAME_AFTER = '.pdf'

    # Handles string cleaning
    RENDER = 'LATEX'
    UNCLASSIFIED = False

    # Sets up the rare threshhold for
    RARE_THRESH = 0.0
    SUM_MIN = 1

    # Sets up axis parameters
    AXIS_LENGTH = 7.25
    AXIS_BORDER = 0.01
    AXIS_TITLE = 0
    AXIS_LEGEND = 7
    # Modifies the axis limits
    AX_LIMS = [-1.05, 1.05]
    # Sets up constants for getting the colormap and plotting
    MAP_NAME = 'BrBG'
    NUM_SHOW = 12
    OTHER_COLOR = array([[85 / 255, 85 / 255, 85 / 255]])
    # Sets up plotting parameters
    FIG_LEGEND = True
    FIG_COLOR_EDGE = False
    FIG_LEG_FRAME = False
    FIG_LEG_OFFSET = [0.95, 0.025, 1.0, 0.95]
    # Sets up the the legend font
    LEG_FONT = FontProperties()
    LEG_FONT.set_size(28)
    LEG_FONT.set_family('sans-serif')
    # Sets the general font properties
    use_latex = True
    rc_font_family = 'sans-serif'
    rc_font = ['Helvetica', 'Arial']

    # Sets up the colormap
    colormap = translate_colors((NUM_SHOW - 1), MAP_NAME)
    colormap = vstack((colormap, OTHER_COLOR))

    # Sets up plotting constants
    (axis_dims,
     fig_dims) = calculate_dimensions_rectangle(axis_width=AXIS_LENGTH,
                                                axis_height=AXIS_LENGTH,
                                                border=AXIS_BORDER,
                                                title=AXIS_TITLE,
                                                legend=AXIS_LEGEND)

    # Walks over a taxa tree and prioritizes based on taxonomy
    (tree, all_taxa) = build_tree_from_taxontable(tax_table)

    # Sets up samples for which tables are being generated
    if samples_to_analyze is not None:
        samples_to_test = samples_to_analyze
    else:
        samples_to_test = all_taxa.keys()

    # Checks the samples exist
    if samples_to_test:
        samples_to_test = set(samples_to_test)
        tmp = {k: v for k, v in all_taxa.items() if k in samples_to_test}
        all_taxa = tmp
        if not samples_to_test:
            raise ValueError("No samples!")

    # Walks over the table
    filt_fun = lambda v, i, md: v.sum() > 0
    for samp, filtered_table, rare, unique in sample_rare_unique(
            tree, tax_table, all_taxa, RARE_THRESH):
        # abund_fun = lambda v, i, md: i in all_taxa[samp]
        filtered_table = tax_table.filterObservations(filt_fun)
        sample_data = filtered_table.sampleData(samp)
        taxa = filtered_table.ObservationIds

        # Calculates abundance and limits to the top n samples.
        abund_rank = calculate_abundance(sample=sample_data,
                                         taxa=taxa,
                                         sum_min=SUM_MIN)

        abund_rank = abund_rank[:(NUM_SHOW - 1)]

        # Cleans the greengenes strings and adds an "Other" Category for
        # missing taxa
        [sample_tax, sample_freq] = [list(a) for a in zip(*abund_rank)]
        clean_tax = [
            clean_greengenes_string(tax, RENDER, unclassified=UNCLASSIFIED)
            for tax in sample_tax
        ]
        clean_tax.append('Other')
        sample_freq.append(1 - sum(sample_freq))

        # Sets up the sample filename
        filename = pjoin(output_dir,
                         '%s%s%s' % (FILENAME_BEFORE, samp, FILENAME_AFTER))

        # Creates the pie chart
        render_single_pie(data_vec=sample_freq,
                          group_names=clean_tax,
                          axis_dims=axis_dims,
                          fig_dims=fig_dims,
                          file_out=filename,
                          legend=FIG_LEGEND,
                          colors=colormap,
                          show_edge=FIG_COLOR_EDGE,
                          legend_frame=FIG_LEG_FRAME,
                          rc_font=rc_font,
                          legend_offset=FIG_LEG_OFFSET,
                          rc_fam=rc_font_family,
                          legend_font=LEG_FONT,
                          use_latex=use_latex,
                          x_lims=AX_LIMS,
                          y_lims=AX_LIMS)