簡體   English   中英

如何訪問snakemake python 代碼中的重試嘗試?

[英]How to access retry attempts in snakemake python code?

當您使用 --restart-times >= 1 執行蛇形腳本時,它將嘗試重新執行失敗的運行。 重新執行后,可以通過“資源”中的 lambda function 訪問執行嘗試次數。 但是,我想在我的規則之外訪問 python 代碼塊中的嘗試次數。 我試圖將嘗試變量從資源塊傳遞給我的 python function,但無濟於事。 我的蛇形版本是 5.32.1,使用 6.0.3 進行的快速測試看起來非常相似。

def getTargetFiles(files, attempted):
    do stuff
    return modified-target-files

rule do_things_rule:
    input: 
        ...
    output:
        getTargetFiles("file/path.txt", resources.attempt)
    resources:
        attempt=lambda wildcards, attempt: attempt,

不幸的是,這會產生錯誤。 “xxxx.py 第 172 行中的 NameError:未定義名稱‘資源’”

我最接近的是訪問“workflow.attempt”,但這似乎總是設置為 1。也許這是嘗試的默認值?

rule do_things_rule:
    input: 
        ...
    output:
        getTargetFiles("file/path.txt", workflow.attempt)

我正在查看snakemake的內部結構,希望能找到解決方案。 不幸的是,我的 python 知識無法勝任這項任務。 可以訪問一些變量來代替 workflow.attempt,它們沒有 integer 值。 不確定是否有一種方法可以通過稍微不同的方式獲得當前的嘗試次數:

print snakemake.jobs.Job.attempt
<property object at 0x7f4eecba66d0>

print snakemake.jobs.Job._attempt
<member '_attempt' of 'Job' objects>

這是一個最小的工作示例,我可以用它來重現您的錯誤。

def getTargetFiles(files, attempted):
  return f"{files[:-4]}-{attempted}.txt"

rule do_things_rule:
  resources:
    nr = lambda wildcards, attempt: attempt
  output:
    getTargetFiles("test.txt", resources.nr)
  shell:
    'echo "Failing on purpose to produce file'
    '{output} at attempt {resources.nr}'
    '"; exit 1 '

確實, output不知道resources 我認為這是因為需要在規則運行之前訪問它(見下文)。 相反,如果您將getTargetFiles("test.txt", resources.nr)替換為getTargetFiles("test.txt", 1) ,則規則運行正確的次數並且 shell 命令可以訪問resources.nr

據我了解,這個問題是有根本原因的。

snakemake 工作流程“根據定義如何從輸入文件創建 output 文件的規則進行定義。規則之間的依賴關系是自動確定的”。 (引自教程)這意味着snakemake需要知道該規則將創建哪個output文件。 然后,它將確定是否需要運行該規則。 因此,嘗試次數至少通常不應該是 output 文件名的一部分。

也許您想組合失敗嘗試的不同文件? 但是,如果規則失敗,則不會有 output 文件。 即使你強迫它。 該文件將被snakemake 刪除。 (見下面的例子)

def getTargetFiles(files, attempted):
  return f"{files[:-4]}-{attempted}.txt"

rule combine:
  input:
    'test-1.txt'
  output:
    'test-combined.txt'
  shell:
    'cat test-[0-9]*.txt > test-combined.txt'

rule do_things_rule:
  resources:
    nr = lambda wildcards, attempt: attempt
  output:
    getTargetFiles("test.txt", 1)
  shell:
    'touch {output}; '
    'echo "Failing on purpose to produce file'
    '{output} at attempt {resources.nr}'
    '"; exit 1 '

在 shell 命令中使用resources.nr代替文件名中的嘗試次數怎么樣?

希望這能為您的問題提供解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM