Search code examples
snakemake

ambiguousruleexception in snakemake, parms crash of two branches


I have two classes of samples to be processed by different params in my first run, then merge them together in second run. like the following example:

SAMPLES = ['1', '2', '3']
CLASS1 = ['1', '2']
CLASS2 = ['3']

rule all:
    input:
        expand('{class1}.first.txt', class1 = CLASS1),
        expand('{class2}.first.txt', class2 = CLASS2),
        expand('{sample}.second.tx', sample = SAMPLES)

rule first_CLASS1:
    input:
        '{class1}.txt'
    output:
        '{class1}.first.txt'
    shell:
        'touch {wildcards.class1}.first.txt'

rule first_CLASS2:
    input:
        '{class2}.txt'
    output:
        '{class2}.first.txt'
    shell:
        'touch {wildcards.class2}.first.txt'

rule second:
    input:
        '{sample}.first.txt'
    output:
        '{sample}.second.txt'
    shell:
        'touch {wildcards.sample}.second.txt'

but I got the AmbiguousRuleException like this:

AmbiguousRuleException:
Rules first_CLASS2 and first_CLASS1 are ambiguous for the file 1.first.txt.
Consider starting rule output with a unique prefix, constrain your wildcards, or use the ruleorder directive.
Wildcards:
    first_CLASS2: class2=1
    first_CLASS1: class1=1
Expected input files:
    first_CLASS2: 1.txt
    first_CLASS1: 1.txtExpected output files:
    first_CLASS2: 1.first.txt
    first_CLASS1: 1.first.txt

I declared

expand('{class2}.first.txt', class2 = CLASS2)

in run all, and CLASS2 = ['3']. However it reported class2 = 1, which really made me confused.


Solution

  • I think it is because {class1}.first.txt and {class2}.first.txt share the same constant part, i.e. .first.txt and as the error message suggests they are not unique. You can resolve it by constraining the wildcards by putting before the rule all:

    wildcard_constraints:
        class1= '|'.join([re.escape(x) for x in CLASS1]),
        class2= '|'.join([re.escape(x) for x in CLASS2]),
    

    See also this question of mine at https://groups.google.com/forum/#!msg/snakemake/wVlJW9X-9EU/gSZh4U0_CQAJ. Personally I would prefer if the constraints were the default behaviour...