2017-08-10 45 views
0

即使某些規則失敗,我也希望能夠讓我的smakemake工作流繼續運行。什麼是預防snakemake失敗shell/R錯誤的優雅方法?

例如,我使用各種工具來執行ChIP-seq數據的峯值調用。但是,某些程序在無法識別峯值時會發出錯誤。我寧願在這種情況下創建一個空的輸出文件,而不是讓蛇形失敗(就像一些峯值呼叫者已經這樣做)。

使用「shell」和「run」關鍵字處理這種情況是否有類似snakemake的方式?

感謝

回答

1

對於shell命令,你總是可以利用的條件「或」 ||

rule some_rule: 
    output: 
     "outfile" 
    shell: 
     """ 
     command_that_errors || true 
     """ 

# or... 

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

通常的退出代碼零(0)表示成功,任何非零指示失敗。包括|| true可確保在命令以非零退出代碼退出時成功退出(true始終返回0)。

如果您需要允許特定的非零退出代碼,則可以使用shell或Python來檢查代碼。對於Python來說,它將如下所示。使用shlex.split()模塊,因此shell命令不需要作爲參數數組傳遞。

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 

在shell腳本:

rule some_rule: 
    output: 
     "outfile" 
    run: 
     shell("command_that_errors {output} || rc=$?; if [[ $rc == 2 ]]; then exit 0; else exit $?; fi") 
相關問題