Exemple #1
0
def parse_table_to_biom(table_lines, table_format="tab-delimited",\
    biom_format = 'otu table'):
    """Read the lines of an open trait table file, and output a .biom table object 
     
    The trait table must be either a biom file, or a picrust tab-delimited file 
    table_format -- must be either 'tab-delimited' or 'biom' 
     
    """
    if table_format == "biom":
        return parse_biom_table(table_lines)
    elif table_format == "tab-delimited":

        idx = 0  # a sparse BIOM table
        BIOM_TYPES = {
            'otu table': [SparseOTUTable, DenseOTUTable],
            'pathway table': [SparsePathwayTable, DensePathwayTable],
            'function table': [SparseFunctionTable, DenseFunctionTable],
            'ortholog table': [SparseOrthologTable, DenseOrthologTable],
            'gene table': [SparseGeneTable, DenseGeneTable],
            'metabolite table': [SparseMetaboliteTable, DenseMetaboliteTable],
            'taxon table': [SparseTaxonTable, DenseTaxonTable]
        }

        constructor = BIOM_TYPES[biom_format][idx]
        sample_mapping = None
        obs_mapping = None
        process_func = None
        try:
            converted_table = (convert_table_to_biom(table_lines,\
              sample_mapping,obs_mapping,process_func, constructor))
            biom_table = parse_biom_table(converted_table)
            #print biom_table
        except ValueError:
            raise ValueError("Input does not look like a classic table.")

        #headers, fields = parse_trait_table(table_lines)
        #now convert to biom
        return biom_table
Exemple #2
0
def parse_table_to_biom(table_lines, table_format="tab-delimited",\
    biom_format = 'otu table'): 
    """Read the lines of an open trait table file, and output a .biom table object 
     
    The trait table must be either a biom file, or a picrust tab-delimited file 
    table_format -- must be either 'tab-delimited' or 'biom' 
     
    """ 
    if table_format == "biom": 
        return parse_biom_table(table_lines) 
    elif table_format == "tab-delimited": 
         
        idx = 0 # a sparse BIOM table 
        BIOM_TYPES = {'otu table':[SparseOTUTable, DenseOTUTable], 
              'pathway table':[SparsePathwayTable, DensePathwayTable], 
              'function table':[SparseFunctionTable, DenseFunctionTable], 
              'ortholog table':[SparseOrthologTable, DenseOrthologTable], 
              'gene table':[SparseGeneTable, DenseGeneTable], 
              'metabolite table':[SparseMetaboliteTable, DenseMetaboliteTable], 
              'taxon table':[SparseTaxonTable, DenseTaxonTable]} 
 
 
        constructor = BIOM_TYPES[biom_format][idx] 
        sample_mapping = None 
        obs_mapping = None 
        process_func = None
        try: 
            converted_table = (convert_table_to_biom(table_lines,\
              sample_mapping,obs_mapping,process_func, constructor)) 
            biom_table = parse_biom_table(converted_table) 
            #print biom_table 
        except ValueError: 
            raise ValueError("Input does not look like a classic table.") 
     
        #headers, fields = parse_trait_table(table_lines) 
        #now convert to biom 
        return biom_table  
Exemple #3
0
    def run(self, **kwargs):
        table_file = kwargs['table_file']
        matrix_type = kwargs['matrix_type']
        biom_to_classic_table = kwargs['biom_to_classic_table']
        sparse_biom_to_dense_biom = kwargs['sparse_biom_to_dense_biom']
        dense_biom_to_sparse_biom = kwargs['dense_biom_to_sparse_biom']
        sample_metadata = kwargs['sample_metadata']
        observation_metadata = kwargs['observation_metadata']
        header_key = kwargs['header_key']
        output_metadata_id = kwargs['output_metadata_id']
        process_obs_metadata = kwargs['process_obs_metadata']
        table_type = kwargs['table_type']

        if sum([biom_to_classic_table, sparse_biom_to_dense_biom,
                dense_biom_to_sparse_biom]) > 1:
            raise CommandError("Converting between classic/BIOM formats and "
                               "sparse/dense representations are mutually "
                               "exclusive. You may only specify a single "
                               "operation at a time.")

        # if the user does not specify a name for the output metadata column,
        # set it to the same as the header key
        output_metadata_id = output_metadata_id or header_key

        convert_error_msg = ("Input does not look like a BIOM-formatted file. "
                             "Did you accidentally specify that a classic "
                             "table file should be created from a BIOM table "
                             "file?")
        if biom_to_classic_table:
            try:
                result = convert_biom_to_table(table_file, header_key,
                                               output_metadata_id)
            except ValueError:
                raise CommandError(convert_error_msg)
        elif sparse_biom_to_dense_biom:
            try:
                table = parse_biom_table(table_file)
            except ValueError:
                raise CommandError(convert_error_msg)

            conv_constructor = self.TableTypes[table._biom_type.lower()][1]
            conv_table = table_factory(table._data, table.SampleIds,
                            table.ObservationIds, table.SampleMetadata,
                            table.ObservationMetadata, table.TableId,
                            constructor=conv_constructor)
            result = conv_table.getBiomFormatJsonString(generatedby())
        elif dense_biom_to_sparse_biom:
            try:
                table = parse_biom_table(table_file)
            except ValueError:
                raise CommandError(convert_error_msg)

            conv_constructor = self.TableTypes[table._biom_type.lower()][0]
            conv_table = table_factory(table._data, table.SampleIds, 
                            table.ObservationIds, table.SampleMetadata, 
                            table.ObservationMetadata, table.TableId, 
                            constructor=conv_constructor)
            result = conv_table.getBiomFormatJsonString(generatedby())
        else:
            if table_type is None:
                raise CommandError("Must specify the BIOM table type: %s" %
                                   ', '.join(self.TableTypes.keys()))
            else:
                table_type = table_type.lower()

            if table_type not in self.TableTypes:
                raise CommandError("Unknown BIOM table type, must be one of: "
                                   "%s" % ', '.join(self.TableTypes.keys()))

            if matrix_type not in self.MatrixTypes:
                raise CommandError("Unknown BIOM matrix type, must be one of: "
                                   "%s" % ', '.join(self.MatrixTypes))

            if process_obs_metadata not in \
                    self.ObservationMetadataTypes.keys():
                raise CommandError("Unknown observation metadata processing "
                        "method, must be one of: %s" %
                        ', '.join(self.ObservationMetadataTypes.keys()))

            idx = 0 if matrix_type == 'sparse' else 1
            constructor = self.TableTypes[table_type][idx]


            convert_error_msg = ("Input does not look like a classic table. "
                                 "Did you forget to specify that a classic "
                                 "table file should be created from a BIOM "
                                 "table file?")
            try:
                result = convert_table_to_biom(table_file, sample_metadata,
                        observation_metadata,
                        self.ObservationMetadataTypes[process_obs_metadata],
                        constructor)
            except ValueError:
                raise CommandError(convert_error_msg)
            except IndexError:
                raise CommandError(convert_error_msg)

        return {'table_str': result}
    def run(self, **kwargs):
        table_file = kwargs["table_file"]
        biom_to_classic_table = kwargs["biom_to_classic_table"]
        sparse_biom_to_dense_biom = kwargs["sparse_biom_to_dense_biom"]
        dense_biom_to_sparse_biom = kwargs["dense_biom_to_sparse_biom"]
        sample_metadata = kwargs["sample_metadata"]
        observation_metadata = kwargs["observation_metadata"]
        header_key = kwargs["header_key"]
        output_metadata_id = kwargs["output_metadata_id"]
        process_obs_metadata = kwargs["process_obs_metadata"]

        if sum([biom_to_classic_table, sparse_biom_to_dense_biom, dense_biom_to_sparse_biom]) > 1:
            raise CommandError(
                "Converting between classic/BIOM formats and "
                "sparse/dense representations are mutually "
                "exclusive. You may only specify a single "
                "operation at a time."
            )

        # if the user does not specify a name for the output metadata column,
        # set it to the same as the header key
        output_metadata_id = output_metadata_id or header_key

        convert_error_msg = (
            "Input does not look like a BIOM-formatted file. "
            "Did you accidentally specify that a classic "
            "table file should be created from a BIOM table "
            "file?"
        )
        if biom_to_classic_table:
            try:
                result = convert_biom_to_table(table_file, header_key, output_metadata_id)
            except (ValueError, TypeError):
                raise CommandError(convert_error_msg)
        elif sparse_biom_to_dense_biom:
            try:
                table = parse_biom_table(table_file)
            except (ValueError, TypeError):
                raise CommandError(convert_error_msg)

            conv_table = table_factory(
                table._data,
                table.sample_ids,
                table.observation_ids,
                table.sample_metadata,
                table.observation_metadata,
                table.TableId,
            )
            result = conv_table.get_biom_format_json_string(generatedby())
        elif dense_biom_to_sparse_biom:
            try:
                table = parse_biom_table(table_file)
            except (ValueError, TypeError):
                raise CommandError(convert_error_msg)

            conv_table = table_factory(
                table._data,
                table.sample_ids,
                table.observation_ids,
                table.sample_metadata,
                table.observation_metadata,
                table.table_id,
            )
            result = conv_table.get_biom_format_json_string(generatedby())
        else:
            if process_obs_metadata not in self.ObservationMetadataTypes.keys():
                raise CommandError(
                    "Unknown observation metadata processing method, must be "
                    "one of: %s" % ", ".join(self.ObservationMetadataTypes.keys())
                )

            convert_error_msg = (
                "Input does not look like a classic table. "
                "Did you forget to specify that a classic "
                "table file should be created from a BIOM "
                "table file?"
            )
            try:
                result = convert_table_to_biom(
                    table_file,
                    sample_metadata,
                    observation_metadata,
                    self.ObservationMetadataTypes[process_obs_metadata],
                )
            except (ValueError, TypeError, IndexError):
                raise CommandError(convert_error_msg)

        return {"table_str": result}
def main():
    opts,args = parser.parse_args()

    if opts.input_fp is None:
        parser.print_help()
        parser.error('Must specify an input file!')
    if opts.output_fp is None:
        parser.print_help()
        parser.error('Must specify an output file!')

    biom_to_classic_table = opts.biom_to_classic_table
    sparse_biom_to_dense_biom = opts.sparse_biom_to_dense_biom
    dense_biom_to_sparse_biom = opts.dense_biom_to_sparse_biom
    process_obs_metadata = opts.process_obs_metadata
    
    if sum([biom_to_classic_table,
            sparse_biom_to_dense_biom,
            dense_biom_to_sparse_biom]) > 1:
        parser.print_help()
        option_parser.error("The --biom_to_classic_table, --sparse_biom_to_dense_biom, "
         "and --dense_biom_to_sparse_biom options are mutually exclusive. Pass only one at a time.")
    
    input_f = open(opts.input_fp,'U')
    output_f = open(opts.output_fp,'w')
    
    #dense = opts.biom_type == 'dense'
    count_map_f = int
    sample_mapping_fp = opts.sample_mapping_fp
    obs_mapping_fp = opts.observation_mapping_fp
    
    if sample_mapping_fp != None:
        sample_mapping = parse_mapping(open(sample_mapping_fp,'U'))
    else:
        sample_mapping = None
    
    if obs_mapping_fp != None:
        obs_mapping = parse_mapping(open(obs_mapping_fp, 'U'))
    else:
        obs_mapping = None
    
    # if the user does not specify a name for the output metadata column, set it to the
    # same as the header key
    header_key = opts.header_key
    output_metadata_id = opts.output_metadata_id or header_key
    if biom_to_classic_table:
        try:
            output_f.write(convert_biom_to_table(\
                           input_f, header_key, output_metadata_id))
        except ValueError:
            raise ValueError, "Input does not look like a .biom file. Did you accidentally specify -b?"
    elif sparse_biom_to_dense_biom:
        try:
            table = parse_biom_table(input_f)
        except ValueError:
            raise ValueError, "Input does not look like a .biom file. Did you accidentally specify -b?"        

        conv_constructor = BIOM_TYPES[table._biom_type.lower()][1]
        conv_table = table_factory(table._data, table.SampleIds, 
                        table.ObservationIds, table.SampleMetadata, 
                        table.ObservationMetadata, table.TableId, 
                        constructor=conv_constructor)
        output_f.write(conv_table.getBiomFormatJsonString(generatedby()))
    elif dense_biom_to_sparse_biom:
        try:
            table = parse_biom_table(input_f)
        except ValueError:
            raise ValueError, "Input does not look like a .biom file. Did you accidentally specify -b?"

        conv_constructor = BIOM_TYPES[table._biom_type.lower()][0]
        conv_table = table_factory(table._data, table.SampleIds, 
                        table.ObservationIds, table.SampleMetadata, 
                        table.ObservationMetadata, table.TableId, 
                        constructor=conv_constructor)

        output_f.write(conv_table.getBiomFormatJsonString(generatedby()))
    else:
        if opts.biom_table_type is None:
            parser.error('Must specify the BIOM table type: %s' % \
                    ', '.join(BIOM_TYPES.keys()))
        else:
            biom_table_type = opts.biom_table_type.lower()
        if biom_table_type not in BIOM_TYPES:
            parser.error('Unknown BIOM table type, must be one of: %s' % \
                    ', '.join(BIOM_TYPES.keys()))
        if opts.biom_type is None or opts.biom_type not in ['dense', 'sparse']:
            parser.error('Must specify the BIOM matrix type, ' + \
                    'either "dense" or "sparse"')

        idx = 0 if opts.biom_type == 'sparse' else 1
        constructor = BIOM_TYPES[biom_table_type][idx]
        
        try:
            output_f.write(convert_table_to_biom(input_f,sample_mapping, 
                                obs_mapping, OBS_META_TYPES[process_obs_metadata], constructor))
        except ValueError:
            raise ValueError, "Input does not look like a classic table. Do you need to pass -b?"
    input_f.close()
    output_f.close()