mgkit.utils.common module

Utility functions

mgkit.utils.common.apply_func_window(func, data, window, step=0)
mgkit.utils.common.average_length(a1s, a1e, a2s, a2e)

Given two sets of coordinates, a1 and a2, returns the average length.

  • a1s (int) – a1 leftmost number
  • a1e (int) – a1 rightmost number
  • a2s (int) – a2 leftmost number
  • a2e (int) – a2 rightmost number
Return float:

the average length

mgkit.utils.common.between(pos, start, end)

Tests if a number is between two others

  • pos (int) – number to test
  • start (int) – leftmost number
  • end (int) – rightmost number
Return bool:

if the number is between start and end

mgkit.utils.common.complement_ranges(intervals, end=None)

New in version 0.3.1.

Perform a complement operation of the list of intervals, i.e. returning the ranges (tuples) that are not included in the list of intervals. union_ranges() is first called on the intervals.


the end parameter is there for cases where the ranges passed don’t cover the whole space. Assuming a list of ranges from annotations on a nucleotidic sequence, if the last range doesn’t include the last position of the sequence, passing end equal to the length of the sequence will make the function include a last range that includes it

  • intervals (intervals) – iterable where each element is a closed range (tuple)
  • end (int) – if the end of the complement intervals is supposed to be outside the last range.

the list of intervals that complement the ones passed.

Return type:



>>> complement_ranges([(1, 10), (11, 20), (25, 30)], end=100)
[(21, 24), (31, 100)]
>>> complement_ranges([(1, 10), (11, 20), (25, 30)])
[(21, 24)]
>>> complement_ranges([(0, 2), (3, 17), (18, 20)])
>>> complement_ranges([(0, 2), (3, 17), (18, 20)], end=100)
[(21, 100)]

This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.


mgkit.utils.common.range_intersect(start1, end1, start2, end2)

New in version 0.1.13.

Given two ranges in the form (start, end), it returns the range that is the intersection of the two.

  • start1 (int) – start position for the first range
  • end1 (int) – end position for the first range
  • start2 (int) – start position for the second range
  • end2 (int) – end position for the second range

returns a tuple with the start and end position for the intersection of the two ranges, or None if the intersection is empty

Return type:

(None, tuple)

mgkit.utils.common.range_substract(start1, end1, start2, end2)

New in version 0.1.12.

Given an iterable where each element is a range, a tuple whose elements are numbers with the first being less than or equal to the second, the function sums the lengths of all ranges.


it’s supposed to be used on intervals that were first passed to functions like union_ranges(). If values overlap, there the sum will be wrong

Parameters:ranges (iterable) – each element is a tuple like (1, 10)
Returns:sum of all ranges lengths
Return type:int
mgkit.utils.common.union_range(start1, end1, start2, end2)

New in version 0.1.12.

Changed in version 0.3.1: changed behaviour, since the intervals are meant to be closed

If two numeric ranges overlap, it returns the new range, otherwise None is returned. Works on both int and float numbers, even mixed.

  • start1 (numeric) – start of range 1
  • end1 (numeric) – end of range 1
  • start2 (numeric) – start of range 2
  • end2 (numeric) – end of range 2

union of the ranges or None if the ranges don’t overlap

Return type:

(tuple or None)


>>> union_range(10, 13, 1, 10)
(1, 13)
>>> union_range(1, 10, 11, 13)
(1, 13)
>>> union_range(1, 10, 12, 13)

New in version 0.3.1.

From a list of ranges, assumed to be closed, performs a union of all elements.

Parameters:intervals (intervals) – iterable where each element is a closed range (tuple)
Returns:the list of ranges that are the union of all elements passed
Return type:list


>>> union_ranges([(1, 2), (3, 7), (6, 12), (9, 17), (18, 20)])
[(1, 20)]
>>> union_ranges([(1, 2), (3, 7), (6, 12), (9, 14), (18, 20)])
[(1, 14), (18, 20)]