Mercurial > repos > galaxyp > ms_data_converter
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 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 import optparse | |
3 import os | |
4 import sys | |
5 import tempfile | |
6 import shutil | |
7 import subprocess | |
8 import re | |
9 import logging | |
10 | |
11 assert sys.version_info[:2] >= (2, 6) | |
12 | |
13 log = logging.getLogger(__name__) | |
14 working_directory = os.getcwd() | |
15 tmp_stderr_name = tempfile.NamedTemporaryFile(dir=working_directory, suffix='.stderr').name | |
16 tmp_stdout_name = tempfile.NamedTemporaryFile(dir=working_directory, suffix='.stdout').name | |
17 | |
18 | |
19 def stop_err(msg): | |
20 sys.stderr.write("%s\n" % msg) | |
21 sys.exit() | |
22 | |
23 | |
24 def read_stderr(): | |
25 stderr = '' | |
26 if(os.path.exists(tmp_stderr_name)): | |
27 with open(tmp_stderr_name, 'rb') as tmp_stderr: | |
28 buffsize = 1048576 | |
29 try: | |
30 while True: | |
31 stderr += tmp_stderr.read(buffsize) | |
32 if not stderr or len(stderr) % buffsize != 0: | |
33 break | |
34 except OverflowError: | |
35 pass | |
36 return stderr | |
37 | |
38 | |
39 def execute(command, stdin=None): | |
40 try: | |
41 with open(tmp_stderr_name, 'wb') as tmp_stderr: | |
42 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 returncode = proc.wait() | |
45 if returncode != 0: | |
46 raise Exception("Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr())) | |
47 finally: | |
48 print(( open(tmp_stderr_name, "r").read() )) | |
49 print(( open(tmp_stdout_name, "r").read() )) | |
50 | |
51 | |
52 def delete_file(path): | |
53 if os.path.exists(path): | |
54 try: | |
55 os.remove(path) | |
56 except: | |
57 pass | |
58 | |
59 | |
60 def delete_directory(directory): | |
61 if os.path.exists(directory): | |
62 try: | |
63 shutil.rmtree(directory) | |
64 except: | |
65 pass | |
66 | |
67 | |
68 def symlink(source, link_name): | |
69 import platform | |
70 if platform.system() == 'Windows': | |
71 try: | |
72 import win32file | |
73 win32file.CreateSymbolicLink(source, link_name, 1) | |
74 except: | |
75 shutil.copy(source, link_name) | |
76 else: | |
77 os.symlink(source, link_name) | |
78 | |
79 | |
80 def copy_to_working_directory(data_file, relative_path): | |
81 if os.path.abspath(data_file) != os.path.abspath(relative_path): | |
82 symlink(data_file, relative_path) | |
83 return relative_path | |
84 | |
85 | |
86 def __main__(): | |
87 run_script() | |
88 | |
89 #ENDTEMPLATE | |
90 | |
91 def str_to_bool(v): | |
92 """ From http://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python """ | |
93 return v.lower() in ["yes", "true", "t", "1"] | |
94 | |
95 def shellquote(s): | |
96 return '"' + s.replace('"', '\\"') + '"' | |
97 | |
98 def run_script(): | |
99 parser = optparse.OptionParser() | |
100 parser.add_option('--input', dest='inputs', action='append', default=[]) | |
101 parser.add_option('--input_name', dest='input_names', action='append', default=[]) | |
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.') | |
103 parser.add_option('--output', dest='output') | |
104 parser.add_option('--fromextension', dest='fromextension') | |
105 parser.add_option('--toextension', dest='toextension', default='mzML', choices=['mzML', 'mgf']) | |
106 parser.add_option('--content_type', dest='content_type', default='centroid', choices=['profile', 'centroid', 'proteinpilot']) | |
107 parser.add_option('--binaryencoding', dest='binaryencoding', choices=['32', '64']) | |
108 parser.add_option('--zlib', dest='zlib', default="false") | |
109 parser.add_option('--index', dest='index', default="false") | |
110 parser.add_option('--debug', dest='debug', action='store_true', default=False) | |
111 | |
112 (options, args) = parser.parse_args() | |
113 if len(options.inputs) < 1: | |
114 stop_err("No input files to ms_data_convert specified") | |
115 if len(options.input_names) > 0 and len(options.input_names) != len(options.inputs): | |
116 stop_err("Number(s) of supplied input names and input files do not match") | |
117 if not options.output: | |
118 stop_err("Must specify output location") | |
119 input_files = [] | |
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 | 124 input_base = None |
125 if len(options.input_names) > i: | |
126 input_base = options.input_names[i] | |
127 input_base = input_base.replace("'", "").replace("\"", "") | |
128 print("1- input_base: %s" % input_base) | |
129 if not input_base: | |
130 input_base = 'input%s' % i | |
131 print("2- input_base: %s" % input_base) | |
132 if not input_base.lower().endswith(options.fromextension.lower()) and input not in options.implicits: | |
133 input_file = '%s.%s' % (input_base, options.fromextension) | |
134 print("3- input_base: %s" % input_base) | |
135 print("3- input_file: %s" % input_file) | |
136 else: | |
137 input_file = input_base | |
138 print("4- input_base: %s" % input_base) | |
139 print("4- input_file: %s" % input_file) | |
140 input_file = input_file | |
141 copy_to_working_directory(input, input_file) | |
142 if input not in options.implicits: | |
143 input_files.append(input_file) | |
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] | |
145 inputs_as_str = " ".join(['%s' % shellquote(input) for input in input_files]) | |
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 | 148 if str_to_bool(options.zlib): |
149 cmd = "%s %s" % (cmd, "/zlib") | |
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 | 152 if str_to_bool(options.zlib): |
153 cmd = "%s %s" % (cmd, "/index") | |
154 if options.debug: | |
155 print(cmd) | |
156 execute(cmd) | |
157 shutil.copy(output_file, options.output) | |
158 | |
159 if __name__ == '__main__': | |
160 __main__() |