Question: If loop in snakemake
0
gravatar for mrmrwinter
12 months ago by
mrmrwinter30
University of Hull
mrmrwinter30 wrote:

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

if shell snakemake else loop • 896 views
ADD COMMENTlink written 12 months ago by mrmrwinter30
1

Sounds like you want a "checkpoint".

ADD REPLYlink written 12 months ago by Devon Ryan98k
1

To help the OP: https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#data-dependent-conditional-execution

ADD REPLYlink written 12 months ago by gb1.9k

Great, thank you both

ADD REPLYlink written 12 months ago by mrmrwinter30
1

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.

ADD REPLYlink written 12 months ago by Ram32k

Ok, thanks. Still new to the terminology

ADD REPLYlink written 12 months ago by mrmrwinter30

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

ADD REPLYlink written 12 months ago by EVR570

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

ADD REPLYlink written 12 months ago by mrmrwinter30

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

ADD REPLYlink written 12 months ago by gb1.9k

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

ADD REPLYlink modified 12 months ago • written 12 months ago by mrmrwinter30
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1674 users visited in the last hour
_