-
Notifications
You must be signed in to change notification settings - Fork 1
/
align.py
233 lines (196 loc) · 7.15 KB
/
align.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
# Authors: Stefan Jokic, Adrian Taubner
import numpy as np
mismatch = 2
gap = 2.5
match = -1
def reverse_complement(sequence):
'''
Generates the reverse complement of a sequence.
:param sequence: input sequence.
:return: reverse complement of input sequence.
'''
comp = {'A' : 'T', 'C' : 'G', 'G' : 'C', 'T' : 'A'}
rev_comp = ''
for i in range(0, len(sequence)):
rev_comp = comp[sequence[i]] + rev_comp
return rev_comp
def align(query, target):
'''
Performs semi-global alignment using a variation of the Needleman-Wunsch
algorithm and computes the associated cost matrix.
:param query: read string
:param target: genome string (at certain index and range)
:return: cost matrix for alignment.
'''
M = np.zeros((len(query)+1, len(target)+1))
for i in range(0, M.shape[0]):
M[i][0] = i * gap
for i in range(1, M.shape[0]):
for j in range(1, M.shape[1]):
cost = match
if (query[i - 1] != target[j - 1]):
cost = mismatch
M[i][j] = min(M[i-1][j] + gap, M[i][j-1] + gap, M[i-1][j-1] + cost)
return M
def backtrack(M, query, sequence):
'''
Performs backtracking given alignment matrix computed by align()
to generate optimal alignment strings for given query and target string.
:param M: Cost matrix for alignment returned by align()
:param query: read string
:param sequence: genome string (at certain index and range)
:return: aligned strings
'''
endIndexI = M.shape[0] - 1
endIndexJ = np.argmin(M[endIndexI])
queryI = ''
queryJ = ''
#queryI = query[endIndexI - 1]
#queryJ = sequence[endIndexJ - 1]
while (endIndexI >= 1 and endIndexJ >= 1):
if (M[endIndexI][endIndexJ] == M[endIndexI - 1][endIndexJ] + gap):
queryI = query[endIndexI - 1] + queryI
queryJ = '-' + queryJ
endIndexI -= 1
elif (M[endIndexI][endIndexJ] == M[endIndexI][endIndexJ - 1] + gap):
queryI = '-' + queryI
queryJ = sequence[endIndexJ - 1] + queryJ
endIndexJ -= 1
else:
queryI = query[endIndexI - 1] + queryI
queryJ = sequence[endIndexJ - 1] + queryJ
endIndexI -= 1
endIndexJ -= 1
return queryI, queryJ, endIndexJ # - 1 # -1 because we added extra column/row for M
def cigar(query, target):
'''
Given aligned sequences, generate the CIGAR string.
Supports matches =, mismatches X, insertion I, deletion D and
soft clipping S.
:param M: Cost matrix for alignment returned by align()
:param query: aligned read string
:param target: aligned genome string (at certain index and range)
:return: CIGAR string for the aligned sequences
'''
cigar = ''
match_count = 0
mismatch_count = 0
insertion_count = 0
deletion_count = 0
type = -1 # 0 mismatch, 1 match, 2 insertion, 3 deletion, 4 soft clipping
posStart = 0
posEnd = len(query) - 1
while ((posStart < len(query)) & (query[posStart] == '-')):
posStart += 1
while ((posEnd >= 0) & (query[posEnd] == '-')):
posEnd -= 1
if posStart > 0:
cigar += str(posStart)
cigar += 'S'
for i in range(posStart, posEnd + 1):
if ((query[i] != target[i]) & (query[i] != '-') & (target[i] != '-')): # mismatch
if type == 0:
mismatch_count += 1
elif type != 0:
mismatch_count = 1
type = 0
if(match_count > 0):
cigar += str(match_count)
cigar += '='
elif(insertion_count > 0):
cigar += str(insertion_count)
cigar += 'I'
elif(deletion_count > 0):
cigar += str(deletion_count)
cigar += 'D'
match_count = 0
insertion_count = 0
deletion_count = 0
elif ((query[i] == target[i]) & (query[i] != '-') & (target[i] != '-')): # match
if type == 1:
match_count += 1
elif type != 1:
match_count = 1
type = 1
if(mismatch_count > 0):
cigar += str(mismatch_count)
cigar += 'X'
elif(insertion_count > 0):
cigar += str(insertion_count)
cigar += 'I'
elif(deletion_count > 0):
cigar += str(deletion_count)
cigar += 'D'
mismatch_count = 0
insertion_count = 0
deletion_count = 0
elif ((query[i] != target[i]) & (query[i] != '-') & (target[i] == '-')): # insertion
if type == 2:
insertion_count += 1
elif type != 2:
insertion_count = 1
type = 2
if(match_count > 0):
cigar += str(match_count)
cigar += '='
elif(mismatch_count > 0):
cigar += str(mismatch_count)
cigar += 'X'
elif(deletion_count > 0):
cigar += str(deletion_count)
cigar += 'D'
match_count = 0
mismatch_count = 0
deletion_count = 0
elif ((query[i] != target[i]) & (query[i] == '-') & (target[i] != '-')): # deletion
if type == 3:
deletion_count += 1
elif type != 3:
deletion_count = 1
type = 3
if(match_count > 0):
cigar += str(match_count)
cigar += '='
elif(mismatch_count > 0):
cigar += str(mismatch_count)
cigar += 'X'
elif(insertion_count > 0):
cigar += str(insertion_count)
cigar += 'I'
match_count = 0
mismatch_count = 0
insertion_count = 0
if(match_count > 0):
cigar += str(match_count)
cigar += '='
elif(mismatch_count > 0):
cigar += str(mismatch_count)
cigar += 'X'
elif(insertion_count > 0):
cigar += str(insertion_count)
cigar += 'I'
elif(deletion_count > 0):
cigar += str(deletion_count)
cigar += 'D'
if posEnd < (len(query) - 1):
cigar += str(len(query) - posEnd - 1)
cigar += 'S'
return cigar
def getCigarFromIndex(query, target):
M = align(query, target)
x, y, index = backtrack(M, query, target)
return cigar(x, y), np.min(M[M.shape[0] - 1]), index
'''
q = 'ATTCGAA'
t = 'ARUIAATCGAATTCA'
cigar, cost, index = getCigarFromIndex(q, t)
print(cigar)
print(cost)
print(index)
#print(np.min(M[M.shape[0] -1]))
#print(cigar(x, y))
#x = '-AT--TA--'
#y = 'AATCG-AGT'
#print(cigar(x,y))
#print(reverse_complement("ATCGTAAGT"))
'''