annotate galaxyp-galaxyp-toolshed-msconvert-9663ae998499/msconvert_raw_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 with open(tmp_stderr_name, 'wb') as tmp_stderr:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
39 with open(tmp_stdout_name, 'wb') as tmp_stdout:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
40 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
41 returncode = proc.wait()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
42 if returncode != 0:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
43 raise Exception, "Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr())
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
44
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
45 def delete_file(path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
46 if os.path.exists(path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
47 try:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
48 os.remove(path)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
49 except:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
50 pass
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
51
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
52 def delete_directory(directory):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
53 if os.path.exists(directory):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
54 try:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
55 shutil.rmtree(directory)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
56 except:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
57 pass
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
58
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
59 def symlink(source, link_name):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
60 import platform
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
61 if platform.system() == 'Windows':
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
62 import win32file
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
63 win32file.CreateSymbolicLink(source, link_name, 1)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
64 else:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
65 os.symlink(source, link_name)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
66
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
67
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
68 def copy_to_working_directory(data_file, relative_path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
69 if os.path.abspath(data_file) != os.path.abspath(relative_path):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
70 shutil.copy(data_file, relative_path)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
71 return relative_path
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
72
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
73 def __main__():
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
74 run_script()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
75
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
76 #ENDTEMPLATE
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
77
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
78 to_extensions = ['mzML', 'mzXML', 'mgf', 'txt', 'ms2', 'cms2']
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
79
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
80 def str_to_bool(v):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
81 """ From http://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python """
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
82 return v.lower() in ["yes", "true", "t", "1"]
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
83
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
84
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
85 def run_script():
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
86 parser = optparse.OptionParser()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
87 parser.add_option('--input', dest='input')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
88 parser.add_option('--output', dest='output')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
89 parser.add_option('--fromextension', dest='fromextension')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
90 parser.add_option('--toextension', dest='toextension', default='mzML', choices=to_extensions)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
91 parser.add_option('--binaryencoding', dest='binaryencoding', choices=['32', '64'])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
92 parser.add_option('--mzencoding', dest='mzencoding', choices=['32', '64'])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
93 parser.add_option('--intensityencoding', dest='intensityencoding', choices=['32', '64'])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
94 parser.add_option('--noindex', dest='noindex')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
95 parser.add_option('--zlib', dest='zlib')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
96 parser.add_option('--filter', dest='filter', action='append', default=[])
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
97
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
98 (options, args) = parser.parse_args()
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
99
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
100 filter_commands = ''
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
101 for filter in options.filter:
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
102 filter_commands = "%s --filter \"%s\"" % (filter_commands, filter)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
103
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
104 input_file = 'input.%s' % options.fromextension
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
105 copy_to_working_directory(options.input, input_file)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
106 os.mkdir('output')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
107 cmd = "msconvert --%s -o output" % (options.toextension)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
108 if str_to_bool(options.noindex):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
109 cmd = "%s %s" % (cmd, "--noindex")
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
110 if str_to_bool(options.zlib):
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
111 cmd = "%s %s" % (cmd, "--zlib")
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
112 cmd = "%s --%s" % (cmd, options.binaryencoding)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
113 cmd = "%s --mz%s" % (cmd, options.mzencoding)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
114 cmd = "%s --inten%s" % (cmd, options.intensityencoding)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
115 cmd = "%s %s" % (cmd, input_file)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
116 cmd = "%s %s" % (cmd, filter_commands)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
117 print cmd
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
118 execute(cmd)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
119 output_files = os.listdir('output')
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
120 assert len(output_files) == 1
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
121 output_file = output_files[0]
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
122 shutil.copy(os.path.join('output', output_file), options.output)
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
123
942cec8d04c7 Initial commit.
jmchilton
parents:
diff changeset
124 if __name__ == '__main__': __main__()