-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal_.py
149 lines (106 loc) · 2.8 KB
/
global_.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
#!/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_score(seq1, seq2):
score = 0
for i in range(len(seq1)):
if seq1[i] == seq2[i]:
print (identicalMatch, end='', sep='')
score = score + identicalMatch
else:
if seq1[i]=='-' or seq2[i]=='-':
print ('(',d,')', end='', sep='')
score = score + d
else:
print ('(',mismatch ,')', end='', sep='')
score = score + mismatch
print ('+', end='', sep='')
print("\b",end="")
print ('=', score)
def get_sequences(F, i, j, alignmented_seq1 = "", alignmented_seq2 = ""):
if F[i][j][1]==0:
print (alignmented_seq1)
print (alignmented_seq2)
get_score(alignmented_seq1, alignmented_seq2)
print ()
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 global_alignment(F, i, j):
directions = ''
value = identicalMatch
if seq2[i-1] != seq1[j-1]:
value = mismatch
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)
if F[i][j][0]==left:
directions = directions + 'L'
if F[i][j][0]==diag:
directions = directions + 'D'
if F[i][j][0]==up:
directions = directions + 'U'
F[i][j][1] = directions
if i==row-1 and j==column-1:
save_result(F)
get_sequences(F, i, j)
return
elif j<column-1:
global_alignment(F, i ,j+1)
else:
global_alignment(F, i+1 ,1)
if __name__ == "__main__":
d = int(sys.argv[1])
result_file = "resultado.txt"
identicalMatch = 1
mismatch = -1
#seq1 = "AAG"
#seq2 = "AGC"
seq1 = "ATCG"
seq2 = "TTCG"
# 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')
# Adding zeros
for i in range(1,column):
F[0][i][0] = i*d
F[0][i][1] = 'L'
for i in range(1,row):
F[i][0][0] = i*d
F[i][0][1] = 'U'
global_alignment(F, 1, 1)