Ejemplo n.º 1
0
def parseCTDCommandLine(argv, model, openms_param):
    # Configure CTDOpt to use OpenMS style on the command line.
    directives = parse_cl_directives(argv,
                                     input_ctd='ini',
                                     write_tool_ctd='write_ini',
                                     prefix='-')

    if directives[
            "write_tool_ctd"] is not None:  # triggered if -write_ini was provided on CML
        # if called with -write_ini write CTD
        model.write_ctd(directives["write_tool_ctd"])
        exit(0)
    elif directives["input_ctd"] is not None:  # read ctd/ini file
        model = CTDModel(from_file=directives["input_ctd"])
        #        print(model.get_defaults())

        param = pms.Param()
        fh = pms.ParamXMLFile()
        fh.load(directives["input_ctd"], param)
        openms_param.update(param, True)
        return model.get_defaults(), openms_param

    else:  # only command line options provided
        temp = tempfile.NamedTemporaryFile(
            suffix='ini')  # makes sure we get a writable file
        tmp_name = temp.name
        temp.close()  # removes the file

        model.write_ctd(tmp_name)
        param = pms.Param()
        fh = pms.ParamXMLFile()
        fh.load(tmp_name, param)
        openms_param.update(param)
        os.remove(tmp_name)
        return model.parse_cl_args(argv), openms_param
Ejemplo n.º 2
0
def main():

    # register command line arguments
    model = CTDModel(
        name='NameOfThePyTOPPTool',  # required
        version='1.0',  # required
        description=
        'This is an example tool how to write pyTOPP tools compatible with the OpenMS workflow ecosystem.',
        manual='RTF',
        docurl='http://dummy.url/docurl.html',
        category='Example',
        executableName='exampletool',
        executablePath='/path/to/exec/exampletool-1.0/exampletool')

    # Register in / out etc. with CTDModel
    model.add(
        'input',
        required=True,
        type='input-file',
        is_list=False,
        file_formats=['mzML'],  # filename restrictions
        description='Input file')

    model.add(
        'output',
        required=True,
        type='output-file',
        is_list=False,
        file_formats=['mzML'],  # filename restrictions
        description='Output file')

    defaults = pms.PeakPickerHiRes().getDefaults()

    # expose algorithm parameters in command line options
    addParamToCTDopts(defaults, model)

    # parse command line
    # if -write_ini is provided, store model in CTD file, exit with error code 0
    # if -ini is provided, load CTD file into defaults Param object and return new model with paraneters set as defaults
    arg_dict, openms_params = parseCTDCommandLine(sys.argv, model, defaults)

    # data processing
    fh = pms.MzMLFile()
    fh.setLogType(pms.LogType.CMD)
    input_map = pms.MSExperiment()

    fh.load(arg_dict["input"], input_map)

    pp = pms.PeakPickerHiRes()
    pp.setParameters(openms_params)
    out_map = pms.MSExperiment()
    pp.pickExperiment(input_map, out_map)

    out_map = addDataProcessing(
        out_map, openms_params,
        pms.DataProcessing.ProcessingAction.PEAK_PICKING)
    fh = pms.FileHandler()
    fh.storeExperiment(arg_dict["output"], out_map)
Ejemplo n.º 3
0
def parse_input_ctds(xsd_location, input_ctds, output_destination,
                     output_file_extension):
    is_converting_multiple_ctds = len(input_ctds) > 1
    parsed_ctds = []
    schema = None
    if xsd_location is not None:
        try:
            logger.info("Loading validation schema from %s" % xsd_location, 0)
            schema = etree.XMLSchema(etree.parse(xsd_location))
        except Exception as e:
            logger.error(
                "Could not load validation schema %s. Reason: %s" %
                (xsd_location, str(e)), 0)
    else:
        logger.warning("Validation against a schema has not been enabled.", 0)

    for input_ctd in input_ctds:
        if schema is not None:
            validate_against_schema(input_ctd, schema)

        output_file = output_destination
        # if multiple inputs are being converted, we need to generate a different output_file for each input
        if is_converting_multiple_ctds:
            output_file = os.path.join(
                output_file,
                get_filename_without_suffix(input_ctd) + "." +
                output_file_extension)
        logger.info("Parsing %s" % input_ctd)

        model = None
        try:
            model = CTDModel(from_file=input_ctd)
        except ModelTypeError:
            pass
        try:
            model = Parameters(from_file=input_ctd)
        except ModelTypeError:
            pass
        assert model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (
            input_ctd)

        parsed_ctds.append(ParsedCTD(model, input_ctd, output_file))

    return parsed_ctds
Ejemplo n.º 4
0
from CTDopts.CTDopts import CTDModel

model = CTDModel(
    name='exampleTool',  # required
    version='1.0',  # required
    description='This is an example tool presenting CTDopts usage',
    manual='manual string',
    docurl='http://dummy.url/docurl.html',
    category='testing',
    executableName='exampletool',
    executablePath='/path/to/exec/exampletool-1.0/exampletool')

# if here, it's all OK!
# do NOT change this output string! FindCTDopts.cmake relies on stdout being exactly this string!
print("CTDopts seems to be working.")
Ejemplo n.º 5
0
import pprint
import os
import tempfile

from CTDopts.CTDopts import CTDModel, args_from_file, parse_cl_directives, flatten_dict, override_args, ArgumentRestrictionError

#The tool's model
msgf_percolator_model = CTDModel(
    name='Start_Percolator',
    version='0.1',
    description='This tool converts a mzid file to a tab file, starts the percolator and converts the percolators pout '
                'file to mzid')

msgf_percolator_model.add(
    name='ctd',
    type=str,
    default='false',
    description='Path to CTD file if already available'
)

######## mzid2pin ################

mzid2pinparams = msgf_percolator_model.add_group('mzid2pin', 'Grouped settings')

mzid2pinparams.add(
    'mzid2pin_path',
    required=True,
    type=str,
    description='set the path to your mzid2pin binary'
)
Ejemplo n.º 6
0
                        default=None,
                        required=True)
    parser.add_argument("--ctd_file",
                        dest="ctd_file",
                        help="input ctd file"
                        "if given then optional parameters from the ini file"
                        "will be filled with the defaults from this CTD file",
                        metavar='CTD',
                        default=None,
                        required=False)
    args, cliargs = parser.parse_known_args()

    # load CTDModel
    ini_model = None
    try:
        ini_model = CTDModel(from_file=args.ini_file)
    except ModelTypeError:
        pass
    try:
        ini_model = Parameters(from_file=args.ini_file)
    except ModelTypeError:
        pass
    assert ini_model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (
        args.ini_file)

    # get a dictionary of the ctd arguments where the values of the parameters
    # given on the command line are overwritten
    ini_values = ini_model.parse_cl_args(cl_args=cliargs, ignore_required=True)

    if args.ctd_file:
        ctd_model = CTDModel(from_file=args.ctd_file)
Ejemplo n.º 7
0

# let's set up a PrettyPrinter so nested dictionaries are easier to follow later
pp = pprint.PrettyPrinter(indent=4)
pretty_print = pp.pprint

# First, we'll set up a CTD model. There are two different ways to do that:
#    1. Define it in Python using CTDopts.CTDModel's methods
#    2. load it from a CTD file

# Every CTD Model has to have at least a name and a version, plus any of the optional attributes below them.
model = CTDModel(
    name='exampleTool',  # required
    version='1.0',  # required
    description='This is an example tool presenting CTDopts usage',
    manual='manual string',
    docurl='http://dummy.url/docurl.html',
    category='testing',
    executableName='exampletool',
    executablePath='/path/to/exec/exampletool-1.0/exampletool'
    )

# The parameters of the tool have to be registered the following way:
model.add(
    'positive_int',  # parameter name
    type=int,  # parameter type. For a list of CTD-supported types see CTDopts.CTDTYPE_TO_TYPE.keys()
    num_range=(0, None),  # numeric range restriction: tuple with minimum and maximum values. None means unlimited
    default=5,
    tags=['advanced', 'magic'],  # for certain workflow engines that make use of parameter tags
    description='A positive integer parameter'
    )
Ejemplo n.º 8
0
# insert the hc_args into the args
mergeDicts(args, hc_args)

if "adv_opts_cond" in args:
    args.update(args["adv_opts_cond"])
    del args["adv_opts_cond"]

# IDMapper has in and spectra:in params, in is used in out as format_source",
# which does not work in Galaxy: https://github.com/galaxyproject/galaxy/pull/9493"
# therefore hardcoded params change the name of spectra:in to spectra:_in
# which is corrected here again
# TODO remove once PR is in and adapt profile accordingly
fix_underscores(args)

model = CTDModel(from_file=input_ctd)

# transform values from json that correspond to
# - old style booleans (string + restrictions) -> transformed to a str
# - new style booleans that get a string (happens for hidden parameters [-test])
#   are transformed to a bool
# - unrestricted ITEMLIST which are represented as strings
#   ("=quoted and space separated) in Galaxy -> transform to lists
# - optional data input parameters that have defaults and for which no
#   value is given -> overwritte with the default
for p in model.get_parameters():

    # check if the parameter is in the arguments from the galaxy tool
    # (from the json file(s)), since advanced parameters are absent
    # if the conditional is set to basic parameters
    try:
Ejemplo n.º 9
0
from CTDopts.CTDopts import CTDModel, args_from_file, parse_cl_directives, flatten_dict, override_args, ArgumentRestrictionError

# let's set up a PrettyPrinter so nested dictionaries are easier to follow later
pp = pprint.PrettyPrinter(indent=4)
pretty_print = pp.pprint

# First, we'll set up a CTD model. There are two different ways to do that:
#    1. Define it in Python using CTDopts.CTDModel's methods
#    2. load it from a CTD file

# Every CTD Model has to have at least a name and a version, plus any of the optional attributes below them.
model = CTDModel(
    name='exampleTool',  # required
    version='1.0',  # required
    description='This is an example tool presenting CTDopts usage',
    manual='manual string',
    docurl='http://dummy.url/docurl.html',
    category='testing',
    executableName='exampletool',
    executablePath='/path/to/exec/exampletool-1.0/exampletool')

# The parameters of the tool have to be registered the following way:
model.add(
    'positive_int',  # parameter name
    type=
    int,  # parameter type. For a list of CTD-supported types see CTDopts.CTDTYPE_TO_TYPE.keys()
    num_range=(
        0, None
    ),  # numeric range restriction: tuple with minimum and maximum values. None means unlimited
    default=5,
    tags=['advanced', 'magic'
Ejemplo n.º 10
0
# parser = ArgumentParser(prog="mock.py", description="MOCK", add_help=True)
# parser.add_argument("-w", "-write_ctd", dest="write_ctd", metavar='PATH', default=None, required=False,
#                     help="Write CTD to given path")
# parser.add_argument("-i", "-ini", dest="ini", metavar='CTDFILE', default=None, required=False,
#                     help="Process CTDFILE")
# parser.add_argument('moreargs', metavar='ARGS', type=str, nargs='*', help='more arguments')
# args = parser.parse_args()

wd = os.path.dirname(__file__)
bn = os.path.splitext(os.path.basename(__file__))[0]

if sys.argv[1] == "-write_ctd":
    shutil.copyfile(os.path.join(wd, bn + ".ctd"), os.path.join(sys.argv[2], bn + ".ctd"))
elif sys.argv[1] == "-ini":
    fparam = {"input": set(), "output": set()}
    model = CTDModel(from_file=sys.argv[2])
    for p in model.get_parameters():
        cli = ":".join(p.get_lineage(name_only=True))
        if p.type is _InFile:
            fparam["input"].add(cli)
        elif p.type is _OutFile:
            fparam["output"].add(cli)

    i = 3
    while i < len(sys.argv):
        if sys.argv[i].startswith("-"):
            param = sys.argv[i][1:]
            if param in fparam["input"] or param in fparam["output"]:
                if param in fparam["input"]:
                    mode = "r"
                else:
Ejemplo n.º 11
0
            schema = etree.XMLSchema(etree.parse(xsd_location))
        except Exception, e:
            error("Could not load validation schema %s. Reason: %s" % (xsd_location, str(e)), 0)
    else:
        warning("Validation against a schema has not been enabled.", 0)

    for input_ctd in input_ctds:
        if schema is not None:
            validate_against_schema(input_ctd, schema)

        output_file = output_destination
        # if multiple inputs are being converted, we need to generate a different output_file for each input
        if is_converting_multiple_ctds:
            output_file = os.path.join(output_file, get_filename_without_suffix(input_ctd) + "." + output_file_extension)
        info("Parsing %s" % input_ctd)
        parsed_ctds.append(ParsedCTD(CTDModel(from_file=input_ctd), input_ctd, output_file))

    return parsed_ctds


def flatten_list_of_lists(args, list_name):
    setattr(args, list_name, [item for sub_list in getattr(args, list_name) for item in sub_list])


def validate_against_schema(ctd_file, schema):
    try:
        parser = etree.XMLParser(schema=schema)
        etree.parse(ctd_file, parser=parser)
    except etree.XMLSyntaxError, e:
        raise ApplicationException("Invalid CTD file %s. Reason: %s" % (ctd_file, str(e)))
Ejemplo n.º 12
0
    parser = ArgumentParser(prog="fill_ctd_clargs",
                            description="fill command line arguments"
                            "into a CTD file and write the CTD file to",
                            add_help=False,
                            allow_abbrev=False)
    parser.add_argument("--ctd",
                        dest="ctd",
                        help="input ctd file",
                        metavar='CTD',
                        default=None,
                        required=True)
    args, cliargs = parser.parse_known_args()
    # load CTDModel
    model = None
    try:
        model = CTDModel(from_file=args.ctd)
    except ModelTypeError:
        pass
    try:
        model = Parameters(from_file=args.ctd)
    except ModelTypeError:
        pass
    assert model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (
        args.ctd)

    # get a dictionary of the ctd arguments where the values of the parameters
    # given on the command line are overwritten
    margs = model.parse_cl_args(cl_args=cliargs, ignore_required=True)

    # write the ctd with the values taken from the dictionary
    out = StringIO()
Ejemplo n.º 13
0
        warning("Validation against a schema has not been enabled.", 0)

    for input_ctd in input_ctds:
        if schema is not None:
            validate_against_schema(input_ctd, schema)

        output_file = output_destination
        # if multiple inputs are being converted, we need to generate a different output_file for each input
        if is_converting_multiple_ctds:
            output_file = os.path.join(
                output_file,
                get_filename_without_suffix(input_ctd) + "." +
                output_file_extension)
        info("Parsing %s" % input_ctd)
        parsed_ctds.append(
            ParsedCTD(CTDModel(from_file=input_ctd), input_ctd, output_file))

    return parsed_ctds


def flatten_list_of_lists(args, list_name):
    setattr(
        args, list_name,
        [item for sub_list in getattr(args, list_name) for item in sub_list])


def validate_against_schema(ctd_file, schema):
    try:
        parser = etree.XMLParser(schema=schema)
        etree.parse(ctd_file, parser=parser)
    except etree.XMLSyntaxError, e: