【问题标题】:Execute Snakemake rule repeatedly until certain conditions are met重复执行 Snakemake 规则,直到满足某些条件
【发布时间】:2019-12-30 18:51:09
【问题描述】:

我想将 Snakemake 用于需要重复执行某个步骤直到满足某些条件的流程。无法提前确定该步骤需要多少次。它可以是 1 或 6 或任何其他数字。

我的直觉是,这是 Snakemake 无法做到的,因为有向 非循环 Graph 和所有...

不过,我希望检查点可能会有所帮助,因为它会触发对 DAG 的重新评估,但我只是无法确切了解它是如何工作的。

Snakefile 中的循环是否可能?

谢谢!


在下面的优秀答案中添加一些关于实际发生的事情的评论。希望当我不可避免地重新审视这个问题时,它可以帮助其他人和我自己。

all:  call function all_input to determine rule's input requirements.
all_input:  file "succes.txt" doesn't exist.  do checkpoint keep_trying with i == 1.     
keep_trying:  output "round_1" doesn't exist.  do run section.  random() decides to touch output[0], which is "round_1".

snakemake reevaluates graph after checkpoint is complete

all:  call function all_input to determine rule's input requirements.
all_input:  file "succes.txt" doesn't exist.  do checkpoint keep_trying with i == 2.
keep_trying:   output "round_2" doesn't exist.  do run section.  random() decides to touch output[0], which is "round_2".

snakemake reevaluates graph after checkpoint is complete

all:  call function all_input to determine rule's input requirements.
all_input:  file "succes.txt" doesn't exist.  do checkpoint keep_trying with i == 3.
keep_trying:  output "round_3" doesn't exist.  do run section.  random() decides to touch "succes.txt".

snakemake reevaluates graph after checkpoint is complete

all:  call function all_input to determine rule's input requirements.
all_input:  file "succes.txt" exists.  return "success.txt" to rule all.
all:  input requirement is "success.txt", which is now satisfied.

【问题讨论】:

    标签: loops snakemake checkpoint


    【解决方案1】:

    您是对的,您需要为此设置检查点!这是一个小例子,可以满足您的需求:

    import os
    from pathlib import Path
    
    
    tries = 0
    def all_input(wildcards):
        global tries
        if not os.path.exists("succes.txt"):
            tries += 1
            checkpoints.keep_trying.get(i=tries)
        else:
            return "succes.txt"
    
    
    rule all:
        input:
            all_input
    
    
    checkpoint keep_trying:
        output:
            "round_{i}"
        run:
            import random
            if random.random() > 0.9:
                Path('succes.txt').touch()
            Path(output[0]).touch()
    

    这里我们说rule all 需要从函数all_input 返回的内容作为输入。该函数检查文件succes.txt 是否已经存在。如果不是,它将触发检查点的运行,继续尝试,这可能会生成succes.txt 文件(10% 的机会)。如果succes.txt 确实存在,那么这就是rule all 的输入,并且snakemake 成功退出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      相关资源
      最近更新 更多