Snakemake使用检查点结束工作流程

时间:2020-10-27 17:39:24

标签: snakemake

这个问题与我以前的帖子Snakemake 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不为空,则将执行步骤2和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输出中没有任何通配符。任何建议都会很棒。

谢谢!

1 个答案:

答案 0 :(得分:1)

我终于使它起作用了,原来只是语法的一些小修改:

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")
相关问题