-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfittingtest.py
129 lines (117 loc) · 3.35 KB
/
fittingtest.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
import string
import sys
import re
def GetSequence(filename):
f=open (filename, "r")
genome=''
while True:
s=f.readline()
try:
if len(s)>0:
s=s.strip()
genome=''.join([genome, '+'])
genome=''.join([genome, s])
else:
break
except:
break
sequences=genome.split('+')
del sequences[0]
f.close()
return sequences
def GetFitting(pro):
tmp=pro[0]
s2=pro[1]
score=[0]*(len(pro[0])-len(s2)+1)
for n in range(0,len(pro[0])-len(s2)+1):
s1=tmp[n:]
rows=len(s1)
cols=len(s2)
matrix=[[0 for i in range(cols+1)] for j in range(rows+1)]
record=[[0 for i in range(cols+1)] for j in range(rows+1)]
for i in range(0,rows+1):
matrix[i][0]=-1*i
record[i][0]=1
for j in range(0,cols+1):
matrix[0][j]=-1*j
record[0][j]=2
for i in range(1,rows+1):
for j in range(1,cols+1):
m=0
if s1[i-1] ==s2[j-1]:
m=matrix[i-1][j-1]+1
if s1[i-1] != s2[j-1]:
m=matrix[i-1][j-1]-1
L=[ matrix[i-1][j]-1, matrix[i][j-1]-1,m]
matrix[i][j]=max(L)
record[i][j]=L.index(max(L))
# print matrix[i][j],
j=cols
maxcols=[]
for i in range(0,rows+1):
maxcols.append(matrix[i][j])
score[n]=max(maxcols)
maxscore=max(score)
i=score.index(maxscore)
s1=tmp[i:]
rows=len(s1)
cols=len(s2)
#print rows,cols
matrix=[[0 for i in range(cols+1)] for j in range(rows+1)]
record=[[0 for i in range(cols+1)] for j in range(rows+1)]
for i in range(0,rows+1):
matrix[i][0]=-1*i
record[i][0]=1
for j in range(0,cols+1):
matrix[0][j]=-1*j
record[0][j]=2
for i in range(1,rows+1):
for j in range(1,cols+1):
m=0
if s1[i-1] ==s2[j-1]:
m=matrix[i-1][j-1]+1
if s1[i-1] != s2[j-1]:
m=matrix[i-1][j-1]-1
L=[ matrix[i-1][j]-1, matrix[i][j-1]-1,m]
matrix[i][j]=max(L)
record[i][j]=L.index(max(L))
j=cols
maxcols=[]
for i in range(0,rows+1):
maxcols.append(matrix[i][j])
i=maxcols.index(max(maxcols))
new_s1=''
new_s2=''
while (i !=0 or j != 0):
if i==0:
while j!=0:
new_s1=''.join(['-',new_s1])
new_s2=''.join([s2[j-1],new_s2])
j=j-1
break
if j==0:
break
if record[i][j]==0:
new_s1=''.join([s1[i-1],new_s1])
new_s2=''.join(['-',new_s2])
i=i-1
elif record[i][j]==1:
new_s1=''.join(['-',new_s1])
new_s2=''.join([s2[j-1],new_s2])
j=j-1
elif (record[i][j]==2 ):
new_s1=''.join([s1[i-1],new_s1])
new_s2=''.join([s2[j-1],new_s2])
i=i-1
j=j-1
else:
print "Wrong",record[i][j]
break
return maxscore,new_s1,new_s2
def main():
file1=sys.argv[1]
protein=GetSequence(file1)
(score,pro1,pro2)=GetFitting(protein)
print score
print pro1, "\n", pro2
main()