def test_union_of_overlapping(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) second = IntervalSet((i.open(8, 21), i.closed(22, 23))) expected = IntervalSet((i.open(1, 5), i.closedopen(7, 21), i.closed(22, 23))) result = first.union(second) assert result == expected
def test_union_of_disjoint(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) second = IntervalSet((i.open(12, 21), i.closed(22, 23))) expected = IntervalSet( (i.open(1, 5), i.closed(7, 10), i.open(12, 21), i.closed(22, 23))) result = first.union(second) assert result == expected
def test_overlaps_corner_case_closed(): one = i.closed(1, 5) two = i.closed(5, 10) assert one.overlaps(two) assert two.overlaps(one) assert one.overlaps(one) assert two.overlaps(two)
def test_subtract_empty_types(): left = i.closed(datetime(2015, 1, 1), datetime(2020, 1, 1)) right = i.closed(datetime(2010, 1, 1), datetime(2100, 1, 1)) result = left - right assert result.empty() assert isinstance(result.lower_value, datetime) assert isinstance(result.upper_value, datetime)
def test_values_in(): one = i.open(1, 5) two = i.closed(7, 10) ivset = IntervalSet((i.open(1, 5), i.closed(7, 10))) assert 1 not in ivset assert 1.00000001 in ivset assert 3 in ivset assert 7 in ivset assert 8 in ivset assert 10.0001 not in ivset assert one in ivset assert two in ivset
def test_contains(): small = i.closed(1, 5) medium = i.closedopen(3, 10) assert small.lower_value in small assert small._upper_value in small assert medium.lower_value in medium assert medium._upper_value not in medium
def test_complement_closed(): unit = i.closed(0, 1) complement = unit.complement() (lower_interval, upper_interval) = sorted(complement) # an IntervalSet is not sorted assert lower_interval == i.open(i.NEGATIVE_INFINITY, 0) assert upper_interval == i.open(1, i.INFINITY)
def test_non_equality(): small, medium = get_intervals()[:2] assert small != medium assert small != i.openclosed(1, 5) assert small != i.closedopen(1, 5) assert small != i.closed(1, 5) assert small != i.Interval(i.Interval.OPEN, 1, 7, i.Interval.OPEN) assert small != i.open(100, 288)
def test_non_equality(): small, medium = get_intervals()[:2] assert small != medium assert small != i.openclosed(1,5) assert small != i.closedopen(1,5) assert small != i.closed(1,5) assert small != i.Interval(i.Interval.OPEN, 1, 7, i.Interval.OPEN) assert small != i.open(100, 288)
def test_complement(): one = i.open(3, 6) two = i.open(7, 10) intervals = IntervalSet([one, two]) complement = intervals.complement() (lower, middle, upper) = sorted(complement) # an IntervalSet is not sorted assert lower == i.openclosed(i.NEGATIVE_INFINITY, 3) assert middle == i.closed(6, 7) assert upper == i.closedopen(10, i.INFINITY)
def test_closed_interval_construction_using_helper(): expected_lower = i.Interval.CLOSED expectedlower_value = 10 expected_upper_value = 100 expected_upper = i.Interval.CLOSED result = i.closed(10, 100) assert result.lower == expected_lower assert result.lower_value == expectedlower_value assert result.upper_value == expected_upper_value assert result.upper == expected_upper
def test_closed_interval_construction_using_helper(): expected_lower = i.Interval.CLOSED expectedlower_value = 10 expected_upper_value = 100 expected_upper = i.Interval.CLOSED result = i.closed(10, 100) assert result._lower == expected_lower assert result.lower_value == expectedlower_value assert result._upper_value == expected_upper_value assert result._upper == expected_upper
def test_overlaps_empty(): assert not i.open(0, 0).overlaps(i.closed(1, 20)) assert not i.open(1, 1).overlaps(i.closed(1, 20)) assert not i.open(2, 2).overlaps(i.closed(1, 20)) assert not i.closed(1, 20).overlaps(i.open(0, 0)) assert not i.closed(1, 20).overlaps(i.open(1, 1)) assert not i.closed(1, 20).overlaps(i.open(2, 2))
for line in sys.stdin: line = line.strip() if line: ss = line.split() exonStart = ss[1].strip(',').split(',') exonEnd = ss[2].strip(',').split(',') name = ss[0] + ss[3] if name not in dataMap: dataMap[name] = (ss[0], ss[3], interval_set.IntervalSet()) for s, e in zip(exonStart, exonEnd): s = int(s) -1 - padding # bed file 0 based. if s < 0: s = 0 e = int(e) + padding # end not include, therefore do not need -1. dataMap[name][2].add(interval.closed(s, e)) #auto aggregate overlap. #output results. for _,v in dataMap.items(): index = 0 for i in v[2]: index += 1 sys.stdout.write('%s\t%d\t%d\t%s+%d\n'%(v[0], i.lower_value, i.upper_value, v[1], index)) sys.stdout.flush() sys.stdout.close() sys.stderr.flush() sys.stderr.close()
def test_intersection_of_almost_overlapping(): first = IntervalSet((i.open(1, 5), i.closedopen(7, 10))) second = IntervalSet((i.open(1, 5), i.closed(7, 23))) expected = IntervalSet((i.open(1, 5), i.closedopen(7, 10))) result = first.intersection(second) assert result == expected
def test_interval_copy(): assert i.open(1, 4).copy() == i.open(1, 4) assert i.openclosed(1, 4).copy() == i.openclosed(1, 4) assert i.closedopen(1, 4).copy() == i.closedopen(1, 4) assert i.closed(1, 4).copy() == i.closed(1, 4)
def test_overlaps_touching(): assert not i.openclosed(1, 4).overlaps(i.openclosed(4, 5)) assert not i.closedopen(1, 4).overlaps(i.closedopen(4, 5)) assert not i.open(1, 4).overlaps(i.open(4, 5)) assert i.closed(1, 4).overlaps(i.closed(4, 5))
def test_subtract_almost_complete_overlap(): left = i.closed(1, 2) right = i.open(1, 5) expected = i.closed(1, 1) assert left - right == expected
def test_subtract_exact_overlap(): left = i.closed(1, 2) right = i.closed(1, 2) assert (left - right).empty()
def test_union_of_disjoint(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) second = IntervalSet((i.open(12, 21), i.closed(22,23))) expected = IntervalSet((i.open(1, 5), i.closed(7, 10), i.open(12, 21), i.closed(22, 23))) result = first.union(second) assert result == expected
def test_intersection_of_almost_overlapping(): first = IntervalSet((i.open(1, 5), i.closedopen(7, 10))) second = IntervalSet((i.open(1, 5), i.closed(7,23))) expected = IntervalSet((i.open(1, 5), i.closedopen(7, 10))) result = first.intersection(second) assert result == expected
def test_equality_of_not_equal(): one = IntervalSet([i.open(1, 10)]) two = IntervalSet([i.closed(1, 10)]) assert one != two
def test_contains_self_closed_open(): small = i.closedopen(1, 5) medium = i.closed(7, 10) assert small in small assert small not in medium assert medium not in small
def test_contains_fully_overlapping(): small = i.closed(7, 10) medium = i.closedopen(7, 21) assert small in medium
def test_intersection_of_equal(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) result = first.intersection(first) assert result == first
def test_initialising_with_generator_does_not_consume_generator_before_storing_items(): generator = (el for el in (i.open(1, 5), i.closed(7, 10))) assert len(IntervalSet(generator)) == 2
def test_len_works_as_expected(): assert len(IntervalSet((i.open(1, 5), i.closed(7, 10)))) == 2
def test_length_of_unioned(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) second = IntervalSet((i.open(8, 21), i.closed(22,23))) # This is of length 3 as 2 of the intervals overlap and therefore join together assert len(first.union(second)) == 3
def test_subtract_complete_overlap_returns_an_empty_interval(): left = i.closed(1, 2) right = i.closed(1, 2) assert (left - right).empty()
def test_initialising_with_generator_does_not_consume_generator_before_storing_items( ): generator = (el for el in (i.open(1, 5), i.closed(7, 10))) assert len(IntervalSet(generator)) == 2
def test_repr(): assert repr(i.open(1, 2)) == '(1, 2)' assert repr(i.openclosed(1, 2)) == '(1, 2]' assert repr(i.closed(1, 2)) == '[1, 2]' assert repr(i.closedopen(1, 2)) == '[1, 2)'
ss = line.split() exonStart = ss[1].strip(',').split(',') exonEnd = ss[2].strip(',').split(',') name = ss[0] + ss[3] if name not in dataMap: dataMap[name] = (ss[0], ss[3], interval_set.IntervalSet()) for s, e in zip(exonStart, exonEnd): s = int(s) - 1 - padding # bed file 0 based. if s < 0: s = 0 e = int( e) + padding # end not include, therefore do not need -1. #automatic merge overlapped interval. dataMap[name][2].add(interval.closed( s, e)) #auto aggregate overlap. #output results. for _, v in dataMap.items(): index = 0 for i in v[2]: index += 1 sys.stdout.write('%s\t%d\t%d\t%s+%d\n' % (v[0], i.lower_value, i.upper_value, v[1], index)) sys.stdout.flush() sys.stdout.close() sys.stderr.flush() sys.stderr.close()
def test_length_of_unioned(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) second = IntervalSet((i.open(8, 21), i.closed(22, 23))) # This is of length 3 as 2 of the intervals overlap and therefore join together assert len(first.union(second)) == 3
def test_intersection_of_disjoint_is_empty(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) second = IntervalSet((i.open(20, 21), i.closed(22, 23))) expected = IntervalSet() result = first.intersection(second) assert result == expected
def test_intersection_of_disjoint_is_empty(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) second = IntervalSet((i.open(20,21), i.closed(22,23))) expected = IntervalSet() result = first.intersection(second) assert result == expected
def test_union_of_overlapping(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) second = IntervalSet((i.open(8, 21), i.closed(22,23))) expected = IntervalSet((i.open(1, 5), i.closedopen(7, 21), i.closed(22, 23))) result = first.union(second) assert result == expected
def test_union_of_equal(): first = IntervalSet((i.open(1, 5), i.closed(7, 10))) result = first.union(first) assert result == first