forked from sness/courses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffine-align-first-try.py
executable file
·146 lines (116 loc) · 3.76 KB
/
affine-align-first-try.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
#!/usr/bin/python
#
# Do a global alignment of two strings using the Needleman-Wunsch
# algorithm.
#
import sys
import numpy as np
import operator
inf = float("inf")
def matchScore(a,b,matchCost,mismatchCost):
"""Return the score for aligning character a with b"""
if a==b:
return matchCost
else:
return mismatchCost
# pretty print
def pp(d):
outStr = ""
for i in range(d.shape[0]):
for j in range(d.shape[1]):
outStr += "%.3f\t" % (d[i][j])
outStr += "\n"
print outStr
def doAffineAlign(x, y, matchCost, mismatchCost, openCost, extendCost):
lx = len(x)+1
ly = len(y)+1
M = np.zeros((lx,ly))
X = np.zeros((lx,ly))
Y = np.zeros((lx,ly))
for i in xrange(1, lx):
M[i][0] = -inf
X[i][0] = -inf
Y[i][0] = openCost + i * extendCost
for i in xrange(1, ly):
M[0][i] = -inf
X[0][i] = openCost + i * extendCost
Y[0][i] = -inf
for i in xrange(1, len(x)+1):
for j in xrange(1, len(y)+1):
M[i][j] = matchScore(x[i-1], y[j-1], matchCost, mismatchCost) + max(
M[i-1][j-1],
X[i-1][j-1],
Y[i-1][j-1]
)
X[i][j] = max(
M[i][j-1] + openCost + extendCost,
X[i][j-1] + extendCost,
Y[i][j-1] + openCost + extendCost
)
Y[i][j] = max(
M[i-1][j], openCost + extendCost,
X[i-1][j] + openCost + extendCost,
Y[i-1][j] + extendCost
)
pp(X)
pp(M)
pp(Y)
print "------------------------------"
# The path can now travel
# Start in table S
# If S[i,j] = GA[i,j] or GB[i,j], jump to the corresponding table, without making a move.
# Paths through GAmust move up
# Paths through GBmust move left
i = lx-1
j = ly-1
# print "i=%i\tj=%i" % (i, j)
# print "X[i][j]=%f\tM[i][j]=%f\tY[i][j]=%f" % (X[i][j], M[i][j], Y[i][j])
xStr = ""
yStr = ""
print x
print y
currArray = ""
while i > 0 and j > 0:
print "------------------------------"
print "i=%i\tj=%i" % (i, j)
arrays = {X[i][j] : "X", M[i][j] : "M", Y[i][j] : "Y"}
print arrays
maxArray = max(arrays.iteritems(), key=operator.itemgetter(0))[1]
if currArray == "":
currArray = maxArray
print "maxArray=%s\tcurrArray=%s" % (maxArray, currArray)
if maxArray == currArray:
if maxArray == "X":
xStr += "-"
yStr += y[j-1]
if maxArray == "M":
xStr += x[i-1]
yStr += y[j-1]
if maxArray == "Y":
xStr += x[i-1]
yStr += "-"
values = [X[i-1][j-1], M[i-1][j-1], Y[i-1][j-1]]
vindex = values.index(max(values))
if vindex == 0:
j = j - 1
if vindex == 1:
i = i - 1
j = j - 1
if vindex == 2:
i = i - 1
currArray = maxArray
print xStr[::-1]
print yStr[::-1]
def run(seq1, seq2, matchCost, mismatchCost, openCost, extendCost):
doAffineAlign(seq1, seq2, matchCost, mismatchCost, openCost, extendCost)
if __name__ == "__main__":
if len(sys.argv) < 6:
print "Usage: affine-align.py seq1 seq2 matchCost mismatchCost openCost extendCost"
sys.exit(1)
seq1 = sys.argv[1]
seq2 = sys.argv[2]
matchCost = float(sys.argv[3])
mismatchCost = float(sys.argv[4])
openCost = float(sys.argv[5])
extendCost = float(sys.argv[6])
run(seq1, seq2, matchCost, mismatchCost, openCost, extendCost)