ManJi
ManJi

Reputation: 107

When using the `name` directive, is there a way to get the rule name in `rules` in Snakemake?

Snakemake version:

❯ snakemake --version
8.20.6

Unexpected behavior

rule all:
    input:
        "test2"

rule:
    name: "test",
    output:
        "test",
    shell:
        "touch {output} "

rule:
    name: "test2",
    input: rules.test.output,
    # input: rules._rules["2"].output,
    output:
        "test2",
    shell:
        "touch {output} "

Above code will raise error:

❯ snakemake -n
WorkflowError in file /path/to/Snakefile, line 14:
Rule test is not defined in this workflow. Available rules: all, 2

Although I can access the output using rules._rules["2"].output, this approach seems inappropriate because "2" is changeable.

Question

Is there a way to access rules.test.output or to reach similar goals?

Update

I found I could write like below, it works! But I'm not sure if there's a danger to doing this. Can anyone give me some advice?

rule all:
    input:
        "test2"

rule a:
    name: "test",
    output:
        "test",
    shell:
        "touch {output} "

rule b:
    name: "test2",
    input: rules.a.output,
    output:
        "test2",
    shell:
        "touch {output} "

log:

Building DAG of jobs...
Using shell: /usr/bin/bash
Provided cores: 16
Rules claiming more threads will be scaled down.
Job stats:
job      count
-----  -------
all          1
test         1
test2        1
total        3

Select jobs to execute...
Execute 1 jobs...

[Sat Oct 12 14:33:20 2024]
localrule test:
    output: test
    jobid: 2
    reason: Missing output files: test
    resources: tmpdir=/tmp

touch test
[Sat Oct 12 14:33:20 2024]
Finished job 2.
1 of 3 steps (33%) done
Select jobs to execute...
Execute 1 jobs...

[Sat Oct 12 14:33:20 2024]
localrule test2:
    input: test
    output: test2
    jobid: 1
    reason: Missing output files: test2; Input files updated by another job: test
    resources: tmpdir=/tmp

touch test2
[Sat Oct 12 14:33:20 2024]
Finished job 1.
2 of 3 steps (67%) done
Select jobs to execute...
Execute 1 jobs...

[Sat Oct 12 14:33:20 2024]
localrule all:
    input: test2
    jobid: 0
    reason: Input files updated by another job: test2
    resources: tmpdir=/tmp

[Sat Oct 12 14:33:20 2024]
Finished job 0.
3 of 3 steps (100%) done
Complete log: .snakemake/log/2024-10-12T143320.208718.snakemake.log

Upvotes: 2

Views: 49

Answers (0)

Related Questions