rule name can't be accessed by {rule} in shell command

Create issue
Issue #199 resolved
Tao Wen created an issue

{rule} works in message keyword: message:"Running Stage: {rule}"

but it doesn't work in the following command: shell("echo 'Running Stage: {rule}'")

I got the following error: The name 'rule' is unknown in this context. Please make sure that you defined that variable. Also note that braces not used for variable access have to be escaped by repeating them, i.e. {{print $1}}

Comments (11)

  1. Pierre Bertin

    To follow this issue thoughts, can we imagine an access to the rule name when the input and output are defined?

    In order to have this shortcut:

    rule myrule:
        input: config[ rule.name ]
        ...
    

    To avoid that hardcoding:

    rule myrule:
        input: config["myrule"]
        ...
    

    Because the wildcard rules.myrule.name is available only after input and output definitions. In my case, all rules have a key in the config file with the exact same name. It can be a very fast way to get the rule name without hardcoding.

    Thanks for help ! ;)

  2. Johannes Köster

    The latter is not possible, because of the way Snakefiles are parsed and translated into Python.

  3. Michael Hall

    Is it possible to get {rule.name} working in the log definition? This is where I tend to use it the most. I.e

    rule a:
        output: "foo.txt"
        log: "logs/a.log"
        shell:
            "echo 'bar' > {output} 2> {log}"
    

    It would be great to be able to do

    rule a:
        output: "foo.txt"
        log: "logs/{rule.name}.log"
        shell:
            "echo 'bar' > {output} 2> {log}"
    
  4. Christian Arnold

    I totally agree, I raised this in another Issue a while ago but it was not implemented as of now I think... Would be a nice convenience feature . Same for benchmark

  5. Fong Chun Chan

    Being able to access the rule name in the log directive would be useful. I usually have a log directory for each rule that is named after the rule. So not needing to repeat the rule name in the log would be useful.

  6. Log in to comment