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()
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!') ## process header information, if provided observation_header = opts.observation_header sample_header = opts.sample_header if opts.observation_header != None: observation_header = observation_header.split(',') if opts.sample_header != None: sample_header = sample_header.split(',') ## define metadata processing functions, if any process_fns = {} sc_separated = opts.sc_separated if sc_separated != None: process_fns.update({}.fromkeys(sc_separated.split(','), split_on_semicolons)) int_fields = opts.int_fields if int_fields != None: process_fns.update({}.fromkeys(int_fields.split(','), int_)) float_fields = opts.float_fields if float_fields != None: process_fns.update({}.fromkeys(float_fields.split(','), float_)) ## parse mapping files 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'), process_fns=process_fns, header=sample_header) else: sample_mapping = None if obs_mapping_fp != None: obs_mapping = parse_mapping(open(obs_mapping_fp, 'U'), process_fns=process_fns, header=observation_header) else: obs_mapping = None if sample_mapping == None and obs_mapping == None: parser.print_help() parser.error('Must specify sample_mapping and/or obs_mapping.') ## parse the table and open the output file for writing output_f = open(opts.output_fp,'w') table = parse_biom_table(open(opts.input_fp,'U')) ## add metadata as necessary if sample_mapping: table.addSampleMetadata(sample_mapping) if obs_mapping: table.addObservationMetadata(obs_mapping) ## write the output file and close it output_f.write(table.getBiomFormatJsonString(generatedby())) output_f.close()