Yujin Kim
Yujin Kim

Reputation: 139

Snakemake ambiguity

I have an ambiguity error and I can't figure out why and how to solve it.

Defining the wildcards:

rule all:
    input:
        xls = expand("reports/{sample}.xlsx", sample = config["samples"]),
        runfolder_xls = expand("{runfolder}.xlsx", runfolder = config["runfolder"])

Actual rules:

rule sample_report:
    input:
        vcf = "vcfs/{sample}.annotated.vcf",
        cov = "stats/{sample}.coverage.gz",
        mod_bed = "tmp/mod_ref_{sample}.bed",
        nirvana_g2t = "/mnt/storage/data/NGS/nirvana_genes2transcripts"
    output:
        "reports/{sample}.xlsx"
    params:
        get_nb_samples()
    log:
        "logs/{sample}.log"
    shell: """
        python /mnt/storage/home/kimy/projects/automate_CP/niles/NILES_create_sample_report.py -v {input.vcf} -c {input.cov} -r {input.mod_bed} -n {input.nirvana_g2t} -r {rule};
        exitcode=$? ;

        if [[ {params} > 1 ]]
        then
            python /mnt/storage/home/kimy/projects/automate_CP/niles/NILES_check_exitcode.py -e $exitcode -r {rule} -n {wildcards.sample}
        elif [[ {params} == 1 ]]
        then 
            python /mnt/storage/home/kimy/projects/automate_CP/niles/NILES_check_exitcode.py -e $exitcode -r sample_mode -n {wildcards.sample}
        else
            python /mnt/storage/home/kimy/projects/automate_CP/niles/NILES_check_exitcode.py -e 1 -r {rule} -n {wildcards.sample}
        fi
    """


rule runfolder_report:
    input:
        sample_sheet = "SampleSheet.csv"
    output:
        "{runfolder}.xlsx"
    log:
        "logs/{runfolder}.log"
    shell: """
        python /mnt/storage/home/kimy/projects/automate_CP/niles/NILES_create_runfolder_report.py -run {wildcards.runfolder} -s {input.sample_sheet} -r {rule} ;
        exitcode=$? ;
        python /mnt/storage/home/kimy/projects/automate_CP/niles/NILES_check_exitcode.py -e $exitcode -r {rule} -n {wildcards.runfolder}
    """

Config file:

runfolder: "CP0340"

samples: ['C014044p', 'C130157', 'C014040p', 'C014054b-1', 'C051198-A', 'C014042p', 'C052007W-C', 'C051198-B', 'C014038p', 'C052004-B', 'C051198-C', 'C052004-C', 'C052003-B', 'C052003-A', 'C052004-A', 'C052002-C', 'C052005-C', 'C052002-A', 'C130157N', 'C052006-B', 'C014063pW', 'C014054b-2', 'C052002-B', 'C052006-C', 'C052007W-B', 'C052003-C', 'C014064bW', 'C052005-B', 'C052006-A', 'C052005-A']

Error:

$ snakemake -n -s ../niles/Snakefile --configfile logs/CP0340_config.yaml
Building DAG of jobs...
AmbiguousRuleException:
Rules runfolder_report and sample_report are ambiguous for the file reports/C014044p.xlsx.
Consider starting rule output with a unique prefix, constrain your wildcards, or use the ruleorder directive.
Wildcards:
        runfolder_report: runfolder=reports/C014044p
        sample_report: sample=C014044p
Expected input files:
        runfolder_report: SampleSheet.csv
        sample_report: vcfs/C014044p.annotated.vcf stats/C014044p.coverage.gz tmp/mod_ref_C014044p.bed /mnt/storage/data/NGS/nirvana_genes2transcriptsExpected output files:
        runfolder_report: reports/C014044p.xlsx
        sample_report: reports/C014044p.xlsx

If I understand Snakemake correctly, the wildcards in the rules are defined in my all rule so I don't understand why the runfolder_report rule tries to put reports/C014044p.xlsx as an output + how the output has a sample name instead of the runfolder name (as defined in the config file).

Upvotes: 1

Views: 255

Answers (2)

dariober
dariober

Reputation: 9062

As the error message suggests, you could assign a distinct prefix to the output of each rule. So your original code will work if you replace {runfolder}.xlsx with, e.g., "runfolder/{runfolder}.xlsx" in rule all and in runfolder_report. Alternatively, constraint the wildcards (my preferred solution) by adding before rule all something like:

wildcard_constraints:
    sample= '|'.join([re.escape(x) for x in config["samples"]]),
    runfolder= re.escape(config["runfolder"]),

The reason for this is that snakemake matches input and output strings using regular expressions (the fine details of how it's done, I must admit, escape me...)

Upvotes: 1

Yujin Kim
Yujin Kim

Reputation: 139

Ok here is my solution:

rule runfolder_report:
    input:
        "SampleSheet.csv"
    output:
        expand("{runfolder}.xlsx", runfolder = config["runfolder"])
    params:
        config["runfolder"]
    log:
        expand("logs/{runfolder}.log", runfolder = config["runfolder"])
    shell: """
        set +e ;

        python /mnt/storage/home/kimy/projects/automate_CP/niles/NILES_create_runfolder_report.py -run {params} -s {input} -r {rule} ;
        exitcode=$? ;
        python /mnt/storage/home/kimy/projects/automate_CP/niles/NILES_check_exitcode.py -e $exitcode -r {rule} -n {params}
    """

However I still don't understand why it had errors and I know that it work previously.

Upvotes: 0

Related Questions