-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
218 lines (179 loc) · 6.33 KB
/
functions.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import numpy as np
import matplotlib.pyplot as plt
#=============================================================================
def freq_table(text, k):
freq_table = {}
n = len(text)
for i in range(n - k + 1):
pattern = text[i : i+k]
if pattern not in freq_table.keys():
freq_table[pattern] = 1
else:
freq_table[pattern] += 1
return freq_table
#=============================================================================
def index_table(seq, k):
index_table = {}
for i in range(0, len(seq)-k+1):
if (pattern := seq[i: i+k]) not in index_table:
index_table[pattern] = [i]
else:
index_table[pattern].append(i)
return index_table
#=============================================================================
def find_clumps(seq, k, L, t):
idx_table = index_table(seq, k)
clumps_num = 0
clump_list = []
for key in idx_table:
for i in range(0, len(value := idx_table[key])):
if (i+t-1 < len(value)) and (value[i+t-1] - value[i]) <= L-k:
clumps_num += 1
clump_list.append(key)
break
return clumps_num, clump_list
#=============================================================================
def reverse_complement(text):
text = text.replace('A', 'a')
text = text.replace('C', 'c')
text = text.replace('T', 'A')
text = text.replace('G', 'C')
text = text.replace('a', 'T')
text = text.replace('c', 'G')
return text[::-1]
#=============================================================================
def best_patterns(text, k):
best_patterns = []
table = freq_table(text, k)
max = np.max(list(table.values()))
for key, value in table.items():
if value == max:
best_patterns.append(key)
return best_patterns
#=============================================================================
def pattern_index(sequence, pattern):
pattern_idx = []
n = len(sequence)
k = len(pattern)
for i in range(n - k + 1):
seq2check = sequence[i : i+k]
if seq2check == pattern:
pattern_idx.append(i)
return pattern_idx
#=============================================================================
def approximate_pattern_matching(pattern, seq, d):
position = []
k = len(pattern)
n = len(seq)
for i in range(n - k + 1):
if hamming_distance(seq[i : i+k], pattern) <= d:
position.append(i)
return position
#=============================================================================
def skew_min_idx(skew_list):
if not skew_list:
return []
min_value = min(skew_list)
min_indices = [index for index, value in enumerate(skew_list) if value == min_value]
return min_indices
#=============================================================================
def skew_diagram(seq):
char_to_num_mapping = {'A': 0, 'T': 0, 'C': -1, 'G': 1}
the_list = [char_to_num_mapping.get(char, None) for char in seq]
skew_list = [0]
cumsum = 0
for num in the_list:
if num != None:
cumsum += num
skew_list.append(cumsum)
min_idx = skew_min_idx(skew_list)
plt.figure(figsize=(6, 3))
plt.plot(skew_list)
plt.show()
return skew_list, min_idx
#=============================================================================
def hamming_distance(p, q):
distance = 0
for i in range(len(p)):
if p[i] != q[i]:
distance += 1
return distance
#=============================================================================
def immidiate_neighbors(pattern):
neighbors = set()
for i in range(len(pattern)):
for j in 'ATCG':
if pattern[i] != j:
to_add = pattern[:i] + j + pattern[i+1:]
neighbors.add(to_add)
return neighbors
#=============================================================================
def neighborhood(pattern, d):
if d == 0:
return {pattern}
if len(pattern) == 1:
return {'A', 'T', 'C', 'G'}
neighbors = set()
suffix_pattern = pattern[1:]
suffix_neighbors = neighborhood(suffix_pattern, d)
for suffix_neighbor in suffix_neighbors:
if hamming_distance(suffix_pattern, suffix_neighbor) < d:
for x in 'ATCG':
neighbors.add(x + suffix_neighbor)
else:
neighbors.add(pattern[0] + suffix_neighbor)
return neighbors
#=============================================================================
def neighborhood_list(pattern, d):
if d == 0:
return pattern
if len(pattern) == 1:
return {'A', 'T', 'C', 'G'}
neighbors = set()
suffix_pattern = pattern[1:]
suffix_neighbors = neighborhood_list(suffix_pattern, d)
for suffix_neighbor in suffix_neighbors:
if hamming_distance(suffix_pattern, suffix_neighbor) < d:
for x in 'ATCG':
neighbors.add(x + suffix_neighbor)
else:
neighbors.add(pattern[0] + suffix_neighbor)
return list(neighbors)
#=============================================================================
def freq_word_with_mistmatch(seq, k, d):
patterns = []
freq_map = {}
n = len(seq)
for i in range(n - k + 1):
pattern = seq[i : i+k]
neighbors = neighborhood(pattern, d)
for neighbor in neighbors:
if neighbor not in freq_map:
freq_map[neighbor] = 1
else:
freq_map[neighbor] += 1
max_value = np.max(list(freq_map.values()))
for key, value in freq_map.items():
if value == max_value:
patterns.append(key)
return patterns
#=============================================================================
def freq_word_with_mistmatch_reverse(seq, k, d):
patterns = []
freq_map = {}
n = len(seq)
for i in range(n - k + 1):
pattern = seq[i : i+k]
neighbors = neighborhood_list(pattern, d)
rev_neighbors = [reverse_complement(neighbor) for neighbor in neighbors]
for neighbor in neighbors + rev_neighbors:
if neighbor not in freq_map:
freq_map[neighbor] = 1
else:
freq_map[neighbor] += 1
max_value = np.max(list(freq_map.values()))
for key, value in freq_map.items():
if value == max_value:
patterns.append(key)
return patterns
#=============================================================================