What would be an elegant way of preventing snakemake from failing upon shell/R error?

后端 未结 1 2023
陌清茗
陌清茗 2020-12-21 03:12

I would like to be able to have my snakemake workflows continue running even when certain rules fail.

For example, I\'m using a variety of tools in order to perform

相关标签:
1条回答
  • 2020-12-21 03:35

    For shell commands, you can always take advantage conditional "or", ||:

    rule some_rule:
        output:
            "outfile"
        shell:
            """
            command_that_errors || true
            """
    
    # or...
    
    rule some_rule:
        output:
            "outfile"
        run:
            shell("command_that_errors || true")
    

    Usually an exit code of zero (0) means success, and anything non-zero indicates failure. Including || true ensures a successful exit when the command exits with a non-zero exit code (true always returns 0).

    If you need to allow a specific non-zero exit code, you can use shell or Python to check the code. For Python, it would be something like the following. The shlex.split() module is used so shell commands do not need to passed as arrays of arguments.

    import shlex
    
    rule some_rule:
        output:
            "outfile"
        run:
            try:
               proc_output = subprocess.check_output(shlex.split("command_that_errors {output}"), shell=True)                       
            # an exception is raised by check_output() for non-zero exit codes (usually returned to indicate failure)
            except subprocess.CalledProcessError as exc: 
                if exc.returncode == 2: # 2 is an allowed exit code
                    # this exit code is OK
                    pass
                else:
                    # for all others, re-raise the exception
                    raise
    

    In shell script:

    rule some_rule:
        output:
            "outfile"
        run:
            shell("command_that_errors {output} || rc=$?; if [[ $rc == 2 ]]; then exit 0; else exit $?; fi")
    
    0 讨论(0)
提交回复
热议问题