def test_make_distance_boxplots_suppress_plots(self):
        """Test correctly suppresses different plot types."""
        obs = make_distance_boxplots(self.dm_f,
                                     self.map_f, ['Bar'],
                                     suppress_all_within=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 4)
        self.assertEqual(len(obs[0][3]), 4)
        self.assertEqual(obs[0][4], [None, None, None, None])

        obs = make_distance_boxplots(self.dm_f,
                                     self.map_f, ['Bar'],
                                     suppress_all_within=True,
                                     suppress_all_between=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 3)
        self.assertEqual(len(obs[0][3]), 3)
        self.assertEqual(obs[0][4], [None, None, None])

        obs = make_distance_boxplots(self.dm_f,
                                     self.map_f, ['Bar'],
                                     suppress_all_within=True,
                                     suppress_all_between=True,
                                     suppress_individual_within=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 1)
        self.assertEqual(len(obs[0][3]), 1)
        self.assertEqual(obs[0][4], [None])

        obs = make_distance_boxplots(self.dm_f,
                                     self.map_f, ['Bar'],
                                     suppress_all_within=True,
                                     suppress_all_between=True,
                                     suppress_individual_between=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 2)
        self.assertEqual(len(obs[0][3]), 2)
        self.assertEqual(obs[0][4], [None, None])

        obs = make_distance_boxplots(self.dm_f,
                                     self.map_f, ['Bar'],
                                     suppress_individual_within=True,
                                     suppress_individual_between=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 2)
        self.assertEqual(len(obs[0][3]), 2)
        self.assertEqual(obs[0][4], [None, None])
    def test_make_distance_boxplots_box_color(self):
        """Test correctly colors boxes in a variety of ways."""
        # Single box color for all.
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     box_color='r')
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 5)
        self.assertEqual(len(obs[0][3]), 5)
        self.assertEqual(obs[0][4], ['r', 'r', 'r', 'r', 'r'])

        # Single box color, with some plots suppressed.
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     box_color='r',
                                     suppress_individual_within=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 3)
        self.assertEqual(len(obs[0][3]), 3)
        self.assertEqual(obs[0][4], ['r', 'r', 'r'])

        # Color individual within boxes.
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo'],
                                     color_individual_within_by_field='Bar')
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Foo')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 7)
        self.assertEqual(len(obs[0][3]), 7)
        self.assertEqual(len(obs[0][4]), 7)
        self.assertIsNone(obs[0][4][0])
        self.assertIsNone(obs[0][4][1])
        self.assertEqual(obs[0][4][2], (1.0, 0.0, 0.0))
        self.assertEqual(obs[0][4][3], (0.0, 0.0, 1.0))
        self.assertIsNone(obs[0][4][4])
        self.assertIsNone(obs[0][4][5])
        self.assertIsNone(obs[0][4][6])

        # Color individual within boxes, make sure box_color is ignored.
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo'],
                                     box_color='pink', color_individual_within_by_field='Bar')
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Foo')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 7)
        self.assertEqual(len(obs[0][3]), 7)
        self.assertEqual(len(obs[0][4]), 7)
        self.assertIsNone(obs[0][4][0])
        self.assertIsNone(obs[0][4][1])
        self.assertEqual(obs[0][4][2], (1.0, 0.0, 0.0))
        self.assertEqual(obs[0][4][3], (0.0, 0.0, 1.0))
        self.assertIsNone(obs[0][4][4])
        self.assertIsNone(obs[0][4][5])
        self.assertIsNone(obs[0][4][6])
    def test_make_distance_boxplots_box_color(self):
        """Test correctly colors boxes in a variety of ways."""
        # Single box color for all.
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     box_color='r')
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 5)
        self.assertEqual(len(obs[0][3]), 5)
        self.assertEqual(obs[0][4], ['r', 'r', 'r', 'r', 'r'])

        # Single box color, with some plots suppressed.
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     box_color='r',
                                     suppress_individual_within=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 3)
        self.assertEqual(len(obs[0][3]), 3)
        self.assertEqual(obs[0][4], ['r', 'r', 'r'])

        # Color individual within boxes.
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo'],
                                     color_individual_within_by_field='Bar')
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Foo')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 7)
        self.assertEqual(len(obs[0][3]), 7)
        self.assertEqual(len(obs[0][4]), 7)
        self.assertIsNone(obs[0][4][0])
        self.assertIsNone(obs[0][4][1])
        self.assertEqual(obs[0][4][2], (1.0, 0.0, 0.0))
        self.assertEqual(obs[0][4][3], (0.0, 0.0, 1.0))
        self.assertIsNone(obs[0][4][4])
        self.assertIsNone(obs[0][4][5])
        self.assertIsNone(obs[0][4][6])

        # Color individual within boxes, make sure box_color is ignored.
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo'],
                                     box_color='pink', color_individual_within_by_field='Bar')
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Foo')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 7)
        self.assertEqual(len(obs[0][3]), 7)
        self.assertEqual(len(obs[0][4]), 7)
        self.assertIsNone(obs[0][4][0])
        self.assertIsNone(obs[0][4][1])
        self.assertEqual(obs[0][4][2], (1.0, 0.0, 0.0))
        self.assertEqual(obs[0][4][3], (0.0, 0.0, 1.0))
        self.assertIsNone(obs[0][4][4])
        self.assertIsNone(obs[0][4][5])
        self.assertIsNone(obs[0][4][6])
    def test_make_distance_boxplots_suppress_plots(self):
        """Test correctly suppresses different plot types."""
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     suppress_all_within=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 4)
        self.assertEqual(len(obs[0][3]), 4)
        self.assertEqual(obs[0][4], [None, None, None, None])

        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     suppress_all_within=True,
                                     suppress_all_between=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 3)
        self.assertEqual(len(obs[0][3]), 3)
        self.assertEqual(obs[0][4], [None, None, None])

        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     suppress_all_within=True,
                                     suppress_all_between=True,
                                     suppress_individual_within=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 1)
        self.assertEqual(len(obs[0][3]), 1)
        self.assertEqual(obs[0][4], [None])

        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     suppress_all_within=True,
                                     suppress_all_between=True,
                                     suppress_individual_between=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 2)
        self.assertEqual(len(obs[0][3]), 2)
        self.assertEqual(obs[0][4], [None, None])

        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
                                     suppress_individual_within=True,
                                     suppress_individual_between=True)
        self.assertEqual(len(obs), 1)
        self.assertEqual(obs[0][0], 'Bar')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 2)
        self.assertEqual(len(obs[0][3]), 2)
        self.assertEqual(obs[0][4], [None, None])
Example #5
0
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    # Create the output dir if it doesn't already exist.
    out_dir = opts.output_dir
    try:
        create_dir(out_dir)
    except:
        option_parser.error("Could not create or access output directory "
                            "specified with the -o option.")

    map_f = open(opts.mapping_fp, 'U')
    dm_f = open(opts.distance_matrix_fp, 'U')

    fields = map(strip, opts.fields.split(','))
    fields = [field.strip('"').strip("'") for field in fields]

    color_individual_within_by_field = opts.color_individual_within_by_field
    results = make_distance_boxplots(dm_f, map_f, fields, width=opts.width,
            height=opts.height, suppress_all_within=opts.suppress_all_within,
            suppress_all_between=opts.suppress_all_between,
            suppress_individual_within=opts.suppress_individual_within,
            suppress_individual_between=opts.suppress_individual_between,
            y_min=opts.y_min, y_max=opts.y_max,
            whisker_length=opts.whisker_length, box_width=opts.box_width,
            box_color=opts.box_color,
            color_individual_within_by_field=color_individual_within_by_field,
            sort=opts.sort)

    for field, plot_figure, plot_data, plot_labels, plot_colors in results:
        output_plot_fp = join(out_dir, "%s_Distances.%s" %
                              (field, opts.imagetype))
        plot_figure.savefig(output_plot_fp, format=opts.imagetype,
                            transparent=opts.transparent)

        if not opts.suppress_significance_tests:
            sig_tests_f = open(join(out_dir, "%s_Stats.txt" % field), 'w')
            sig_tests_results = all_pairs_t_test(plot_labels, plot_data,
                    tail_type=opts.tail_type,
                    num_permutations=opts.num_permutations)
            sig_tests_f.write(sig_tests_results)
            sig_tests_f.close()

        if opts.save_raw_data:
            # Write the raw plot data into a tab-delimited file.
            assert(len(plot_labels) == len(plot_data))
            raw_data_fp = join(out_dir, "%s_Distances.txt" % field)
            raw_data_f = open(raw_data_fp, 'w')

            for label, data in zip(plot_labels, plot_data):
                raw_data_f.write(label.replace(" ", "_") + "\t")
                raw_data_f.write("\t".join(map(str, data)))
                raw_data_f.write("\n")
            raw_data_f.close()
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    # Create the output dir if it doesn't already exist.
    out_dir = opts.output_dir
    try:
        create_dir(out_dir)
    except:
        option_parser.error("Could not create or access output directory "
                            "specified with the -o option.")

    map_f = open(opts.mapping_fp, 'U')
    dm_f = open(opts.distance_matrix_fp, 'U')

    fields = map(strip, opts.fields.split(','))
    fields = [field.strip('"').strip("'") for field in fields]

    color_individual_within_by_field = opts.color_individual_within_by_field
    results = make_distance_boxplots(dm_f, map_f, fields, width=opts.width,
            height=opts.height, suppress_all_within=opts.suppress_all_within,
            suppress_all_between=opts.suppress_all_between,
            suppress_individual_within=opts.suppress_individual_within,
            suppress_individual_between=opts.suppress_individual_between,
            y_min=opts.y_min, y_max=opts.y_max,
            whisker_length=opts.whisker_length, box_width=opts.box_width,
            box_color=opts.box_color,
            color_individual_within_by_field=color_individual_within_by_field,
            sort=opts.sort)

    for field, plot_figure, plot_data, plot_labels, plot_colors in results:
        output_plot_fp = join(out_dir, "%s_Distances.%s" %
                              (field, opts.imagetype))
        plot_figure.savefig(output_plot_fp, format=opts.imagetype,
                            transparent=opts.transparent)

        if not opts.suppress_significance_tests:
            sig_tests_f = open(join(out_dir, "%s_Stats.txt" % field), 'w')
            sig_tests_results = all_pairs_t_test(plot_labels, plot_data,
                    tail_type=opts.tail_type,
                    num_permutations=opts.num_permutations)
            sig_tests_f.write(sig_tests_results)
            sig_tests_f.close()

        if opts.save_raw_data:
            # Write the raw plot data into a tab-delimited file.
            assert(len(plot_labels) == len(plot_data))
            raw_data_fp = join(out_dir, "%s_Distances.txt" % field)
            raw_data_f = open(raw_data_fp, 'w')

            for label, data in zip(plot_labels, plot_data):
                raw_data_f.write(label.replace(" ", "_") + "\t")
                raw_data_f.write("\t".join(map(str, data)))
                raw_data_f.write("\n")
            raw_data_f.close()
    def test_make_distance_boxplots(self):
        """Test correctly generates plot, raw data, and labels."""
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo', 'Bar'])
        self.assertEqual(len(obs), 2)
        self.assertEqual(obs[0][0], 'Foo')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 7)
        self.assertEqual(len(obs[0][3]), 7)
        self.assertEqual(obs[0][4], [None, None, None, None, None, None, None])

        self.assertEqual(obs[1][0], 'Bar')
        self.assertTrue(isinstance(obs[1][1], Figure))
        self.assertEqual(len(obs[1][2]), 5)
        self.assertEqual(len(obs[1][3]), 5)
        self.assertEqual(obs[1][4], [None, None, None, None, None])
    def test_make_distance_boxplots(self):
        """Test correctly generates plot, raw data, and labels."""
        obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo', 'Bar'])
        self.assertEqual(len(obs), 2)
        self.assertEqual(obs[0][0], 'Foo')
        self.assertTrue(isinstance(obs[0][1], Figure))
        self.assertEqual(len(obs[0][2]), 7)
        self.assertEqual(len(obs[0][3]), 7)
        self.assertEqual(obs[0][4], [None, None, None, None, None, None, None])

        self.assertEqual(obs[1][0], 'Bar')
        self.assertTrue(isinstance(obs[1][1], Figure))
        self.assertEqual(len(obs[1][2]), 5)
        self.assertEqual(len(obs[1][3]), 5)
        self.assertEqual(obs[1][4], [None, None, None, None, None])