-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmutation_generators.py
92 lines (69 loc) · 2.79 KB
/
mutation_generators.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright 2019
# Author: Fabio Gutmann <https://github.com/fabio-gut>
from copy import deepcopy
from copomus.indexing import idx_to_array_index
from copomus.measures import get_measure
from copomus.mutation import Mutation
from typing import List, Tuple
def gen_flip(query: str, target: str, qidxpos0: int, tidxpos0: int, bp_list: List[Tuple[int, int]], mms: List[str],
alpha: float, beta: float) -> List[Mutation]:
mutations = []
for q_index, t_index in bp_list:
mm = []
for m in mms:
mm.append(get_measure(m, alpha, beta))
q_index_a = idx_to_array_index(q_index, qidxpos0)
t_index_a = idx_to_array_index(t_index, tidxpos0)
bp_mm = f'{target[t_index_a]}{query[q_index_a]}'
m = Mutation(deepcopy(mm), query, target, qidxpos0, tidxpos0, q_index, t_index, bp_mm)
mutations.append(m)
return mutations
def gen_any(query: str, target: str, qidxpos0: int, tidxpos0: int, bp_list: List[Tuple[int, int]], mms: List[str],
alpha: float, beta: float) -> List[Mutation]:
mutations = []
possible_mutations = {
'AU': ['UA', 'UG', 'CG'],
'UA': ['AU', 'GU', 'GC'],
'GC': ['UA', 'UG', 'CG'],
'CG': ['AU', 'GU', 'GC'],
'GU': ['UA', 'UG', 'CG'],
'UG': ['AU', 'GU', 'GC']
}
for q_index, t_index in bp_list:
mm = []
for m in mms:
mm.append(get_measure(m, alpha, beta))
q_index_a = idx_to_array_index(q_index, qidxpos0)
t_index_a = idx_to_array_index(t_index, tidxpos0)
pos = possible_mutations[f'{query[q_index_a]}{target[t_index_a]}']
for p in pos:
bp_mm = f'{p[0]}{p[1]}'
m = Mutation(deepcopy(mm), query, target, qidxpos0, tidxpos0, q_index, t_index, bp_mm)
mutations.append(m)
return mutations
def gen_specific(query: str, target: str, qidxpos0: int, tidxpos0: int, mms: List[str], alpha: float, beta: float,
encoding: str) -> List[Mutation]:
mutations = []
mm = []
for m in mms:
mm.append(get_measure(m, alpha, beta))
for mut in encoding.split(','): # split block mutations into single mutations
q, t = mut.split('&')
q_index = int(q[1:len(q)-1]) # mutation indices
t_index = int(t[1:len(t)-1])
bp_mm = f'{q[len(q)-1]}{t[len(t)-1]}'
m = Mutation(deepcopy(mm), query, target, qidxpos0, tidxpos0, q_index, t_index, bp_mm)
mutations.append(m)
return mutations
def get_generator(string: str) -> any:
"""
Gets a mutation generator by its string name
:param string: The string name
:return: A reference to a generator function
"""
generators = {
'flip': gen_flip,
'any': gen_any,
'specific': gen_specific
}
return generators.get(string)