Skip to content

Commit e7fc764

Browse files
committed
finish global alignment
1 parent 0d31fde commit e7fc764

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

bioinformatics/dynamic_programming/smith_waterman_algo.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ def global_alignment(seq0, seq1):
55
def get_dp_table():
66
dp_score_table = np.ndarray(shape=(len(seq0) + 1, len(seq1) + 1), dtype=int)
77
dp_score_table.fill(0)
8-
print dp_score_table
98
min_size = min(len(seq0), len(seq1))
109

1110
def match_score(i, j):
@@ -28,16 +27,44 @@ def transition_computation(i, j):
2827
for row_idx in xrange(iter_num + 1, dp_score_table.shape[0]):
2928
transition_computation(row_idx, iter_num)
3029

31-
print 'global alignment table:\n', dp_score_table, '\n'
30+
return dp_score_table
31+
32+
def traceback(table):
33+
"""
34+
:type table: np.ndarray
35+
"""
36+
gap_penalty = -1
37+
38+
def match_score(i, j):
39+
return 1 if seq0[i - 1] == seq1[j - 1] else -1
3240

33-
get_dp_table()
41+
def dfs_detail(row_idx, col_idx, level):
42+
blank_str = ''.join([" "] * level)[:-1] + '|_' if level > 0 else 'root'
43+
print '%-50s' % (blank_str + str((row_idx, col_idx))), 'score at', str((row_idx, col_idx)), ':', \
44+
table[row_idx][col_idx]
45+
46+
if row_idx != 0 and col_idx != 0:
47+
# diagonal
48+
if table[row_idx - 1][col_idx - 1] + match_score(row_idx, col_idx) == table[row_idx][col_idx]:
49+
dfs_detail(row_idx - 1, col_idx - 1, level + 1)
50+
# down
51+
if table[row_idx - 1][col_idx] + gap_penalty == table[row_idx][col_idx]:
52+
dfs_detail(row_idx - 1, col_idx, level + 1)
53+
# right
54+
if table[row_idx][col_idx - 1] + gap_penalty == table[row_idx][col_idx]:
55+
dfs_detail(row_idx, col_idx - 1, level + 1)
56+
57+
dfs_detail(table.shape[0] - 1, table.shape[1] - 1, 0)
58+
59+
dp_score_table = get_dp_table()
60+
print 'global alignment table:\n', dp_score_table, '\n'
61+
traceback(dp_score_table)
3462

3563

3664
def local_alignment(seq0, seq1):
3765
def get_dp_table():
3866
dp_score_table = np.ndarray(shape=(len(seq0) + 1, len(seq1) + 1), dtype=int)
3967
dp_score_table.fill(0)
40-
print dp_score_table
4168
min_size = min(len(seq0), len(seq1))
4269

4370
def match_score(i, j):
@@ -70,4 +97,5 @@ def transition_computation(i, j):
7097
seq_str0 = 'GGTTGACTA'
7198
seq_str1 = 'TGTTACGG'
7299
global_alignment(seq0=seq_str0, seq1=seq_str1)
100+
print '\n\n'
73101
local_alignment(seq0=seq_str0, seq1=seq_str1)

0 commit comments

Comments
 (0)