just fixed the print() issue locally and ran into the next problems which were easy to fix:

- ConfigParser -> configparser
- iteritems -> items

(The modified sources are attached if they are of interest)

Now I have an error that seems to be deeper:

Failed to find specific OpenMS option [shuffle] in node [<Element 'NODE' at 0x2b96a3b1c048>]

Any ideas?



Matthias Bernt
Bioinformatics Service
Molekulare Systembiologie (MOLSYB)
Helmholtz-Zentrum für Umweltforschung GmbH - UFZ/
Helmholtz Centre for Environmental Research GmbH - UFZ
Permoserstraße 15, 04318 Leipzig, Germany
Phone +49 341 235 482296,
m.be...@ufz.de, www.ufz.de

Sitz der Gesellschaft/Registered Office: Leipzig
Registergericht/Registration Office: Amtsgericht Leipzig
Handelsregister Nr./Trade Register Nr.: B 4703
Vorsitzender des Aufsichtsrats/Chairman of the Supervisory Board: MinDirig Wilfried Kraus
Wissenschaftlicher Geschäftsführer/Scientific Managing Director:
Prof. Dr. Dr. h.c. Georg Teutsch
Administrative Geschäftsführerin/ Administrative Managing Director:
Prof. Dr. Heike Graßmann
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)

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)
    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)

    "__SHELL__": _run_shell,

def _fail(message, return_code=1):
    print( message )

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 \
            _fail("Failed to find specific OpenMS option [%s] in node [%s]" % (key, node))
        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.items():
            match = pattern.match(value)
        except TypeError:
            print( "Invalid value found config file %s" % value )
        if match:
            value = function(*match.groups())
    if value is None:
        print( "Failed to properly parse raw value %s" % raw_value )
    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
    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
        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(",")
    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
    return config_parser

if __name__ == "__main__":
Please keep all replies on the list by using "reply all"
in your mail client.  To manage your subscriptions to this
and other Galaxy lists, please use the interface at:

To search Galaxy mailing lists use the unified search at:

Reply via email to