annotate msconvert_raw_wrapper.py @ 5:08bb52273ae1

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