Home > Blockchain >  Can I run a try-except block (or similar) inside a Snakemake rule?
Can I run a try-except block (or similar) inside a Snakemake rule?

Time:08-19

I trying to run a snakemake workflow. What I want to do is that if a given rule (rule a) works out send an email with a small text saying so, and if it fails then send another different email indicating an ERROR. I was wondering, is there a way in snakemake to run something similar to a try-except python block?

I have already tried out the try-except block with some commands inside a shell() directive, but it seems that I am not allowed to run any python code (except part of the block) once I have already written a shell() directive.

CodePudding user response:

One way to achieve this functionality is to add onerror and onsuccess to your workflows:

onsuccess:
    print("Workflow finished, no error")

onerror:
    print("An error occurred")
    shell('mail -s "an error occurred" [email protected] < {log}')

CodePudding user response:

I think your options are to convert your shell directive into a run directive with shell() functions or to use bash. For bash, something like

shell:
    'MyCommandThatMayFail '
        '&& mail -s "passed" [email protected] '
        '|| (mail -s "failed" [email protected] ; exit 1)'

Should work for the email. The exit 1 is necessary to signal to snakemake that the command failed.

A final consideration, if you are using a job scheduler on a cluster, the worker nodes may not have network access so emailing will fail from the submitted jobs.

  • Related