Snakemake
What is Snakemake
Section titled “What is Snakemake”Snakemake is a workflow manager that uses Python syntax to define analysis pipelines. It was created in 2012 by Johannes Köster and has become one of the most popular tools for automating bioinformatics workflows.
Snakemake follows the same philosophy as GNU Make. You define the output files you want. Snakemake figures out which steps to run and in what order to produce them. If an intermediate file already exists and its inputs have not changed, Snakemake skips that step.
Core concepts
Section titled “Core concepts”A rule is a single step in your pipeline. It declares input files, output files, and a shell command. Here is a rule that runs FastQC on a FASTQ file:
rule fastqc: input: "data/{sample}.fastq.gz" output: "results/fastqc/{sample}_fastqc.html" shell: "fastqc {input} --outdir results/fastqc/"Wildcards
Section titled “Wildcards”The {sample} in the rule above is a wildcard. Snakemake replaces it with actual sample names at runtime. If you request output for three samples, Snakemake creates three jobs from the same rule.
rule all: input: expand("results/fastqc/{sample}_fastqc.html", sample=["sample1", "sample2", "sample3"])The rule all is a convention. It lists all final output files you want. Snakemake works backward from these targets to determine which rules to run.
The DAG
Section titled “The DAG”Snakemake builds a Directed Acyclic Graph from your rules. Each node is a job. Edges represent dependencies. Snakemake analyzes which jobs can run in parallel and which must wait for upstream results.
You can visualize the DAG for any workflow:
snakemake --dag | dot -Tpng > dag.pngThis produces a graph showing every job and its dependencies. It is useful for debugging and for explaining your pipeline to collaborators.
A simple RNA-seq workflow
Section titled “A simple RNA-seq workflow”Here is a minimal Snakemake workflow that trims reads, aligns them, and counts features. This is not production code. It shows how rules chain together.
# Snakefile
SAMPLES = ["sample1", "sample2", "sample3"]
rule all: input: "results/counts/all_counts.tsv"
rule trim: input: r1="data/{sample}_R1.fastq.gz", r2="data/{sample}_R2.fastq.gz" output: r1="results/trimmed/{sample}_R1.fastq.gz", r2="results/trimmed/{sample}_R2.fastq.gz" shell: """ trim_galore --paired {input.r1} {input.r2} \ --output_dir results/trimmed/ """
rule align: input: r1="results/trimmed/{sample}_R1.fastq.gz", r2="results/trimmed/{sample}_R2.fastq.gz", index="references/genome_index" output: "results/aligned/{sample}.bam" threads: 8 shell: """ STAR --runThreadN {threads} \ --genomeDir {input.index} \ --readFilesIn {input.r1} {input.r2} \ --readFilesCommand zcat \ --outSAMtype BAM SortedByCoordinate \ --outFileNamePrefix results/aligned/{wildcards.sample} mv results/aligned/{wildcards.sample}Aligned.sortedByCoord.out.bam {output} """
rule count: input: bam="results/aligned/{sample}.bam", gtf="references/genes.gtf" output: "results/counts/{sample}.txt" shell: "featureCounts -a {input.gtf} -o {output} {input.bam}"
rule merge_counts: input: expand("results/counts/{sample}.txt", sample=SAMPLES) output: "results/counts/all_counts.tsv" shell: "paste {input} | cut -f1,7,14,21 > {output}"Snakemake traces dependencies from the rule all target. This sketch shows that structure, but it is not ready to run. In particular, the final cut command assumes exactly three count files. A fourth sample requires a merge step that reads sample names and gene identifiers rather than fixed column positions.
Running Snakemake
Section titled “Running Snakemake”Basic execution
Section titled “Basic execution”# Dry run: show what would be executed without running anythingsnakemake -n
# Allow up to 4 CPU coressnakemake --cores 4
# Run a specific targetsnakemake results/counts/all_counts.tsv --cores 4The --cores flag sets the total CPU cores available for local execution. It does not set the number of simultaneous jobs. Snakemake accounts for each rule’s threads request when it schedules jobs. See the command-line guide.
Resume after failure
Section titled “Resume after failure”Snakemake tracks which output files exist and which are up to date. If a run fails at the alignment step, fix the problem and rerun. Snakemake skips all completed steps automatically. There is no special flag needed.
# Just rerun the same command. Completed steps are skipped.snakemake --cores 4If you suspect a partial output file from a failed run, use the --rerun-incomplete flag to force recomputation of unfinished jobs.
Container support
Section titled “Container support”Snakemake can run each rule inside a container. This separates the rule’s software from the host environment. Reproduction also requires the same image, inputs, reference files, and parameters.
Singularity and Apptainer
Section titled “Singularity and Apptainer”Most HPC clusters do not allow Docker for security reasons. Snakemake supports Singularity and its successor Apptainer natively.
rule align: input: r1="results/trimmed/{sample}_R1.fastq.gz", r2="results/trimmed/{sample}_R2.fastq.gz" output: "results/aligned/{sample}.bam" container: "docker://quay.io/biocontainers/star:2.7.11b--h43eeafb_0" threads: 8 shell: """ STAR --runThreadN {threads} \ --genomeDir references/genome_index \ --readFilesIn {input.r1} {input.r2} \ --readFilesCommand zcat \ --outSAMtype BAM SortedByCoordinate \ --outFileNamePrefix results/aligned/{wildcards.sample} """Run the workflow with container support enabled:
snakemake --cores 4 --use-singularitySnakemake pulls the container image and runs the rule inside it. The example selects a version tag. Tags can change, so record the resolved image digest when you preserve a run.
Conda integration
Section titled “Conda integration”Snakemake also supports Conda environments as an alternative to containers. Each rule can specify a Conda environment file.
rule fastqc: input: "data/{sample}.fastq.gz" output: "results/fastqc/{sample}_fastqc.html" conda: "envs/fastqc.yaml" shell: "fastqc {input} --outdir results/fastqc/"The environment file specifies the packages and versions:
channels: - bioconda - conda-forgedependencies: - fastqc=0.12.1Run with Conda enabled:
snakemake --cores 4 --use-condaSnakemake creates the Conda environment on the first run and reuses it for subsequent runs. This approach is simpler than containers but less portable. Conda environments can behave differently across operating systems.
Configuration files
Section titled “Configuration files”Hard-coding sample names and paths in the Snakefile is fine for small projects. For larger analyses, use a configuration file.
samples: - sample1 - sample2 - sample3
genome_index: "references/genome_index"gtf: "references/genes.gtf"Access the configuration in your Snakefile:
configfile: "config.yaml"
SAMPLES = config["samples"]
rule align: input: r1="results/trimmed/{sample}_R1.fastq.gz", r2="results/trimmed/{sample}_R2.fastq.gz", index=config["genome_index"] ...Cluster and cloud execution
Section titled “Cluster and cloud execution”Snakemake supports execution on HPC clusters and cloud platforms.
SLURM clusters
Section titled “SLURM clusters”snakemake --cores 100 --executor slurm \ --default-resources slurm_partition=main mem_mb=8000This command requires the SLURM executor plugin and a cluster account. Replace the example partition and resource limits with your institution’s configuration. Snakemake submits jobs to SLURM and monitors their status. Your support team may provide a profile with these settings.
Cloud execution
Section titled “Cloud execution”Snakemake uses executor plugins for cloud execution. Storage plugins and configuration determine where inputs and outputs reside. Select the executor and storage setup before you transfer data. The plugin catalog lists the requirements for each service.
The Snakemake Workflow Catalog
Section titled “The Snakemake Workflow Catalog”The Snakemake Workflow Catalog is a curated collection of community workflows. It serves a similar purpose to nf-core for Nextflow. Workflows in the catalog follow standardized structure and testing guidelines.
The catalog is smaller than nf-core. It covers common use cases like RNA-seq, variant calling, and ATAC-seq. If you need a pipeline for a standard analysis, check the catalog before writing your own.
When to choose Snakemake vs Nextflow
Section titled “When to choose Snakemake vs Nextflow”Both tools solve the same fundamental problem. The choice often comes down to your environment and team.
Choose Snakemake if:
- Your team already writes Python.
- You work on an HPC cluster with SLURM.
- You want tight Conda integration for managing environments.
- You prefer file-based dependency resolution.
- Your workflows are custom and not covered by nf-core.
Choose Nextflow if:
- You want to use nf-core pipelines out of the box.
- You run analyses on AWS Batch or other cloud platforms.
- Your institution already uses Nextflow.
- You need production-grade pipelines maintained by a large community.
Both tools support containers. Both support resuming failed runs. Both produce reproducible results when configured correctly. The best workflow manager is the one your team will actually use.
Installing Snakemake
Section titled “Installing Snakemake”The recommended installation uses Conda or Mamba:
# Install with Mamba (faster than Conda)mamba create -n snakemake -c conda-forge -c bioconda snakemake
# Activate the environmentmamba activate snakemake
# Verify the installationsnakemake --versionThe Snakemake environment does not include every cluster or cloud integration. Install the executor and storage plugins required by your chosen environment. Follow the current installation guide and your institution’s instructions.
Next steps
Section titled “Next steps”The Nextflow & nf-core page provides a local FASTA example and introduces community pipelines. Its cloud pages are optional. Choose a workflow manager that your team can operate and validate with its available compute resources.
Scientific references
Section titled “Scientific references”Mölder and colleagues, 2021 describe sustainable data analysis with Snakemake.
Further reading
Section titled “Further reading”The Snakemake basic tutorial develops a complete mapping workflow and explains wildcard dependencies. Use its supplied inputs when you need a runnable example beyond the RNA-seq sketches above.
The Snakemake command reference defines resource limits, software deployment, rerun triggers, and graph output. Check the reference for the installed version before transferring a local command to an executor plugin.