How to use the snakemake.workflow.Workflow 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 nh13 / snakeparse / src / snakeparse / api.py View on Github external
exec_exception = None

        # 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 \
github percyfal / snakemake-rules / scripts / syncrules.py View on Github external
def create_workflow(snakefile):
    workflow = Workflow(snakefile=snakefile, use_conda=True)

    try:
        workflow.include(snakefile,
                         overwrite_first_rule=True,
                         print_compilation=False)
        workflow.check()
    except (Exception, BaseException) as ex:
        print_exception(ex, workflow.linemaps)
        success = False

    return workflow
github percyfal / snakemakelib / snakemakelib / rules.py View on Github external
"""Create rule from a template.
    
    Create rule from existing rule and add it to the workflow. By
    passing keyword arguments it is also possible to update/modify the
    input, output and/or params.

    Args:
        name (str): name of new rule
        template (str): name of existing template rule
        workflow (:class:`Workflow `): snakemake workflow
        kw (dict): keyword argument for updating input, output and/or params
        
    Returns:
        None
    """
    assert type(workflow) is  Workflow, "workflow is not a Workflow: {}".format(workflow)
    try:
        rule = copy.copy(workflow.get_rule(template))
        rule.name = name
    except:
        smllogger.warn("no such template rule '{}'; make sure you have included the template rule file".format(template))
        raise
    workflow.add_rule(name=rule.name)
    workflow._rules[name] = rule
    if kw.get('output'):
        assert type(kw['output']) is tuple, "output argument must be a tuple of type (tuple, dict)"
        rule._output = OutputFiles()
        workflow._rules[name].set_output(*kw['output'][0], **kw['output'][1])
    if kw.get('input'):
        assert type(kw['input']) is tuple, "input argument must be a tuple of type (tuple, dict)"
        workflow._rules[name].set_input(*kw['input'][0], **kw['input'][1])
    if kw.get('params'):
github epruesse / ymp / src / ymp / snakemake.py View on Github external
'format': 'callable',
    },
    'script': {
        'format': 'string',
    }
    # restart_times
    # env_modules
    # shadow_depth
    # group
    # notebook
    # cwl
    # cache
}


class ExpandableWorkflow(Workflow):
    """Adds hook for additional rule expansion methods to Snakemake"""
    global_workflow = None
    __expanders = []

    @classmethod
    def activate(cls):
        """Installs the ExpandableWorkflow

        Replaces the Workflow object in the snakemake.workflow module
        with an instance of this class and initializes default expanders
        (the snakemake syntax).
        """
        check_snakemake()

        try:
            from snakemake.workflow import workflow