'Snakemake: Pass all wildcards to a single shell command

Snakemake supports generalizing rules with wildcards, like so:

rule conversion:
    input:
        "file_{name}.txt"
    output:
        "file_{name}.csv"
    shell:
        "python process.py {wildcards.name}"

Now say I have another rule like:

rule all:
    input:
        expand("file_{output_types}.csv", output_types=["A", "B"])

Calling the all rule, Snakemake will see that conversion can produce the requested files file_A.csv and file_B.csv (assuming the input files exist).

What ends up running are two shell commands:

  1. python process.py A
  2. python process.py B

Is it possible to run just a single shell command using wildcards?

What I would like to happen is to use the wildcards to run the command:

python process.py A B

(In my use-case, process.py has a long spin-up, so I want to avoid running it multiple times in a row.)



Solution 1:[1]

What about this?

names = ['A', 'B']

rule conversion:
    input:
        expand("file_{output_types}.txt", output_types= names),
    output:
        expand("file_{output_types}.csv", output_types= names),
    params:
        names= names,
    shell:
        "python process.py {params.names}"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dariober