def test_translate_colors(self): """Checks translate_colors is sane""" # Sets up knowns values known_def_8 = array([[0.83529412, 0.24313725, 0.30980392], [0.95686275, 0.42745098, 0.26274510], [0.99215686, 0.68235294, 0.38039216], [0.99607843, 0.87843137, 0.54509804], [0.90196078, 0.96078431, 0.59607843], [0.67058824, 0.86666667, 0.64313725], [0.40000000, 0.76078431, 0.64705882], [0.19607843, 0.53333333, 0.74117647]]) known_PuRd_9 = array([[0.96862745, 0.95686275, 0.97647059], [0.90588235, 0.88235294, 0.93725490], [0.83137255, 0.72549020, 0.85490196], [0.78823529, 0.58039216, 0.78039216], [0.87450980, 0.39607843, 0.69019608], [0.90588235, 0.16078431, 0.54117647], [0.80784314, 0.07058824, 0.33725490], [0.59607843, 0.00000000, 0.26274510], [0.40392157, 0.00000000, 0.12156863]]) # Test the calls with self.assertRaises(ValueError): translate_colors(5, 'Winchester') with self.assertRaises(ValueError): translate_colors(13) def_map = translate_colors(8) PuRd_map = translate_colors(9, 'PuRd') # Checks the outputs are sane assert_almost_equal(known_def_8, def_map) assert_almost_equal(known_PuRd_9, PuRd_map)
def test_translate_colors(self): """Checks translate_colors is sane""" # Sets up knowns values known_def_8 = array([[0.83529412, 0.24313725, 0.30980392], [0.95686275, 0.42745098, 0.26274510], [0.99215686, 0.68235294, 0.38039216], [0.99607843, 0.87843137, 0.54509804], [0.90196078, 0.96078431, 0.59607843], [0.67058824, 0.86666667, 0.64313725], [0.40000000, 0.76078431, 0.64705882], [0.19607843, 0.53333333, 0.74117647]]) known_PuRd_9 = array([[0.96862745, 0.95686275, 0.97647059], [0.90588235, 0.88235294, 0.93725490], [0.83137255, 0.72549020, 0.85490196], [0.78823529, 0.58039216, 0.78039216], [0.87450980, 0.39607843, 0.69019608], [0.90588235, 0.16078431, 0.54117647], [0.80784314, 0.07058824, 0.33725490], [0.59607843, 0.00000000, 0.26274510], [0.40392157, 0.00000000, 0.12156863]]) # Test the calls with self.assertRaises(ValueError): translate_colors(5, 'Winchester') with self.assertRaises(ValueError): translate_colors(13) def_map = translate_colors(8) PuRd_map = translate_colors(9, 'PuRd') # Checks the outputs are sane assert_almost_equal(known_def_8, def_map) assert_almost_equal(known_PuRd_9, PuRd_map)
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)
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)