comparison msconvert_wrapper.py @ 6:6b6bba73eadb draft

planemo upload commit d56659dd48f8c554a832787e71aca6ae65c90848
author galaxyp
date Tue, 14 Mar 2017 16:52:39 -0400
parents 637e309295cf
children
comparison
equal deleted inserted replaced
5:637e309295cf 6:6b6bba73eadb
5 import tempfile 5 import tempfile
6 import shutil 6 import shutil
7 import subprocess 7 import subprocess
8 import re 8 import re
9 import logging 9 import logging
10 import shlex
10 11
11 assert sys.version_info[:2] >= (2, 6) 12 assert sys.version_info[:2] >= (2, 6)
12 13
13 log = logging.getLogger(__name__) 14 log = logging.getLogger(__name__)
14 working_directory = os.getcwd() 15 working_directory = os.getcwd()
38 39
39 def execute(command, stdin=None): 40 def execute(command, stdin=None):
40 try: 41 try:
41 with open(tmp_stderr_name, 'wb') as tmp_stderr: 42 with open(tmp_stderr_name, 'wb') as tmp_stderr:
42 with open(tmp_stdout_name, 'wb') as tmp_stdout: 43 with open(tmp_stdout_name, 'wb') as tmp_stdout:
43 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ) 44 args = shlex.split(command) # handle proper splitting of quoted args
45 proc = subprocess.Popen(args=args, shell=False, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ)
44 returncode = proc.wait() 46 returncode = proc.wait()
45 if returncode != 0: 47 if returncode != 0:
46 raise Exception("Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr())) 48 raise Exception("Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr()))
47 finally: 49 finally:
48 print(( open(tmp_stderr_name, "r").read() )) 50 print(( open(tmp_stderr_name, "r").read() ))
86 def __main__(): 88 def __main__():
87 run_script() 89 run_script()
88 90
89 #ENDTEMPLATE 91 #ENDTEMPLATE
90 92
91 to_extensions = ['mzML', 'mzXML', 'unindexed_mzML', 'unindexed_mzXML', 'mgf', 'txt', 'ms2', 'cms2'] 93 to_extensions = ['mzML', 'mzXML', 'unindexed_mzML', 'unindexed_mzXML', 'mgf', 'mz5', 'txt', 'ms2', 'cms2']
92 94
93 95
94 def str_to_bool(v): 96 def str_to_bool(v):
95 """ From http://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python """ 97 """ From http://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python """
96 return v.lower() in ["yes", "true", "t", "1"] 98 return v.lower() in ["yes", "true", "t", "1"]
169 if debug: 171 if debug:
170 print(( open(filters_file_path, "r").read() )) 172 print(( open(filters_file_path, "r").read() ))
171 return filters_file_path 173 return filters_file_path
172 174
173 175
174 def _build_base_cmd(options): 176 def _build_base_cmd(options,args=None):
175 to_extension = options.toextension 177 to_extension = options.toextension
176 if to_extension.startswith("unindexed_"): 178 if to_extension.startswith("unindexed_"):
177 to_extension = to_extension[len("unindexed_"):] 179 to_extension = to_extension[len("unindexed_"):]
178 to_params = "--noindex" 180 to_params = "--noindex"
179 else: 181 else:
180 to_params = "" 182 to_params = ""
181 cmd = "msconvert --%s %s" % (to_extension, to_params) 183 cmd = "msconvert --%s %s" % (to_extension, to_params)
184 if args:
185 cmd = "%s %s" % (cmd, ' '.join(args))
182 if str_to_bool(options.zlib): 186 if str_to_bool(options.zlib):
183 cmd = "%s %s" % (cmd, "--zlib") 187 cmd = "%s %s" % (cmd, "--zlib")
184 if options.binaryencoding: 188 if options.binaryencoding:
185 cmd = "%s --%s" % (cmd, options.binaryencoding) 189 cmd = "%s --%s" % (cmd, options.binaryencoding)
186 if options.mzencoding: 190 if options.mzencoding:
206 def run_script(): 210 def run_script():
207 parser = optparse.OptionParser() 211 parser = optparse.OptionParser()
208 parser.add_option('--input', dest='inputs', action='append', default=[]) 212 parser.add_option('--input', dest='inputs', action='append', default=[])
209 parser.add_option('--input_name', dest='input_names', action='append', default=[]) 213 parser.add_option('--input_name', dest='input_names', action='append', default=[])
210 parser.add_option('--implicit', dest='implicits', action='append', default=[], help='input files that should NOT be on the msconvert command line.') 214 parser.add_option('--implicit', dest='implicits', action='append', default=[], help='input files that should NOT be on the msconvert command line.')
215 parser.add_option('--ident', dest='idents', action='append', default=[])
216 parser.add_option('--ident_name', dest='ident_names', action='append', default=[])
211 parser.add_option('--output', dest='output') 217 parser.add_option('--output', dest='output')
218 parser.add_option('--refinement', dest='refinement')
212 parser.add_option('--fromextension', dest='fromextension') 219 parser.add_option('--fromextension', dest='fromextension')
213 parser.add_option('--toextension', dest='toextension', default='mzML', choices=to_extensions) 220 parser.add_option('--toextension', dest='toextension', default='mzML', choices=to_extensions)
214 parser.add_option('--binaryencoding', dest='binaryencoding', choices=['32', '64']) 221 parser.add_option('--binaryencoding', dest='binaryencoding', choices=['32', '64'])
215 parser.add_option('--mzencoding', dest='mzencoding', choices=['32', '64']) 222 parser.add_option('--mzencoding', dest='mzencoding', choices=['32', '64'])
216 parser.add_option('--intensityencoding', dest='intensityencoding', choices=['32', '64']) 223 parser.add_option('--intensityencoding', dest='intensityencoding', choices=['32', '64'])
251 input_file = input_file 258 input_file = input_file
252 copy_to_working_directory(input, input_file) 259 copy_to_working_directory(input, input_file)
253 if input in options.implicits: 260 if input in options.implicits:
254 continue 261 continue
255 input_files.append(input_file) 262 input_files.append(input_file)
256 263 for i, ident in enumerate(options.idents):
257 cmd = _build_base_cmd(options) 264 ident_file = options.ident_names[i]
265 copy_to_working_directory(ident, ident_file)
266
267 cmd = _build_base_cmd(options,args=args)
258 file_column = options.filter_table_file_column 268 file_column = options.filter_table_file_column
259 if not file_column: 269 if not file_column:
260 # Apply same filters to all files, just create a unviersal filter files 270 # Apply same filters to all files, just create a unviersal filter files
261 # and run msconvert once. 271 # and run msconvert once.
262 filters_file_path = _create_filters_file(options, debug=options.debug) 272 filters_file_path = _create_filters_file(options, debug=options.debug)
273 input_files = filtered_files 283 input_files = filtered_files
274 if len(input_files) > 1: 284 if len(input_files) > 1:
275 cmd = "%s --merge" % cmd 285 cmd = "%s --merge" % cmd
276 output_file = _run(cmd, output_dir='output', inputs=input_files, debug=options.debug) 286 output_file = _run(cmd, output_dir='output', inputs=input_files, debug=options.debug)
277 shutil.copy(output_file, options.output) 287 shutil.copy(output_file, options.output)
278 288 if options.refinement:
289 # .mzRefinement.tsv
290 files = os.listdir(os.getcwd())
291 for fname in files:
292 if fname.endswith('.mzRefinement.tsv'):
293 shutil.copy(fname, options.refinement)
294 break
295
296 def __main__():
297 run_script()
279 298
280 if __name__ == '__main__': 299 if __name__ == '__main__':
281 __main__() 300 __main__()