diff openms_wrapper.py @ 0:ba86fd127f5a draft

Uploaded
author galaxyp
date Wed, 19 Dec 2012 00:32:25 -0500
parents
children cf0d72c7b482
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/openms_wrapper.py	Wed Dec 19 00:32:25 2012 -0500
@@ -0,0 +1,118 @@
+import os
+import sys
+from optparse import OptionParser
+from ConfigParser import SafeConfigParser
+from xml.etree import ElementTree
+import subprocess
+
+DEBUG = False
+
+
+def main():
+    (options, args) = _parse_args()
+    for executable, config_path in zip(options.executables, options.configs):
+        _run_openms(executable, config_path)
+
+
+def _run_openms(executable, config_path):
+    _exec("%s -write_ini openms.ini" % executable)
+    tree = ElementTree.parse("openms.ini")
+    options = _load_options(config_path)
+    _set_options(tree, executable, options)
+    tree.write("openms.ini")
+    if DEBUG:
+        print 'With openms.ini as:\n%s\n, calling: %s -ini openms.ini' % (open("openms.ini", "r").read(), executable)
+    _exec("%s -ini openms.ini" % executable)
+
+
+def _exec(command):
+    proc = subprocess.Popen(args=command, shell=True)
+    return_code = proc.wait()
+    if return_code != 0:
+        sys.exit(return_code)
+
+
+def _set_options(tree, executable, options):
+    executable_node = tree.find("./NODE[@name='%s']" % executable)
+    options_node = executable_node.find("./NODE[@name='1']")
+    for key, raw_value in options.items("simple_options"):
+        value = _parse_value(raw_value)
+        _set_option(options_node, key.split("!"), value)
+    _set_option(options_node, ["no_progress"], "true", required=False)
+
+
+def _set_option(node, key_parts, value, required=True):
+    key = key_parts[0]
+    if len(key_parts) == 1:
+        item = node.find("./ITEM[@name='%s']" % key)
+        if item is not None:
+            item.set("value", value)
+        elif required:
+            raise Exception("Failed to find specific OpenMS option [%s] in node [%s]" % (key, node))
+    else:
+        sub_node = node.find("./NODE[@name='%s']" % key)
+        _set_option(sub_node, key_parts[1:], value, required)
+
+
+def _parse_value(raw_value):
+    value = raw_value
+    if raw_value in VALUE_FUNCTIONS:
+        value = VALUE_FUNCTIONS[raw_value](raw_value)
+    return value
+
+
+## Special value parser for various OpenMS components
+def _get_pepnovo_models_path(_):
+    pepnovo_path = _get_pepnovo_executable_path(None)
+    pepnovo_dir = os.path.split(pepnovo_path)[0]
+    guesses = [os.path.join(pepnovo_dir, 'Models'),
+               os.path.join(pepnovo_dir, '..', 'Models'),
+               os.path.join(pepnovo_dir, '..', 'share', 'pepnovo', 'Models')]
+    models_dir = None
+    for guess in guesses:
+        if os.path.isdir(guess):
+            models_dir = guess
+            break
+    return models_dir
+
+
+def _get_pepnovo_executable_path(_):
+    return _which("PepNovo")
+
+VALUE_FUNCTIONS = {"@PEPNOVO_MODELS_PATH@": _get_pepnovo_models_path,
+                   "@PEPNOVO_EXECUTABLE_PATH@": _get_pepnovo_executable_path}
+
+
+# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
+def _which(program):
+
+    def is_exe(fpath):
+        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
+
+    fpath, fname = os.path.split(program)
+    if fpath:
+        if is_exe(program):
+            return program
+    else:
+        for path in os.environ["PATH"].split(os.pathsep):
+            exe_file = os.path.join(path, program)
+            if is_exe(exe_file):
+                return exe_file
+
+    return None
+
+
+def _parse_args():
+    parser = OptionParser()
+    parser.add_option("-e", "--executable", dest="executables", default=[], action="append")
+    parser.add_option("-c", "--config", dest="configs", default=[], action="append")
+    return parser.parse_args()
+
+
+def _load_options(config_path):
+    config_parser = SafeConfigParser()
+    config_parser.read(config_path)
+    return config_parser
+
+if __name__ == "__main__":
+    main()