comparison openms_wrapper.py @ 0:ba86fd127f5a draft

Uploaded
author galaxyp
date Wed, 19 Dec 2012 00:32:25 -0500
parents
children cf0d72c7b482
comparison
equal deleted inserted replaced
-1:000000000000 0:ba86fd127f5a
1 import os
2 import sys
3 from optparse import OptionParser
4 from ConfigParser import SafeConfigParser
5 from xml.etree import ElementTree
6 import subprocess
7
8 DEBUG = False
9
10
11 def main():
12 (options, args) = _parse_args()
13 for executable, config_path in zip(options.executables, options.configs):
14 _run_openms(executable, config_path)
15
16
17 def _run_openms(executable, config_path):
18 _exec("%s -write_ini openms.ini" % executable)
19 tree = ElementTree.parse("openms.ini")
20 options = _load_options(config_path)
21 _set_options(tree, executable, options)
22 tree.write("openms.ini")
23 if DEBUG:
24 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)
26
27
28 def _exec(command):
29 proc = subprocess.Popen(args=command, shell=True)
30 return_code = proc.wait()
31 if return_code != 0:
32 sys.exit(return_code)
33
34
35 def _set_options(tree, executable, options):
36 executable_node = tree.find("./NODE[@name='%s']" % executable)
37 options_node = executable_node.find("./NODE[@name='1']")
38 for key, raw_value in options.items("simple_options"):
39 value = _parse_value(raw_value)
40 _set_option(options_node, key.split("!"), value)
41 _set_option(options_node, ["no_progress"], "true", required=False)
42
43
44 def _set_option(node, key_parts, value, required=True):
45 key = key_parts[0]
46 if len(key_parts) == 1:
47 item = node.find("./ITEM[@name='%s']" % key)
48 if item is not None:
49 item.set("value", value)
50 elif required:
51 raise Exception("Failed to find specific OpenMS option [%s] in node [%s]" % (key, node))
52 else:
53 sub_node = node.find("./NODE[@name='%s']" % key)
54 _set_option(sub_node, key_parts[1:], value, required)
55
56
57 def _parse_value(raw_value):
58 value = raw_value
59 if raw_value in VALUE_FUNCTIONS:
60 value = VALUE_FUNCTIONS[raw_value](raw_value)
61 return value
62
63
64 ## Special value parser for various OpenMS components
65 def _get_pepnovo_models_path(_):
66 pepnovo_path = _get_pepnovo_executable_path(None)
67 pepnovo_dir = os.path.split(pepnovo_path)[0]
68 guesses = [os.path.join(pepnovo_dir, 'Models'),
69 os.path.join(pepnovo_dir, '..', 'Models'),
70 os.path.join(pepnovo_dir, '..', 'share', 'pepnovo', 'Models')]
71 models_dir = None
72 for guess in guesses:
73 if os.path.isdir(guess):
74 models_dir = guess
75 break
76 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
85
86 # http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
87 def _which(program):
88
89 def is_exe(fpath):
90 return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
91
92 fpath, fname = os.path.split(program)
93 if fpath:
94 if is_exe(program):
95 return program
96 else:
97 for path in os.environ["PATH"].split(os.pathsep):
98 exe_file = os.path.join(path, program)
99 if is_exe(exe_file):
100 return exe_file
101
102 return None
103
104
105 def _parse_args():
106 parser = OptionParser()
107 parser.add_option("-e", "--executable", dest="executables", default=[], action="append")
108 parser.add_option("-c", "--config", dest="configs", default=[], action="append")
109 return parser.parse_args()
110
111
112 def _load_options(config_path):
113 config_parser = SafeConfigParser()
114 config_parser.read(config_path)
115 return config_parser
116
117 if __name__ == "__main__":
118 main()