有没有一种方法可以打印出有用的消息并允许Snakemake退出工作流程而不会出错?我有这个示例工作流程:
def readFile(file):
with open(file) as f:
line = f.readline()
return(line.strip())
def isFileEmpty(file):
with open(file) as f:
line = f.readline()
if line.strip() != '':
return True
else:
return False
rule all:
input: "output/final.txt"
rule step1:
input: "input.txt"
output: "out.txt"
run:
if readFile(input[0]) == 'a':
shell("echo 'a' > out.txt")
else:
shell("echo '' > out.txt")
rule step2:
input: "out.txt"
output: dynamic("output/{files}")
run:
i = isFileEmpty(input[0])
if i:
shell("echo 'out2' > output/out2.txt")
else:
print("Out.txt is empty, workflow ended")
rule step3:
input: "output/out2.txt"
output: "output/final.txt"
run: shell("echo 'final' > output/final.txt")
在步骤1中,我正在读取input.txt的文件内容,如果不包含字母“ a”,则将生成一个空的out.txt。在步骤2中,检查out.txt是否为空。如果不为空,将执行步骤2和3,最后给出final.txt。如果为空,我希望Snakemake打印消息“ Out.txt为空,工作流结束”,并立即退出,而不执行步骤3并给出错误消息。现在,如果input.txt为空,我拥有的代码将在第2步显示消息,但仍将尝试运行第3步,并会给出MissingOutputException,因为未生成final.txt。我知道原因是因为final.txt是所有规则中的输入文件之一,但是我在编写此工作流时遇到了麻烦,因为可能会或可能不会产生final.txt。