Wednesday, February 7, 2024

[SOLVED] How To Handle Wildcards in Snakemake

Issue

Let's assume I have a snakemake rule like this:

rule test:
input: "{mywildcard}"
output: "{mywildcard}.txt"
shell: "some command {input} > {output}"

In order to get the output file, I would like to remove the last extension inside my {mywildcard} first which might look like this "A.tar".

My output should be "A.txt". How can I do this with a dynamic approach in Snakemake without modifying my wildcard in the input directive?

I thought about using

output: os.path.splitext("{mywildcard}")[0] + ".txt"

But this approach does not work with the Snakemake wildcards.


Solution

Snakemake works from output to input files. It can be a little confusing at first, but you request outputs and snakemake determines what inputs it needs. In your case, I think you want the extension of the input to also be used in the rule?

rule test:
    input: "{mywildcard}.tar"
    output: "{mywildcard}.txt"
    shell: "some command {input} > {output}"

That will work, if you need the value of {mywildcard}.tar you can reference it with {input} in your shell command. If you have a more exotic use case please update your question.



Answered By - Troy Comi
Answer Checked By - Mildred Charles (WPSolving Admin)