-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseq_alignment.py
More file actions
216 lines (172 loc) · 8.12 KB
/
seq_alignment.py
File metadata and controls
216 lines (172 loc) · 8.12 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 15 02:21:26 2020
@author: tomachache
"""
import numpy as np
# Adaptation of Needleman-Wunsch algorithm, which computes global alignment
# between sequences, with custom gaps length and associated costs.
# Each gap length has costs corresponding to opening and extending the gap.
# E.g. : {1 : (4,2), 3 : (5,3)} means we can have gaps of length 1, for which the costs
# for opening and extending such a gap are 4 and 2 respectively. We can also have gaps
# of length 3, for which the costs for opening and extending are 5 and 3 respectively.
def cost(x, y, gaps, match_cost = -1, mismatch_cost = 1):
# Compute the minimimum cost of aligning x and y,
# and return a dict containing the path leading to the alignment
# gaps : dict/list/array containing gaps and costs
n = len(x)
m = len(y)
S = np.zeros((n+1, m+1))
D = np.zeros((n+1, m+1), dtype = bool) # Deletions (D[i][j] = True <=> currently in a deletion gap)
I = np.zeros((n+1, m+1), dtype = bool) # Insertions
bp = {} # dictionnary of backpointers (in order to retrieve the path)
# bp[(i,j)] = (p,q) means the path goes from cell (p,q) to cell (i,j)
if type(gaps) == dict: # convert the dict into an array
array_cost = np.array([(k,) + gaps[k] for k in gaps.keys()])
else:
array_cost = np.array(gaps)
nb_gap = array_cost.shape[0]
max_gap_length = np.max(array_cost[:,0])
### INITIALISATION ###
for i in range(1, n+1): # initialize first column
if i <= max_gap_length:
costs_del = D[np.maximum(i - array_cost[:,0], 0), 0] * array_cost[:,2] \
+ (1 - D[np.maximum(i - array_cost[:,0], 0), 0]) * array_cost[:,1]
# if D is true, we are extending a gap, otherwise we are opening it
# the max is here to prevent having to separately handle the cases
# where i is smaller than the gap we considered, as such a gap would be ill-defined
L = S[np.maximum(i - array_cost[:,0], 0), 0] + costs_del \
- np.minimum(i - array_cost[:,0], 0) * (2 * costs_del + 1)
# the "2 * current_cost + 1" ensures that the min won't be the ill-defined gaps
S[i,0] = np.min(L)
D[i,0] = True
bp[i,0] = (i - array_cost[np.argmin(L), 0], 0)
else: # in this case we are always extending, so no need for current_cost or the max
L = S[i - array_cost[:,0], 0] + array_cost[:,2]
S[i,0] = np.min(L)
D[i,0] = True
bp[i,0] = (i - array_cost[np.argmin(L), 0], 0)
for j in range(1, m+1): # initialize first row
if j <= max_gap_length:
costs_ins = I[0, np.maximum(j - array_cost[:,0], 0)] * array_cost[:,2] \
+ (1 - I[0, np.maximum(j - array_cost[:,0], 0)]) * array_cost[:,1]
L = S[0, np.maximum(j - array_cost[:,0], 0)] + costs_ins \
- np.minimum(j - array_cost[:,0], 0) * (2 * costs_ins + 1)
S[0,j] = np.min(L)
I[0,j] = True
bp[0,j] = (0, j - array_cost[np.argmin(L), 0])
else:
L = S[0, j - array_cost[:,0]] + array_cost[:,2]
S[0,j] = np.min(L)
I[0,j] = True
bp[0,j] = (0, j - array_cost[np.argmin(L), 0])
### CORE OF THE ALGORITHM ###
for i in range(1, n+1):
for j in range(1, m+1):
delta = match_cost * (x[i-1] == y[j-1]) + mismatch_cost * (x[i-1] != y[j-1])
# delta is match or mismatch cost
costs_del = D[np.maximum(i - array_cost[:,0], 0), j] * array_cost[:,2] \
+ (1 - D[np.maximum(i - array_cost[:,0], 0), j]) * array_cost[:,1]
L_del = S[np.maximum(i - array_cost[:,0], 0), j] + costs_del \
- np.minimum(i - array_cost[:,0], 0) * (2 * costs_del + 1)
costs_ins = I[i, np.maximum(j - array_cost[:,0], 0)] * array_cost[:,2] \
+ (1 - I[i, np.maximum(j - array_cost[:,0], 0)]) * array_cost[:,1]
L_ins = S[i, np.maximum(j - array_cost[:,0], 0)] + costs_ins \
- np.minimum(j - array_cost[:,0], 0) * (2 * costs_ins + 1)
L = np.concatenate(([S[i-1][j-1] + delta], L_del, L_ins))
S[i][j] = np.min(L) # compute new value
index = np.argmin(L) # save index
if index == 0: # means the best alignment at position (i,j) is match/mismatch
bp[i,j] = (i-1, j-1)
elif index > 0 and index <= nb_gap : # means best alignment is a deletion
D[i,j] = True
bp[i,j] = (i - array_cost[index - 1, 0], j)
else: # means best alignment is an insertion
I[i,j] = True
bp[i,j] = (i, j - array_cost[index - nb_gap - 1, 0])
return S[n][m], bp # return cost and dict of backpointers
def backtracking_seq(bp, n, m):
# Unwind the path from the backpointers dict
i = n
j = m
seq = []
while (i,j) != (0,0):
(i_new, j_new) = bp[i,j] # unroll the backpointers dictionnary
if i_new == i-1:
if j_new == j-1:
seq += ['M'] # we did a match/mismatch
else: # then j_new = j
seq += ['D'] # we did a deletion
elif i_new == i:
if j_new == j-1:
seq += ['I'] # we did an insertion
else: # then j_new = j-3
seq += ['I', 'I', 'I'] # we did 3 insertions
else: # only 1 case left (i_new = i - 3 and j_new = j)
seq += ['D', 'D', 'D'] # we did 3 deletions
i = i_new
j = j_new
seq.reverse() # reverse the list to have the path from (0,0) --> (n,m)
return seq
def print_alignment(x, y, gaps):
# Print alignment and cost
c, bp = cost(x, y, gaps)
n = len(x)
m = len(y)
s = backtracking_seq(bp, n, m)
x_new = ''
y_new = ''
trans = '' # will be used for a nice rendering (show '|' when nucleotides match)
q = len(s)
ind_x = 0 # counter
ind_y = 0 # counter
for k in range(q):
if s[k] == 'I': # i.e. we did an insertion in x
x_new += '-'
y_new += y[ind_y]
trans += ' '
ind_y += 1
elif s[k] == 'D': # i.e. we did an insertion in y (i.e. a deletion in x)
x_new += x[ind_x]
y_new += '-'
trans += ' '
ind_x += 1
else: # i.e. we did a match/mismatch
x_new += x[ind_x]
y_new += y[ind_y]
if x[ind_x] == y[ind_y]: # i.e. we have a match
trans += '|'
else :
trans += ' '
ind_x += 1
ind_y += 1
# Now we can print the sequences (in a nice way)
z = q//50
r = q % 50
for k in range(z):
print("Seq 1 (part {}): ".format(k+1) + x_new[50*k : 50*(k+1)])
print(" " + trans[50*k : 50*(k+1)])
print("Seq 2 (part {}): ".format(k+1) + y_new[50*k : 50*(k+1)])
print("\n")
if r > 0:
print("Seq 1 (part {}): ".format(z+1) + x_new[50*z:])
print(" " + trans[50*z:])
print("Seq 2 (part {}): ".format(z+1) + y_new[50*z:])
print("\n")
print("Score : {}.".format(int(c)))
return
def generate_random_seq(n, A = ['A', 'C', 'G', 'T']):
# Generate random sequence of length n for alphabet A
return ''.join(np.random.choice(A, n))
# Some examples
if __name__ == "__main__":
gaps = {1 : (4,2), 3 : (5,3)}
# compare coding sequences of Covid-19 and a previous Coronavirus
s = 'ATGTTGCTTTTCAAACTGTCAAACCCGGTAATTTTAACAAAGACTTCTATGACTTTGCTGTGTCTAAGGG'\
'TTTCTTTAAGGAAGGAAGTTCTGTTGAATTAA'
s_old = 'ATGTTGCTTTTCAAACTGTCAAACCCGGTAATTTTAATAAAGACTTTTATGACTTTGCTGTGTCTAA'\
'AGGTTTCTTTAAGGAAGGAAGTTCTGTTGAACTAA'
print_alignment(s, s_old, gaps)
# compare alignment of random sequences
print_alignment(generate_random_seq(50), generate_random_seq(40), gaps)