如何在snakemake python代码中访问重试尝试?

2024-10-02 22:36:35 发布

您现在位置:Python中文网/ 问答频道 /正文

当您使用--restart times>;执行snakemake脚本时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行中的名称错误:未定义名称‘资源’”

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

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

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

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

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

Tags: lambda函数inputoutputjobfiles资源rule
1条回答
网友
1楼 · 发布于 2024-10-02 22:36:35

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

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如何

希望这能解决您的问题

相关问题 更多 >