-
Notifications
You must be signed in to change notification settings - Fork 0
/
SA.py
188 lines (147 loc) · 4.78 KB
/
SA.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
import numpy as np
import math
class Node():
def __init__(self, value, parent):
self.value = value
self.parent = parent
match_award = 1
mismatch_penalty = -1
open_gap_penalty = -10
extend_gap_penalty = -1
def get_max_M(row, col, S):
num1 = M[row - 1][col - 1].value + S
num2 = Ix[row - 1][col - 1].value + S
num3 = Iy[row - 1][col - 1].value + S
value = max(num1, num2, num3)
parent = []
if value == num1:
parent.append([row - 1, col - 1, 'M'])
if value == num2:
parent.append([row - 1, col - 1, 'X'])
if value == num3:
parent.append([row - 1, col - 1, 'Y'])
return Node(value, parent)
def get_max_Ix(row, col):
num1 = M[row - 1][col].value + open_gap_penalty
num2 = Ix[row - 1][col].value + extend_gap_penalty
value = max(num1, num2)
parent = []
if value == num1:
parent.append([row - 1, col, 'M'])
if value == num2:
parent.append([row - 1, col, 'X'])
return Node(value, parent)
def get_max_Iy(row, col):
num1 = M[row][col - 1].value + open_gap_penalty
num2 = Iy[row][col - 1].value + extend_gap_penalty
value = max(num1, num2)
parent = []
if value == num1:
parent.append([row, col - 1, 'M'])
if value == num2:
parent.append([row, col - 1, 'Y'])
return Node(value, parent)
s1 = input("Enter The First Sequence : ")
s2 = input("Enter The Second Sequence : ")
rows = len(s1) + 1
cols = len(s2) + 1
# init M
M = np.full((rows, cols), Node)
M[0][0] = Node(0, [])
for i in range(1, rows):
M[i][0] = Node(-math.inf, [[i - 1, 0, 'M']])
for i in range(1, cols):
M[0][i] = Node(-math.inf, [[0, i - 1, 'M']])
# init Ix
Ix = np.full((rows, cols), Node)
Ix[0][0] = Node(open_gap_penalty + (0 - 1) * extend_gap_penalty, [])
for i in range(1, rows):
Ix[i][0] = Node(open_gap_penalty + (i - 1) * extend_gap_penalty, [[i - 1, 0, 'X']])
for i in range(1, cols):
Ix[0][i] = Node(-math.inf, [[0, i - 1, 'X']])
# init Iy
Iy = np.full((rows, cols), Node)
Iy[0][0] = Node(open_gap_penalty + (0 - 1) * extend_gap_penalty, [])
for i in range(1, rows):
Iy[i][0] = Node(-math.inf, [[i - 1, 0, 'Y']])
for i in range(1, cols):
Iy[0][i] = Node(open_gap_penalty + (i - 1) * extend_gap_penalty, [[0, i - 1, 'Y']])
for row in range(1, rows):
for col in range(1, cols):
# match
if s1[row - 1] == s2[col - 1]:
S = match_award
M[row][col] = get_max_M(row, col, S)
else:
S = mismatch_penalty
M[row][col] = get_max_M(row, col, S)
Ix[row][col] = get_max_Ix(row, col)
Iy[row][col] = get_max_Iy(row, col)
last1 = M[rows - 1][cols - 1].value
last2 = Ix[rows - 1][cols - 1].value
last3 = Iy[rows - 1][cols - 1].value
start = max(last1, last2, last3)
start_nodes = []
start_node = Node
if start == last1:
start_node = M[rows - 1][cols - 1]
start_nodes.append(start_node)
if start == last2:
start_node = Ix[rows - 1][cols - 1]
start_nodes.append(start_node)
if start == last3:
start_node = Iy[rows - 1][cols - 1]
start_nodes.append(start_node)
cur_row = rows - 1
cur_col = cols - 1
score = start_node.value
def get_gapped(res):
min_gap = len(res) - 1
min_gap_idx = len(res[0]) - 1
i = 0
while i < len(res):
for j in range(len(res[0])):
if res[i][j] == '-' or res[i + 1][j] == '-':
if j <= min_gap_idx:
min_gap_idx = j
min_gap = i
i += 2
res.remove(res[min_gap])
res.remove(res[min_gap])
return res
def traceback(current_node, c1, c2, current_col, current_row):
if not current_node.parent:
return [c1, c2]
paths = []
for parent in current_node.parent:
col = parent[1]
row = parent[0]
if parent[2] == 'M':
neighbor = M[parent[0], parent[1]]
elif parent[2] == 'X':
neighbor = Ix[parent[0], parent[1]]
elif parent[2] == 'Y':
neighbor = Iy[parent[0], parent[1]]
if row == current_row:
paths += traceback(neighbor, c1 + '-', c2 + s2[col], col, row)
elif col == current_col:
paths += traceback(neighbor, c1 + s1[row], c2 + '-', col, row)
else:
paths += traceback(neighbor, c1 + s1[row], c2 + s2[col], col, row)
return paths
final = []
for start_node in start_nodes:
results = traceback(start_node, "", "", cur_col, cur_row)
for i in range(len(results)):
results[i] = results[i][::-1]
final.append(results)
final_final = []
for i in range(len(final)):
for j in range(len(final[i])):
final_final.append(final[i][j])
while True:
if len(final_final) == 2:
break
final_final = get_gapped(final_final)
print('Final Alignment is: ', final_final)
print('Final Score is: ', score)