view openms_wrapper.py @ 6:42b843627623 draft default tip

Uploaded
author galaxyp
date Fri, 21 Jun 2013 17:01:53 -0400
parents 1183846e70a1
children
line wrap: on
line source

import os
import sys
from optparse import OptionParser
from ConfigParser import SafeConfigParser
from xml.etree import ElementTree
import subprocess
from re import compile

DEBUG = False


def main():
    (options, args) = _parse_args()
    for executable, config_path in zip(options.executables, options.configs):
        command_handler = COMMAND_HANDLERS.get(executable, _run_openms)
        command_handler(executable, config_path)


def _run_shell(executable, config_path):
    command = open(config_path, "r").read().strip()
    if DEBUG:
        print "Running shell command %s" % command
    _exec(command)


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)

COMMAND_HANDLERS = {
    "__SHELL__": _run_shell,
}


def _fail(message, return_code=1):
    print message
    sys.exit(return_code)


def _exec(command):
    proc = subprocess.Popen(args=command, shell=True)
    return_code = proc.wait()
    if return_code != 0:
        _fail("Error executing command [%s], return code is %d" % (command, return_code), return_code)


def _set_options(tree, executable, options):
    executable_node = tree.find("./NODE[@name='%s']" % executable)
    if executable_node is None:
        _fail("Could not find options for executable %s" % executable)
    options_node = executable_node.find("./NODE[@name='1']")
    for key, raw_value in options.items("simple_options"):
        if raw_value is None:
            _fail("No value found key %s" % key)
        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:
        if not _set_item_value(node, key, value) and \
           not _set_list_item_value(node, key, value) and \
           required:
            _fail("Failed to find specific OpenMS option [%s] in node [%s]" % (key, node))
    else:
        if node is None:
            _fail("Failed to find specific OpenMS option [%s] in node [%s]" % (key, node))
        sub_node = node.find("./NODE[@name='%s']" % key)
        if sub_node is None:
            _fail("Failed to find node for key %s" % key)
        _set_option(sub_node, key_parts[1:], value, required)


def _set_item_value(node, key, value):
    item = node.find("./ITEM[@name='%s']" % key)
    if item is not None:
        item.set("value", value)
    return item is not None


def _set_list_item_value(node, key, values):
    item = node.find("./ITEMLIST[@name='%s']" % key)
    if item is not None:
        for value in values.split(","):
            ElementTree.SubElement(item, "LISTITEM", {"value": value})
    return item is not None


def _parse_value(raw_value):
    value = raw_value
    for pattern, function in VALUE_FUNCTIONS.iteritems():
        try:
            match = pattern.match(value)
        except TypeError:
            print "Invalid value found config file %s" % value
            sys.exit(1)
        if match:
            value = function(*match.groups())
    if value is None:
        print "Failed to properly parse raw value %s" % raw_value
        sys.exit(1)
    return value


## Special value parser for various OpenMS components
def _get_pepnovo_models_path():
    pepnovo_path = _which('PepNovo')
    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


# 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)
            print path
            if is_exe(exe_file):
                return exe_file

    return None


def _mapped_outputs(output_directory, inputs):
    inputs = inputs.split(",")
    os.makedirs(output_directory)
    outputs = [os.path.join(output_directory, os.path.basename(input)) for input in inputs]
    return ",".join(outputs)


VALUE_FUNCTIONS = {compile(r"\@WHICH\((.*)\)\@"): _which,
                   compile(r"\@PEPNOVO_MODELS_PATH\@"): _get_pepnovo_models_path,
                   compile(r"\@MULTI_OUTPUT\(([^,]*),(.*)\)\@"): _mapped_outputs,
                  }


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.optionxform = str
    config_parser.read(config_path)
    return config_parser

if __name__ == "__main__":
    main()