-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatrfinder.py
259 lines (189 loc) · 5.53 KB
/
atrfinder.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
def initial_matrix(n, m):
d = []
for i in range(n+1):
d.append([0]*(m+1))
d[i][0] = i
for j in range(m+1):
d[0][j] = j
return d
def print_matrix(seq, mseq, matrix, start, n, mlen, direction=1):
print('\t\t{}'.format('\t'.join(list(mseq))))
for i in range(n+1):
if i > 0:
base = seq[start+i*direction]
else:
base = ''
print("{}\t{}".format(base, '\t'.join(map(str, matrix[i][0:mlen+1]))))
def wrap_around_distance(base, mseq, mlen, i, matrix):
#first pass
#j = 1
if base == mseq[0]:
cost = 0
else:
cost = 1
matrix[i][1] = min(matrix[i-1][0]+cost, matrix[i-1][mlen]+cost, matrix[i-1][1]+1)
#j > 1
for j in range(2, mlen+1):
if base == mseq[j-1]:
cost = 0
else:
cost = 1
matrix[i][j] = min(matrix[i-1][j-1]+cost, matrix[i][j-1]+1, matrix[i-1][j]+1)
#second pass
#j = 1
matrix[i][1] = min(matrix[i][1], matrix[i][mlen]+1)
#j > 1
for j in range(2, mlen):
matrix[i][j] = min(matrix[i][j], matrix[i][j-1]+1)
return matrix[i][mlen] > matrix[i-1][mlen]
def wrap_around_extend(seq, mseq, mlen, matrix, start, size, max_error, direction):
current_error = 0
if size <= 0: return 0
for i in range(1, size+1):
if wrap_around_distance(seq[start+i*direction], mseq, mlen, i, matrix):
current_error += 1
else:
current_error = 0
if current_error > max_error: break
i -= current_error
return i
def wrap_around_backtrace(mlen, matrix, i):
num_mat = num_sub = num_ins = num_del = 0
j = mlen
path = []
while i > 0 or j > 0:
print(i, j)
#go back through second pass
if i > 0 and j > 0 and j < mlen:
if j == 1:
if matrix[i][j] == matrix[i][mlen] + 1:
num_del += 1
j = mlen
continue
else:
if matrix[i][j] == matrix[i][j-1] + 1:
num_del += 1
j -= 1
continue
elif i == 0:
num_del += 1
j -= 1
continue
#go back through first pass
if j == 1:
v = min(matrix[i-1][mlen], matrix[i-1][0], matrix[i-1][1])
if v == matrix[i-1][mlen]:
if v == matrix[i][j]:
num_mat += 1
else:
num_sub += 1
i -= 1
j = mlen
elif v == matrix[i-1][0]:
if v == matrix[i][j]:
num_mat += 1
else:
num_sub += 1
i -= 1
j -= 1
elif v == matrix[i-1][1]:
num_ins += 1
i -= 1
else:
v = min(matrix[i-1][j-1], matrix[i-1][j], matrix[i][j-1])
if v == matrix[i-1][j-1]:
if v == matrix[i][j]:
num_mat += 1
else:
num_sub += 1
i -= 1
j -= 1
elif v == matrix[i-1][j]:
num_ins += 1
i -= 1
elif v == matrix[i][j-1]:
num_del += 1
j -= 1
return num_mat, num_sub, num_ins, num_del, path
def atr_finder(seq, max_motif_size=6, min_seed_repeat=3, min_seed_length=10,
max_consecutive_error=3, min_identity=0.7, max_extend_length=1000):
matrix = initial_matrix(max_extend_length, max_motif_size)
size = len(seq)
atrs = []
i = 0
while i < size:
if seq[i] == 'N':
i += 1
continue
seed_start = i
for j in range(1, max_motif_size+1):
b = size - j
while i < b and seq[i] == seq[i+j]:
i += 1
seed_length = i + j - seed_start
seed_repeat = int(seed_length / j)
seed_length = seed_repeat * j
if seed_repeat >= min_seed_repeat and seed_length >= min_seed_length:
motif = seq[seed_start:seed_start+j]
#0-based end position
seed_end = seed_start + seed_length - 1
tandem_match = seed_length
tandem_substitute = 0
tandem_insert = 0
tandem_delete = 0
#extend to left
extend_start = seed_start
extend_maxlen = extend_start
if extend_maxlen > max_extend_length:
extend_maxlen = max_extend_length
extend_len = wrap_around_extend(seq, motif[::-1], j, matrix, extend_start,
extend_maxlen, max_consecutive_error, -1)
if extend_len > 0:
print("left: {} {} {}".format(extend_start, j, seed_length))
print_matrix(seq, motif[::-1], matrix, extend_start, extend_len, j, -1)
ed = wrap_around_backtrace(j, matrix, extend_len)
tandem_match += ed[0]
tandem_substitute += ed[1]
tandem_insert += ed[2]
tandem_delete += ed[3]
#path = ed[4]
#for a, b in path:
# matrix[a][b] = "{}*".format(matrix[a][b])
tandem_start = extend_start - extend_len + 1
#extend to right
extend_start = seed_end
extend_maxlen = size - extend_start - 1
if extend_maxlen > max_extend_length:
extend_maxlen = max_extend_length
extend_len = wrap_around_extend(seq, motif, j, matrix, extend_start, extend_maxlen,
max_consecutive_error, 1)
if extend_len > 0:
#print_matrix(seq, motif, matrix, extend_start, extend_len, j)
#ed = wrap_around_backtrace(j, matrix, extend_len)
tandem_match += ed[0]
tandem_substitute += ed[1]
tandem_insert += ed[2]
tandem_delete += ed[3]
#path = ed[4]
#for a, b in path:
# matrix[a][b] = "{}*".format(matrix[a][b])
#print_matrix(seq, motif, matrix, extend_start, extend_len, j)
tandem_align = tandem_match + tandem_insert + tandem_substitute + tandem_delete
tandem_identity = tandem_match / tandem_align
if tandem_identity >= min_identity:
tandem_end = extend_start + extend_len + 1
tandem_length = tandem_end - tandem_start + 1
atrs.append((motif, j, tandem_start, tandem_end, tandem_length, tandem_match,
tandem_substitute, tandem_insert, tandem_delete, tandem_identity))
i = tandem_end
break
i = seed_start
i += 1
return atrs
if __name__ == '__main__':
#s = "AAGAAGAAGAAGCCGAGAAGGTAGATAG"
#s = "ATGCATGCATGCAGGCTGC"
import pyfastx
for s in pyfastx.Fasta('../data/chr2.fa.gz'):
pass
atrs = atr_finder(s.seq)