Snakemake
The previous page set out the pipeline, the volume of the unit ball, with a plan that is known only once each dimension’s pilot has run. This page writes it as a Snakefile, with an environment for each rule that needs one.
The Snakefile
# The volume of the unit ball in d dimensions: for each d, a pilot batch, a plan
# that reads it, as many batches as the plan says, and a reduce; then the table,
# and its plot.
DIMS = range(2, config.get("dmax", 12) + 1)
wildcard_constraints:
d=r"\d+",
k=r"\d+",
rule all:
input:
"volumes.tsv",
"volumes.svg",
# Batch k of dimension d; batch 0 is the pilot.
rule count:
output:
"counts/{d}/{k}.txt",
conda:
"envs/count.yml"
shell:
"python ball.py count {wildcards.d} {wildcards.k} > {output}"
# How many batches d needs is known only once its pilot has run.
checkpoint plan:
input:
"counts/{d}/0.txt",
output:
"plans/{d}.txt",
shell:
"python ball.py plan {input} > {output}"
def batches(wildcards):
with checkpoints.plan.get(d=wildcards.d).output[0].open() as f:
n = int(f.read())
return expand("counts/{d}/{k}.txt", d=wildcards.d, k=range(1, n + 1))
rule reduce:
input:
batches,
output:
"volumes/{d}.txt",
shell:
"python ball.py reduce {wildcards.d} {input} > {output}"
rule table:
input:
expand("volumes/{d}.txt", d=DIMS),
output:
"volumes.tsv",
shell:
"python ball.py table {input} > {output}"
rule plot:
input:
"volumes.tsv",
output:
"volumes.svg",
conda:
"envs/plot.yml"
shell:
"python ball.py plot {input} {output}"As for π, each rule says how to make the files whose names match its output, and Snakemake works back from the files of rule all, the first rule, to the rules that make them. rule count makes counts/{d}/{k}.txt, batch k of dimension d, where batch 0 is the pilot. d and k are wildcards, and wildcard_constraints makes both match digits only, so that a name like counts/12/0.txt can be read only one way. DIMS reads the largest d from the configuration, and is 12 unless the command line says otherwise.
plan is a checkpoint rather than a rule, and reduce takes its inputs from a function, batches, rather than from a list. While a d’s plan has not run, checkpoints.plan.get(d=...) in batches raises an exception, which Snakemake takes to mean that the reduce depends on that plan. Once the plan has run, Snakemake calls batches again, which now reads the number of batches from the plan’s output and returns their files, and Snakemake adds the jobs that make them to the graph.
Running it
The environment has Snakemake itself, and its plugin for Slurm, for later:
name: ball-snakemake
channels:
- conda-forge
- bioconda
dependencies:
- python=3.13
- snakemake
# for running each job through Slurm
- snakemake-executor-plugin-slurmThe job runs the whole workflow inside one allocation, as on the page on workflow managers:
#!/bin/bash
#SBATCH --job-name=ball-snakemake
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=8
#SBATCH --time=00:10:00
set -eo pipefail
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate "${CONDA_ENV:-ball-snakemake}"
# Every task is a process with one thread, so as many tasks as CPUs at a time.
source ../pi/num-threads.sh 1
# The whole workflow inside this job, each rule in the environment it names.
snakemake --cores "$SLURM_CPUS_PER_TASK" --sdm conda
cat volumes.tsv--sdm conda, short for --software-deployment-method conda, makes Snakemake run each rule in the environment its conda: names; without it, Snakemake ignores those lines, and runs every rule in the environment it runs in itself. It creates the two environments before the first job, and its plan at the start is
Job stats:
job count
------ -------
count 11
plan 11
reduce 11
table 1
plot 1
all 1
total 36
the 11 pilots, the 11 plans, a reduce for each d, the table, the plot and all. As each plan finishes, Snakemake adds the batches it asks for, and the job ends with
115 of 115 steps (100%) done
and the table of the previous page, after 79 jobs that were not in the plan at the start.
A dry run, and a single result
snakemake -n lists the jobs it would run, without running any. Before the plans have run, it lists the same 36, with the input of each reduce still to be determined:
rule reduce:
input: <TBD>
output: volumes/4.txt
jobid: 8
reason: Missing output files: volumes/4.txt; Input files updated by another job: plans/4.txt
wildcards: d=4
and says so at the end:
The run involves checkpoint jobs, which will result in alteration of the DAG of jobs (e.g. adding more jobs) after their completion.
You can also ask for any one file of the workflow, rather than for all of them. snakemake --cores 8 --sdm conda volumes/12.txt runs only what the row of d = 12 needs, its pilot, its plan, its 46 batches and its reduce, 49 jobs in all, and nothing for the other dimensions.
Running it again
Submit the job again, and Snakemake runs nothing:
Nothing to be done (all requested files are present and up to date).
Add a dimension, with --config dmax=13 on the command line, and it plans 6 jobs: the pilot, plan and reduce of d = 13, the table, the plot and all. Once that plan has run, it adds the 134 batches d = 13 needs, and ends with 140 jobs done, none of them for d = 2 to 12.
What decides that is a record for each output, kept in .snakemake/metadata, of what made it. Snakemake reruns a job when one of its rerun triggers has fired since its output was made: an input newer than the output, a different set of inputs, different parameters, different code, or a different software environment. --rerun-triggers mtime keeps only the first, which is how make decides. Of the five, the code and the environment need care.
The code is the rule’s own, the command of its shell:, not the scripts that command runs. Change TARGET in ball.py, and Snakemake still finds nothing to be done. If a change to a script should rerun the rules that use it, list it among their inputs, and a newer script then makes their outputs out of date.
An environment is identified by a hash of its file and of the directory it is created in, the latter because the files of a conda environment can hold its absolute path. So when you copy the workflow’s directory elsewhere, from $SCRATCH to $PROJECTDIR say,1 even with cp -a, which keeps modification times, Snakemake creates the environments anew in the copy, and reruns every job that has one, and every job after them, which here is all of them. With --conda-prefix set to a directory outside the workflow’s, which stays where it is, the copy reruns nothing.
An environment for each rule
conda: names an environment file, relative to the Snakefile, and Snakemake creates the environment under .snakemake/conda/, named by its hash, the first time a rule needs it. On a cluster whose compute nodes cannot reach conda-forge, create them on the login node first, with snakemake --sdm conda --conda-create-envs-only, on a filesystem every node sees, as in the questions specific to a cluster.
The file is a recipe, a list of names, so which packages it gives depends on when the environment is created, as Freezing an environment explains. To pin it, put a lockfile next to it, named after the file and the platform, in the format of conda list --explicit:
conda list --explicit --md5 -p .snakemake/conda/<hash>_ > envs/count.linux-64.pin.txtSnakemake then creates the environment from envs/count.linux-64.pin.txt on linux-64, and from envs/count.yml on any other platform, or if the pin fails. The pin is part of the environment’s hash, so changing it reruns the rules that use it, and it has to be made again whenever the recipe changes.
A rule can also run in a container, with container: and --sdm apptainer, and the image is pinned by its digest, docker://python@sha256:.... Or it can load modules, with envmodules: and --sdm env-modules, which pins nothing, so record them.
Its report
After a run, snakemake --report report.html writes one self-contained HTML file with the graph of the rules, when each job ran and for how long, and for each rule, its code and its environment file. That file is the recipe, so what says which packages ran is the pin, or the conda list of the environment.
On Slurm
To run each job as a Slurm job of its own, rather than all of them inside one, use the Slurm executor and a profile, with the counts grouped into fewer jobs, as on the page on workflow managers, which also covers where to run Snakemake itself, throttling its submissions, and its lock on the working directory. On nodes of the same kind, the table is the same either way, since each batch is seeded by its (d, k). Across kinds of node, it is the same within a tolerance, as the page on the machine explains.
Limitations
A checkpoint makes the dry run partial: everything after it is undetermined until it has run, and a workflow with many checkpoints shows little in advance. The code trigger covers only the rule’s own command, so a change to a script or a library it calls goes unnoticed unless the script is an input. And the environments belong to the directory they were created in, so a copy of the workflow reruns everything, unless they live elsewhere.
The next page writes the same pipeline in Nextflow.
Footnotes
$SCRATCHis what many clusters call their scratch space, large and fast but purged of files not used for a while, and$PROJECTDIRis what some call the space shared by a project, with its own quota. Neither name is universal, so look for those kinds of storage in your centre’s documentation; the part on storage is about the kinds there are.↩︎