Esempio n. 1
0
    def test_make_edge_table(self):
        '''Test that edge table is created properly.'''
        bt = parse_biom_table(BIOM_STRING_3)
        obs_out = make_edge_table(bt)
        exp_out = [
            '#Sample\tOTU\tAbundance', 's3\to1\t3.0', 's3\to2\t8.0',
            's3\to3\t13.0', 's3\to4\t18.0', 's3\to5\t23.0', 's3\to6\t28.0',
            's3\to7\t33.0', 's3\to8\t38.0', 's4\to1\t4.0', 's4\to2\t9.0',
            's4\to3\t14.0', 's4\to4\t19.0', 's4\to5\t24.0', 's4\to6\t29.0',
            's4\to7\t34.0', 's4\to8\t39.0', 's5\to1\t5.0', 's5\to2\t10.0',
            's5\to3\t15.0', 's5\to4\t20.0', 's5\to5\t25.0', 's5\to6\t30.0',
            's5\to7\t35.0', 's5\to8\t40.0'
        ]
        self.assertEqual(set(obs_out), set(exp_out))

        # test with a row and a col that are all zero
        bt = parse_biom_table(BIOM_STRING_6)
        obs_out = make_edge_table(bt)
        exp_out = [
            '#Sample\tOTU\tAbundance', 's2\to1\t2.0', 's2\to2\t7.0',
            's2\to3\t12.0', 's2\to4\t17.0', 's2\to6\t27.0', 's2\to7\t32.0',
            's2\to8\t37.0', 's3\to1\t3.0', 's3\to2\t8.0', 's3\to3\t13.0',
            's3\to4\t18.0', 's3\to6\t28.0', 's3\to7\t33.0', 's3\to8\t38.0',
            's4\to1\t4.0', 's4\to2\t9.0', 's4\to3\t14.0', 's4\to4\t19.0',
            's4\to6\t29.0', 's4\to7\t34.0', 's4\to8\t39.0', 's5\to1\t5.0',
            's5\to2\t10.0', 's5\to3\t15.0', 's5\to4\t20.0', 's5\to6\t30.0',
            's5\to7\t35.0', 's5\to8\t40.0'
        ]
        self.assertEqual(set(obs_out), set(exp_out))
def main():
    option_parser, opts, args =\
        parse_command_line_parameters(**script_info)

    otu_table_fp = opts.biom_fp
    map_fp = opts.map_fp
    output_dir = opts.output_dir
    scolors = opts.scolors.split(',')
    ocolors = opts.ocolors.split(',')
    sshapes = opts.sshapes.split(',')
    oshapes = opts.oshapes.split(',')
    ssizes = opts.ssizes.split(',')
    osizes = opts.osizes.split(',')
    md_fields = opts.md_fields.split(',')

    # check that the otu fields asked for are available
    shared_options = ['NodeType', 'Abundance']
    if not all(
        [i in md_fields + shared_options for i in ocolors + oshapes + osizes]):
        option_parser.error('The fields specified for observation colors, '
                            'sizes, or shapes are not in either the shared '
                            'options (NodeType,Abundance) or the supplied '
                            'md_fields. These fields must be a subset of the '
                            'union of these sets. Have you passed ocolors, '
                            'osizes or oshapes that are not in the md_fields?')
    # check that the sample fields asked for are available. mapping file
    # elements should all have same metadata keys
    sopts = parse_mapping_file_to_dict(map_fp)[0].items()[0][1].keys()
    if not all(
        [i in sopts + shared_options for i in scolors + sshapes + ssizes]):
        option_parser.error('The fields specified for sample colors, sizes, '
                            'or shapes are not in either the shared options '
                            '(NodeType,Abundance) or the supplied mapping '
                            'file. These fields must be a subset of the union '
                            'of these sets. Have you passed scolors, ssizes '
                            'or sshapes that are not in the mapping file '
                            'headers?')

    # actual compuation begins
    try:
        create_dir(output_dir, fail_on_exist=True)
    except OSError:
        option_parser.error('Directory already exists. Will not overwrite.')

    bt = load_table(otu_table_fp)
    pmf = parse_mapping_file_to_dict(map_fp)[0]  # [1] is comments, don't need
    sample_node_table = make_sample_node_table(bt, pmf)
    otu_node_table = make_otu_node_table(bt, opts.observation_md_header_key,
                                         md_fields)
    node_attr_table = make_node_attr_table(otu_node_table, sample_node_table,
                                           scolors, ocolors, ssizes, osizes,
                                           sshapes, oshapes)
    edge_table = make_edge_table(bt)

    _write_table(sample_node_table,
                 os.path.join(output_dir, 'SampleNodeTable.txt'))
    _write_table(otu_node_table, os.path.join(output_dir, 'OTUNodeTable.txt'))
    _write_table(node_attr_table, os.path.join(output_dir,
                                               'NodeAttrTable.txt'))
    _write_table(edge_table, os.path.join(output_dir, 'EdgeTable.txt'))
Esempio n. 3
0
def main():
    option_parser, opts, args =\
       parse_command_line_parameters(**script_info)

    otu_table_fp = opts.biom_fp
    map_fp = opts.map_fp
    output_dir = opts.output_dir
    taxonomy_key = opts.observation_md_header_key
    scolors = opts.scolors.split(',')
    ocolors = opts.ocolors.split(',')
    sshapes = opts.sshapes.split(',')
    oshapes = opts.oshapes.split(',')
    ssizes = opts.ssizes.split(',')
    osizes = opts.osizes.split(',')
    md_fields = opts.md_fields.split(',')

    # check that the otu fields asked for are available
    shared_options = ['NodeType','Abundance']
    if not all([i in md_fields+shared_options for i in ocolors+oshapes+osizes]):
        option_parser.error('The fields specified for observation colors, sizes, or '+\
            'shapes are not in either the shared options (NodeType,Abundance)'+\
            ' or the supplied md_fields. These fields must be a subset of '+\
            'the union of these sets. Have you passed ocolors, osizes or '+\
            'oshapes that are not in the md_fields?')
    # check that the sample fields asked for are available. mapping file 
    # elements should all have same metadata keys
    sopts = parse_mapping_file_to_dict(map_fp)[0].items()[0][1].keys()
    if not all([i in sopts+shared_options for i in scolors+sshapes+ssizes]):
        option_parser.error('The fields specified for sample colors, sizes, or '+\
            'shapes are not in either the shared options (NodeType,Abundance)'+\
            ' or the supplied mapping file. These fields must be a subset of '+\
            'the union of these sets. Have you passed scolors, ssizes or '+\
            'sshapes that are not in the mapping file headers?')

    # actual compuation begins
    try:
        create_dir(output_dir, fail_on_exist=True)
    except OSError:
        option_parser.error('Directory already exists. Will not overwrite.')

    bt = parse_biom_table(open(otu_table_fp))
    pmf = parse_mapping_file_to_dict(map_fp)[0] # [1] is comments, don't need
    sample_node_table = make_sample_node_table(bt, pmf)
    otu_node_table = make_otu_node_table(bt, opts.observation_md_header_key, 
        md_fields)
    node_attr_table = make_node_attr_table(otu_node_table, sample_node_table,
        scolors, ocolors, ssizes, osizes, sshapes, oshapes)
    edge_table = make_edge_table(bt)

    _write_table(sample_node_table, os.path.join(output_dir,'SampleNodeTable.txt'))
    _write_table(otu_node_table, os.path.join(output_dir,'OTUNodeTable.txt'))
    _write_table(node_attr_table, os.path.join(output_dir,'NodeAttrTable.txt'))
    _write_table(edge_table, os.path.join(output_dir,'EdgeTable.txt'))
    def test_make_edge_table(self):
        '''Test that edge table is created properly.'''
        bt = parse_biom_table(BIOM_STRING_3)
        obs_out = make_edge_table(bt)
        exp_out = [
            '#Sample\tOTU\tAbundance',
            's3\to1\t3.0',
            's3\to2\t8.0',
            's3\to3\t13.0',
            's3\to4\t18.0',
            's3\to5\t23.0',
            's3\to6\t28.0',
            's3\to7\t33.0',
            's3\to8\t38.0',
            's4\to1\t4.0',
            's4\to2\t9.0',
            's4\to3\t14.0',
            's4\to4\t19.0',
            's4\to5\t24.0',
            's4\to6\t29.0',
            's4\to7\t34.0',
            's4\to8\t39.0',
            's5\to1\t5.0',
            's5\to2\t10.0',
            's5\to3\t15.0',
            's5\to4\t20.0',
            's5\to5\t25.0',
            's5\to6\t30.0',
            's5\to7\t35.0',
            's5\to8\t40.0']
        self.assertEqual(set(obs_out), set(exp_out))

        # test with a row and a col that are all zero
        bt = parse_biom_table(BIOM_STRING_6)
        obs_out = make_edge_table(bt)
        exp_out = [
            '#Sample\tOTU\tAbundance',
            's2\to1\t2.0',
            's2\to2\t7.0',
            's2\to3\t12.0',
            's2\to4\t17.0',
            's2\to6\t27.0',
            's2\to7\t32.0',
            's2\to8\t37.0',
            's3\to1\t3.0',
            's3\to2\t8.0',
            's3\to3\t13.0',
            's3\to4\t18.0',
            's3\to6\t28.0',
            's3\to7\t33.0',
            's3\to8\t38.0',
            's4\to1\t4.0',
            's4\to2\t9.0',
            's4\to3\t14.0',
            's4\to4\t19.0',
            's4\to6\t29.0',
            's4\to7\t34.0',
            's4\to8\t39.0',
            's5\to1\t5.0',
            's5\to2\t10.0',
            's5\to3\t15.0',
            's5\to4\t20.0',
            's5\to6\t30.0',
            's5\to7\t35.0',
            's5\to8\t40.0']
        self.assertEqual(set(obs_out), set(exp_out))