Mercurial > repos > galaxyp > openms
comparison openms_wrapper.py @ 2:cf0d72c7b482 draft
Update.
author | galaxyp |
---|---|
date | Fri, 10 May 2013 17:31:05 -0400 |
parents | ba86fd127f5a |
children | 1183846e70a1 |
comparison
equal
deleted
inserted
replaced
1:5c65f8116244 | 2:cf0d72c7b482 |
---|---|
2 import sys | 2 import sys |
3 from optparse import OptionParser | 3 from optparse import OptionParser |
4 from ConfigParser import SafeConfigParser | 4 from ConfigParser import SafeConfigParser |
5 from xml.etree import ElementTree | 5 from xml.etree import ElementTree |
6 import subprocess | 6 import subprocess |
7 from re import compile | |
7 | 8 |
8 DEBUG = False | 9 DEBUG = False |
9 | 10 |
10 | 11 |
11 def main(): | 12 def main(): |
12 (options, args) = _parse_args() | 13 (options, args) = _parse_args() |
13 for executable, config_path in zip(options.executables, options.configs): | 14 for executable, config_path in zip(options.executables, options.configs): |
14 _run_openms(executable, config_path) | 15 command_handler = COMMAND_HANDLERS.get(executable, _run_openms) |
16 command_handler(executable, config_path) | |
17 | |
18 | |
19 def _run_shell(executable, config_path): | |
20 command = open(config_path, "r").read().strip() | |
21 if DEBUG: | |
22 print "Running shell command %s" % command | |
23 _exec(command) | |
15 | 24 |
16 | 25 |
17 def _run_openms(executable, config_path): | 26 def _run_openms(executable, config_path): |
18 _exec("%s -write_ini openms.ini" % executable) | 27 _exec("%s -write_ini openms.ini" % executable) |
19 tree = ElementTree.parse("openms.ini") | 28 tree = ElementTree.parse("openms.ini") |
22 tree.write("openms.ini") | 31 tree.write("openms.ini") |
23 if DEBUG: | 32 if DEBUG: |
24 print 'With openms.ini as:\n%s\n, calling: %s -ini openms.ini' % (open("openms.ini", "r").read(), executable) | 33 print 'With openms.ini as:\n%s\n, calling: %s -ini openms.ini' % (open("openms.ini", "r").read(), executable) |
25 _exec("%s -ini openms.ini" % executable) | 34 _exec("%s -ini openms.ini" % executable) |
26 | 35 |
36 COMMAND_HANDLERS = { | |
37 "__SHELL__": _run_shell, | |
38 } | |
39 | |
40 | |
41 def _fail(message, return_code=1): | |
42 print message | |
43 sys.exit(return_code) | |
44 | |
27 | 45 |
28 def _exec(command): | 46 def _exec(command): |
29 proc = subprocess.Popen(args=command, shell=True) | 47 proc = subprocess.Popen(args=command, shell=True) |
30 return_code = proc.wait() | 48 return_code = proc.wait() |
31 if return_code != 0: | 49 if return_code != 0: |
32 sys.exit(return_code) | 50 _fail("Error executing command [%s], return code is %d" % (command, return_code), return_code) |
33 | 51 |
34 | 52 |
35 def _set_options(tree, executable, options): | 53 def _set_options(tree, executable, options): |
36 executable_node = tree.find("./NODE[@name='%s']" % executable) | 54 executable_node = tree.find("./NODE[@name='%s']" % executable) |
55 if executable_node is None: | |
56 _fail("Could not find options for executable %s" % executable) | |
37 options_node = executable_node.find("./NODE[@name='1']") | 57 options_node = executable_node.find("./NODE[@name='1']") |
38 for key, raw_value in options.items("simple_options"): | 58 for key, raw_value in options.items("simple_options"): |
59 if raw_value is None: | |
60 _fail("No value found key %s" % key) | |
39 value = _parse_value(raw_value) | 61 value = _parse_value(raw_value) |
40 _set_option(options_node, key.split("!"), value) | 62 _set_option(options_node, key.split("!"), value) |
41 _set_option(options_node, ["no_progress"], "true", required=False) | 63 _set_option(options_node, ["no_progress"], "true", required=False) |
42 | 64 |
43 | 65 |
44 def _set_option(node, key_parts, value, required=True): | 66 def _set_option(node, key_parts, value, required=True): |
45 key = key_parts[0] | 67 key = key_parts[0] |
46 if len(key_parts) == 1: | 68 if len(key_parts) == 1: |
47 item = node.find("./ITEM[@name='%s']" % key) | 69 if not _set_item_value(node, key, value) and \ |
48 if item is not None: | 70 not _set_list_item_value(node, key, value) and \ |
49 item.set("value", value) | 71 required: |
50 elif required: | 72 _fail("Failed to find specific OpenMS option [%s] in node [%s]" % (key, node)) |
51 raise Exception("Failed to find specific OpenMS option [%s] in node [%s]" % (key, node)) | |
52 else: | 73 else: |
74 if not node: | |
75 _fail("Failed to find specific OpenMS option [%s] in node [%s]" % (key, node)) | |
53 sub_node = node.find("./NODE[@name='%s']" % key) | 76 sub_node = node.find("./NODE[@name='%s']" % key) |
77 if not sub_node: | |
78 _fail("Failed to find node for key %s" % key) | |
54 _set_option(sub_node, key_parts[1:], value, required) | 79 _set_option(sub_node, key_parts[1:], value, required) |
80 | |
81 | |
82 def _set_item_value(node, key, value): | |
83 item = node.find("./ITEM[@name='%s']" % key) | |
84 if item is not None: | |
85 item.set("value", value) | |
86 return item is not None | |
87 | |
88 | |
89 def _set_list_item_value(node, key, values): | |
90 item = node.find("./ITEMLIST[@name='%s']" % key) | |
91 if item is not None: | |
92 for value in values.split(","): | |
93 ElementTree.SubElement(item, "LISTITEM", {"value": value}) | |
94 return item is not None | |
55 | 95 |
56 | 96 |
57 def _parse_value(raw_value): | 97 def _parse_value(raw_value): |
58 value = raw_value | 98 value = raw_value |
59 if raw_value in VALUE_FUNCTIONS: | 99 for pattern, function in VALUE_FUNCTIONS.iteritems(): |
60 value = VALUE_FUNCTIONS[raw_value](raw_value) | 100 try: |
101 match = pattern.match(value) | |
102 except TypeError: | |
103 print "Invalid value found config file %s" % value | |
104 sys.exit(1) | |
105 if match: | |
106 value = function(*match.groups()) | |
107 if value is None: | |
108 print "Failed to properly parse raw value %s" % raw_value | |
109 sys.exit(1) | |
61 return value | 110 return value |
62 | 111 |
63 | 112 |
64 ## Special value parser for various OpenMS components | 113 ## Special value parser for various OpenMS components |
65 def _get_pepnovo_models_path(_): | 114 def _get_pepnovo_models_path(): |
66 pepnovo_path = _get_pepnovo_executable_path(None) | 115 pepnovo_path = _which('PepNovo') |
67 pepnovo_dir = os.path.split(pepnovo_path)[0] | 116 pepnovo_dir = os.path.split(pepnovo_path)[0] |
68 guesses = [os.path.join(pepnovo_dir, 'Models'), | 117 guesses = [os.path.join(pepnovo_dir, 'Models'), |
69 os.path.join(pepnovo_dir, '..', 'Models'), | 118 os.path.join(pepnovo_dir, '..', 'Models'), |
70 os.path.join(pepnovo_dir, '..', 'share', 'pepnovo', 'Models')] | 119 os.path.join(pepnovo_dir, '..', 'share', 'pepnovo', 'Models')] |
71 models_dir = None | 120 models_dir = None |
72 for guess in guesses: | 121 for guess in guesses: |
73 if os.path.isdir(guess): | 122 if os.path.isdir(guess): |
74 models_dir = guess | 123 models_dir = guess |
75 break | 124 break |
76 return models_dir | 125 return models_dir |
77 | |
78 | |
79 def _get_pepnovo_executable_path(_): | |
80 return _which("PepNovo") | |
81 | |
82 VALUE_FUNCTIONS = {"@PEPNOVO_MODELS_PATH@": _get_pepnovo_models_path, | |
83 "@PEPNOVO_EXECUTABLE_PATH@": _get_pepnovo_executable_path} | |
84 | 126 |
85 | 127 |
86 # http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python | 128 # http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python |
87 def _which(program): | 129 def _which(program): |
88 | 130 |
94 if is_exe(program): | 136 if is_exe(program): |
95 return program | 137 return program |
96 else: | 138 else: |
97 for path in os.environ["PATH"].split(os.pathsep): | 139 for path in os.environ["PATH"].split(os.pathsep): |
98 exe_file = os.path.join(path, program) | 140 exe_file = os.path.join(path, program) |
141 print path | |
99 if is_exe(exe_file): | 142 if is_exe(exe_file): |
100 return exe_file | 143 return exe_file |
101 | 144 |
102 return None | 145 return None |
146 | |
147 | |
148 VALUE_FUNCTIONS = {compile(r"\@WHICH\((.*)\)\@"): _which, | |
149 compile(r"\@PEPNOVO_MODELS_PATH\@"): _get_pepnovo_models_path, | |
150 } | |
103 | 151 |
104 | 152 |
105 def _parse_args(): | 153 def _parse_args(): |
106 parser = OptionParser() | 154 parser = OptionParser() |
107 parser.add_option("-e", "--executable", dest="executables", default=[], action="append") | 155 parser.add_option("-e", "--executable", dest="executables", default=[], action="append") |
109 return parser.parse_args() | 157 return parser.parse_args() |
110 | 158 |
111 | 159 |
112 def _load_options(config_path): | 160 def _load_options(config_path): |
113 config_parser = SafeConfigParser() | 161 config_parser = SafeConfigParser() |
162 config_parser.optionxform = str | |
114 config_parser.read(config_path) | 163 config_parser.read(config_path) |
115 return config_parser | 164 return config_parser |
116 | 165 |
117 if __name__ == "__main__": | 166 if __name__ == "__main__": |
118 main() | 167 main() |