comparison ms_data_converter.py @ 0:dfafbfd7983d

Uploaded
author galaxyp
date Wed, 11 Mar 2015 11:36:26 -0400
parents
children a36e9f847308
comparison
equal deleted inserted replaced
-1:000000000000 0:dfafbfd7983d
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):
121 input_base = None
122 if len(options.input_names) > i:
123 input_base = options.input_names[i]
124 input_base = input_base.replace("'", "").replace("\"", "")
125 print("1- input_base: %s" % input_base)
126 if not input_base:
127 input_base = 'input%s' % i
128 print("2- input_base: %s" % input_base)
129 if not input_base.lower().endswith(options.fromextension.lower()) and input not in options.implicits:
130 input_file = '%s.%s' % (input_base, options.fromextension)
131 print("3- input_base: %s" % input_base)
132 print("3- input_file: %s" % input_file)
133 else:
134 input_file = input_base
135 print("4- input_base: %s" % input_base)
136 print("4- input_file: %s" % input_file)
137 input_file = input_file
138 copy_to_working_directory(input, input_file)
139 if input not in options.implicits:
140 input_files.append(input_file)
141 ## AB_SCIEX_MS_Converter <input format> <input data> <output content type> <output format> <output file> [data compression setting] [data precision setting] [create index flag]
142 inputs_as_str = " ".join(['%s' % shellquote(input) for input in input_files])
143 output_file = re.sub('(%s)?$' % options.fromextension.lower(), options.toextension, input_files[0].lower())
144 cmd = "AB_SCIEX_MS_Converter %s %s %s %s %s" % (options.fromextension.upper(), inputs_as_str, options.content_type, options.toextension.upper(), output_file )
145 if str_to_bool(options.zlib):
146 cmd = "%s %s" % (cmd, "/zlib")
147 if options.binaryencoding:
148 cmd = "%s %s" % (cmd, "/singleprecision" if options.binaryencoding == '32' else "/doubleprecision")
149 if str_to_bool(options.zlib):
150 cmd = "%s %s" % (cmd, "/index")
151 if options.debug:
152 print(cmd)
153 execute(cmd)
154 shutil.copy(output_file, options.output)
155
156 if __name__ == '__main__':
157 __main__()