# mavis/interval

## class Interval

Attributes

• start (`int`)
• end (`int`)
• freq (`int`)
• forward_to_reverse (`Optional[bool]`)

### Interval.overlaps()

checks if two intervals have any portion of their given ranges in common

``````@classmethod
def overlaps(cls, first: 'Interval', other: 'Interval') -> bool:
``````

Args

• first (Interval): an interval to be compared
• other (Interval): an interval to be compared

Returns

• `bool`

Examples

``````>>> Interval.overlaps(Interval(1, 4), Interval(5, 7))
False
>>> Interval.overlaps(Interval(1, 10), Interval(10, 11))
True
>>> Interval.overlaps((1, 10), (10, 11))
True
``````

### Interval.center()

the middle of the interval

``````@property
def center(self) -> float:
``````

Args

• self

Returns

• `float`

Examples

``````>>> Interval(1, 10).center
5.5
>>> Interval(1, 11).center
6
``````

### Interval.dist()

returns the minimum distance between intervals

``````@classmethod
def dist(cls, first, other) -> int:
``````

Args

• first
• other

Returns

• `int`

Examples

``````>>> Interval.dist((1, 4), (5, 7))
-1
>>> Interval.dist((5, 7), (1, 4))
1
>>> Interval.dist((5, 8), (7, 9))
0
``````

### Interval.convert_ratioed_pos()

convert any given position given a mapping of intervals to another range

``````@classmethod
def convert_ratioed_pos(
cls, mapping: 'IntervalMapping', pos: int, forward_to_reverse=None
) -> 'Interval':
``````

Args

• mapping (IntervalMapping): a mapping of a set of continuous intervals
• pos (`int`): a position in the first coordinate system
• forward_to_reverse

Returns

• Interval: the position in the alternate coordinate system given the input mapping

Raises

• `AttributeError`: if the input position is outside the set of input segments
• `IndexError`: if the input position cannot be converted to the output system

Examples

``````>>> mapping = {(1, 10): (101, 110), (11, 20): (555, 564)}
>>> Interval.convert_pos(mapping, 5)
5
>>> Interval.convert_pos(mapping, 15)
559
``````

### Interval.union()

returns the union of the set of input intervals

``````@classmethod
def union(cls, *intervals) -> 'Interval':
``````

Returns

Examples

``````>>> Interval.union((1, 2), (4, 6), (4, 9), (20, 21))
Interval(1, 21)
``````

### Interval.intersection()

returns None if there is no intersection

``````@classmethod
def intersection(cls, *intervals) -> Optional['Interval']:
``````

Returns

Examples

``````>>> Interval.intersection((1, 10), (2, 8), (7, 15))
Interval(7, 8)
>>> Interval.intersection((1, 2), (5, 9))
None
``````

### Interval.min_nonoverlapping()

for a list of intervals, orders them and merges any overlap to return a list of non-overlapping intervals O(nlogn)

``````@classmethod
def min_nonoverlapping(cls, *intervals: 'Interval') -> List['Interval']:
``````

Returns

Examples

``````>>> Interval.min_nonoverlapping((1, 10), (7, 8), (6, 14), (17, 20))
[Interval(1, 14), Interval(17, 20)]
``````

### Interval.split_overlap()

for a given set of intervals splits any overlap so that the result is a new list of intervals with a single bp overlap

``````@classmethod
def split_overlap(cls, *intervals, weight_mapping={}):
``````

Warning

this method is meant for integer intervals only

## class IntervalMapping

mapping between coordinate systems using intervals. source intervals cannot overlap but no such assertion is enforced on the target intervals

### IntervalMapping.__init__()

``````def __init__(self, mapping=None, opposing=None):
``````

Args

• mapping
• opposing

### IntervalMapping.convert_ratioed_pos()

convert any given position given a mapping of intervals to the mapped range

``````def convert_ratioed_pos(self, pos: int) -> Interval:
``````

Args

• pos (`int`): a position in the first coordinate system

Returns

• Interval: the Interval the position lands in in the new coordinate system

Raises

• `IndexError`: if the input position is not in any of the mapped intervals

### IntervalMapping.convert_pos()

convert any given position given a mapping of intervals to another range

``````def convert_pos(self, pos: int) -> int:
``````

Args

• pos (`int`): a position in the first coordinate system

Returns

• `int`: the position in the alternate coordinate system given the input mapping

Raises

• `IndexError`: if the input position is not in any of the mapped intervals

Examples

``````>>> mapping = IntervalMapping(mapping={(1, 10): (101, 110), (11, 20): (555, 564)})
>>> mapping.convert_pos(5)
5
>>> mapping.convert_pos(15)
559
``````