-
Notifications
You must be signed in to change notification settings - Fork 1
/
global_local_alignment.py
171 lines (129 loc) · 4.15 KB
/
global_local_alignment.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
import sys
import numpy as np
import time
import pandas as pd
import tracemalloc
COL_MAP = {
'A': 0,
'C': 1,
'T': 2,
'G': 3,
'-': 4
}
def get_optimal_point(self, i, j):
max_score = float('-inf')
for r in range(1,self.row):
for c in range(1, self.column):
if self.path_matrix[r][c][0] >= max_score:
i = r
j = c
max_score = self.path_matrix[r][c][0]
return i, j, max_score
def get_max_score(self):
return self.path_matrix[self.row-1][self.column-1][0]
def get_sequences(self, i, j, aligned_seq1 = "", aligned_seq2 = ""):
if (self.mode == "global" and self.path_matrix[i][j][1]==0) or (self.mode == "local" and self.path_matrix[i][j][0]==0):
self.alignments.append([aligned_seq1, aligned_seq2])
return
if len(self.path_matrix[i][j][1])>1:
paths = self.path_matrix[i][j][1]
for n in range(len(paths)):
self.path_matrix[i][j][1] = paths[n]
get_sequences(self, i, j, aligned_seq1, aligned_seq2)
else:
if self.path_matrix[i][j][1] == 'D':
aligned_seq1 = self.seq1[j-1] + aligned_seq1
aligned_seq2 = self.seq2[i-1] + aligned_seq2
i = i-1
j = j-1
elif self.path_matrix[i][j][1] == 'U':
aligned_seq1 = "-" + aligned_seq1
aligned_seq2 = self.seq2[i-1] + aligned_seq2
i = i-1
else:
aligned_seq2 = "-" + aligned_seq2
aligned_seq1 = self.seq1[j-1] + aligned_seq1
j = j-1
get_sequences(self, i, j, aligned_seq1, aligned_seq2)
def get_path(self, i, j):
paths = ''
match_mismatch_score = self.score_matrix.iloc[COL_MAP[self.seq2[i-1]]][self.seq1[j-1]]
diag = self.path_matrix[i-1][j-1][0] + match_mismatch_score
up = self.path_matrix[i-1][j][0] + self.score_matrix.iloc[4]['A']
left = self.path_matrix[i][j-1][0] + self.score_matrix.iloc[0]['-']
if self.mode == "global":
self.path_matrix[i][j][0] = max(diag, up, left)
else:
self.path_matrix[i][j][0] = max(diag, up, left, 0)
if self.path_matrix[i][j][0]==left:
paths = paths + 'L'
if self.path_matrix[i][j][0]==diag:
paths = paths + 'D'
if self.path_matrix[i][j][0]==up:
paths = paths + 'U'
self.path_matrix[i][j][1] = paths
if i==self.row-1 and j==self.column-1:
if self.mode == "global":
self.max_score = get_max_score(self)
elif self.mode == "local":
i, j, self.max_score = get_optimal_point(self, i, j)
get_sequences(self, i, j)
elif j<self.column-1:
get_path(self, i ,j+1)
else:
get_path(self, i+1 ,1)
def init_path_matrix(self):
matrix = np.zeros([self.row, self.column], dtype='i,O')
if self.mode == "local":
return matrix
for i in range(1, self.column):
matrix[0][i][0] = i * self.score_matrix.iloc[0]['-']
matrix[0][i][1] = 'L'
for i in range(1, self.row):
matrix[i][0][0] = i * self.score_matrix.iloc[4]['A']
matrix[i][0][1] = 'U'
return matrix
class SequenceInit(object):
def __init__(self, seq1, seq2, score_matrix, mode):
self.seq1 = seq1
self.seq2 = seq2
self.mode = mode
self.score_matrix = score_matrix
self.column = len(self.seq1)+1
self.row = len(self.seq2)+1
self.max_score = 0
self.alignments = []
self.path_matrix = init_path_matrix(self)
tracemalloc.start()
snapshot1 = tracemalloc.take_snapshot()
get_path(self, 1, 1)
snapshot2 = tracemalloc.take_snapshot()
self.top_stats = snapshot2.compare_to(snapshot1, 'lineno')
def print_results(self, t1, t0):
print("\n\n========== Needleman–Wunsch ==========")
print(self.path_matrix)
print('Max {} alignment Score is: {}'.format(self.mode, self.max_score))
print("Total run time in seconds: ", str(round(t1-t0, 4)))
count = 1
if len(self.alignments[0][0]) == 0:
return
for alignment in self.alignments:
print("Alignmnet #", count)
print(alignment[0])
print(alignment[1])
print()
count += 1
print("Memory Usage(using `tracemalloc`, the first stats):")
for stat in self.top_stats[:1]:
print(str(stat).rsplit(":", 1)[1])
print()
def get_all_alignments(self):
return self.alignments
if __name__ == "__main__":
file = sys.argv[1]
if not file.endswith('.csv'):
raise ValueError("Only .csv is accepted for score_matrix")
t0 = time.time()
sequence = SequenceInit("AGTACGGTACGTAA", "TAGAAGTT", pd.read_csv(file), "local")
t1 = time.time()
sequence.print_results(t1, t0)