-
Notifications
You must be signed in to change notification settings - Fork 0
/
align.py
138 lines (117 loc) · 4 KB
/
align.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
import sys
import numpy as np
def globalAlign(S, T, matchScore, missmatchScore, B):
match = np.empty([len(S)+1, len(T)+1])
deletionT = np.empty([len(S)+1, len(T)+1])
deletionS = np.empty([len(S)+1, len(T)+1])
#first dimension = match, second = deletionT, third = deletionS
pointerArray = {}
match[0][0] = 0
deletionT[0][0] = 0
deletionS[0][0] = 0
deletionT[1][0] = B
deletionS[0][1] = B
for i in range(1,len(S)+1):
match[i][0] = float('-inf')
deletionS[i][0] = float('-inf')
if i != 1:
deletionT[i][0] = float('-inf')
for i in range(1,len(T)+1):
match[0][i] = float('-inf')
deletionT[0][i] = float('-inf')
if i != 1:
deletionS[0][i] = float('-inf')
pointerArray[(0,1,1)] = (0,0,0)
pointerArray[(1,0,2)] = (0,0,0)
pointerArray[(0,0,0)] = (-1,-1,-1)
for i in range(1,len(S)+1):
for j in range(1,len(T)+1):
#updating the match matrix
[maximum, index] = maxPlusIndex(match[i-1][j-1], deletionS[i-1][j-1], deletionT[i-1][j-1])
match[i][j] = compare(S[i-1], T[j-1], matchScore, missmatchScore) + maximum
if index == 0:
pointerArray[(i,j,0)] = (i-1, j-1, 0)
elif index == 1:
pointerArray[(i,j,0)] = (i-1, j-1, 1)
else:
pointerArray[(i,j,0)] = (i-1,j-1,2)
#updating the deletionT matrix
#if deletionT[i-1][j] != float("-inf"):
# deletionTScore = float("-inf")
#else:
# deletionTScore = del
[maximum, index] = maxPlusIndex(match[i-1][j] - 1, deletionS[i-1][j] - 1)
deletionT[i][j] = maximum
if index == 0:
pointerArray[(i,j,2)] = (i-1, j, 0)
else:
pointerArray[(i,j,2)] = (i-1, j, 1)
#updating the deletionS matrix
#if deletionS[i][j-1] != float("-inf"):
# deletionS = float("-inf")
#else:
[maximum, index] = maxPlusIndex(match[i][j-1] - 1, deletionT[i][j-1] - 1)
deletionS[i][j] = maximum
if index == 0:
pointerArray[(i,j,1)] = (i, j-1, 0)
else:
pointerArray[(i,j,1)] = (i, j-1, 2)
#backtracking
[score, index] = maxPlusIndex(match[-1][-1], deletionT[-1][-1], deletionS[-1][-1])
alignment = ['','']
end = pointerArray[(len(S),len(T),index)]
sCur = len(S)
tCur = len(T)
while (end != (-1,-1,-1)):
positionS = end[0]
positionT = end[1]
diffS = sCur - positionS
diffT = tCur - positionT
sCur = positionS
tCur = positionT
end = pointerArray[end]
if (diffS == 1 and diffT == 0):
print("yeet")
alignment[0] = S[sCur] + alignment[0]
alignment[1] = '-' + alignment[1]
elif (diffT == 1 and diffS == 0):
alignment[0] = '-' + alignment[0]
alignment[1] = T[tCur] + alignment[1]
else:
alignment[0] = S[sCur] + alignment[0]
alignment[1] = T[tCur] + alignment[1]
return [score, alignment]
def compare(S, T, matchScore, missmatchScore):
if (S == T):
return matchScore
else:
return missmatchScore
def maxPlusIndex(num1, num2, num3=float("-inf")):
maximum = max(num1,num2, num3)
if maximum == num1:
return [maximum, 0]
if maximum == num2:
return [maximum, 1]
if maximum == num3:
return [maximum, 2]
def main():
f = open(sys.argv[1],"r")
S = ''
T = ''
flag = 0
for x in f:
if(x[0] == '>'):
continue
if(x[0] == '\n'):
flag = 1
continue
if(flag == 0 ):
S += x.strip()
else:
T += x.strip()
f.close()
[score, alignment] = globalAlign(S, T, 1, -1, -1)
print (score)
print (alignment)
if __name__ == '__main__':
main()