Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current-rule's name in Snakemake

I am working with Snakemake and I can't find a way to access to the current-rule's name.

For instance, is there a way to have an access like this:

rule job1:
    input: check_inputs(rules.current.name)
    output: ...

This can be very helpful when the check_inputs function is more or less the same for each rules.

For sure, I made this and it works:

rule job1:
    input: check_inputs("job1")
    output: ...

However, I was wondering that if a more "Snakemaker way" to get the current-rule's name exists to avoid writing / hardcoding the rule's name each time.

Any kind of help or suggestion will be highly appreciated.

--- EDIT1 ---
The rule name is accessible via {rules.myrule.name} only when the input and output statements are parsed by snakemake. So the use of {rules.myrule.name} is not possible in input/output definition.

The idea is to have a quick access to the current rule's name {rules.current} for instance, because {rules.myrule.name} is also repetitive.

like image 744
glihm Avatar asked Dec 05 '16 13:12

glihm


People also ask

How do you run one rule in Snakemake?

-R selects the one rule (and all its dependent rules also!), -n does a "dry run", it just prints what it would do without -n.

What is a Snakefile?

Snakefiles are Python code. Completing the Pipeline. Resources and parallelism. Make your workflow portable and reduce duplication. Scaling a pipeline across a cluster.


2 Answers

(Edit: Proposed a workaround)

{rule}can be used for rulename during shell:/run: directives. As op stated, this does not work in input/output:. However the current template is a work-araound

myrule = "foo"
rule foo:
    output: touch(myrule + ".ok")
    shell:
        'echo "I am {rule}, making {output}"'

In the example above, introducing the variable myrule is unnecessary since it is used only once. But it makes more sense when you want to use the rule-name several times in the various snakemake directives. And it also facilitates rule-templating.

like image 184
gutorm Avatar answered Oct 07 '22 03:10

gutorm


I thought rule.name should work, but it looks like it's just rule, which however can't be used in all contexts: See https://bitbucket.org/snakemake/snakemake/issues/199/rule-name-cant-be-accessed-by-rule-in

Andreas

like image 28
Andreas Avatar answered Oct 07 '22 03:10

Andreas