def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) otu_table_fp = opts.otu_table_fp mapping_fp = opts.mapping_fp mapping_field = opts.mapping_field output_dir = opts.output_dir # column_rename_ids = opts.column_rename_ids # include_repeat_cols = opts.include_repeat_cols create_dir(output_dir) # split mapping file mapping_f = open(mapping_fp, 'U') for fp_str, sub_mapping_s in split_mapping_file_on_field(mapping_f, mapping_field): mapping_output_fp = join(output_dir, 'mapping_%s.txt' % fp_str) open(mapping_output_fp, 'w').write(sub_mapping_s) # split otu table otu_table_base_name = splitext(split(otu_table_fp)[1])[0] mapping_f = open(mapping_fp, 'U') otu_table = load_table(otu_table_fp) try: for fp_str, sub_otu_table_s in split_otu_table_on_sample_metadata( otu_table, mapping_f, mapping_field): otu_table_output_fp = join(output_dir, '%s_%s.biom' % ( otu_table_base_name, fp_str)) write_biom_table(sub_otu_table_s, otu_table_output_fp) except OTUTableSplitError as e: option_parser.error(e)
def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) otu_table_fp = opts.otu_table_fp mapping_fp = opts.mapping_fp mapping_field = opts.mapping_field output_dir = opts.output_dir # column_rename_ids = opts.column_rename_ids # include_repeat_cols = opts.include_repeat_cols create_dir(output_dir) # split mapping file mapping_f = open(mapping_fp, 'U') for fp_str, sub_mapping_s in split_mapping_file_on_field(mapping_f, mapping_field): mapping_output_fp = join(output_dir, 'mapping_%s.txt' % fp_str) open(mapping_output_fp, 'w').write(sub_mapping_s) # split otu table otu_table_base_name = splitext(split(otu_table_fp)[1])[0] mapping_f = open(mapping_fp, 'U') otu_table_f = open(otu_table_fp, 'U') for fp_str, sub_otu_table_s in split_otu_table_on_sample_metadata( otu_table_f, mapping_f, mapping_field): otu_table_output_fp = join( output_dir, '%s_%s.biom' % (otu_table_base_name, fp_str)) open(otu_table_output_fp, 'w').write(sub_otu_table_s)
def test_split_otu_table_on_sample_metadata_exceptions(self): # mapping file 3 has no sample identifiers matching the OTU table with self.assertRaises(OTUTableSplitError): a = list(split_otu_table_on_sample_metadata(self.otu_table_f1, self.mapping_f3, "Treatment"))
def test_split_otu_table_on_sample_metadata_extra_mapping_entries(self): """ split_otu_table_on_sample_metadata functions as expected with extra mapping data """ actual = list(split_otu_table_on_sample_metadata(self.otu_table_f1, self.mapping_f2, "Treatment")) actual = [(id_, e) for id_, e in actual] exp = [(id_, parse_biom_table(e)) for id_, e in otu_table_exp1] actual.sort() exp.sort() for a, e in zip(actual, exp): self.assertTrue(a == e)
def test_split_otu_table_on_sample_metadata_extra_mapping_entries(self): """ split_otu_table_on_sample_metadata functions as expected with extra mapping data """ actual = list(split_otu_table_on_sample_metadata(self.otu_table_f1, self.mapping_f2, "Treatment")) actual = [(id_, parse_biom_table(e)) for id_, e in actual] exp = [(id_, parse_biom_table(e)) for id_, e in otu_table_exp1] actual.sort() exp.sort() for a, e in zip(actual, exp): self.assertEqual(a, e, "OTU tables are not equal:\n%s\n%s" % (format_biom_table(a[1]), format_biom_table(e[1])))
def test_split_otu_table_on_sample_metadata_extra_mapping_entries(self): """ split_otu_table_on_sample_metadata functions as expected with extra mapping data """ actual = list(split_otu_table_on_sample_metadata(self.otu_table_f1, self.mapping_f2, "Treatment")) actual = [(id_,parse_biom_table(e)) for id_, e in actual] exp = [(id_,parse_biom_table(e)) for id_, e in otu_table_exp1] actual.sort() exp.sort() for a,e in zip(actual,exp): self.assertEqual(a,e,"OTU tables are not equal:\n%s\n%s" % \ (format_biom_table(a[1]),format_biom_table(e[1])))
def test_split_otu_table_on_sample_metadata(self): """ split_otu_table_on_sample_metadata functions as expected with valid input """ actual = list(split_otu_table_on_sample_metadata(self.otu_table_f1, self.mapping_f1, "Treatment")) for id_, e in actual: try: parse_biom_table(e) except: print e actual = [(id_,parse_biom_table(e)) for id_, e in actual] exp = [(id_,parse_biom_table(e)) for id_, e in otu_table_exp1] actual.sort() exp.sort() for a,e in zip(actual,exp): self.assertEqual(a,e,"OTU tables are not equal:\n%s\n%s" % \ (format_biom_table(a[1]),format_biom_table(e[1])))
def test_split_otu_table_on_sample_metadata(self): """ split_otu_table_on_sample_metadata functions as expected with valid input """ actual = list(split_otu_table_on_sample_metadata(self.otu_table_f1, self.mapping_f1, "Treatment")) for id_, e in actual: try: parse_biom_table(e) except: print e actual = [(id_, parse_biom_table(e)) for id_, e in actual] exp = [(id_, parse_biom_table(e)) for id_, e in otu_table_exp1] actual.sort() exp.sort() for a, e in zip(actual, exp): self.assertEqual(a, e, "OTU tables are not equal:\n%s\n%s" % (format_biom_table(a[1]), format_biom_table(e[1])))