AmbiguousRuleException when there is no ambiguity

Viewed 55

In this Snakemake script the rule all defines a target, and there are three other rules that claim this target as an output:

rule all:
    input:
         "target.txt"


rule from_non_existing_file:
    input:
        "non_existing_file.txt"
    output:
        "target.txt"


rule broad_input:
    output:
        "target.txt"


rule narrow_input:
    input:
        "optional_input.txt"
    output:
        "target.txt"

ruleorder: narrow_input > broad_input

The file non_existing_file.txt doesn't exist, so the rule from_non_existing_file should not be regarded by Snakemake. The rule broad_input has no input files, so it always can produce the output, and the rule narrow_input can produce the output whenever the file optional_input.txt exists. To resolve the ambiguity between broad and narrow inputs, the ruleorder is defined.

Whenever the file optional_input.txt exists, the script prefers the rule narrow_input:

Job counts:
        count   jobs
        1       all
        1       narrow_input
        2

This script works most of the times, but sometimes it fails:

AmbiguousRuleException:
Rules narrow_input and broad_input are ambiguous for the file target.txt.
Consider starting rule output with a unique prefix, constrain your wildcards, or use the ruleorder directive.
Wildcards:
        narrow_input:
        broad_input:
Expected input files:
        narrow_input: optional_input.txt
        broad_input: Expected output files:
        narrow_input: target.txt
        broad_input: target.txt

Here Snakemake ignores the fact that the ruleorder directive is defined, and advises to define it again.

To confirm this behavior I've designed the test script below:

import os

def test_snakemake():
    for i in range(100):
        rcode = os.system("snakemake --cores=1 --printshellcmds --forceall --dry-run")
        assert(rcode == 0)

This test fails within first 20 iterations with high confidence.

I've conducted some experiments and got surprising results:

  • The test pass if optional_input.txt doesn't exist
  • The test pass if any of the three rules is removed

This problem is confirmed on two different Windows machines with Snakemake versions 5.7.4 and 6.5.3.

My question is whether that is a Snakemake bug. Is there another explanation of this behavior?

0 Answers
Related