def test_run_core_diversity_analyses_no_tree(self):
        """run_core_diversity_analyses functions without tree
        """
        # this takes a long time, so use a longer sigalrm
        run_core_diversity_analyses(
            self.test_data['biom'][0],
            self.test_data['map'][0],
            20,
            output_dir=self.test_out,
            params=parse_qiime_parameters(
                ['beta_diversity:metrics bray_curtis',
                 'alpha_diversity:metrics observed_species,chao1']),
            qiime_config=self.qiime_config,
            categories=['SampleType'],
            tree_fp=None,
            parallel=False,
            status_update_callback=no_status_updates)

        # Basic sanity test that output directories and files exist
        fps = [
            '%s/bdiv_even20' % self.test_out,
            '%s/arare_max20' % self.test_out,
            '%s/taxa_plots' % self.test_out,
            '%s/bdiv_even20/bray_curtis_dm.txt' % self.test_out,
            '%s/arare_max20/compare_observed_species/SampleType_boxplots.pdf' % self.test_out,
            '%s/index.html' % self.test_out,
            '%s/table_mc%d.biom.gz' % (self.test_out, 20)
        ]
        for fp in fps:
            self.assertTrue(exists(fp))

        # phylogenetic diversity output files do not exist
        self.assertFalse(exists(
            '%s/bdiv_even20/unweighted_unifrac_dm.txt' % self.test_out))
    def test_run_core_diversity_analyses_parallel(self):
        """run_core_diversity_analyses functions with categories in parallel
        """
        run_core_diversity_analyses(
            self.test_data['biom'][0],
            self.test_data['map'][0],
            20,
            output_dir=self.test_out,
            params=parse_qiime_parameters({}),
            arare_num_steps=3,
            qiime_config=self.qiime_config,
            categories=['SampleType', 'days_since_epoch'],
            tree_fp=self.test_data['tree'][0],
            parallel=True,
            status_update_callback=no_status_updates)

        # Basic sanity test that output directories and files exist
        fps = [
            '%s/bdiv_even20' % self.test_out,
            '%s/arare_max20' % self.test_out,
            '%s/taxa_plots' % self.test_out,
            '%s/bdiv_even20/unweighted_unifrac_dm.txt' % self.test_out,
            '%s/bdiv_even20/weighted_unifrac_pc.txt' % self.test_out,
            '%s/arare_max20/compare_chao1/days_since_epoch_stats.txt' % self.test_out,
            '%s/arare_max20/compare_PD_whole_tree/SampleType_boxplots.pdf' % self.test_out,
            '%s/index.html' % self.test_out,
            '%s/table_mc%d.biom.gz' % (self.test_out, 20)
        ]
        for fp in fps:
            self.assertTrue(exists(fp))
    def test_run_core_diversity_analyses_no_categories(self):
        """run_core_diversity_analyses functions without categories
        """
        # this takes a long time, so use a longer sigalrm
        run_core_diversity_analyses(
            self.test_data['biom'][0],
            self.test_data['map'][0],
            20,
            output_dir=self.test_out,
            params=parse_qiime_parameters({}),
            qiime_config=self.qiime_config,
            categories=None,
            tree_fp=self.test_data['tree'][0],
            parallel=False,
            status_update_callback=no_status_updates)

        # Basic sanity test that output directories and files exist
        fps = [
            '%s/bdiv_even20' % self.test_out,
            '%s/arare_max20' % self.test_out,
            '%s/taxa_plots' % self.test_out,
            '%s/bdiv_even20/unweighted_unifrac_dm.txt' % self.test_out,
            '%s/bdiv_even20/weighted_unifrac_pc.txt' % self.test_out,
            '%s/index.html' % self.test_out,
            '%s/table_mc%d.biom.gz' % (self.test_out, 20)
        ]
        for fp in fps:
            self.assertTrue(exists(fp))

        # categorical output files don't exist
        self.assertFalse(exists(
            '%s/arare_max20/compare_chao1/days_since_epoch_stats.txt' % self.test_out))
        self.assertFalse(exists(
            '%s/arare_max20/compare_PD_whole_tree/SampleType_boxplots.pdf' % self.test_out))
    def test_run_core_diversity_analyses(self):
        """run_core_diversity_analyses functions with categories
        """
        run_core_diversity_analyses(
            self.test_data["biom"][0],
            self.test_data["map"][0],
            20,
            output_dir=self.test_out,
            params=parse_qiime_parameters({}),
            qiime_config=self.qiime_config,
            categories=["SampleType", "days_since_epoch"],
            tree_fp=self.test_data["tree"][0],
            parallel=False,
            status_update_callback=no_status_updates,
        )

        # Basic sanity test that output directories and files exist
        fps = [
            "%s/bdiv_even20" % self.test_out,
            "%s/arare_max20" % self.test_out,
            "%s/taxa_plots" % self.test_out,
            "%s/bdiv_even20/unweighted_unifrac_dm.txt" % self.test_out,
            "%s/bdiv_even20/weighted_unifrac_pc.txt" % self.test_out,
            "%s/arare_max20/compare_chao1/days_since_epoch_stats.txt" % self.test_out,
            "%s/arare_max20/compare_PD_whole_tree/SampleType_boxplots.pdf" % self.test_out,
            "%s/index.html" % self.test_out,
            "%s/table_mc%d.biom.gz" % (self.test_out, 20),
        ]
        for fp in fps:
            self.assertTrue(exists(fp))
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    verbose = opts.verbose

    input_biom_fp = opts.input_biom_fp
    output_dir = opts.output_dir
    categories = opts.categories
    if categories is not None:
        categories = categories.split(',')
    tree_fp = opts.tree_fp
    mapping_fp = opts.mapping_fp
    verbose = opts.verbose
    parallel = opts.parallel
    sampling_depth = opts.sampling_depth
    nonphylogenetic_diversity = opts.nonphylogenetic_diversity
    print_only = opts.print_only
    suppress_taxa_summary = opts.suppress_taxa_summary
    suppress_beta_diversity = opts.suppress_beta_diversity
    suppress_alpha_diversity = opts.suppress_alpha_diversity
    suppress_group_significance = opts.suppress_group_significance

    if opts.parameter_fp is not None:
        params = parse_qiime_parameters(open(opts.parameter_fp, 'U'))
    else:
        params = parse_qiime_parameters([])

    if nonphylogenetic_diversity:
        # if the user specified --nonphylogenetic_diversity and they
        # didn't define metrics in a parameters file, define them here
        if 'metrics' not in params['beta_diversity']:
            params['beta_diversity']['metrics'] = 'bray_curtis'
        if 'metrics' not in params['alpha_diversity']:
            params['alpha_diversity']['metrics'] = 'observed_otus,chao1'
    else:
        if tree_fp is None:
            option_parser.error(
                "--tree_fp is required unless --nonphylogenetic_diversity "
                "is passed.")

    jobs_to_start = opts.jobs_to_start
    default_jobs_to_start = qiime_config['jobs_to_start']
    validate_and_set_jobs_to_start(params,
                                   jobs_to_start,
                                   default_jobs_to_start,
                                   parallel,
                                   option_parser)

    # Create the output directory. If it already exists and the user
    # isn't trying to recover from a failed run, raise an error.
    create_dir(output_dir, fail_on_exist=not opts.recover_from_failure)

    if print_only:
        command_handler = print_commands
    else:
        command_handler = call_commands_serially

    if verbose:
        status_update_callback = print_to_stdout
    else:
        status_update_callback = no_status_updates

    run_core_diversity_analyses(
        biom_fp=input_biom_fp,
        mapping_fp=mapping_fp,
        sampling_depth=sampling_depth,
        output_dir=output_dir,
        qiime_config=load_qiime_config(),
        command_handler=command_handler,
        tree_fp=tree_fp,
        params=params,
        categories=categories,
        arare_min_rare_depth=10,
        arare_num_steps=10,
        parallel=parallel,
        suppress_taxa_summary=suppress_taxa_summary,
        suppress_beta_diversity=suppress_beta_diversity,
        suppress_alpha_diversity=suppress_alpha_diversity,
        suppress_group_significance=suppress_group_significance,
        status_update_callback=status_update_callback)