annotate galaxyp-galaxyp-toolshed-msconvert-9663ae998499/msconvert_wrapper.py @ 0:942cec8d04c7 draft

Initial commit.
author jmchilton
date Wed, 19 Sep 2012 00:07:16 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
1 #!/usr/bin/env python
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
2 import optparse
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
3 import os
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
4 import sys
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
5 import tempfile
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
6 import shutil
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
7 import subprocess
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
8 import re
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
9 from os.path import basename
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
10 import logging
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
11
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
12 assert sys.version_info[:2] >= ( 2, 6 )
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
13
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
14 log = logging.getLogger(__name__)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
15 working_directory = os.getcwd()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
16 tmp_stderr_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stderr').name
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
17 tmp_stdout_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stdout').name
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
18
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
19 def stop_err( msg ):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
20 sys.stderr.write( "%s\n" % msg )
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
21 sys.exit()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
22
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
23 def read_stderr():
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
24 stderr = ''
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
25 if(os.path.exists(tmp_stderr_name)):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
26 with open(tmp_stderr_name, 'rb') as tmp_stderr:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
27 buffsize = 1048576
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
28 try:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
29 while True:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
30 stderr += tmp_stderr.read(buffsize)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
31 if not stderr or len(stderr) % buffsize != 0:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
32 break
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
33 except OverflowError:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
34 pass
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
35 return stderr
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
36
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
37 def execute(command, stdin=None):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
38 try:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
39 with open(tmp_stderr_name, 'wb') as tmp_stderr:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
40 with open(tmp_stdout_name, 'wb') as tmp_stdout:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
41 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
42 returncode = proc.wait()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
43 if returncode != 0:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
44 raise Exception, "Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr())
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
45 finally:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
46 print open(tmp_stderr_name, "r").read()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
47 print open(tmp_stdout_name, "r").read()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
48
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
49
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
50 def delete_file(path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
51 if os.path.exists(path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
52 try:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
53 os.remove(path)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
54 except:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
55 pass
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
56
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
57 def delete_directory(directory):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
58 if os.path.exists(directory):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
59 try:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
60 shutil.rmtree(directory)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
61 except:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
62 pass
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
63
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
64 def symlink(source, link_name):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
65 import platform
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
66 if platform.system() == 'Windows':
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
67 import win32file
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
68 win32file.CreateSymbolicLink(source, link_name, 1)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
69 else:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
70 os.symlink(source, link_name)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
71
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
72
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
73 def copy_to_working_directory(data_file, relative_path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
74 if os.path.abspath(data_file) != os.path.abspath(relative_path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
75 shutil.copy(data_file, relative_path)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
76 return relative_path
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
77
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
78 def __main__():
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
79 run_script()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
80
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
81 #ENDTEMPLATE
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
82
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
83 to_extensions = ['mzML', 'mzXML', 'unindexed_mzML', 'unindexed_mzXML', 'mgf', 'txt', 'ms2', 'cms2']
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
84
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
85
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
86 def str_to_bool(v):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
87 """ From http://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python """
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
88 return v.lower() in ["yes", "true", "t", "1"]
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
89
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
90
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
91 def _add_filter(filters_file, contents):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
92 filters_file.write("filter=\"%s\"\n" % contents)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
93
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
94
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
95 def _read_table_numbers(path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
96 unique_numbers = set([])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
97 input = open(path, "r")
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
98 first_line = True
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
99 for line in input:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
100 if not line:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
101 continue
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
102 line = line.strip()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
103 if line.startswith("#"):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
104 first_line = False
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
105 continue
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
106 match = re.match("\d+", line)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
107 if match:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
108 unique_numbers.add(int(match.group()))
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
109 first_line = False
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
110 return unique_numbers
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
111
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
112
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
113 def _add_filter_line_from_file(file, filter_file, filter_prefix):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
114 if not file:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
115 return
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
116 numbers = _read_table_numbers(file)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
117 msconvert_int_set = " ".join([str(number) for number in numbers])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
118 _add_filter(filter_file, "%s %s" % (filter_prefix, msconvert_int_set))
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
119
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
120
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
121 def _create_filters_file(options):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
122 filters_file_path = "filters"
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
123 filters_file = open(filters_file_path, "w")
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
124 if options.filters_file:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
125 filters_file.write(open(options.filters_file, "r").read())
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
126 for filter in options.filter:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
127 _add_filter(filters_file, filter)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
128 _add_filter_line_from_file(options.filter_indices_table, filters_file, "index")
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
129 _add_filter_line_from_file(options.filter_numbers_table, filters_file, "scanNumber")
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
130
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
131 filters_file.close()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
132 print open(filters_file_path, "r").read()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
133 return filters_file_path
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
134
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
135
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
136 def run_script():
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
137 parser = optparse.OptionParser()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
138 parser.add_option('--input', dest='input')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
139 parser.add_option('--output', dest='output')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
140 parser.add_option('--fromextension', dest='fromextension')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
141 parser.add_option('--toextension', dest='toextension', default='mzML', choices=to_extensions)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
142 parser.add_option('--binaryencoding', dest='binaryencoding', choices=['32', '64'])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
143 parser.add_option('--mzencoding', dest='mzencoding', choices=['32', '64'])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
144 parser.add_option('--intensityencoding', dest='intensityencoding', choices=['32', '64'])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
145 parser.add_option('--zlib', dest='zlib', default="false")
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
146 parser.add_option('--filter', dest='filter', action='append', default=[])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
147 parser.add_option('--filters_file', dest='filters_file', default=None)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
148 parser.add_option('--filter_indices_table', default=None)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
149 parser.add_option('--filter_numbers_table', default=None)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
150
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
151 (options, args) = parser.parse_args()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
152
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
153 input_file = 'input.%s' % options.fromextension
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
154 copy_to_working_directory(options.input, input_file)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
155 os.mkdir('output')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
156 to_extension = options.toextension
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
157 if to_extension.startswith("unindexed_"):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
158 to_extension = to_extension[len("unindexed_"):]
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
159 to_params = "--noindex"
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
160 else:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
161 to_params = ""
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
162 cmd = "msconvert --%s %s -o output" % (to_extension, to_params)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
163 if str_to_bool(options.zlib):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
164 cmd = "%s %s" % (cmd, "--zlib")
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
165 if options.binaryencoding:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
166 cmd = "%s --%s" % (cmd, options.binaryencoding)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
167 if options.mzencoding:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
168 cmd = "%s --mz%s" % (cmd, options.mzencoding)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
169 if options.intensityencoding:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
170 cmd = "%s --inten%s" % (cmd, options.intensityencoding)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
171 cmd = "%s %s" % (cmd, input_file)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
172 filters_file_path = _create_filters_file(options)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
173 cmd = "%s -c %s" % (cmd, filters_file_path)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
174 print cmd
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
175 execute(cmd)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
176 output_files = os.listdir('output')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
177 assert len(output_files) == 1
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
178 output_file = output_files[0]
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
179 shutil.copy(os.path.join('output', output_file), options.output)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
180
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
181
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
182 if __name__ == '__main__': __main__()