【问题标题】:How to access retry attempts in snakemake python code?如何访问snakemake python代码中的重试尝试?
【发布时间】:2021-06-07 21:43:27
【问题描述】:

当您使用 --restart-times >= 1 执行蛇形脚本时,它将尝试重新执行失败的运行。重新执行时,可以通过“资源”中的 lambda 函数访问执行尝试的次数。但是,我想在我的规则之外访问 python 代码块中的尝试次数。我试图将尝试变量从资源块传递给我的 python 函数,但无济于事。我的 snakemake 版本是 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: name 'resources' is not defined"

我最接近的是访问“workflow.attempt”,但这似乎总是设置为 1。也许这是尝试的默认值?

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

我正在查看snakemake 的内部结构,希望能找到解决方案。不幸的是,我的 python 知识不能胜任这项任务。可以访问一些变量来代替 workflow.attempt,它们没有整数值。不确定是否有一种方法可以通过稍微不同的方式获得当前的尝试次数:

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

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

【问题讨论】:

  • 您能否发布一个可重现的最小示例来说明您正在尝试做什么?

标签: python snakemake


【解决方案1】:

这是一个最小的工作示例,我可以用它来重现您的错误。

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 工作流程是“根据定义如何 从输入文件创建输出文件。规则之间的依赖关系是 自动确定”。(引用自Tutorial)这意味着snakemake需要知道该规则将创建哪个输出文件。然后,它将确定是否需要运行该规则。因此, 尝试至少通常不应该是输出文件名的一部分。

也许您想合并失败尝试的不同文件?但是,如果规则失败,则不会有输出文件。即使你强迫它。该文件将被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

希望这能为您的问题提供解决方案。

【讨论】:

    猜你喜欢
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2013-03-12
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多