| 1 |
class Error (Exception): pass |
| 2 |
|
| 3 |
|
| 4 |
class Interval: |
| 5 |
'''A class to deal with intervals in a genome. Can do things like intersections, unions etc''' |
| 6 |
def __init__(self, start, end): |
| 7 |
try: |
| 8 |
self.start = int(start) |
| 9 |
self.end = int(end) |
| 10 |
except ValueError: |
| 11 |
raise Error('Error making interval from :"' + str(start) + '" and "' + str(end) + '"') |
| 12 |
|
| 13 |
if self.end < self.start: |
| 14 |
raise Error('Error making interval ' + str(self) + '. end < start.') |
| 15 |
|
| 16 |
def __len__(self): |
| 17 |
return self.end - self.start + 1 |
| 18 |
|
| 19 |
def __eq__(self, other): |
| 20 |
return type(other) is type(self) and self.__dict__ == other.__dict__ |
| 21 |
|
| 22 |
def __ne__(self, other): |
| 23 |
return not self.__eq__(other) |
| 24 |
|
| 25 |
def __str__(self): |
| 26 |
return '(' + str(self.start) + ',' + str(self.end) + ')' |
| 27 |
|
| 28 |
def __lt__(self, i): |
| 29 |
return self.start < i.start or (self.start == i.start and self.end < i.end) |
| 30 |
|
| 31 |
def __le__(self, i): |
| 32 |
return self.start < i.start or (self.start == i.start and self.end <= i.end) |
| 33 |
|
| 34 |
def distance_to_point(self, p): |
| 35 |
'''Returns the distance from the point to the interval. Zero if the point lies inside the interval.''' |
| 36 |
if self.start <= p <= self.end: |
| 37 |
return 0 |
| 38 |
else: |
| 39 |
return min(abs(self.start - p), abs(self.end - p)) |
| 40 |
|
| 41 |
def intersects(self, i): |
| 42 |
'''Returns true iff this interval intersects the interval i''' |
| 43 |
return self.start <= i.end and i.start <= self.end |
| 44 |
|
| 45 |
def contains(self, i): |
| 46 |
'''Returns true iff this interval contains the interval i''' |
| 47 |
return self.start <= i.start and i.end <= self.end |
| 48 |
|
| 49 |
def union(self, i): |
| 50 |
'''If intervals intersect, returns their union, otherwise returns None''' |
| 51 |
if self.intersects(i) or self.end + 1 == i.start or i.end + 1 == self.start: |
| 52 |
return Interval(min(self.start, i.start), max(self.end, i.end)) |
| 53 |
else: |
| 54 |
return None |
| 55 |
|
| 56 |
def union_fill_gap(self, i): |
| 57 |
'''Like union, but ignores whether the two intervals intersect or not''' |
| 58 |
return Interval(min(self.start, i.start), max(self.end, i.end)) |
| 59 |
|
| 60 |
def intersection(self, i): |
| 61 |
'''If intervals intersect, returns their intersection, otherwise returns None''' |
| 62 |
if self.intersects(i): |
| 63 |
return Interval(max(self.start, i.start), min(self.end, i.end)) |
| 64 |
else: |
| 65 |
return None |
| 66 |
|
| 67 |
|
| 68 |
def intersection(l1, l2): |
| 69 |
'''Returns intersection of two lists. Assumes the lists are sorted by start positions''' |
| 70 |
if len(l1) == 0 or len(l2) == 0: |
| 71 |
return [] |
| 72 |
|
| 73 |
out = [] |
| 74 |
l2_pos = 0 |
| 75 |
|
| 76 |
for l in l1: |
| 77 |
while l2_pos < len(l2) and l2[l2_pos].end < l.start: |
| 78 |
l2_pos += 1 |
| 79 |
|
| 80 |
if l2_pos == len(l2): |
| 81 |
break |
| 82 |
|
| 83 |
while l2_pos < len(l2) and l.intersects(l2[l2_pos]): |
| 84 |
out.append(l.intersection(l2[l2_pos])) |
| 85 |
l2_pos += 1 |
| 86 |
|
| 87 |
l2_pos = max(0, l2_pos - 1) |
| 88 |
|
| 89 |
return out |
| 90 |
|
| 91 |
|
| 92 |
def merge_overlapping_in_list(l): |
| 93 |
'''Sorts list, merges any overlapping intervals, and also adjacent intervals. e.g. |
| 94 |
[0,1], [1,2] would be merge to [0,.2].''' |
| 95 |
i = 0 |
| 96 |
l.sort() |
| 97 |
|
| 98 |
while i < len(l) - 1: |
| 99 |
u = l[i].union(l[i+1]) |
| 100 |
if u is not None: |
| 101 |
l[i] = u |
| 102 |
l.pop(i+1) |
| 103 |
else: |
| 104 |
i += 1 |
| 105 |
|
| 106 |
|
| 107 |
def remove_contained_in_list(l): |
| 108 |
'''Sorts list in place, then removes any intervals that are completely |
| 109 |
contained inside another interval''' |
| 110 |
i = 0 |
| 111 |
l.sort() |
| 112 |
|
| 113 |
while i < len(l) - 1: |
| 114 |
if l[i+1].contains(l[i]): |
| 115 |
l.pop(i) |
| 116 |
elif l[i].contains(l[i+1]): |
| 117 |
l.pop(i+1) |
| 118 |
else: |
| 119 |
i += 1 |
| 120 |
|
| 121 |
|
| 122 |
def length_sum_from_list(l): |
| 123 |
'''Returns total length of intervals from a list''' |
| 124 |
return sum([len(x) for x in l]) |