@@ -5,7 +5,6 @@ def global_alignment(seq0, seq1):
5
5
def get_dp_table ():
6
6
dp_score_table = np .ndarray (shape = (len (seq0 ) + 1 , len (seq1 ) + 1 ), dtype = int )
7
7
dp_score_table .fill (0 )
8
- print dp_score_table
9
8
min_size = min (len (seq0 ), len (seq1 ))
10
9
11
10
def match_score (i , j ):
@@ -28,16 +27,44 @@ def transition_computation(i, j):
28
27
for row_idx in xrange (iter_num + 1 , dp_score_table .shape [0 ]):
29
28
transition_computation (row_idx , iter_num )
30
29
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
32
40
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 )
34
62
35
63
36
64
def local_alignment (seq0 , seq1 ):
37
65
def get_dp_table ():
38
66
dp_score_table = np .ndarray (shape = (len (seq0 ) + 1 , len (seq1 ) + 1 ), dtype = int )
39
67
dp_score_table .fill (0 )
40
- print dp_score_table
41
68
min_size = min (len (seq0 ), len (seq1 ))
42
69
43
70
def match_score (i , j ):
@@ -70,4 +97,5 @@ def transition_computation(i, j):
70
97
seq_str0 = 'GGTTGACTA'
71
98
seq_str1 = 'TGTTACGG'
72
99
global_alignment (seq0 = seq_str0 , seq1 = seq_str1 )
100
+ print '\n \n '
73
101
local_alignment (seq0 = seq_str0 , seq1 = seq_str1 )
0 commit comments