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.assertCountEqual([ranges.make_range(*args) for args in expected],
                         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_regions_and_exclude_regions_flags(self):
    FLAGS.mode = 'calling'
    FLAGS.ref = test_utils.CHR20_FASTA
    FLAGS.reads = test_utils.CHR20_BAM
    FLAGS.regions = 'chr20:10,000,000-11,000,000'
    FLAGS.examples = 'examples.tfrecord'
    FLAGS.exclude_regions = 'chr20:10,010,000-10,100,000'

    options = make_examples.default_options(add_flags=True)
    self.assertCountEqual(
        list(
            ranges.RangeSet(
                make_examples.processing_regions_from_options(options))),
        _from_literals_list(
            ['chr20:10,000,000-10,009,999', 'chr20:10,100,001-11,000,000']))
 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_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_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')
 def setUp(self):
     self.labeler = variant_labeler.VariantLabeler(
         vcf_reader=mock_vcf_reader(self.variants),
         confident_regions=ranges.RangeSet(
             [ranges.make_range(self.snp.reference_name, 10, 100)]))