Mercurial > repos > jmchilton > johnstestrepo
comparison galaxyp-galaxyp-toolshed-msconvert-9663ae998499/msconvert_wrapper.py @ 0:942cec8d04c7 draft
Initial commit.
author | jmchilton |
---|---|
date | Wed, 19 Sep 2012 00:07:16 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:942cec8d04c7 |
---|---|
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 from os.path import basename | |
10 import logging | |
11 | |
12 assert sys.version_info[:2] >= ( 2, 6 ) | |
13 | |
14 log = logging.getLogger(__name__) | |
15 working_directory = os.getcwd() | |
16 tmp_stderr_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stderr').name | |
17 tmp_stdout_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stdout').name | |
18 | |
19 def stop_err( msg ): | |
20 sys.stderr.write( "%s\n" % msg ) | |
21 sys.exit() | |
22 | |
23 def read_stderr(): | |
24 stderr = '' | |
25 if(os.path.exists(tmp_stderr_name)): | |
26 with open(tmp_stderr_name, 'rb') as tmp_stderr: | |
27 buffsize = 1048576 | |
28 try: | |
29 while True: | |
30 stderr += tmp_stderr.read(buffsize) | |
31 if not stderr or len(stderr) % buffsize != 0: | |
32 break | |
33 except OverflowError: | |
34 pass | |
35 return stderr | |
36 | |
37 def execute(command, stdin=None): | |
38 try: | |
39 with open(tmp_stderr_name, 'wb') as tmp_stderr: | |
40 with open(tmp_stdout_name, 'wb') as tmp_stdout: | |
41 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ) | |
42 returncode = proc.wait() | |
43 if returncode != 0: | |
44 raise Exception, "Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr()) | |
45 finally: | |
46 print open(tmp_stderr_name, "r").read() | |
47 print open(tmp_stdout_name, "r").read() | |
48 | |
49 | |
50 def delete_file(path): | |
51 if os.path.exists(path): | |
52 try: | |
53 os.remove(path) | |
54 except: | |
55 pass | |
56 | |
57 def delete_directory(directory): | |
58 if os.path.exists(directory): | |
59 try: | |
60 shutil.rmtree(directory) | |
61 except: | |
62 pass | |
63 | |
64 def symlink(source, link_name): | |
65 import platform | |
66 if platform.system() == 'Windows': | |
67 import win32file | |
68 win32file.CreateSymbolicLink(source, link_name, 1) | |
69 else: | |
70 os.symlink(source, link_name) | |
71 | |
72 | |
73 def copy_to_working_directory(data_file, relative_path): | |
74 if os.path.abspath(data_file) != os.path.abspath(relative_path): | |
75 shutil.copy(data_file, relative_path) | |
76 return relative_path | |
77 | |
78 def __main__(): | |
79 run_script() | |
80 | |
81 #ENDTEMPLATE | |
82 | |
83 to_extensions = ['mzML', 'mzXML', 'unindexed_mzML', 'unindexed_mzXML', 'mgf', 'txt', 'ms2', 'cms2'] | |
84 | |
85 | |
86 def str_to_bool(v): | |
87 """ From http://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python """ | |
88 return v.lower() in ["yes", "true", "t", "1"] | |
89 | |
90 | |
91 def _add_filter(filters_file, contents): | |
92 filters_file.write("filter=\"%s\"\n" % contents) | |
93 | |
94 | |
95 def _read_table_numbers(path): | |
96 unique_numbers = set([]) | |
97 input = open(path, "r") | |
98 first_line = True | |
99 for line in input: | |
100 if not line: | |
101 continue | |
102 line = line.strip() | |
103 if line.startswith("#"): | |
104 first_line = False | |
105 continue | |
106 match = re.match("\d+", line) | |
107 if match: | |
108 unique_numbers.add(int(match.group())) | |
109 first_line = False | |
110 return unique_numbers | |
111 | |
112 | |
113 def _add_filter_line_from_file(file, filter_file, filter_prefix): | |
114 if not file: | |
115 return | |
116 numbers = _read_table_numbers(file) | |
117 msconvert_int_set = " ".join([str(number) for number in numbers]) | |
118 _add_filter(filter_file, "%s %s" % (filter_prefix, msconvert_int_set)) | |
119 | |
120 | |
121 def _create_filters_file(options): | |
122 filters_file_path = "filters" | |
123 filters_file = open(filters_file_path, "w") | |
124 if options.filters_file: | |
125 filters_file.write(open(options.filters_file, "r").read()) | |
126 for filter in options.filter: | |
127 _add_filter(filters_file, filter) | |
128 _add_filter_line_from_file(options.filter_indices_table, filters_file, "index") | |
129 _add_filter_line_from_file(options.filter_numbers_table, filters_file, "scanNumber") | |
130 | |
131 filters_file.close() | |
132 print open(filters_file_path, "r").read() | |
133 return filters_file_path | |
134 | |
135 | |
136 def run_script(): | |
137 parser = optparse.OptionParser() | |
138 parser.add_option('--input', dest='input') | |
139 parser.add_option('--output', dest='output') | |
140 parser.add_option('--fromextension', dest='fromextension') | |
141 parser.add_option('--toextension', dest='toextension', default='mzML', choices=to_extensions) | |
142 parser.add_option('--binaryencoding', dest='binaryencoding', choices=['32', '64']) | |
143 parser.add_option('--mzencoding', dest='mzencoding', choices=['32', '64']) | |
144 parser.add_option('--intensityencoding', dest='intensityencoding', choices=['32', '64']) | |
145 parser.add_option('--zlib', dest='zlib', default="false") | |
146 parser.add_option('--filter', dest='filter', action='append', default=[]) | |
147 parser.add_option('--filters_file', dest='filters_file', default=None) | |
148 parser.add_option('--filter_indices_table', default=None) | |
149 parser.add_option('--filter_numbers_table', default=None) | |
150 | |
151 (options, args) = parser.parse_args() | |
152 | |
153 input_file = 'input.%s' % options.fromextension | |
154 copy_to_working_directory(options.input, input_file) | |
155 os.mkdir('output') | |
156 to_extension = options.toextension | |
157 if to_extension.startswith("unindexed_"): | |
158 to_extension = to_extension[len("unindexed_"):] | |
159 to_params = "--noindex" | |
160 else: | |
161 to_params = "" | |
162 cmd = "msconvert --%s %s -o output" % (to_extension, to_params) | |
163 if str_to_bool(options.zlib): | |
164 cmd = "%s %s" % (cmd, "--zlib") | |
165 if options.binaryencoding: | |
166 cmd = "%s --%s" % (cmd, options.binaryencoding) | |
167 if options.mzencoding: | |
168 cmd = "%s --mz%s" % (cmd, options.mzencoding) | |
169 if options.intensityencoding: | |
170 cmd = "%s --inten%s" % (cmd, options.intensityencoding) | |
171 cmd = "%s %s" % (cmd, input_file) | |
172 filters_file_path = _create_filters_file(options) | |
173 cmd = "%s -c %s" % (cmd, filters_file_path) | |
174 print cmd | |
175 execute(cmd) | |
176 output_files = os.listdir('output') | |
177 assert len(output_files) == 1 | |
178 output_file = output_files[0] | |
179 shutil.copy(os.path.join('output', output_file), options.output) | |
180 | |
181 | |
182 if __name__ == '__main__': __main__() |