-
Notifications
You must be signed in to change notification settings - Fork 1
/
local_.py
135 lines (95 loc) · 2.44 KB
/
local_.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
#!/usr/bin/python
import sys
import numpy as np
def save_result(F):
f = open(result_file, "w")
f.write(' ')
for i in range(column-1):
f.write(seq1[i]+' ')
f.write('\n')
seq2_t = ' '+ seq2
for r in range(row):
f.write(seq2_t[r])
for c in range(column):
f.write(' ( '+ '{}'.format(F[r][c][0])+' '+'{}'.format(F[r][c][1])+') ')
f.write('\n')
f.close()
f = open(result_file, "r")
print(f.read())
def get_sequences(F, i, j, alignmented_seq1 = "", alignmented_seq2 = ""):
if F[i][j][0]==0:
print ()
print (alignmented_seq1)
print (alignmented_seq2)
return
if len(F[i][j][1])>1:
directions = F[i][j][1]
for n in range(len(directions)):
F[i][j][1] = directions[n]
get_sequences(F, i, j, alignmented_seq1, alignmented_seq2)
else:
if F[i][j][1] == 'D':
alignmented_seq1 = seq1[j-1] + alignmented_seq1
alignmented_seq2 = seq2[i-1] + alignmented_seq2
i = i-1
j = j-1
elif F[i][j][1]=='U':
alignmented_seq1 = "-" + alignmented_seq2
alignmented_seq2 = seq2[i-1] + alignmented_seq2
i = i-1
else:
alignmented_seq2 = "-" + alignmented_seq2
alignmented_seq1 = seq1[j-1] + alignmented_seq1
j = j-1
get_sequences(F, i, j, alignmented_seq1, alignmented_seq2)
def local_alignment(F, i, j):
value = identicalMatch
if seq2[i-1] != seq1[j-1]:
value = mismatch
directions = ''
diag = F[i-1][j-1][0] + value
up = F[i-1][j][0] + d
left = F[i][j-1][0] + d
F[i][j][0] = max(diag, up, left, 0)
if F[i][j][0]==diag:
directions = directions + 'D'
#F[i][j][1] = 'D'
if F[i][j][0]==up:
directions = directions + 'U'
#F[i][j][1] = 'U'
if F[i][j][0]==left:
directions = directions + 'L'
#F[i][j][1] = 'L'
F[i][j][1] = directions
if i==row-1 and j==column-1:
save_result(F)
major = -1000
s_i = 0
s_j = 0
for r in range(1,row):
for c in range(1, column):
if F[r][c][0]>=major:
s_i = i
s_j = j
i = r
j = c
major = F[r][c][0]
get_sequences(F, i, j)
get_sequences(F, s_i, s_j)
return
if j<column-1:
local_alignment(F, i ,j+1)
else:
local_alignment(F, i+1 ,1)
if __name__ == "__main__":
d = int(sys.argv[1])
result_file = "resultado.txt"
identicalMatch = 1
mismatch = -1
seq1 = "TGACTGAG"
seq2 = "ATACTGGG"
# zeros column and row at the beginning of matrix F
column = len(seq1)+1
row = len(seq2)+1
F = np.zeros([row, column], dtype='i,O')
local_alignment(F, 1, 1)