def test_partitions_bad_interval_size_raises(self): # list() is necessary to force the generator to execute. with self.assertRaisesRegexp(ValueError, 'max_size'): list( ranges.RangeSet([ranges.make_range('chrM', 0, 100)]).partition(-10)) with self.assertRaisesRegexp(ValueError, 'max_size'): list( ranges.RangeSet([ranges.make_range('chrM', 0, 100)]).partition(0))
def test_envelops(self): start_ix = 5 end_ix = 10 start_ix2 = end_ix + 1 end_ix2 = end_ix + 5 range_set = ranges.RangeSet([ ranges.make_range('chr1', start_ix, end_ix), ranges.make_range('chr1', start_ix2, end_ix2) ]) # No start position before the first start range is enveloped. for i in range(start_ix): self.assertFalse(range_set.envelops('chr1', i, start_ix + 1)) # All regions within a single record are enveloped. for six in range(start_ix, end_ix): for eix in range(six, end_ix + 1): self.assertTrue(range_set.envelops('chr1', six, eix), 'chr1 {} {} not enveloped'.format(six, eix)) # Bridging across two ranges is not enveloped. for six in range(start_ix, end_ix): for eix in range(start_ix2, end_ix2 + 1): self.assertFalse(range_set.envelops('chr1', six, eix)) # Other chromosome is not spanned. self.assertFalse(range_set.envelops('chr2', start_ix, start_ix + 1))
def test_overlaps_variant_with_ranges(self): variant = variants_pb2.Variant(reference_name='chr2', start=10, end=11) range_set = ranges.RangeSet([ranges.make_range('chr1', 0, 5)]) with mock.patch.object(range_set, 'overlaps') as mock_overlaps: mock_overlaps.return_value = True self.assertEqual(range_set.variant_overlaps(variant), True) mock_overlaps.assert_called_once_with('chr2', 10)
def test_detector_ranges(self): test_ranges = [ ranges.make_range('chr1', 0, 5), ranges.make_range('chr1', 8, 10), ranges.make_range('chr1', 12, 13), ranges.make_range('chr2', 2, 5), ] range_set = ranges.RangeSet(test_ranges) self.assertEqual(bool(range_set), True) self.assertEqual(len(range_set), 4) self.assertEqual(range_set.overlaps('chr1', 0), True) self.assertEqual(range_set.overlaps('chr1', 1), True) self.assertEqual(range_set.overlaps('chr1', 2), True) self.assertEqual(range_set.overlaps('chr1', 3), True) self.assertEqual(range_set.overlaps('chr1', 4), True) self.assertEqual(range_set.overlaps('chr1', 5), False) self.assertEqual(range_set.overlaps('chr1', 6), False) self.assertEqual(range_set.overlaps('chr1', 7), False) self.assertEqual(range_set.overlaps('chr1', 8), True) self.assertEqual(range_set.overlaps('chr1', 9), True) self.assertEqual(range_set.overlaps('chr1', 10), False) self.assertEqual(range_set.overlaps('chr1', 11), False) self.assertEqual(range_set.overlaps('chr1', 12), True) self.assertEqual(range_set.overlaps('chr1', 13), False) self.assertEqual(range_set.overlaps('chr1', 100), False) self.assertEqual(range_set.overlaps('chr1', 1000), False) self.assertEqual(range_set.overlaps('chr2', 0), False) self.assertEqual(range_set.overlaps('chr2', 1), False) self.assertEqual(range_set.overlaps('chr2', 2), True) self.assertEqual(range_set.overlaps('chr2', 3), True) self.assertEqual(range_set.overlaps('chr2', 4), True) self.assertEqual(range_set.overlaps('chr2', 5), False) self.assertEqual(range_set.overlaps('chr2', 6), False) self.assertEqual(range_set.overlaps('chr3', 3), False)
def test_partition_of_multiple_intervals(self, interval_size, expected): rangeset = ranges.RangeSet([ ranges.make_range('1', 0, 10), ranges.make_range('1', 20, 40), ranges.make_range('1', 45, 50), ]) self.assertCountEqual([ranges.make_range(*args) for args in expected], rangeset.partition(interval_size))
def test_partitions(self, interval_size, expected): rangeset = ranges.RangeSet([ ranges.make_range('chrM', 0, 100), ranges.make_range('chr1', 0, 76), ranges.make_range('chr2', 0, 121), ]) self.assertEqual([ranges.make_range(*args) for args in expected], list(rangeset.partition(interval_size)))
def test_intersection(self, regions, expected): regions_list = [ranges.RangeSet.from_regions(r) for r in regions] copies = [ranges.RangeSet(rs) for rs in regions_list] # Check that the intersection is as expected. self.assertCountEqual(ranges.RangeSet.from_regions(expected), regions_list[0].intersection(*regions_list[1:])) # Check that no one was modified. for pre, post in zip(copies, regions_list): self.assertCountEqual(pre, post)
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])
def test_unrecognized_contig_triggers_exception(self): with self.assertRaises(ValueError): _ = ranges.RangeSet([ranges.make_range('bogus_chromosome', 1, 10)], _TEST_CONTIGS)
def test_detector_no_ranges(self): range_set = ranges.RangeSet() # don't have any ranges by default self.assertEqual(bool(range_set), False) # make sure we can call overlaps without any ranges self.assertFalse(range_set.overlaps('chr1', 10))
def test_overlaps_variant_empty_range(self): variant = variants_pb2.Variant(reference_name='chr2', start=10, end=11) empty_set = ranges.RangeSet() self.assertEqual( empty_set.variant_overlaps(variant, empty_set_return_value='foo'), 'foo')