def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    # Create the output dir if it doesn't already exist.
    try:
        if not path.exists(opts.output_dir):
            create_dir(opts.output_dir)
    except:
        option_parser.error("Could not create or access output directory "
                            "specified with the -o option.")
    sample_id_map = None
    if opts.sample_id_map_fp:
        sample_id_map = dict([(k, v[0]) for k, v in fields_to_dict(
            open(opts.sample_id_map_fp, "U")).items()])
    input_dm_fps = opts.input_dms
    distmats = [parse_distmat(open(dm_fp, 'U')) for dm_fp in input_dm_fps]

    if opts.method == 'mantel':
        output_f = open(path.join(opts.output_dir, 'mantel_results.txt'), 'w')
        output_f.write(
            run_mantel_test('mantel',
                            input_dm_fps,
                            distmats,
                            opts.num_permutations,
                            opts.tail_type,
                            comment_mantel_pmantel,
                            sample_id_map=sample_id_map))
    elif opts.method == 'partial_mantel':
        output_f = open(
            path.join(opts.output_dir, 'partial_mantel_results.txt'), 'w')
        output_f.write(
            run_mantel_test('partial_mantel',
                            input_dm_fps,
                            distmats,
                            opts.num_permutations,
                            opts.tail_type,
                            comment_mantel_pmantel,
                            control_dm_fp=opts.control_dm,
                            control_dm=parse_distmat(open(
                                opts.control_dm, 'U')),
                            sample_id_map=sample_id_map))
    elif opts.method == 'mantel_corr':
        output_f = open(
            path.join(opts.output_dir, 'mantel_correlogram_results.txt'), 'w')
        result_str, correlogram_fps, correlograms = run_mantel_correlogram(
            input_dm_fps,
            distmats,
            opts.num_permutations,
            comment_corr,
            opts.alpha,
            sample_id_map=sample_id_map,
            variable_size_distance_classes=opts.variable_size_distance_classes)

        output_f.write(result_str)
        for corr_fp, corr in zip(correlogram_fps, correlograms):
            corr.savefig(path.join(opts.output_dir, corr_fp + opts.image_type),
                         format=opts.image_type)
    output_f.close()
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    # Create the output dir if it doesn't already exist.
    try:
        if not path.exists(opts.output_dir):
            create_dir(opts.output_dir)
    except:
        option_parser.error("Could not create or access output directory " "specified with the -o option.")
    sample_id_map = None
    if opts.sample_id_map_fp:
        sample_id_map = dict([(k, v[0]) for k, v in fields_to_dict(open(opts.sample_id_map_fp, "U")).items()])
    input_dm_fps = opts.input_dms
    distmats = [parse_distmat(open(dm_fp, "U")) for dm_fp in input_dm_fps]

    if opts.method == "mantel":
        output_f = open(path.join(opts.output_dir, "mantel_results.txt"), "w")
        output_f.write(
            run_mantel_test(
                "mantel",
                input_dm_fps,
                distmats,
                opts.num_permutations,
                opts.tail_type,
                comment_mantel_pmantel,
                sample_id_map=sample_id_map,
            )
        )
    elif opts.method == "partial_mantel":
        output_f = open(path.join(opts.output_dir, "partial_mantel_results.txt"), "w")
        output_f.write(
            run_mantel_test(
                "partial_mantel",
                input_dm_fps,
                distmats,
                opts.num_permutations,
                opts.tail_type,
                comment_mantel_pmantel,
                control_dm_fp=opts.control_dm,
                control_dm=parse_distmat(open(opts.control_dm, "U")),
                sample_id_map=sample_id_map,
            )
        )
    elif opts.method == "mantel_corr":
        output_f = open(path.join(opts.output_dir, "mantel_correlogram_results.txt"), "w")
        result_str, correlogram_fps, correlograms = run_mantel_correlogram(
            input_dm_fps, distmats, opts.num_permutations, comment_corr, opts.alpha, sample_id_map=sample_id_map
        )
        output_f.write(result_str)
        for corr_fp, corr in zip(correlogram_fps, correlograms):
            corr.savefig(path.join(opts.output_dir, corr_fp + opts.image_type), format=opts.image_type)
    output_f.close()
 def test_run_mantel_test_single_matrix(self):
     """Test running mantel test on a single dm."""
     exp = '# A sample comment.\nDM\tDM\tNumber of entries\tMantel r ' + \
           'statistic\tp-value\tNumber of permutations\tTail type\n'
     obs = run_mantel_test('mantel', [self.fp1], [self.dm3], self.num_perms,
             'less', self.comment, 0.5)
     self.assertEqual(self.remove_nums(obs), exp)
 def test_run_mantel_test(self):
     """Test running mantel test on two distmats."""
     exp = '# A sample comment.\nDM\tDM\tNumber of entries\tMantel r ' + \
           'statistic\tp-value\tNumber of permutations\tTail type\n' + \
           'foo.txt\tbar.txt\t\t.\t.\t\tgreater\n'
     obs = run_mantel_test('mantel', [self.fp1, self.fp2],
             [self.dm1, self.dm2], self.num_perms, self.tail_type,
             self.comment, self.alpha)
     self.assertEqual(self.remove_nums(obs), exp)
 def test_run_mantel_test_partial_mantel_sample_id_map(self):
     """Test running partial mantel test with incompatible dms to map."""
     exp = '# A sample comment.\nDM\tDM\tCDM\tNumber of entries\tMantel' + \
           ' r statistic\tp-value\tNumber of permutations\tTail type\n' + \
           'foo.txt\tbar.txt\tbaz.txt\t\t.\t.\t\tgreater\n'
     obs = run_mantel_test('partial_mantel', [self.fp1, self.fp2],
             [self.dm1, self.dm2], self.num_perms, self.tail_type,
             self.comment, self.fp3, self.dm4, self.sample_id_map)
     self.assertEqual(self.remove_nums(obs), exp)
 def test_run_mantel_test_partial_mantel_too_small(self):
     """Test running partial mantel test with incompatible dms."""
     exp = '# A sample comment.\nDM\tDM\tCDM\tNumber of entries\t' + \
           'Mantel r statistic\tp-value\tNumber of permutations\t' + \
           'Tail type\nfoo.txt\tbar.txt\tbaz.txt\t\tToo few samples\n'
     obs = run_mantel_test('partial_mantel', [self.fp1, self.fp2],
             [self.dm1, self.dm2], self.num_perms, self.tail_type,
             self.comment, self.fp3, self.dm4)
     self.assertEqual(self.remove_nums(obs), exp)
 def test_run_mantel_test_partial_mantel(self):
     """Test running partial mantel test with two dms and a control dm."""
     exp = '# A sample comment.\nDM\tDM\tCDM\tNumber of entries\t' + \
           'Mantel r statistic\tp-value\tNumber of permutations\tTail ' + \
           'type\nfoo.txt\tbar.txt\tbaz.txt\t\t.\t.\t\tgreater\n'
     obs = run_mantel_test('partial_mantel', [self.fp1, self.fp2],
             [self.dm1, self.dm2], self.num_perms, self.tail_type,
             self.comment, self.fp3, self.dm3)
     self.assertEqual(self.remove_nums(obs), exp)
 def test_run_mantel_test_too_small(self):
     """Test running mantel test on two distmats that are incompatible."""
     exp = '# A sample comment.\nDM\tDM\tNumber of entries\tMantel r ' + \
     'statistic\tp-value\tNumber of permutations\tTail type\nfoo.txt\t' + \
     'bar.txt\t\tToo few samples\n'
     obs = run_mantel_test('mantel', [self.fp1, self.fp2],
             [self.dm3, self.dm4], self.num_perms, 'less',
             self.comment, 0.5)
     self.assertEqual(self.remove_nums(obs), exp)
 def test_run_mantel_test_sample_id_map(self):
     """Test running mantel test on two distmats that need IDs mapped."""
     exp = '# A sample comment.\nDM\tDM\tNumber of entries\tMantel r ' + \
           'statistic\tp-value\tNumber of permutations\tTail type\n' + \
           'foo.txt\tbar.txt\t\t.\t.\t\tless\n'
     obs = run_mantel_test('mantel', [self.fp1, self.fp2],
             [self.dm3, self.dm4], self.num_perms, 'less',
             self.comment, 0.5, sample_id_map=self.sample_id_map)
     self.assertEqual(self.remove_nums(obs), exp)
 def test_run_mantel_test_multiple(self):
     """Test running mantel test on three distmats."""
     exp = (
         "# A sample comment.\nDM\tDM\tNumber of entries\tMantel r "
         + "statistic\tp-value\tNumber of permutations\tTail type\n"
         + "foo.txt\tbar.txt\t\t.\t.\t\tgreater\nfoo.txt\tbaz.txt\t\t-."
         + "\t.\t\tgreater\nbar.txt\tbaz.txt\t\t-.\t.\t\tgreater\n"
     )
     obs = run_mantel_test(
         "mantel",
         [self.fp1, self.fp2, self.fp3],
         [self.dm1, self.dm2, self.dm3],
         self.num_perms,
         self.tail_type,
         self.comment,
         self.alpha,
     )
     self.assertEqual(self.remove_nums(obs), exp)