glihm
glihm

Reputation: 1246

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.

Upvotes: 11

Views: 2290

Answers (2)

gutorm
gutorm

Reputation: 109

(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.

Upvotes: 3

Andreas
Andreas

Reputation: 736

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

Upvotes: 1

Related Questions