def test_compute_index(self):
        increased = set(['bar'])
        decreased = set(['not bar'])

        exp = [('S1', log(1 / 3)), ('S2', nan), ('S3', log(2 / 2))]
        obs = sorted(compute_index(self.t1, increased, decreased, 'taxonomy'))
        self.assertEqual(obs, exp)

        decreased = set(['foo'])
        increased = set(['bar'])
        exp = [('S1', -inf), ('S2', log(2.0 / 2.0)), ('S3', log(2.0 / 4.0))]
        obs = sorted(compute_index(self.t2, increased, decreased, 'taxonomy'))
        self.assertEqual(obs, exp)
    def test_compute_index(self):
        increased = set(['bar'])
        decreased = set(['not bar'])

        exp = [('S1', log(1 / 3)), ('S2', nan), ('S3', log(2 / 2))]
        obs = sorted(compute_index(self.t1, increased, decreased, 'taxonomy'))
        self.assertEqual(obs, exp)

        decreased = set(['foo'])
        increased = set(['bar'])
        exp = [('S1', -inf), ('S2', log(2.0 / 2.0)), ('S3', log(2.0 / 4.0))]
        obs = sorted(compute_index(self.t2, increased, decreased, 'taxonomy'))
        self.assertEqual(obs, exp)
Ejemplo n.º 3
0
def main():
    option_parser, opts, args =\
        parse_command_line_parameters(**script_info)

    if opts.show_indices:
        for idx_key in sorted(known_indices):
            idx = known_indices[idx_key]
            print "%s: %s, %s" % (idx_key, idx['name'], idx['source'])
            print '\t', 'increased:'
            print '\n'.join(['\t\t%s' % t for t in idx['increased']])
            print '\t', 'decreased:'
            print '\n'.join(['\t\t%s' % t for t in idx['decreased']])
        exit(0)

    if opts.index is not None and known_indices.get(opts.index) is None:
        option_parser.error("%s is not a known index. Known indices are: %s" %
                            (opts.index, ','.join(known_indices.keys())))

    if opts.index is not None and (opts.increased or opts.decreased):
        option_parser.error("Cannot specify both an existing and custom index")

    if opts.index is None and opts.increased is None and \
            opts.decreased is None:
        option_parser.error("Must specify an existing or custom index")

    if opts.increased and opts.decreased is None:
        option_parser.error("Must specify decreased taxa")

    if opts.decreased and opts.increased is None:
        option_parser.error("Must specify increased taxa")

    if opts.index is not None:
        name = opts.name if opts.name is not None else opts.index
        increased = known_indices[opts.index]['increased']
        decreased = known_indices[opts.index]['decreased']
    else:
        name = opts.name if opts.name is not None else 'index'
        increased = set(opts.increased.split(','))
        decreased = set(opts.decreased.split(','))

    if opts.input is None:
        option_parser.error("Input not specified")

    if opts.output is None:
        option_parser.error("Output not specified")

    table = load_table(opts.input)

    if opts.mapping_file:
        mapping_file = open(opts.mapping_file, 'U')
        output_file = TemporaryFile()
    else:
        mapping_file = None
        output_file = open(opts.output, 'w')

    output_file.write("#SampleID\t%s\n" % name)
    for id_, value in compute_index(table, increased, decreased, opts.key):
        output_file.write("%s\t%f\n" % (id_, value))

    if opts.mapping_file:
        output_file.seek(0)
        mapping_data = MetadataMap.mergeMappingFiles(
            [output_file, mapping_file], no_data_value=nan)
        with open(opts.output, 'w') as f:
            f.write(str(mapping_data))

    output_file.close()
    def test_compute_index_raises(self):
        with self.assertRaises(KeyError):
            next(compute_index(self.t1, set(['foo']), set(['bar']), 'missing'))

        with self.assertRaises(ValueError):
            next(compute_index(self.t1, set(['foo']), set(['x']), 'taxonomy'))
    def test_compute_index_raises(self):
        with self.assertRaises(KeyError):
            next(compute_index(self.t1, set(['foo']), set(['bar']), 'missing'))

        with self.assertRaises(ValueError):
            next(compute_index(self.t1, set(['foo']), set(['x']), 'taxonomy'))
Ejemplo n.º 6
0
def main():
    option_parser, opts, args =\
        parse_command_line_parameters(**script_info)

    if opts.show_indices:
        for idx_key in sorted(known_indices):
            idx = known_indices[idx_key]
            print "%s: %s, %s" % (idx_key, idx['name'], idx['source'])
            print '\t', 'increased:'
            print '\n'.join(['\t\t%s' % t for t in idx['increased']])
            print '\t', 'decreased:'
            print '\n'.join(['\t\t%s' % t for t in idx['decreased']])
        exit(0)

    if opts.index is not None and known_indices.get(opts.index) is None:
        option_parser.error("%s is not a known index. Known indices are: %s"
                            % (opts.index, ','.join(known_indices.keys())))

    if opts.index is not None and (opts.increased or opts.decreased):
        option_parser.error("Cannot specify both an existing and custom index")

    if opts.index is None and opts.increased is None and \
            opts.decreased is None:
        option_parser.error("Must specify an existing or custom index")

    if opts.increased and opts.decreased is None:
        option_parser.error("Must specify decreased taxa")

    if opts.decreased and opts.increased is None:
        option_parser.error("Must specify increased taxa")

    if opts.index is not None:
        name = opts.name if opts.name is not None else opts.index
        increased = known_indices[opts.index]['increased']
        decreased = known_indices[opts.index]['decreased']
    else:
        name = opts.name if opts.name is not None else 'index'
        increased = set(opts.increased.split(','))
        decreased = set(opts.decreased.split(','))

    if opts.input is None:
        option_parser.error("Input not specified")

    if opts.output is None:
        option_parser.error("Output not specified")

    table = load_table(opts.input)

    if opts.mapping_file:
        mapping_file = open(opts.mapping_file, 'U')
        output_file = TemporaryFile()
    else:
        mapping_file = None
        output_file = open(opts.output, 'w')

    output_file.write("#SampleID\t%s\n" % name)
    for id_, value in compute_index(table, increased, decreased, opts.key):
        output_file.write("%s\t%f\n" % (id_, value))

    if opts.mapping_file:
        output_file.seek(0)
        mapping_data = MetadataMap.mergeMappingFiles([output_file, mapping_file],
                                                     no_data_value=nan)
        with open(opts.output, 'w') as f:
            f.write(str(mapping_data))

    output_file.close()