How to use the snakemake.parser.parse function in snakemake

To help you get started, we’ve selected a few snakemake examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github JetBrains-Research / snakecharm / snakemake2py.py View on Github external
description="Converts *.smk file to *.py using snakemake parser",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument("input", metavar="INPUT",
                        help="Snakemake file (or *.smk file)")

    parser.add_argument("output", metavar="OUTPUT",
                        help="Result python file")

    args = parser.parse_args()
    input_path = args.input
    output_path = args.output

    ########################################################################

    compilation, _linemap, rc = smp.parse(input_path)
    with open(output_path, mode="w") as out:
        print(compilation, file=out)
    print("Done, rules:", str(rc))
    print("Saved to :", output_path)
github nh13 / snakeparse / src / snakeparse / api.py View on Github external
# Compile and execute it!
        from snakemake.workflow import Workflow

        snakefile = str(workflow.snakefile)

        # create a config with an undefined argument file so that parsing is skipped
        global config
        config = dict([(SnakeParse.ARGUMENT_FILE_NAME_KEY, None)])  # type: ignore
        # add the config to a globals copy
        globals_copy = dict(globals())
        globals_copy['config'] = dict([(SnakeParse.ARGUMENT_FILE_NAME_KEY, None)])
        # parsing with snakemake requires there to be a global workflow object
        globals_copy['workflow'] = Workflow(snakefile=snakefile)

        # compile the snakefile using snakemake's parse method
        code, linemap, rulecount = snakemake_parser.parse(snakefile)
        code = compile(code, snakefile, 'exec')
        try:
            exec(code, globals_copy)
        except Exception as e:
            # in the case of required parser arguments, we may get some type
            # of exception
            # if not str(e).startswith("'Namespace'"):
            #     raise e
            exec_exception = SnakeParseException(f'Could not compile {snakefile}', e)

        def classes_predicate(obj: Any) -> bool:
            return inspect.isclass(obj) and \
                not inspect.isabstract(obj) and \
                SnakeParser in inspect.getmro(obj)

        def methods_predicate(key: str, obj: Any) -> bool:
github percyfal / snakemake-rules / snakemake_rules / core / ruleinfo.py View on Github external
def parse_rule(rule, prefix=None):
    """Generate information for rule stored in a dictionary.

    Params:
      rule (str): file containing a snakemake rule 
    
    Results:
      input (list): list of input files
      output (list): list of output targets
    """
    d = {}
    codemap = {'rule': "", 'input': "", 'output': "", 'wildcard_constraints': ""}
    rn = os.path.basename(rule).replace(".rule", "")
    app = os.path.basename(os.path.dirname(rule))
    code, linemake, rulecount = parse(rule)

    l = regex_workflow.findall(code)
    for k, v in l:
        codemap[k] = re.sub("[\t\n ]", "", v)

    m_name = regex_name.search(codemap['rule'])
    d['name'] = m_name.group("name")
    d['output'] = get_targets(codemap["output"])
    d['input'] = get_targets(codemap["input"])
    d['wildcard_constraints'] = {k:v for k, v in regex_wildcard_constraints.findall(codemap["wildcard_constraints"])}
    if d['wildcard_constraints'] == '':
        d['wildcard_constraints'] = {}

    # Output missing; return input case if possible
    return d