forked from biopython/biopython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_Muscle_tool.py
364 lines (342 loc) · 16.2 KB
/
test_Muscle_tool.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Copyright 2009 by Peter Cock. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
import os
import sys
import subprocess
import unittest
from Bio import MissingExternalDependencyError
from Bio.Align.Applications import MuscleCommandline
from Bio import SeqIO
from Bio import AlignIO
#################################################################
#Try to avoid problems when the OS is in another language
os.environ['LANG'] = 'C'
muscle_exe = None
if sys.platform=="win32":
try:
#This can vary depending on the Windows language.
prog_files = os.environ["PROGRAMFILES"]
except KeyError:
prog_files = r"C:\Program Files"
#For Windows, MUSCLE just comes as a zip file which contains the
#a Muscle directory with the muscle.exe file plus a readme etc,
#which the user could put anywhere. We'll try a few sensible
#locations under Program Files... and then the full path.
likely_dirs = ["", #Current dir
prog_files,
os.path.join(prog_files,"Muscle3.6"),
os.path.join(prog_files,"Muscle3.7"),
os.path.join(prog_files,"Muscle3.8"),
os.path.join(prog_files,"Muscle3.9"),
os.path.join(prog_files,"Muscle")] + sys.path
for folder in likely_dirs:
if os.path.isdir(folder):
if os.path.isfile(os.path.join(folder, "muscle.exe")):
muscle_exe = os.path.join(folder, "muscle.exe")
break
if muscle_exe : break
else:
import commands
output = commands.getoutput("muscle -version")
#Since "not found" may be in another language, try and be sure this is
#really the MUSCLE tool's output
if "not found" not in output and "MUSCLE" in output \
and "Edgar" in output:
muscle_exe = "muscle"
if not muscle_exe:
raise MissingExternalDependencyError(\
"Install MUSCLE if you want to use the Bio.Align.Applications wrapper.")
#################################################################
class MuscleApplication(unittest.TestCase):
def setUp(self):
self.infile1 = "Fasta/f002"
self.infile2 = "Fasta/fa01"
self.infile3 = "Fasta/f001"
self.outfile1 = "Fasta/temp align out1.fa" #with spaces!
self.outfile2 = "Fasta/temp_align_out2.fa"
self.outfile3 = "Fasta/temp_align_out3.fa"
self.outfile4 = "Fasta/temp_align_out4.fa"
def tearDown(self):
if os.path.isfile(self.outfile1):
os.remove(self.outfile1)
if os.path.isfile(self.outfile2):
os.remove(self.outfile2)
if os.path.isfile(self.outfile3):
os.remove(self.outfile3)
if os.path.isfile(self.outfile4):
os.remove(self.outfile4)
def test_Muscle_simple(self):
"""Simple round-trip through app just infile and outfile"""
cmdline = MuscleCommandline(muscle_exe,
input=self.infile1,
out=self.outfile1)
self.assertEqual(str(cmdline), muscle_exe \
+ ' -in Fasta/f002 -out "Fasta/temp align out1.fa"')
self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
output, error = cmdline()
self.assertEqual(output, "")
self.assertTrue("ERROR" not in error)
def test_Muscle_with_options(self):
"""Round-trip through app with a switch and valued option"""
cmdline = MuscleCommandline(muscle_exe)
cmdline.set_parameter("input", self.infile1) #"input" is alias for "in"
cmdline.set_parameter("out", self.outfile2)
#Use property:
cmdline.objscore = "sp"
cmdline.noanchors = True
self.assertEqual(str(cmdline), muscle_exe +\
" -in Fasta/f002" + \
" -out Fasta/temp_align_out2.fa" + \
" -objscore sp -noanchors")
self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
output, error = cmdline()
self.assertEqual(output, "")
self.assertTrue("ERROR" not in error)
self.assertTrue(error.strip().startswith("MUSCLE"), output)
def test_Muscle_profile_simple(self):
"""Simple round-trip through app doing a profile alignment"""
cmdline = MuscleCommandline(muscle_exe)
cmdline.set_parameter("out", self.outfile3)
cmdline.set_parameter("profile", True)
cmdline.set_parameter("in1", self.infile2)
cmdline.set_parameter("in2", self.infile3)
self.assertEqual(str(cmdline), muscle_exe + \
" -out Fasta/temp_align_out3.fa" + \
" -profile -in1 Fasta/fa01 -in2 Fasta/f001")
self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
output, error = cmdline()
self.assertEqual(output, "")
self.assertTrue("ERROR" not in error)
self.assertTrue(error.strip().startswith("MUSCLE"), output)
def test_Muscle_profile_with_options(self):
"""Profile alignment, and switch and valued options"""
#Using some keyword arguments, note -stable isn't supported in v3.8
cmdline = MuscleCommandline(muscle_exe, out=self.outfile4,
in1=self.infile2, in2=self.infile3,
profile=True, stable=True,
cluster1="neighborjoining")
self.assertEqual(str(cmdline), muscle_exe + \
" -out Fasta/temp_align_out4.fa" + \
" -profile -in1 Fasta/fa01 -in2 Fasta/f001" + \
" -cluster1 neighborjoining -stable")
self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
"""
#TODO - Why doesn't this work with MUSCLE 3.6 on the Mac?
#It may be another bug fixed in MUSCLE 3.7 ...
result, stdout, stderr = generic_run(cmdline)
#NOTE: generic_run has been removed from Biopython
self.assertEqual(result.return_code, 0)
self.assertEqual(stdout.read(), "")
self.assertTrue("ERROR" not in stderr.read())
self.assertEqual(str(result._cl), str(cmdline))
"""
class SimpleAlignTest(unittest.TestCase):
"""Simple MUSCLE tests"""
"""
#FASTA output seems broken on Muscle 3.6 (on the Mac).
def test_simple_fasta(self):
input_file = "Fasta/f002"
self.assertTrue(os.path.isfile(input_file))
records = list(SeqIO.parse(input_file,"fasta"))
#Prepare the command...
cmdline = MuscleCommandline(muscle_exe)
cmdline.set_parameter("in", input_file)
#Preserve input record order (makes checking output easier)
cmdline.set_parameter("stable")
#Set some others options just to test them
cmdline.set_parameter("maxiters", 2)
self.assertEqual(str(cmdline).rstrip(), "muscle -in Fasta/f002 -maxiters 2 -stable")
result, out_handle, err_handle = generic_run(cmdline)
#NOTE: generic_run has been removed from Biopython
print err_handle.read()
print out_handle.read()
align = AlignIO.read(out_handle, "fasta")
self.assertEqual(len(records),len(align))
for old, new in zip(records, align):
self.assertEqual(old.id, new.id)
self.assertEqual(str(new.seq).replace("-",""), str(old.seq))
"""
def test_simple_clustal(self):
"""Simple muscle call using Clustal output with a MUSCLE header"""
input_file = "Fasta/f002"
self.assertTrue(os.path.isfile(input_file))
records = list(SeqIO.parse(input_file,"fasta"))
records.sort(key = lambda rec: rec.id)
#Prepare the command... use Clustal output (with a MUSCLE header)
cmdline = MuscleCommandline(muscle_exe, input=input_file, clw = True)
self.assertEqual(str(cmdline).rstrip(), muscle_exe + \
" -in Fasta/f002 -clw")
self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
child = subprocess.Popen(str(cmdline),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform!="win32"))
#Didn't use -quiet so there should be progress reports on stderr,
align = AlignIO.read(child.stdout, "clustal")
align.sort() #by record.id
self.assertTrue(child.stderr.read().strip().startswith("MUSCLE"))
return_code = child.wait()
self.assertEqual(return_code, 0)
child.stdout.close()
child.stderr.close()
del child
self.assertEqual(len(records),len(align))
for old, new in zip(records, align):
self.assertEqual(old.id, new.id)
self.assertEqual(str(new.seq).replace("-",""), str(old.seq))
def test_simple_clustal_strict(self):
"""Simple muscle call using strict Clustal output"""
input_file = "Fasta/f002"
self.assertTrue(os.path.isfile(input_file))
records = list(SeqIO.parse(input_file,"fasta"))
records.sort(key = lambda rec: rec.id)
#Prepare the command...
cmdline = MuscleCommandline(muscle_exe)
cmdline.set_parameter("in", input_file)
#Use clustal output (with a CLUSTAL header)
cmdline.set_parameter("clwstrict", True) #Default None treated as False!
self.assertEqual(str(cmdline).rstrip(), muscle_exe + \
" -in Fasta/f002 -clwstrict")
self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
child = subprocess.Popen(str(cmdline),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform!="win32"))
#Didn't use -quiet so there should be progress reports on stderr,
align = AlignIO.read(child.stdout, "clustal")
align.sort()
self.assertTrue(child.stderr.read().strip().startswith("MUSCLE"))
self.assertEqual(len(records),len(align))
for old, new in zip(records, align):
self.assertEqual(old.id, new.id)
self.assertEqual(str(new.seq).replace("-",""), str(old.seq))
return_code = child.wait()
self.assertEqual(return_code, 0)
child.stdout.close()
child.stderr.close()
del child
def test_long(self):
"""Simple muscle call using long file"""
#Create a large input file by converting some of another example file
temp_large_fasta_file = "temp_cw_prot.fasta"
records = list(SeqIO.parse("NBRF/Cw_prot.pir", "pir"))[:40]
SeqIO.write(records, temp_large_fasta_file, "fasta")
#Prepare the command...
cmdline = MuscleCommandline(muscle_exe)
cmdline.set_parameter("in", temp_large_fasta_file)
#Use fast options
cmdline.set_parameter("maxiters", 1)
cmdline.set_parameter("diags", True) #Default None treated as False!
#Use clustal output
cmdline.set_parameter("clwstrict", True) #Default None treated as False!
#Shoudn't need this, but just to make sure it is accepted
cmdline.set_parameter("maxhours", 0.1)
#No progress reports to stderr
cmdline.set_parameter("quiet", True) #Default None treated as False!
self.assertEqual(str(cmdline).rstrip(), muscle_exe + \
" -in temp_cw_prot.fasta -diags -maxhours 0.1" + \
" -maxiters 1 -clwstrict -quiet")
self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
child = subprocess.Popen(str(cmdline),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform!="win32"))
align = AlignIO.read(child.stdout, "clustal")
align.sort()
records.sort(key = lambda rec: rec.id)
self.assertEqual(len(records), len(align))
for old, new in zip(records, align):
self.assertEqual(old.id, new.id)
self.assertEqual(str(new.seq).replace("-",""), str(old.seq))
os.remove(temp_large_fasta_file)
#See if quiet worked:
self.assertEqual("", child.stderr.read().strip())
return_code = child.wait()
self.assertEqual(return_code, 0)
child.stdout.close()
child.stderr.close()
del child
def test_using_stdin(self):
"""Simple alignment using stdin"""
input_file = "Fasta/f002"
self.assertTrue(os.path.isfile(input_file))
records = list(SeqIO.parse(input_file,"fasta"))
#Prepare the command... use Clustal output (with a MUSCLE header)
cline = MuscleCommandline(muscle_exe, clw=True)
self.assertEqual(str(cline).rstrip(), muscle_exe + " -clw")
self.assertEqual(str(eval(repr(cline))), str(cline))
child = subprocess.Popen(str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform!="win32"))
SeqIO.write(records, child.stdin, "fasta")
child.stdin.close()
#Alignment will now run...
align = AlignIO.read(child.stdout, "clustal")
align.sort()
records.sort(key = lambda rec: rec.id)
self.assertEqual(len(records),len(align))
for old, new in zip(records, align):
self.assertEqual(old.id, new.id)
self.assertEqual(str(new.seq).replace("-",""), str(old.seq))
self.assertEqual(0, child.wait())
child.stdout.close()
child.stderr.close()
del child
def test_with_multiple_output_formats(self):
"""Simple muscle call with multiple output formats"""
input_file = "Fasta/f002"
output_html = "temp_f002.html"
output_clwstrict = "temp_f002.clw"
self.assertTrue(os.path.isfile(input_file))
records = list(SeqIO.parse(input_file,"fasta"))
records.sort(key = lambda rec: rec.id)
#Prepare the command... use Clustal output (with a MUSCLE header)
cmdline = MuscleCommandline(muscle_exe, input=input_file,
clw=True, htmlout = output_html,
clwstrictout = output_clwstrict)
self.assertEqual(str(cmdline).rstrip(), muscle_exe + \
" -in Fasta/f002 -clw -htmlout temp_f002.html" +\
" -clwstrictout temp_f002.clw")
self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
child = subprocess.Popen(str(cmdline),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform!="win32"))
#Clustalw on stdout:
align = AlignIO.read(child.stdout, "clustal")
align.sort()
#Didn't use -quiet so there should be progress reports on stderr,
self.assertTrue(child.stderr.read().strip().startswith("MUSCLE"))
return_code = child.wait()
self.assertEqual(return_code, 0)
self.assertEqual(len(records),len(align))
for old, new in zip(records, align):
self.assertEqual(old.id, new.id)
child.stdout.close()
child.stderr.close()
del child
handle = open(output_html,"rU")
html = handle.read().strip().upper()
handle.close()
self.assertTrue(html.startswith("<HTML"))
self.assertTrue(html.endswith("</HTML>"))
#ClustalW strict:
align = AlignIO.read(output_clwstrict, "clustal")
align.sort()
self.assertEqual(len(records),len(align))
for old, new in zip(records, align):
self.assertEqual(old.id, new.id)
os.remove(output_html)
os.remove(output_clwstrict)
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity = 2)
unittest.main(testRunner=runner)