问题
Is there a way to print a helpful message and allow Snakemake to exit the workflow without giving an error? I have this example workflow:
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")
In step 1, I'm reading the file contents of input.txt and if doesn't contain the letter 'a' then an empty out.txt will be produced. In step 2, whether out.txt is empty is checked. If it's not empty, step2 and 3 will be performed to give final.txt at the end. If it's empty, I want Snakemake to print the message "Out.txt is empty, workflow ended" and exit immediately without performing step 3 and giving an error message. Right now the code I have will print the message at step 2 if input.txt is empty, but it'll still try to run step 3 and will give MissingOutputException because final.txt is not generated. I understand the reason is because final.txt is one of the input files in the rule all, but I'm having trouble writing up this workflow because final.txt may or may not be produced.
来源:https://stackoverflow.com/questions/64468895/snakemake-exit-a-rule-during-execution