def main(table_loc, otu_list, collapsed_name, output_file, classic=False): table = load_table(table_loc) f = open(otu_list) otus = f.read().strip().split() otus = set(otus) & set(table.ids(axis="observation")) table1 = table.filter(otus, axis="observation", inplace=False) table2 = table.filter(otus, axis="observation", invert=True, inplace=False) sums1 = table1.sum(axis='sample') sums2 = table2.sum(axis='sample') new_table = Table(numpy.array([sums1,sums2]), [collapsed_name, "not_"+collapsed_name], table.ids(axis="sample"), type="otu baptable") if classic: # print to tab delimited biom table open(output_file, 'w').write(new_table.to_tsv()) else: # print biom table new_table.to_json("predict_reactions.py", open(output_file, 'w'))
# print "Taxonomy: %s" %Taxonomy for taxon in observ_ids: # print taxon # print Taxonomy[taxon] observation_metadata.append(Taxonomy[taxon]) #print "observation metadata:\n%s" %observation_metadata #print len(observation_metadata) table = Table(data, observ_ids, sample_id, observation_metadata, sample_metadata, table_id='Example Table') print table out = open(args.prefix + ".biom", "w") table.to_json('pplacer converted by jplace_to_biom.py v.' + VERSION, direct_io=out) out.close() out = open(args.prefix + ".tsv", "w") out.write(table.to_tsv( header_key='taxonomy', header_value='taxomomy')) #to_json('generaged by test', direct_io=out) out.close() print "\n##### DONE! #####\n"
# print "index: %i" %index ind_taxonomy.append('%s%s' %(syn[levels[index]], taxon[0]['ScientificName'])) # print ind_taxonomy Taxonomy[taxon[0]['ScientificName']]['taxonomy'] = ind_taxonomy # print "Taxonomy: %s" %Taxonomy for taxon in observ_ids: # print taxon # print Taxonomy[taxon] observation_metadata.append(Taxonomy[taxon]) #print "observation metadata:\n%s" %observation_metadata #print len(observation_metadata) table = Table(data, observ_ids, sample_id, observation_metadata, sample_metadata, table_id='Example Table') print table out=open(args.prefix+".biom","w") table.to_json('pplacer converted by jplace_to_biom.py v.'+VERSION, direct_io=out) out.close() out=open(args.prefix+".tsv","w") out.write(table.to_tsv(header_key='taxonomy', header_value='taxomomy')) #to_json('generaged by test', direct_io=out) out.close() print "\n##### DONE! #####\n"
def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) lower_percentage = opts.lower_percentage upper_percentage = opts.upper_percentage otu_table_fp = opts.otu_table_fp otu_table = load_table(otu_table_fp) delimiter = opts.delimiter mapping_fp = opts.mapping md_as_string = opts.md_as_string md_identifier = opts.md_identifier levels = opts.level.split(',') suppress_classic_table_output = opts.suppress_classic_table_output suppress_biom_table_output = opts.suppress_biom_table_output if upper_percentage is not None and lower_percentage is not None: raise ValueError( "upper_percentage and lower_percentage are mutually exclusive") if upper_percentage is not None and lower_percentage is not None and \ mapping: raise ValueError("upper_percentage and lower_percentage can not be " "using with mapping file") if upper_percentage is not None and \ (upper_percentage < 0 or upper_percentage > 1.0): raise ValueError('max_otu_percentage should be between 0.0 and 1.0') if lower_percentage is not None and \ (lower_percentage < 0 or lower_percentage > 1.0): raise ValueError('lower_percentage should be between 0.0 and 1.0') if mapping_fp: mapping_file = open(mapping_fp, 'U') mapping, header, comments = parse_mapping_file(mapping_file) # use the input Mapping file for producing the output filenames map_dir_path, map_fname = split(mapping_fp) map_basename, map_fname_ext = splitext(map_fname) else: if suppress_classic_table_output and suppress_biom_table_output: option_parser.error("Both classic and BIOM output formats were " "suppressed.") if not opts.absolute_abundance: otu_table = otu_table.norm(axis='sample', inplace=False) # introduced output directory to will allow for multiple outputs if opts.output_dir: create_dir(opts.output_dir, False) output_dir_path = opts.output_dir else: output_dir_path = './' # use the input OTU table to produce the output filenames dir_path, fname = split(otu_table_fp) basename, fname_ext = splitext(fname) # Iterate over the levels and generate a summarized taxonomy for each for level in levels: if mapping_fp: # define output filename output_fname = join(output_dir_path, map_basename + '_L%s.txt' % (level)) summary, tax_order = add_summary_mapping(otu_table, mapping, int(level), md_as_string, md_identifier) write_add_taxa_summary_mapping(summary, tax_order, mapping, header, output_fname, delimiter) else: # define the output filename. The extension will be added to the # end depending on the output format output_fname = join(output_dir_path, basename + '_L%s' % level) summary, header = make_summary(otu_table, int(level), upper_percentage, lower_percentage, md_as_string, md_identifier) sample_ids = header[1:] observation_ids = [] data = [] for row in summary: # Join taxonomic levels to create an observation ID. observation_ids.append(delimiter.join(row[0])) data.append(row[1:]) table = Table(np.asarray(data), observation_ids, sample_ids) if opts.transposed_output: table = table.transpose() if not suppress_classic_table_output: with open(output_fname + '.txt', 'w') as outfile: outfile.write(table.to_tsv()) if not suppress_biom_table_output: write_biom_table(table, output_fname + '.biom')