annotate ms_data_converter.py @ 1:a36e9f847308 default tip

Fixes for running on Windows via pulsar
author Jim Johnson <jj@umn.edu>
date Wed, 11 Mar 2015 16:47:12 -0500
parents dfafbfd7983d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
1 #!/usr/bin/env python
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
2 import optparse
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
3 import os
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
4 import sys
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
5 import tempfile
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
6 import shutil
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
7 import subprocess
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
8 import re
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
9 import logging
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
10
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
11 assert sys.version_info[:2] >= (2, 6)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
12
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
13 log = logging.getLogger(__name__)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
14 working_directory = os.getcwd()
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
15 tmp_stderr_name = tempfile.NamedTemporaryFile(dir=working_directory, suffix='.stderr').name
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
16 tmp_stdout_name = tempfile.NamedTemporaryFile(dir=working_directory, suffix='.stdout').name
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
17
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
18
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
19 def stop_err(msg):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
20 sys.stderr.write("%s\n" % msg)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
21 sys.exit()
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
22
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
23
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
24 def read_stderr():
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
25 stderr = ''
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
26 if(os.path.exists(tmp_stderr_name)):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
27 with open(tmp_stderr_name, 'rb') as tmp_stderr:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
28 buffsize = 1048576
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
29 try:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
30 while True:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
31 stderr += tmp_stderr.read(buffsize)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
32 if not stderr or len(stderr) % buffsize != 0:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
33 break
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
34 except OverflowError:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
35 pass
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
36 return stderr
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
37
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
38
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
39 def execute(command, stdin=None):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
40 try:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
41 with open(tmp_stderr_name, 'wb') as tmp_stderr:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
42 with open(tmp_stdout_name, 'wb') as tmp_stdout:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
43 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
44 returncode = proc.wait()
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
45 if returncode != 0:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
46 raise Exception("Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr()))
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
47 finally:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
48 print(( open(tmp_stderr_name, "r").read() ))
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
49 print(( open(tmp_stdout_name, "r").read() ))
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
50
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
51
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
52 def delete_file(path):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
53 if os.path.exists(path):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
54 try:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
55 os.remove(path)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
56 except:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
57 pass
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
58
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
59
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
60 def delete_directory(directory):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
61 if os.path.exists(directory):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
62 try:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
63 shutil.rmtree(directory)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
64 except:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
65 pass
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
66
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
67
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
68 def symlink(source, link_name):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
69 import platform
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
70 if platform.system() == 'Windows':
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
71 try:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
72 import win32file
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
73 win32file.CreateSymbolicLink(source, link_name, 1)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
74 except:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
75 shutil.copy(source, link_name)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
76 else:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
77 os.symlink(source, link_name)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
78
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
79
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
80 def copy_to_working_directory(data_file, relative_path):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
81 if os.path.abspath(data_file) != os.path.abspath(relative_path):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
82 symlink(data_file, relative_path)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
83 return relative_path
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
84
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
85
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
86 def __main__():
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
87 run_script()
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
88
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
89 #ENDTEMPLATE
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
90
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
91 def str_to_bool(v):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
92 """ From http://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python """
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
93 return v.lower() in ["yes", "true", "t", "1"]
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
94
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
95 def shellquote(s):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
96 return '"' + s.replace('"', '\\"') + '"'
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
97
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
98 def run_script():
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
99 parser = optparse.OptionParser()
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
100 parser.add_option('--input', dest='inputs', action='append', default=[])
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
101 parser.add_option('--input_name', dest='input_names', action='append', default=[])
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
102 parser.add_option('--implicit', dest='implicits', action='append', default=[], help='input files that should NOT be on the AB SCIEX MS Data Converter command line.')
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
103 parser.add_option('--output', dest='output')
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
104 parser.add_option('--fromextension', dest='fromextension')
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
105 parser.add_option('--toextension', dest='toextension', default='mzML', choices=['mzML', 'mgf'])
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
106 parser.add_option('--content_type', dest='content_type', default='centroid', choices=['profile', 'centroid', 'proteinpilot'])
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
107 parser.add_option('--binaryencoding', dest='binaryencoding', choices=['32', '64'])
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
108 parser.add_option('--zlib', dest='zlib', default="false")
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
109 parser.add_option('--index', dest='index', default="false")
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
110 parser.add_option('--debug', dest='debug', action='store_true', default=False)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
111
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
112 (options, args) = parser.parse_args()
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
113 if len(options.inputs) < 1:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
114 stop_err("No input files to ms_data_convert specified")
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
115 if len(options.input_names) > 0 and len(options.input_names) != len(options.inputs):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
116 stop_err("Number(s) of supplied input names and input files do not match")
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
117 if not options.output:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
118 stop_err("Must specify output location")
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
119 input_files = []
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
120 for i, input in enumerate(options.inputs):
1
a36e9f847308 Fixes for running on Windows via pulsar
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
121 # the commnadline template cannot determine if optional files exists, so do it here
a36e9f847308 Fixes for running on Windows via pulsar
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
122 if not os.path.exists(input):
a36e9f847308 Fixes for running on Windows via pulsar
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
123 continue
0
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
124 input_base = None
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
125 if len(options.input_names) > i:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
126 input_base = options.input_names[i]
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
127 input_base = input_base.replace("'", "").replace("\"", "")
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
128 print("1- input_base: %s" % input_base)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
129 if not input_base:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
130 input_base = 'input%s' % i
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
131 print("2- input_base: %s" % input_base)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
132 if not input_base.lower().endswith(options.fromextension.lower()) and input not in options.implicits:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
133 input_file = '%s.%s' % (input_base, options.fromextension)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
134 print("3- input_base: %s" % input_base)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
135 print("3- input_file: %s" % input_file)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
136 else:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
137 input_file = input_base
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
138 print("4- input_base: %s" % input_base)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
139 print("4- input_file: %s" % input_file)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
140 input_file = input_file
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
141 copy_to_working_directory(input, input_file)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
142 if input not in options.implicits:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
143 input_files.append(input_file)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
144 ## AB_SCIEX_MS_Converter <input format> <input data> <output content type> <output format> <output file> [data compression setting] [data precision setting] [create index flag]
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
145 inputs_as_str = " ".join(['%s' % shellquote(input) for input in input_files])
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
146 output_file = re.sub('(%s)?$' % options.fromextension.lower(), options.toextension, input_files[0].lower())
1
a36e9f847308 Fixes for running on Windows via pulsar
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
147 cmd = "AB_SCIEX_MS_Converter %s %s -%s %s %s" % (options.fromextension.upper(), inputs_as_str, options.content_type, options.toextension.upper(), output_file )
0
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
148 if str_to_bool(options.zlib):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
149 cmd = "%s %s" % (cmd, "/zlib")
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
150 if options.binaryencoding:
1
a36e9f847308 Fixes for running on Windows via pulsar
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
151 cmd = "%s %s" % (cmd, "/singleprecision" if options.binaryencoding == '32' else "")
0
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
152 if str_to_bool(options.zlib):
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
153 cmd = "%s %s" % (cmd, "/index")
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
154 if options.debug:
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
155 print(cmd)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
156 execute(cmd)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
157 shutil.copy(output_file, options.output)
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
158
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
159 if __name__ == '__main__':
dfafbfd7983d Uploaded
galaxyp
parents:
diff changeset
160 __main__()