简体   繁体   中英

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

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.

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" you@email.com '
        '|| (mail -s "failed" you@email.com ; 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.

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" youremail@provider.com < {log}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM