-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindexing.py
50 lines (44 loc) · 1.37 KB
/
indexing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Copyright 2019
# Author: Fabio Gutmann <https://github.com/fabio-gut>
def idx_to_array_index(idx: int, idxpos0: int):
"""
Converts a nucleotide index to an 0-included array index
:param idx: The index to convert
:param idxpos0: The start index
:return: The index of the element in the array
>>> idx_to_array_index(10, 5)
5
>>> idx_to_array_index(10, -200)
209
>>> idx_to_array_index(1, -1)
1
"""
return idx - idxpos0 - (1 if (idxpos0 < 0 < idx or idx < 0 < idxpos0) else 0)
def array_index_to_idx(i: int, idxpos0: int):
"""
Converts a nucleotide index to an 0-included array index
:param i: The array index
:param idxpos0: The start index
:return: The index of the element in the array
>>> array_index_to_idx(5, 5)
10
>>> array_index_to_idx(209, -200)
10
>>> array_index_to_idx(1, -1)
1
"""
return idxpos0 + i + (1 if (idxpos0 < 0 < i or i < 0 < idxpos0) else 0)
def add_sub_idx(i1: int, change: int):
"""
Adds or subtracts an index from another one and skips 0 if encountered.
:param i1: The start index
:param change: The change (+ or -)
:return: The new index
>>> add_sub_idx(-10, 10)
1
>>> add_sub_idx(10, -10)
-1
>>> add_sub_idx(-5, 10)
6
"""
return i1 + change + (1 if i1 < 0 < i1+change+1 else -1 if i1+change-1 < 0 < i1 else 0)