annotate openms_wrapper.py @ 1:5c65f8116244 draft

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