coding utf-8 from numpy import __author__ denis matrix 10 -1 -3 -4 -1

 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
# coding=utf-8
from numpy import *
__author__ = 'denis'
matrix = [
[10, -1, -3, -4],
[-1, 7, -5, 3],
[-3, -5, 9, 0],
[-4, -3, 0, 1]
]
d = -5
def main():
s1 = 'AGACTAGTTAC'
s2 = 'CGA---GACGT'
alphabet = ['A', 'G', 'C', 'T']
d = 5
start_smoothing(alphabet, s1, s2)
def start_smoothing(alphabet, s1, s2):
f = arange(len(s1) * len(s2)).reshape(len(s1), len(s2))
for i in range(0, len(s1)):
for j in range(0, len(s2)):
f[i][j] = 0
for i in range(0, len(s1)):
f[i][0] = d * i
for j in range(0, len(s2)):
f[0][j] = d * j
for i in range(1, len(s1)):
for j in range(1, len(s2)):
if s1[i] is '-' or s2[j] is '-':
f[i][j] = max([f[i-1][j] + d,
f[i][j-1] + d]
)
else:
f[i][j] = max([f[i-1][j-1] + matrix[alphabet.index(s1[i])][alphabet.index(s2[j])],
f[i-1][j] + d,
f[i][j-1] + d]
)
do(s1, s2, f, alphabet)
def do(s1, s2, f, alphabet):
alignment_s1 = ''
alignment_s2 = ''
i = len(s1) - 1
j = len(s2) - 1
while i > 0 and j > 0:
score = f[i][j]
score_diag = f[i-1][j-1]
score_up = f[i][j-1]
score_left = f[i-1][j]
if s1[i] is not '-' and s2[j] is not '-' and score == score_diag + matrix[alphabet.index(s1[i])][alphabet.index(s2[j])]:
alignment_s1 = s1[i] + alignment_s1
alignment_s2 = s2[j] + alignment_s2
i -= 1
j -= 1
elif score == score_left + d:
alignment_s1 = s1[i] + alignment_s1
alignment_s2 = "-" + alignment_s2
i -= 1
elif score == score_up + d:
alignment_s1 = "-" + alignment_s1
alignment_s2 = s2[j] + alignment_s2
j -= 1
while i > 0:
alignment_s1 = s1[i] + alignment_s1
alignment_s2 = "-" + alignment_s2
i -= 1
while j > 0:
alignment_s1 = "-" + alignment_s1
alignment_s2 = s2[j] + alignment_s2
j -= 1
pass
if __name__ == '__main__':
main()