-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalignGap.py
128 lines (93 loc) · 2.96 KB
/
alignGap.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
import sys
def align(v,w,scoreMatrix2,sig,epsi):
matrixSol = [[[0 for i in range(len(w)+1)] for j in range(len(v)+1)] for x in range(3)]
back = [[[0 for i in range(len(w)+1)] for j in range(len(v)+1)] for x in range(3)]
for i in range(1,len(v) +1):
matrixSol[0][i][0] = -sig - (i-1)*epsi
matrixSol[1][i][0] = -sig - (i-1)*epsi
matrixSol[2][i][0] = -sig - (i-1)*epsi
for j in range(1,len(w)+1):
matrixSol[0][0][j] = -sig - (j-1)*epsi
matrixSol[1][0][j] = -sig - (j-1)*epsi
matrixSol[2][0][j] = -sig - (j-1)*epsi
for i in range(0,len(v)+1):
back[1][i][0] = 1
for j in range(0,len(w)+1):
back[1][0][j] = 1
for i in range(0,len(v)+1):
back[2][i][0] = 2
for j in range(0,len(w)+1):
back[2][0][j] = 2
for i in range(1,len(v)+1):
for j in range(1,len(w)+1):
#lower
score = [matrixSol[0][i-1][j] - epsi, matrixSol[1][i-1][j]-sig]
matrixSol[0][i][j] = max(score)
back[0][i][j] = score.index(matrixSol[0][i][j])
#upper
score = [-float("inf"), matrixSol[1][i][j-1]-sig,matrixSol[2][i][j-1] - epsi]
matrixSol[2][i][j] = max(score)
back[2][i][j] = score.index(matrixSol[2][i][j])
#middle
index = alphatbet.index(w[j-1])
score = [matrixSol[0][i][j], matrixSol[1][i-1][j-1] + int(scoreMatrix2[v[i-1]][index+1]),matrixSol[2][i][j]]
matrixSol[1][i][j] = max(score)
back[1][i][j] = score.index(matrixSol[1][i][j])
bestScoreFromMatrix = [matrixSol[0][i][j],matrixSol[1][i][j], matrixSol[2][i][j]]
score = max(bestScoreFromMatrix)
backIndex = bestScoreFromMatrix.index(score)
print score
outputAlignment(back,v,w,len(v),len(w),backIndex)
return 0
def outputAlignment(backTrack, v,w, i ,j, nextLevel):
if i == 0 or j == 0:
print v
print w
return 0
if nextLevel == 0:
#print "lower"
if backTrack[0][i][j] == 1:
nextLevel = 1
w = w[:j] + '-' + w[j:]
outputAlignment(backTrack,v,w,i-1,j,nextLevel)
elif nextLevel == 2:
#print "upper"
if backTrack[2][i][j] == 1:
nextLevel = 1
v = v[:i] + '-' + v[i:]
outputAlignment(backTrack,v,w,i,j-1,nextLevel)
elif nextLevel == 1:
#print "middle"
if backTrack[1][i-1][j-1] == 0:
nextLevel = 0
elif backTrack[1][i-1][j-1] == 2:
nextLevel = 2
outputAlignment(backTrack,v,w,i-1,j-1,nextLevel)
#blosum file read
filename = sys.argv[1]
file = open(filename, 'r')
data = file.read().splitlines()
array = []
alphatbet = ['A','C','D','E', 'F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y']
for line in data:
array.append(line.split(" "))
scoreMatrix = dict()
for i in range(1,len(array)):
letter = array[i][0]
for element in array[i]:
if element == "":
continue
if letter not in scoreMatrix.keys():
scoreMatrix[letter] = [element]
else:
scoreMatrix[letter].append(element)
#Reads 2 strings from file
filename = sys.argv[2]
file = open(filename, 'r')
data = file.read().splitlines()
array2 = []
for line in data:
array2.append(line.split(" "))
string1 = array2[0][0]
string2 = array2[1][0]
align(string1,string2,scoreMatrix, 11, 1)