Search code examples
pythonsnakemake

Why does setting `wildcard_constraints` in a snakefile prevent the deletion of files marked `temp`?


Consider the following snakefile:

NUMS = ["1", "2"]

#wildcard_constraints:
#    num="\d+"

rule all:
    input:
        "all_text.txt"

rule generate_text:
    output:
        text_file = temp("text_{num}.txt")
    shell:
        """
        echo "test" > {output.text_file}
        """

rule gather_results:
    input:
        expand("text_{num}.txt", num=NUMS)
    output:
        "all_text.txt"
    shell:
        """
        cat {input} > {output}
        """

If I uncomment the wildcard_constraints section, the files marked temp are not deleted.

What could be the cause of this ?

More tests

Putting wildcard_constraints in the rule:

rule generate_text:
    output:
        text_file = temp("text_{num}.txt")
    wildcard_constraints:
        num="\d+"
    shell:
        """
        echo "test" > {output.text_file}
        """

This has the same effect: temp files are not deleted.

Putting the wildcard constraint in the output file name of the generate_text rule:

rule generate_text:
    output:
        text_file = temp("text_{num,\d+}.txt")
    shell:
        """
        echo "test" > {output.text_file}
        """

In this case, temp files are deleted as expected.

Putting the constraint in the input filename of the gather_results rule:

rule gather_results:
    input:
        expand("text_{num,\d+}.txt", num=NUMS)
    output:
        "all_text.txt"
    shell:
        """
        cat {input} > {output}
        """

This results in an error:

WildcardError in line 20 of /tmp/Snakefile:
No values given for wildcard 'num,\d+'.
File "/tmp/Snakefile", line 20, in

I suspect this is due to the use of expand.


Solution

  • I just checked the source code. You actually found a bug. When wildcard constraints are applied, the flags are lost. I have fixed it in the master branch.