def test_natsort_case_insensitive(self): """natsort should perform numeric comparisons on strings and is _not_ case-sensitive""" # string with alpha and numerics sort correctly s = [ 'sample1', 'sample2', 'sample11', 'sample12', 'SAmple1', 'Sample2'] # expected values exp_natsort = ['SAmple1', 'Sample2', 'sample1', 'sample2', 'sample11', 'sample12'] exp_natsort_case_insensitive = ['sample1', 'SAmple1', 'sample2', 'Sample2', 'sample11', 'sample12'] # test natsort self.assertEqual(natsort(s), exp_natsort) # test natsort_case_insensitive self.assertEqual(natsort_case_insensitive(s), exp_natsort_case_insensitive) s.reverse() # test natsort self.assertEqual(natsort(s), exp_natsort) # test natsort_case_insensitive self.assertEqual(natsort(list('cbaA321')), list('123Aabc')) # strings with alpha only sort correctly self.assertEqual(natsort_case_insensitive(list('cdBa')), list('aBcd')) # string of ints sort correctly self.assertEqual(natsort_case_insensitive(['11', '2', '1', '0']), ['0', '1', '2', '11']) # strings of floats sort correctly self.assertEqual(natsort_case_insensitive(['1.11', '1.12', '1.00', '0.009']), ['0.009', '1.00', '1.11', '1.12']) # string of ints sort correctly self.assertEqual(natsort_case_insensitive([('11', 'A'), ('2', 'B'), ('1', 'C'), ('0', 'D')]), [('0', 'D'), ('1', 'C'), ('2', 'B'), ('11', 'A')])
def main(): option_parser, opts, args =\ parse_command_line_parameters(**script_info) otu_table_data = parse_biom_table(open(opts.input_otu_table, 'U')) sort_field = opts.sort_field mapping_fp = opts.mapping_fp sorted_sample_ids_fp = opts.sorted_sample_ids_fp if sort_field and mapping_fp: mapping_data = parse_mapping_file(open(mapping_fp, 'U')) result = sort_otu_table_by_mapping_field(otu_table_data, mapping_data, sort_field) elif sorted_sample_ids_fp: sorted_sample_ids = sample_ids_from_f(open(sorted_sample_ids_fp, 'U')) result = sort_otu_table(otu_table_data, sorted_sample_ids) else: result = sort_otu_table( otu_table_data, natsort_case_insensitive(otu_table_data.SampleIds)) # format and write the otu table result_str = format_biom_table(result) of = open(opts.output_fp, 'w') of.write(result_str) of.close()
def main(): option_parser, opts, args =\ parse_command_line_parameters(**script_info) otu_table_data = parse_biom_table(open(opts.input_otu_table,'U')) sort_field = opts.sort_field mapping_fp = opts.mapping_fp sorted_sample_ids_fp = opts.sorted_sample_ids_fp if sort_field and mapping_fp: mapping_data = parse_mapping_file(open(mapping_fp,'U')) result = sort_otu_table_by_mapping_field(otu_table_data, mapping_data, sort_field) elif sorted_sample_ids_fp: sorted_sample_ids = sample_ids_from_f(open(sorted_sample_ids_fp,'U')) result = sort_otu_table(otu_table_data, sorted_sample_ids) else: result = sort_otu_table(otu_table_data, natsort_case_insensitive(otu_table_data.SampleIds)) # format and write the otu table result_str = format_biom_table(result) of = open(opts.output_fp,'w') of.write(result_str) of.close()
def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) otu_table_data = load_table(opts.input_otu_table) sort_field = opts.sort_field mapping_fp = opts.mapping_fp sorted_sample_ids_fp = opts.sorted_sample_ids_fp if sort_field and mapping_fp: mapping_data = parse_mapping_file(open(mapping_fp, 'U')) result = sort_otu_table_by_mapping_field(otu_table_data, mapping_data, sort_field) elif sorted_sample_ids_fp: sorted_sample_ids = sample_ids_from_f(open(sorted_sample_ids_fp, 'U')) result = sort_otu_table(otu_table_data, sorted_sample_ids) else: result = sort_otu_table(otu_table_data, natsort_case_insensitive(otu_table_data.ids())) write_biom_table(result, opts.output_fp)