0
|
1 #!/usr/bin/env python
|
|
2 import optparse
|
|
3 import os
|
|
4 import sys
|
|
5 import tempfile
|
|
6 import shutil
|
|
7 import subprocess
|
|
8 import re
|
|
9 from os.path import basename
|
|
10 import logging
|
|
11
|
|
12 assert sys.version_info[:2] >= ( 2, 6 )
|
|
13
|
|
14 log = logging.getLogger(__name__)
|
|
15 working_directory = os.getcwd()
|
|
16 tmp_stderr_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stderr').name
|
|
17 tmp_stdout_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stdout').name
|
|
18
|
|
19 def stop_err( msg ):
|
|
20 sys.stderr.write( "%s\n" % msg )
|
|
21 sys.exit()
|
|
22
|
|
23 def read_stderr():
|
|
24 stderr = ''
|
|
25 if(os.path.exists(tmp_stderr_name)):
|
|
26 with open(tmp_stderr_name, 'rb') as tmp_stderr:
|
|
27 buffsize = 1048576
|
|
28 try:
|
|
29 while True:
|
|
30 stderr += tmp_stderr.read(buffsize)
|
|
31 if not stderr or len(stderr) % buffsize != 0:
|
|
32 break
|
|
33 except OverflowError:
|
|
34 pass
|
|
35 return stderr
|
|
36
|
|
37 def execute(command, stdin=None):
|
|
38 with open(tmp_stderr_name, 'wb') as tmp_stderr:
|
|
39 with open(tmp_stdout_name, 'wb') as tmp_stdout:
|
|
40 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ)
|
|
41 returncode = proc.wait()
|
|
42 if returncode != 0:
|
|
43 raise Exception, "Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr())
|
|
44
|
|
45 def delete_file(path):
|
|
46 if os.path.exists(path):
|
|
47 try:
|
|
48 os.remove(path)
|
|
49 except:
|
|
50 pass
|
|
51
|
|
52 def delete_directory(directory):
|
|
53 if os.path.exists(directory):
|
|
54 try:
|
|
55 shutil.rmtree(directory)
|
|
56 except:
|
|
57 pass
|
|
58
|
|
59 def symlink(source, link_name):
|
|
60 import platform
|
|
61 if platform.system() == 'Windows':
|
|
62 import win32file
|
|
63 win32file.CreateSymbolicLink(source, link_name, 1)
|
|
64 else:
|
|
65 os.symlink(source, link_name)
|
|
66
|
|
67
|
|
68 def copy_to_working_directory(data_file, relative_path):
|
|
69 if os.path.abspath(data_file) != os.path.abspath(relative_path):
|
|
70 shutil.copy(data_file, relative_path)
|
|
71 return relative_path
|
|
72
|
|
73 def __main__():
|
|
74 run_script()
|
|
75
|
|
76 #ENDTEMPLATE
|
|
77
|
|
78 to_extensions = ['mzML', 'mzXML', 'mgf', 'txt', 'ms2', 'cms2']
|
|
79
|
|
80 def str_to_bool(v):
|
|
81 """ From http://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python """
|
|
82 return v.lower() in ["yes", "true", "t", "1"]
|
|
83
|
|
84
|
|
85 def run_script():
|
|
86 parser = optparse.OptionParser()
|
|
87 parser.add_option('--input', dest='input')
|
|
88 parser.add_option('--output', dest='output')
|
|
89 parser.add_option('--fromextension', dest='fromextension')
|
|
90 parser.add_option('--toextension', dest='toextension', default='mzML', choices=to_extensions)
|
|
91 parser.add_option('--binaryencoding', dest='binaryencoding', choices=['32', '64'])
|
|
92 parser.add_option('--mzencoding', dest='mzencoding', choices=['32', '64'])
|
|
93 parser.add_option('--intensityencoding', dest='intensityencoding', choices=['32', '64'])
|
|
94 parser.add_option('--noindex', dest='noindex')
|
|
95 parser.add_option('--zlib', dest='zlib')
|
|
96 parser.add_option('--filter', dest='filter', action='append', default=[])
|
|
97
|
|
98 (options, args) = parser.parse_args()
|
|
99
|
|
100 filter_commands = ''
|
|
101 for filter in options.filter:
|
|
102 filter_commands = "%s --filter \"%s\"" % (filter_commands, filter)
|
|
103
|
|
104 input_file = 'input.%s' % options.fromextension
|
|
105 copy_to_working_directory(options.input, input_file)
|
|
106 os.mkdir('output')
|
|
107 cmd = "msconvert --%s -o output" % (options.toextension)
|
|
108 if str_to_bool(options.noindex):
|
|
109 cmd = "%s %s" % (cmd, "--noindex")
|
|
110 if str_to_bool(options.zlib):
|
|
111 cmd = "%s %s" % (cmd, "--zlib")
|
|
112 cmd = "%s --%s" % (cmd, options.binaryencoding)
|
|
113 cmd = "%s --mz%s" % (cmd, options.mzencoding)
|
|
114 cmd = "%s --inten%s" % (cmd, options.intensityencoding)
|
|
115 cmd = "%s %s" % (cmd, input_file)
|
|
116 cmd = "%s %s" % (cmd, filter_commands)
|
|
117 print cmd
|
|
118 execute(cmd)
|
|
119 output_files = os.listdir('output')
|
|
120 assert len(output_files) == 1
|
|
121 output_file = output_files[0]
|
|
122 shutil.copy(os.path.join('output', output_file), options.output)
|
|
123
|
|
124 if __name__ == '__main__': __main__()
|