interval module

class mavis.interval.Interval(start, end=None, freq=1, number_type=None)[source]

Bases: object

Parameters:
  • start (int) – the start of the interval (inclusive)
  • end (int) – the end of the interval (inclusive)
  • freq (int) – the frequency or weight of the interval
__and__(other)[source]

the intersection of two intervals

Example

>>> Interval(1, 10) & Interval(5, 50)
Interval(5, 10)
>>> Interval(1, 2) & Interval(10, 11)
None
__len__()[source]

the length of the interval

Example

>>> len(Interval(1, 11))
12

Warning

only works for integer intervals

__or__(other)[source]

the union of two intervals

Example

>>> Interval(1, 10) | Interval(5, 50)
Interval(1, 50)
>>> Interval(1, 2) | Interval(10, 11)
Interval(1, 11)
__sub__(other)[source]

the difference of two intervals

Example

>>> Interval(1, 10) - Interval(5, 50)
[Interval(1, 4)]
>>> Interval(1, 2) - Interval(10, 11)
[Interval(1, 2)]
>>> Interval(1, 2) - Interval(-1, 10)
[]
__xor__(other)[source]
center

the middle of the interval

Example

>>> Interval(1, 10).center
5.5
>>> Interval(1, 11).center
6
classmethod convert_pos(mapping, pos, forward_to_reverse=None)[source]
classmethod convert_ratioed_pos(mapping, pos, forward_to_reverse=None)[source]

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

Parameters:
  • mapping (dict of Interval and Interval) – a mapping of a set of continuous intervals
  • pos (int) – a position in the first coordinate system
Returns:

the position in the alternate coordinate system given the input mapping

Return type:

int

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

Example

>>> mapping = {(1, 10): (101, 110), (11, 20): (555, 564)}
>>> Interval.convert_pos(mapping, 5)
5
>>> Interval.convert_pos(mapping, 15)
559
classmethod dist(first, other)[source]

returns the minimum distance between intervals

Example

>>> Interval.dist((1, 4), (5, 7))
-1
>>> Interval.dist((5, 7), (1, 4))
1
>>> Interval.dist((5, 8), (7, 9))
0
classmethod from_iterable(iterable)[source]
classmethod intersection(*intervals)[source]

returns None if there is no intersection

Example

>>> Interval.intersection((1, 10), (2, 8), (7, 15))
Interval(7, 8)
>>> Interval.intersection((1, 2), (5, 9))
None
length()[source]
classmethod min_nonoverlapping(*intervals)[source]

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

Example

>>> Interval.min_nonoverlapping((1, 10), (7, 8), (6, 14), (17, 20))
[Interval(1, 14), Interval(17, 20)]
classmethod overlaps(first, other)[source]

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

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

Example

>>> 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
classmethod position_in_range(segments, pos)[source]
classmethod split_overlap(*intervals, weight_mapping={})[source]

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

Warning

this method is meant for integer intervals only

classmethod union(*intervals)[source]

returns the union of the set of input intervals

Example

>>> Interval.union((1, 2), (4, 6), (4, 9), (20, 21))
Interval(1, 21)
class mavis.interval.IntervalMapping(mapping=None, opposing=None)[source]

Bases: object

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

add(src_interval, tgt_interval, opposing_directions=True)[source]
convert_pos(pos, simplify=True)[source]

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

Parameters:pos (int) – a position in the first coordinate system
Returns:the position in the alternate coordinate system given the input mapping - int: if simplify is True - Interval: if simplify is False
Raises:IndexError – if the input position is not in any of the mapped intervals

Example

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