10

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.

taras
  • 6,566
  • 10
  • 39
  • 50
glihm
  • 1,138
  • 13
  • 29

2 Answers2

3

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

gutorm
  • 99
  • 6
  • Thanks for the workaround @gutorm. This feature would be so nice to have for the `log` parameter... – mrhd May 19 '20 at 17:20
1

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

Andreas
  • 716
  • 4
  • 14
  • Thank you for your answer. No, it's not working. rule.name is defined when input and output are parsed by snakemake. I have checked the Rules object (which contains all the rules and their attributes) and it appears only after output definition. But thank you for the issue you linked, I will create a new one for this. Except if one other person found a trick for that. – glihm Dec 06 '16 at 00:23
  • Was just coming across exactly this problem. It's still not possible to access the rule name before the run/shell block. Now there's an issue on Github tracking it: https://github.com/snakemake/snakemake/issues/156 – mrhd May 19 '20 at 17:19