Snakemake: prevent rerun of subworkflow

Viewed 145

I have a workflow with a subworkflow. The subworkflow's DAG takes a long time to generate. Is there a way to tell Snakemake that the subworkflow is up to date and avoid the long evaluation of its DAG?

1 Answers

I'm not sure that a great way to accomplish this exists, but the following can get the job done, even if it's a bit of a hack.

Suppose your workflow depends on a subworkflow defined by sw/Snakefile, which produces file sw/test.txt that your main workflow uses on. Then you can simply take advantage of conditional Snakemake blocks to use the subworkflow to generate sw/test.txt only when this file doesn't already exist:

import os

if os.path.exists("sw/test.txt"):
    rule result:
        input:
            "sw/test.txt"
        output:
            touch("result.txt")
else:
    subworkflow sw: 
        workdir:
            "sw"
        snakefile:
            os.path.join("sw/Snakefile")

    rule result:
        input:
            sw("test.txt")
        output:
            touch("result.txt")

In this way, the subworkflow DAG is only evaluated when sw/test.txt does not already exist. Of course, this also means that you'll have to explicitly rm sw/test.txt whenever you need the subworkflow to update it.

Related