-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalAlign.py
252 lines (242 loc) · 7.08 KB
/
globalAlign.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import time, os
from Bio.SubsMat.MatrixInfo import blosum62 as blosum62
import mechanize
import re
# init calc
def initCalc(matrix,width,high,gap):
aux = 0
for i in xrange(high):
matrix[i][0] = aux
aux -= gap
aux = 0
for i in xrange(width):
matrix[0][i] = aux
aux -= gap
# init direction
def initDirection(direction,width,high):
direction[0][0] = "\033[1;32mnone\033[1;m"
for i in xrange(1,high):
direction[i][0] = "up"
for i in xrange(1,width):
direction[0][i] = "left"
# cell/direction calc
def cellCalc(matrix,direction,width,high,seq1,seq2,opt,gap):
calc = []
for i in xrange(1,high): #high
for j in xrange(1,width): #width
if opt==1: # ALTO
pair = (seq1[j],seq2[i])
calc.append(matrix[i-1][j]-gap) # UP 0
calc.append(matrix[i-1][j-1]+scoreMatch(pair,blosum62)) # DIAGONAL 1
calc.append(matrix[i][j-1]-gap) # LEFT 2
matrix[i][j] = max(calc)
if(calc.index(max(calc)) == 0):
direction[i][j] = "up"
if (calc.index(max(calc)) == 1):
direction[i][j] = "diag"
if(calc.index(max(calc)) == 2):
direction[i][j] = "left"
calc = []
if opt==2: # BAIXO
pair = (seq1[j],seq2[i])
calc.append(matrix[i][j-1]-gap) # LEFT 0
calc.append(matrix[i-1][j-1]+scoreMatch(pair,blosum62)) # DIAGONAL 1
calc.append(matrix[i-1][j]-gap) # UP 2
matrix[i][j] = max(calc)
if(calc.index(max(calc)) == 0):
direction[i][j] = "left"
if (calc.index(max(calc)) == 1):
direction[i][j] = "diag"
if(calc.index(max(calc)) == 2):
direction[i][j] = "up"
calc = []
def scoreMatch(pair,blosum62):
if pair not in blosum62:
return blosum62[(tuple(reversed(pair)))]
else:
return blosum62[pair]
def scorePairwise(alignX,alignY,blosum62,gap):
score = 0
for i in range(len(alignX)):
pair = (alignX[i], alignY[i])
if '-' in pair:
score -= gap
else:
score += scoreMatch(pair,blosum62)
return score
def globalAlign(matrix,direction,width,high,seq1,seq2,gap):
calc = []
path = "start"
alignX = ""
alignY = ""
i = high-1
j = width-1
while 1:
if(direction[i][j] == "up"):
path +=" up"
alignX += "-"
alignY += seq2[i]
direction[i][j] = "\033[1;32mup\033[1;m"
i = i-1
elif(direction[i][j] == "left"):
path += " left"
alignX += seq1[j]
alignY += "-"
direction[i][j] = "\033[1;32mleft\033[1;m"
j = j-1
elif(direction[i][j] == "diag"):
path += " diagonal"
alignX += seq1[j]
alignY += seq2[i]
direction[i][j] = "\033[1;32mdiag\033[1;m"
i = i-1
j = j-1
elif(i==0 or j==0):
break
print
alignX = alignX[::-1]
alignY = alignY[::-1]
lstX = [alignX[i:i+50] for i in range(0, len(alignX), 50)]
lstY = [alignY[i:i+50] for i in range(0, len(alignY), 50)]
score = scorePairwise(alignX, alignY, blosum62, gap)
print
print "Matrix: BLOSUM62"
print "Gap: "+str(gap)
print "Length: "+str(len(alignX))
print "Score: "+str(score)
print
for (lX,lY) in zip(lstX,lstY):
print "\t\t"+lX
print "\t\t"+lY
print
print
def needleAlign():
print "\t\t\t"+"--------------------"
print "\t\t\t"+"EBI GLOBAL ALIGNMENT"
print "\t\t\t"+"--------------------"
file1 = "protein1"
file2 = "protein2"
url = "http://www.ebi.ac.uk/Tools/services/web_emboss_needle/toolform.ebi"
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.open(url)
br.select_form(predicate=lambda f: f.attrs.get('id', None) == 'jd_toolSubmissionForm')
br.set_all_readonly(False)
br.form.add_file(open(file1), 'text/plain', file1, name='aupfile')
br.form.add_file(open(file2), 'text/plain', file2, name='bupfile')
response = br.submit()
content = response.read()
with open("result", "w") as f:
f.write(content)
textfile = open("result", 'r')
filetext = textfile.read()
textfile.close()
lst = re.findall("(emboss_needle-(\w*\d*)-(\w*\d*)-(\w*\d*)-(\w*\d*)-(\w{2}))", filetext)
jobID = str(lst[0][0])
os.remove("result")
time.sleep(8)
url2 = "http://www.ebi.ac.uk/Tools/services/rest/emboss_needle/result/"+jobID+"/aln"
br.retrieve(url2,'data')[0]
with open("data", 'r') as fileIn:
print fileIn.read()
os.remove("data")
def stretcherAlign():
print "\t\t\t"+"--------------------"
print "\t\t\t"+"EBI GLOBAL ALIGNMENT"
print "\t\t\t"+"--------------------"
file1 = "protein1"
file2 = "protein2"
url = "http://www.ebi.ac.uk/Tools/services/web_emboss_stretcher/toolform.ebi"
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.open(url)
br.select_form(predicate=lambda f: f.attrs.get('id', None) == 'jd_toolSubmissionForm')
br.set_all_readonly(False)
br.form.add_file(open(file1), 'text/plain', file1, name='aupfile')
br.form.add_file(open(file2), 'text/plain', file2, name='bupfile')
response = br.submit()
content = response.read()
with open("result", "w") as f:
f.write(content)
textfile = open("result", 'r')
filetext = textfile.read()
textfile.close()
lst = re.findall("(emboss_stretcher-(\w*\d*)-(\w*\d*)-(\w*\d*)-(\w*\d*)-(\w{2}))", filetext)
jobID = str(lst[0][0])
os.remove("result")
time.sleep(5)
url2 = "http://www.ebi.ac.uk/Tools/services/rest/emboss_stretcher/result/"+jobID+"/aln"
br.retrieve(url2,'data')[0]
with open("data", 'r') as fileIn:
print fileIn.read()
os.remove("data")
def main():
start = time.clock()
print "\t\t\t"+"----------------"
print "\t\t\t"+"GLOBAL ALIGNMENT"
print "\t\t\t"+"----------------"
seq1 = '-'
seq2 = '-'
gapOpen = 0
gapExtend = 0
gap = 0
try:
with open("protein1", "r") as f1:
seq1 += f1.read()
with open("protein2", "r") as f2:
seq2 += f2.read()
except IOError:
print "File doesn't exist !"
opt = input("Highroad = 1 or Lowroad = 2: ")
while 1:
if opt==1 or opt==2:
break
else:
opt = input("Wrong choice, please try again: ")
print
print "1 - EMBOSS Needle\n2 - EMBOSS Stretcher"
opt = input("Choose an EBI Alignment tool: ")
while 1:
if opt == 1:
gapOpen = 10
gapExtend = 0.5
gap = round((gapOpen*1+gapExtend*6)/7,0)+1
width = len(seq1)
high = len(seq2)
matrix = [[0 for x in xrange(width)] for x in xrange(high)]
direction = [[" " for x in xrange(width)] for x in xrange(high)]
# INIT & CALC
initCalc(matrix,width,high,gap)
initDirection(direction,width,high)
cellCalc(matrix,direction,width,high,seq1,seq2,opt,gap)
# GLOBAL ALIGN
globalAlign(matrix,direction,width,high,seq1,seq2,gap)
# EMBOSS NEEDLE
needleAlign()
end = time.clock()
print "Program Running Time: "+str(abs(start-end))+" seconds"
print
return
elif opt == 2:
gapOpen = 12
gapExtend = 2
gap = round((gapOpen*1+gapExtend*6)/7,0)+1
width = len(seq1)
high = len(seq2)
matrix = [[0 for x in xrange(width)] for x in xrange(high)]
direction = [[" " for x in xrange(width)] for x in xrange(high)]
# INIT & CALC
initCalc(matrix,width,high,gap)
initDirection(direction,width,high)
cellCalc(matrix,direction,width,high,seq1,seq2,opt,gap)
# GLOBAL ALIGN
globalAlign(matrix,direction,width,high,seq1,seq2,gap)
# EMBOSS STRETCHER
stretcherAlign()
end = time.clock()
print "Program Running Time: "+str(abs(start-end))+" seconds"
print
return
else:
print "Choose an EBI Alignment tool: ",
opt = input()