def test_sort_ranges(self): contigs = [ reference_pb2.ContigInfo(name='c', n_bases=100, pos_in_fasta=0), reference_pb2.ContigInfo(name='a', n_bases=76, pos_in_fasta=1), reference_pb2.ContigInfo(name='b', n_bases=121, pos_in_fasta=2), ] unsorted = ranges.parse_literals( ['a:10', 'c:20', 'b:30', 'b:10-15', 'b:10', 'a:5']) # Without contigs we sort the contigs by name lexicographically. self.assertEqual( ranges.parse_literals( ['a:5', 'a:10', 'b:10', 'b:10-15', 'b:30', 'c:20']), ranges.sorted_ranges(unsorted)) # With contigs we sort by the position of the contigs themselves. self.assertEqual( ranges.parse_literals( ['c:20', 'a:5', 'a:10', 'b:10', 'b:10-15', 'b:30']), ranges.sorted_ranges(unsorted, contigs))
def test_rangeset_iteration_order(self): contigs = [ reference_pb2.ContigInfo(name='c', n_bases=100, pos_in_fasta=0), reference_pb2.ContigInfo(name='b', n_bases=121, pos_in_fasta=2), reference_pb2.ContigInfo(name='a', n_bases=76, pos_in_fasta=1), ] unsorted = ranges.parse_literals( ['a:10', 'c:20', 'b:30', 'b:10-15', 'a:5']) # Iteration order over a RangeSet instantiated with a contigs list is # determined by pos_in_fasta, start, end. range_set_with_contigs = ranges.RangeSet(unsorted, contigs) self.assertEqual( ranges.parse_literals(['c:20', 'a:5', 'a:10', 'b:10-15', 'b:30']), [range_ for range_ in range_set_with_contigs]) # For a RangeSet instantiated *without* a contig map, the iteration order # is determined by reference_name, start, end. range_set_no_contigs = ranges.RangeSet(unsorted) self.assertEqual( ranges.parse_literals(['a:5', 'a:10', 'b:10-15', 'b:30', 'c:20']), [range_ for range_ in range_set_no_contigs])