Snakemake 使用检查点结束工作流

Snakemake using checkpoint to end workflow

这个问题和我之前的postSnakemake exit a rule during execution有关。基本上对于我的工作流程,可能会在其中一个规则中生成一个空文件,我想用一条有用的消息退出工作流程。有人建议使用检查点功能,这是我的:

def readFile(file):
    with open(file) as f:
        line = f.readline()
        return(line.strip())

def isFileEmpty():
        with checkpoints.step1.output[0].open() as f:
                line = f.readline()
                if line.strip() != '':
                        return "output/final.txt"
                else:
                        return "out.txt"

rule all:
    input: isFileEmpty()

checkpoint step1:
    input: "input.txt"
    output: "out.txt"
    run:
        if readFile(input[0]) == 'a':
            shell("echo 'a' > out.txt")
        else:
            shell("echo '' > out.txt")
            print("Out.txt is empty")

rule step2:
    input: "out.txt"
    output: "output/out2.txt"
    run:
        shell("echo 'out2' > output/out2.txt")
            

rule step3:
    input: "output/out2.txt"
    output: "output/final.txt"
    run: shell("echo 'final' > output/final.txt")

在检查点步骤 1 中,我正在读取 input.txt 的文件内容,如果不包含字母 'a',则会生成一个空的 out.txt。如果out.txt不为空,则执行step2和3,最后给出output/out2.txt和output/final.txt。如果它是空的,工作流应该在检查点步骤 1 结束,只产生 out.txt。现在,当我 运行 工作流程给我这个错误时:

AttributeError in line 7 of Snakefile:
'Checkpoints' object has no attribute 'step1'

我的 checkpoints.step1.output[0].open() 语法错了吗?在检查点文档中,它被写为 checkpoints.somestep.get(sample=wildcards.sample).output[0] 但我的 snakemake 输出中没有任何通配符。任何建议都会很棒。

谢谢!

我终于让它工作了,结果只是对语法做了一些小的修改:

def readFile(file):
        with open(file) as f:
                line = f.readline()
                return(line.strip())

def isFileEmpty(wildcards):
        with checkpoints.step1.get(**wildcards).output[0].open() as f:
                line = f.readline()
                if line.strip() != '':
                        return "output/final.txt"
                else:
                        return "out.txt"

rule all:
        input: isFileEmpty

checkpoint step1:
        input: "input.txt"
        output: "out.txt"
        run:
                if readFile(input[0]) == 'a':
                        shell("echo 'a' > out.txt")
                else:
                        shell("echo '' > out.txt")
                        print("Out.txt is empty")

rule step2:
        input: "out.txt"
        output: "output/out2.txt"
        run:
                shell("echo 'out2' > output/out2.txt")


rule step3:
        input: "output/out2.txt"
        output: "output/final.txt"
        run: shell("echo 'final' > output/final.txt")