-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy.py
75 lines (68 loc) · 1.86 KB
/
greedy.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
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()
sequences=re.split('[( )]',s)
#genome=''.join([genome, s])
else:
break
except:
break
#sequences=genome.split(' ')
del sequences[0]
del sequences[-1]
#sequences=map(int, sequences)
f.close()
#print sequences
return sequences
def GreedySorting(P,out):
for i in range(0,len(P)):
L=map(int, P)
L=map(abs, L)
#print L
if (abs(int(P[i])) != (i+1)):
pos=L.index(i+1)
tmp= P[i:pos+1]
tmp.reverse()
#print tmp
for j in range(0,len(tmp)):
if (int(tmp[j])>0):
tmp[j]=tmp[j].replace('+','-')
else:
tmp[j]=tmp[j].replace('-','+')
P[i:pos+1]=tmp
#P[pos],P[i]=P[i],P[pos]
#P.insert(i,tmp)
#del P[pos+1]
#print "(",repr(P),")"
s=' '.join(P)
s=''.join(['(',s,')'])
out.writelines (s)
out.writelines ("\n")
if (int(P[i]) != (i+1)):
#print 'lllll'
P[i]=P[i].replace('-','+')
s=' '.join(P)
s=''.join(['(',s,')'])
out.writelines (s)
out.writelines ("\n")
return P
def main():
file1=sys.argv[1]
out=open("out.txt","w")
seq=GetSequence(file1)
print seq
P=GreedySorting(seq,out)
#(Blosumprotein,Blusummatrix)=GetBlosum62(file2)
#print Blosumprotein, Blusummatrix
#(maxscore, s1,s2)=getMatrix_Alignment(protein,Blosumprotein,Blusummatrix)
#print maxscore
#print s1, "\n", s2
main()