If loop in snakemake
0
0
Entering edit mode
15 months ago
mrmrwinter ▴ 30

Hi

im currently trying to write an if loop to use in a snakemake rule

The code for the rule is as follows:

rule empty_fasta_workaround:
input:
"results/02_trimmed/{library}/{sample}.merged.tmp.derep.fasta"
output:
denoise = "results/02_trimmed/{library}/{sample}.merged.derep.fasta",
rerep = "results/rereplicated/{library}/{sample}.fasta"
priority:
50
run:
"""
if [wc -l {input}] -gt 0
then
mv {input} {output.denoise}
else
mv {input} {output.rerep}
fi
"""


Some files in results/02_trimmed/{library}/{sample}.merged.tmp.derep.fasta are empty. I want this loop to count lines in these files and move files with >0 lines to output denoise and files with no lines to output rerep.

The second issue is that another rule also has the output results/rereplicated/{library}/{sample}.fasta This is throwing an error for Ambiguity. Can i fix this with RuleOrder or RuleDependencies? Or some other way?

Many thanks

shell snakemake if else loop • 1.2k views
1
Entering edit mode

Sounds like you want a "checkpoint".

1
Entering edit mode
0
Entering edit mode

Great, thank you both

1
Entering edit mode

On a related note, calling an if statement a "loop" is a pet peeve of mine. It's a conditional statement, not a loop. Loops run the same piece of code multiple times, conditional statements execute a particular block of code based on a condition.

0
Entering edit mode

Ok, thanks. Still new to the terminology

0
Entering edit mode

for second issue, just the change the name of the rule

0
Entering edit mode

Hi, this doesnt work; the rules already have different names

The problem is that the outputs of two rules are the same wildcard/file, results/rereplicated/{library}/{sample}.fasta, and this raises an ambiguity error

0
Entering edit mode

Think about it, even if you don't use snakemake you can not have two files with the same filename in the same folder...

0
Entering edit mode

Maybe im not explaining it well enough.

"the outputs of two rules are the same wildcard/file, results/rereplicated/{library}/{sample}.fasta, and this raises an ambiguity error"

It is not trying to have two files with the same filename in the same folder, the wildcards will be different.

Ie, results/rereplicated/{library}/{sample}.fasta

becomes

results/rereplicated/lib1/sample1.fasta from output of rule A

results/rereplicated/lib1/sample2.fasta from output of rule B

The problem is that snakemake wont let me have two outputs of different rules the same. It's not complaining about trying to make the same file twice.

As above, checkpoints are probably the way forward