-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaffine-align.py
More file actions
executable file
·162 lines (127 loc) · 4.43 KB
/
affine-align.py
File metadata and controls
executable file
·162 lines (127 loc) · 4.43 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/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 = "\t"
for j in range(d.shape[1]):
outStr += "j=%i\t" % j
outStr += "\n"
for i in range(d.shape[0]):
outStr += "i=%i\t" % i
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 j in xrange(1, ly):
M[0][j] = -inf
X[0][j] = openCost + j * extendCost
Y[0][j] = -inf
for i in xrange(1, lx):
for j in xrange(1, ly):
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
)
i = lx - 1
j = ly - 1
#
# Find matrix to start in
#
arrays = [M,X,Y]
values = [M[i][j],Y[i][j],X[i][j]]
currArray = values.index(max(values))
xStr = ""
yStr = ""
# Iterate until we are at the 0,0 element
while i + j > 0:
# M array
if currArray == 0:
values = [M[i-1][j-1],Y[i-1][j-1],X[i-1][j-1]]
maxArray = values.index(max(values))
xStr += x[i-1]
yStr += y[j-1]
i = i - 1
j = j - 1
# Y array
if currArray == 1:
values = [M[i-1][j],Y[i-1][j],X[i-1][j]]
maxArray = values.index(max(values))
xStr += x[i-1]
yStr += "-"
i = i - 1
# X array
if currArray == 2:
values = [M[i][j-1],Y[i][j-1],X[i][j-1]]
maxArray = values.index(max(values))
xStr += "-"
yStr += y[j-1]
j = j - 1
currArray = maxArray
return (xStr[::-1],yStr[::-1])
def testAlgorithm(seq1, seq2, align1, align2, matchCost, mismatchCost, openCost, extendCost):
print "Asserting that %s aligned with %s should give %s and %s as alignments" % (seq1, seq2, align1, align2)
test1,test2 = doAffineAlign(seq1, seq2, matchCost, mismatchCost, openCost, extendCost)
assert test1 == align1
assert test2 == align2
print "Passed"
print
def run(seq1, seq2, matchCost, mismatchCost, openCost, extendCost):
print "Running tests"
testAlgorithm("AAA", "AAA", "AAA", "AAA", +1, -1, -3, 0.1)
testAlgorithm("ATA", "AAA", "ATA", "AAA", +1, -1, -3, 0.1)
testAlgorithm("ATTTTT", "A", "ATTTTT", "A-----", +1, -1, -3, 0.1)
testAlgorithm("A", "ATTTTT", "A-----", "ATTTTT", +1, -1, -3, 0.1)
testAlgorithm("ACCCCG", "AG", "ACCCCG", "A----G", +1, -1, -3, 0.1)
testAlgorithm("GTA", "GCCCCCCCCA", "G-------TA", "GCCCCCCCCA", +1, -1, -3, 0.1)
testAlgorithm("GTTTTCCCCCTTTA", "GCCCCA", "GTTTTCCCCCTTTA", "G------CCCC--A", +1, -1, -3, 0.1)
print "--------------------------------------------------------------------------------"
# Do the alignment
print "Running Alignment"
a,b = doAffineAlign(seq1, seq2, matchCost, mismatchCost, openCost, extendCost)
print a
print b
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)