Is it possible in Snakemake to have "optional" wildcard or part of the filename?

Viewed 722

I wonder if it is possible to join these two rules in Snakemake in a single rule (they do the same in the "run:"):

rule without_d:
    input:
        vals_pca    = 'stats/input_{type}.npz',
    output:
        for_cnv     = 'stats/output_{type, tt|gs}.npz'
    run:
        # DO STUFF

rule with_d:
    input:
        vals_pca    = 'stats/input_{type}_d{amount}.npz',
    output:
        for_cnv     = 'stats/output_{type, tt|gs}_d{amount}.npz'
    run:
        # DO STUFF

I have tried to define stats/output_{type}{amount}.npz, but wildcards apparently do not match an empty string. The second idea was to put it into "or" like stats/{output, output_{type}|output_{type}_d{amount}}, but here the problem is with a wildcard in a wildcard.

Thanks!

2 Answers

Thanks to Eric I realized that regular expressions can be used in snakemake rules' wildcards. The only problem is then that snakemake does not accept "empty" wildcards, but this can be overridden as explained in the post here: https://groups.google.com/g/snakemake/c/S7fTL4jAYIM?pli=1 Thus the solution to my problem is as follows:

rule both:
    input:
        vals_pca    = 'stats/input_{type}{amount}.npz',
    output:
        for_cnv     = 'stats/output_{type, tt|gs}{amount, .{0}|_d.+}.npz'
    run:
        # DO STUFF

Snakemake will match {amount, .{0}|_d.+} as an empty string or string beginning with _d. Hope it will help somebody.

Can't you do it the very simple way:

rule with_something:
    input:
        vals_pca    = 'stats/input_{something}.npz',
    output:
        for_cnv     = 'stats/output_{something}.npz'
    run:
        # DO STUFF

Wildcards are defined by the output files. If you ask for file output_typeWhatever_dWhatever.npz then the rule will look for input_typeWhatever_dWhatever.npz. If you ask for file output_typeWhatever.npz then the rule will use as input input_typeWhatever.npz.

Keep you rules as simple/generic as possible. Don't forget that snakemake resolves the wildcards from the final file you ask for and goes down the rules to see how to produce this file.

Related