示例#1
0
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)
    otu_table_fp = opts.input_path

    otu_table = load_table(otu_table_fp)

    sample_ids = otu_table.ids()
    otu_ids = otu_table.ids(axis='observation')

    # This is not memory safe: need to be able to load the otu table as ints
    otu_table_array = array(list(otu_table.iter_data(axis='observation')),
                            dtype='int')

    if opts.type_of_test == 'all_together':
        type_of_test = TEST_ON_TREE
        header_text = "sample\tp value\tp value (Bonferroni corrected)\n"
    elif opts.type_of_test == 'each_pair':
        type_of_test = TEST_ON_PAIRWISE
        header_text = "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n"
    elif opts.type_of_test == 'each_sample':
        type_of_test = TEST_ON_ENVS
        header_text = "sample\tp value\tp value (Bonferroni corrected)\n"
        if opts.significance_test == 'p-test':
            raise RuntimeError(
                'significance test type "each_sample" not allowed for p-test')
    else:
        raise RuntimeError('significance test type "%s" not found' %
                           opts.type_of_test)

    # note, uses ugly temp file
    if opts.significance_test == 'unweighted_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(sample_ids, otu_ids,
                                               otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')
        try:
            result = fast_unifrac_permutations_file(tree_in,
                                                    envs_in,
                                                    weighted=False,
                                                    num_iters=opts.num_iters,
                                                    verbose=opts.verbose,
                                                    test_on=type_of_test)
        except ValueError as e:
            if e.message == ("No valid samples/environments found. Check"
                             " whether tree tips match otus/taxa present in"
                             " samples/environments"):
                raise ValueError(e.message + " and that the otu abundance is"
                                 " not relative.")
            raise e

        envs_in.close()
        os.remove(output_fp)

        of = open(opts.output_path, 'w')
        of.write("#unweighted unifrac significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'p-test':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(sample_ids, otu_ids,
                                               otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_p_test_file(tree_in,
                                  envs_in,
                                  num_iters=opts.num_iters,
                                  verbose=opts.verbose,
                                  test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write("#andy martin's p-test significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'weighted_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(sample_ids, otu_ids,
                                               otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_unifrac_permutations_file(tree_in,
                                                envs_in,
                                                weighted=True,
                                                num_iters=opts.num_iters,
                                                verbose=opts.verbose,
                                                test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write("#weighted unifrac significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'weighted_normalized_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(sample_ids, otu_ids,
                                               otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_unifrac_permutations_file(tree_in,
                                                envs_in,
                                                weighted='correct',
                                                num_iters=opts.num_iters,
                                                verbose=opts.verbose,
                                                test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write("#weighted normalized unifrac significance test\n")
        of.write(
            "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n")
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    else:
        raise RuntimeError('significance test "%s" not found' %
                           opts.significance_test)
def main():
    option_parser, opts, args =\
        parse_command_line_parameters(**script_info)

    if opts.tree_path==None:
        if opts.significance_test in ['unweighted_unifrac','weighted_unifrac','p-test']:
            raise RuntimeError('please supply a phylogenetic tree for %s' %\
                opts.significance_test)

    # note, uses ugly temp file
    if opts.significance_test == 'unweighted_unifrac':
        tree_in = open(opts.tree_path,'U')
        otu_table_fp = opts.input_path
        output_fp = opts.output_path + '_envs.tmp'
    
        otu_table_lines = open(otu_table_fp, 'U')
        sample_ids, otu_ids, otu_table_array, lineages = \
        parse_otu_table(otu_table_lines)
        result = format_unifrac_sample_mapping(
            sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp,'U')

        result = fast_unifrac_permutations_file(tree_in, envs_in,
            weighted=False, num_iters=opts.num_iters, verbose=opts.verbose)
        envs_in.close()
        os.remove(output_fp)
        
        of = open(opts.output_path,'w')
        of.write(\
            "#unweighted unifrac significance test\n")
        of.write(\
            "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n")
        for line in result:
            of.write('\t'.join(map(str,line)) + '\n')
        of.close()

    elif opts.significance_test == 'p-test':
        tree_in = open(opts.tree_path,'U')
        otu_table_fp = opts.input_path
        output_fp = opts.output_path + '_envs.tmp'
    
        otu_table_lines = open(otu_table_fp, 'U')
        sample_ids, otu_ids, otu_table_array, lineages = \
        parse_otu_table(otu_table_lines)
        result = format_unifrac_sample_mapping(
            sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp,'U')

        result = fast_p_test_file(tree_in, envs_in,
            num_iters=opts.num_iters, verbose=opts.verbose)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path,'w')
        of.write(\
            "#andy martin's p-test significance test\n")
        of.write(\
            "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n")
        for line in result:
            of.write('\t'.join(map(str,line)) + '\n')
        of.close()

    elif opts.significance_test == 'weighted_unifrac':
        tree_in = open(opts.tree_path,'U')
        otu_table_fp = opts.input_path
        output_fp = opts.output_path + '_envs.tmp'
    
        otu_table_lines = open(otu_table_fp, 'U')
        sample_ids, otu_ids, otu_table_array, lineages = \
        parse_otu_table(otu_table_lines)
        result = format_unifrac_sample_mapping(
            sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp,'U')

        result = fast_unifrac_permutations_file(tree_in, envs_in,
            weighted=True, num_iters=opts.num_iters, verbose=opts.verbose)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path,'w')
        of.write(\
            "#weighted unifrac significance test\n")
        of.write(\
            "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n")
        for line in result:
            of.write('\t'.join(map(str,line)) + '\n')
        of.close()

    else:
        raise RuntimeError('significance test "%s" not found' %\
            opts.significance_test)
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)
    otu_table_fp = opts.input_path

    otu_table = load_table(otu_table_fp)

    sample_ids = otu_table.ids()
    otu_ids = otu_table.ids(axis='observation')

    # This is not memory safe: need to be able to load the otu table as ints
    otu_table_array = array(list(otu_table.iter_data(axis='observation')),
                            dtype='int')

    if opts.type_of_test == 'all_together':
        type_of_test = TEST_ON_TREE
        header_text = "sample\tp value\tp value (Bonferroni corrected)\n"
    elif opts.type_of_test == 'each_pair':
        type_of_test = TEST_ON_PAIRWISE
        header_text = "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n"
    elif opts.type_of_test == 'each_sample':
        type_of_test = TEST_ON_ENVS
        header_text = "sample\tp value\tp value (Bonferroni corrected)\n"
        if opts.significance_test == 'p-test':
            raise RuntimeError(
                'significance test type "each_sample" not allowed for p-test')
    else:
        raise RuntimeError('significance test type "%s" not found' %
                           opts.type_of_test)

    # note, uses ugly temp file
    if opts.significance_test == 'unweighted_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(
            sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_unifrac_permutations_file(tree_in, envs_in,
                                                weighted=False, num_iters=opts.num_iters, verbose=opts.verbose, test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)

        of = open(opts.output_path, 'w')
        of.write("#unweighted unifrac significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'p-test':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(
            sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_p_test_file(tree_in, envs_in,
                                  num_iters=opts.num_iters, verbose=opts.verbose, test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write(
            "#andy martin's p-test significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'weighted_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(
            sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_unifrac_permutations_file(tree_in, envs_in,
                                                weighted=True, num_iters=opts.num_iters, verbose=opts.verbose, test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write(
            "#weighted unifrac significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'weighted_normalized_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(
            sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_unifrac_permutations_file(tree_in, envs_in,
                                                weighted='correct', num_iters=opts.num_iters, verbose=opts.verbose, test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write(
            "#weighted normalized unifrac significance test\n")
        of.write(
            "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n")
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    else:
        raise RuntimeError('significance test "%s" not found' %
                           opts.significance_test)
示例#4
0
def main():
    option_parser, opts, args =\
        parse_command_line_parameters(**script_info)

    otu_table_fp = opts.input_path
    otu_table = parse_biom_table(open(otu_table_fp, 'U'))
    sample_ids = otu_table.SampleIds
    otu_ids = otu_table.ObservationIds
    # This is not memory safe: need to be able to load the otu table as ints
    otu_table_array = array(list(otu_table.iterObservationData()), dtype='int')

    if opts.type_of_test == 'all_together':
        type_of_test = TEST_ON_TREE
        header_text = "sample\tp value\tp value (Bonferroni corrected)\n"
    elif opts.type_of_test == 'each_pair':
        type_of_test = TEST_ON_PAIRWISE
        header_text = "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n"
    elif opts.type_of_test == 'each_sample':
        type_of_test = TEST_ON_ENVS
        header_text = "sample\tp value\tp value (Bonferroni corrected)\n"
        if opts.significance_test == 'p-test':
            raise RuntimeError(
                'significance test type "each_sample" not allowed for p-test')
    else:
        raise RuntimeError('significance test type "%s" not found' %
                           opts.type_of_test)

    # note, uses ugly temp file
    if opts.significance_test == 'unweighted_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(sample_ids, otu_ids,
                                               otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_unifrac_permutations_file(tree_in,
                                                envs_in,
                                                weighted=False,
                                                num_iters=opts.num_iters,
                                                verbose=opts.verbose,
                                                test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)

        of = open(opts.output_path, 'w')
        of.write("#unweighted unifrac significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'p-test':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(sample_ids, otu_ids,
                                               otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_p_test_file(tree_in,
                                  envs_in,
                                  num_iters=opts.num_iters,
                                  verbose=opts.verbose,
                                  test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write("#andy martin's p-test significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'weighted_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(sample_ids, otu_ids,
                                               otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_unifrac_permutations_file(tree_in,
                                                envs_in,
                                                weighted=True,
                                                num_iters=opts.num_iters,
                                                verbose=opts.verbose,
                                                test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write("#weighted unifrac significance test\n")
        of.write(header_text)
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    elif opts.significance_test == 'weighted_normalized_unifrac':
        tree_in = open(opts.tree_path, 'U')
        output_fp = opts.output_path + '_envs.tmp'

        result = format_unifrac_sample_mapping(sample_ids, otu_ids,
                                               otu_table_array)
        of = open(output_fp, 'w')
        of.write('\n'.join(result))
        of.close()
        envs_in = open(output_fp, 'U')

        result = fast_unifrac_permutations_file(tree_in,
                                                envs_in,
                                                weighted='correct',
                                                num_iters=opts.num_iters,
                                                verbose=opts.verbose,
                                                test_on=type_of_test)
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, 'w')
        of.write("#weighted normalized unifrac significance test\n")
        of.write(
            "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n")
        for line in result:
            of.write('\t'.join(map(str, line)) + '\n')
        of.close()

    else:
        raise RuntimeError('significance test "%s" not found' %
                           opts.significance_test)
示例#5
0
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    otu_table_fp = opts.input_path
    otu_table = parse_biom_table(open(otu_table_fp, "U"))
    sample_ids = otu_table.SampleIds
    otu_ids = otu_table.ObservationIds
    ## This is not memory safe: need to be able to load the otu table as ints
    otu_table_array = array(list(otu_table.iterObservationData()), dtype="int")

    if opts.type_of_test == "all_together":
        type_of_test = TEST_ON_TREE
        header_text = "sample\tp value\tp value (Bonferroni corrected)\n"
    elif opts.type_of_test == "each_pair":
        type_of_test = TEST_ON_PAIRWISE
        header_text = "sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n"
    elif opts.type_of_test == "each_sample":
        type_of_test = TEST_ON_ENVS
        header_text = "sample\tp value\tp value (Bonferroni corrected)\n"
        if opts.significance_test == "p-test":
            raise RuntimeError('significance test type "each_sample" not allowed for p-test')
    else:
        raise RuntimeError('significance test type "%s" not found' % opts.type_of_test)

    # note, uses ugly temp file
    if opts.significance_test == "unweighted_unifrac":
        tree_in = open(opts.tree_path, "U")
        output_fp = opts.output_path + "_envs.tmp"

        result = format_unifrac_sample_mapping(sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, "w")
        of.write("\n".join(result))
        of.close()
        envs_in = open(output_fp, "U")

        result = fast_unifrac_permutations_file(
            tree_in, envs_in, weighted=False, num_iters=opts.num_iters, verbose=opts.verbose, test_on=type_of_test
        )
        envs_in.close()
        os.remove(output_fp)

        of = open(opts.output_path, "w")
        of.write("#unweighted unifrac significance test\n")
        of.write(header_text)
        for line in result:
            of.write("\t".join(map(str, line)) + "\n")
        of.close()

    elif opts.significance_test == "p-test":
        tree_in = open(opts.tree_path, "U")
        output_fp = opts.output_path + "_envs.tmp"

        result = format_unifrac_sample_mapping(sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, "w")
        of.write("\n".join(result))
        of.close()
        envs_in = open(output_fp, "U")

        result = fast_p_test_file(
            tree_in, envs_in, num_iters=opts.num_iters, verbose=opts.verbose, test_on=type_of_test
        )
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, "w")
        of.write("#andy martin's p-test significance test\n")
        of.write(header_text)
        for line in result:
            of.write("\t".join(map(str, line)) + "\n")
        of.close()

    elif opts.significance_test == "weighted_unifrac":
        tree_in = open(opts.tree_path, "U")
        output_fp = opts.output_path + "_envs.tmp"

        result = format_unifrac_sample_mapping(sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, "w")
        of.write("\n".join(result))
        of.close()
        envs_in = open(output_fp, "U")

        result = fast_unifrac_permutations_file(
            tree_in, envs_in, weighted=True, num_iters=opts.num_iters, verbose=opts.verbose, test_on=type_of_test
        )
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, "w")
        of.write("#weighted unifrac significance test\n")
        of.write(header_text)
        for line in result:
            of.write("\t".join(map(str, line)) + "\n")
        of.close()

    elif opts.significance_test == "weighted_normalized_unifrac":
        tree_in = open(opts.tree_path, "U")
        output_fp = opts.output_path + "_envs.tmp"

        result = format_unifrac_sample_mapping(sample_ids, otu_ids, otu_table_array)
        of = open(output_fp, "w")
        of.write("\n".join(result))
        of.close()
        envs_in = open(output_fp, "U")

        result = fast_unifrac_permutations_file(
            tree_in, envs_in, weighted="correct", num_iters=opts.num_iters, verbose=opts.verbose, test_on=type_of_test
        )
        envs_in.close()
        os.remove(output_fp)
        of = open(opts.output_path, "w")
        of.write("#weighted normalized unifrac significance test\n")
        of.write("sample 1\tsample 2\tp value\tp value (Bonferroni corrected)\n")
        for line in result:
            of.write("\t".join(map(str, line)) + "\n")
        of.close()

    else:
        raise RuntimeError('significance test "%s" not found' % opts.significance_test)