Workflow Engines (Snakemake, Nextflow): Difference between revisions

From HPCwiki
Jump to navigation Jump to search
Moiti001 (talk | contribs)
mNo edit summary
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Author: Carolina Pita Barros <br/>
Workflow engines let you describe a multi-step analysis as a set of rules — which steps depend on which, and how to run each — and then execute the whole pipeline reproducibly, submitting the individual steps to the scheduler for you. The two most common on Anunna are [https://snakemake.github.io/ Snakemake] and [https://www.nextflow.io/ Nextflow].
Contact: carolina.pitabarros@wur.nl <br/>
ABG


<br/><br/>
Using a workflow engine has real advantages on an HPC cluster: steps run as SLURM jobs with the right resources, only the parts that need to run are rerun, and the same pipeline can be shared and reproduced by others.
You can find my pipelines [https://github.com/CarolinaPB/ here]


The Snakemake shared here use modules loaded from the HPC and tools installed with conda.
== Snakemake ==


Click [https://github.com/CarolinaPB/snakemake-template/blob/master/Short%20introduction%20to%20Snakemake.pdf here] for an introduction to Snakemake
Snakemake describes a pipeline as a set of rules in a <code>Snakefile</code>. It can submit each rule's work to SLURM and manage the dependencies between steps.


== Clone the repository ==
=== Set up ===


==== From github ====
Snakemake is available in through our modules, for instance, in the 2025 bucket


Go to the repository’s page, click the green “Code” button and copy the path  <br/>
<syntaxhighlight lang="bash">
In your terminal go to where you want to download it to and run
module load 2025
module load snakemake/9.14.0
</syntaxhighlight>If you need additional python packages installed. Condiser creating a virtual environment.


<pre>git clone &lt;path you copied from github&gt;</pre>
==== From the the WUR HPC (Anunna) ====


Go to <code>/lustre/nobackup/WUR/ABGC/shared/PIPELINES/</code> and choose which pipeline you want to use.


<pre>cp &lt;pipeline directory&gt; &lt;directory where you want to save it to&gt;</pre>
=== SLURM profile ===
First you’ll need to do some set up. Go to the pipeline’s directory.


== Installation ==
To let Snakemake submit jobs to SLURM, create a profile. Make a directory for it:


Install <code>conda</code> if you don’t have it
<syntaxhighlight lang="bash">
mkdir -p ~/.config/snakemake/my-pipeline
</syntaxhighlight>


=== Create conda environment ===
and create a <code>config.yaml</code> inside it that tells Snakemake how to submit jobs, for example:


<pre>conda create --name &lt;name-of-pipeline&gt; --file requirements.txt</pre>
<syntaxhighlight lang="yaml">
<blockquote>I recommend giving it the same name as the pipeline
executor: slurm
</blockquote>
jobs: 10
This environment contains snakemake and the other packages that are needed to run the pipeline.
software-deployment-method: conda
default-resources:
  slurm_partition: "main"      # your Anunna partition
  slurm_account: "your_account"
  runtime: 60                  # minutes
  mem_mb: 16000
  cpus_per_task: 16
# per-rule overrides:
# set-resources:
#  big_rule:
#    mem_mb: 64000
#    runtime: 240
</syntaxhighlight>


=== Activate environment ===
Adjust the resources (time, memory, cores) to what your rules need.
=== Configure and run ===


<pre>conda activate &lt;name-of-pipeline&gt;</pre>
Open the pipeline's own <code>config.yaml</code> and set the input and output paths, keeping the variable names already in the file:
=== To deactivate the environment (if you want to leave the conda environment) ===
 
<syntaxhighlight lang="yaml">
OUTDIR: /path/to/output
READS_DIR: /path/to/reads/
ASSEMBLY: /path/to/assembly
PREFIX: output_name
</syntaxhighlight>
 
Because pipelines can take a long time, run Snakemake inside a persistent session ([https://linuxize.com/post/how-to-use-linux-screen/ screen] or tmux) so it keeps running if your connection drops. First do a dry run to check what will happen:
 
<syntaxhighlight lang="bash">
snakemake -np
</syntaxhighlight>
 
If the steps and commands look right, run the pipeline with your profile:
 
<syntaxhighlight lang="bash">
snakemake --profile my-pipeline
</syntaxhighlight>
 
The jobs are submitted to SLURM and you can follow the progress in your terminal and with the usual tools — see [[Monitoring Jobs]].
 
==== Pitfalls ====
If you run your pipelines with <code>--use-conda</code>, please make sure to point the variable <code>SNAKEMAKE_CONDA_PREFIX</code> to a directory in lustre. For example make sure this line is in your <code>~/.bash_aliases</code>. <syntaxhighlight lang="bash">
export SNAKEMAKE_CONDA_PREFIX=$myScratch/snakemake/conda
</syntaxhighlight>
 
== Nextflow ==
 
Nextflow is another widely used workflow engine, popular in bioinformatics (for example the [https://nf-co.re/ nf-core] pipelines). It describes a pipeline as a set of processes connected by data channels, and it can submit each process to SLURM just like Snakemake does.


<pre>conda deactivate</pre>
== File configuration ==


=== Create HPC config file ===
=== Set up ===


Necessary for snakemake to prepare and send jobs.
Nextflow is available from the 2025 [[Environment Modules|bucket]]. Loading it pulls in Java 25 as a dependency:


==== Start with creating the directory ====
<syntaxhighlight lang="bash">
module load 2025
module load Nextflow/25.10.2
</syntaxhighlight>


<pre>mkdir -p ~/.config/snakemake/&lt;name-of-pipeline&gt;
Check that it works:
cd ~/.config/snakemake/&lt;name-of-pipeline&gt;</pre>
==== Create config.yaml and include the following: ====


<blockquote>My pipelines are configured to work with SLURM
<syntaxhighlight lang="bash">
</blockquote>
nextflow info
<pre>jobs: 10
</syntaxhighlight>
cluster: &quot;sbatch -t 1:0:0 --mem=16000 -c 16 --job-name={rule} --exclude=fat001,fat002,fat101,fat100 --output=logs_slurm/{rule}.out --error=logs_slurm/{rule}.err&quot;


use-conda: true</pre>
If you run pipelines that use containers (all nf-core pipelines do), also load Apptainer:
<blockquote>Here you should configure the resources you want to use.
</blockquote>
=== Go to the pipeline directory and open config.yaml ===


Configure your paths, but keep the variable names that are already in the config file.
<syntaxhighlight lang="bash">
module load utilities
module load Apptainer
</syntaxhighlight>


<pre>OUTDIR: /path/to/output
=== Keep Nextflow out of your home directory ===
READS_DIR: /path/to/reads/  
 
ASSEMBLY: /path/to/assembly
By default Nextflow and Apptainer store their caches in <code>$HOME</code>. On Anunna this will fill your home quota very quickly: nf-core pipelines alone download several gigabytes of reference files and container images. Set these environment variables — for example in your <code>~/.bash_aliases</code> — to point them at fast, large storage instead:
PREFIX: &lt;output name&gt;</pre>
 
If you want the results to be written to this directory (not to a new directory), open the Snakefile and comment out <code>workdir: config[&quot;OUTDIR&quot;]</code> and ignore or comment out the <code>OUTDIR: /path/to/output</code> in the config file.
<syntaxhighlight lang="bash">
export NXF_HOME=/path/to/scratch/.nextflow
export NXF_APPTAINER_CACHEDIR=/path/to/scratch/.apptainer-cache
</syntaxhighlight>
 
* <code>NXF_HOME</code> — where Nextflow keeps its own files: downloaded pipeline code (<code>nextflow run nf-core/...</code> caches the pipeline here), plugins, and secrets. Never point this at <code>$HOME</code>.
* <code>NXF_APPTAINER_CACHEDIR</code> — where container images pulled by Nextflow are stored. Images are single large files and add up fast. Never point this at <code>$HOME</code>.
 
Make sure both variables point to a location in lustr
 
=== SLURM configuration ===
 
To make Nextflow submit jobs to SLURM, add a profile to the pipeline's <code>nextflow.config</code> (or to your personal <code>$NXF_HOME/config</code> so it applies to every pipeline you run):
 
<syntaxhighlight lang="groovy">
profiles {
  slurm {
    workDir = '/path/to/scratch/work'  // Nextflow's working directory; use fast shared storage
    process {
      executor = 'slurm'
      queue = 'main'                  // your Anunna partition
      clusterOptions = '--account=your_account'
      cpus = 4
      memory = '16 GB'
      time = '1h'
 
      // per-process overrides, by process name:
      withName: BIG_STEP {
        memory = '64 GB'
        time = '4h'
      }
      // or by label, if the pipeline sets them (nf-core pipelines do):
      withLabel: process_high {
        memory = '64 GB'
        time = '4h'
      }
    }
    executor {
      queueSize = 10                  // maximum number of jobs in SLURM at once
    }
    apptainer {
      enabled = true
    }
  }
}
</syntaxhighlight>
 
Adjust the resources (time, memory, cores) to what the pipeline needs.
 
=== Configure and run ===
 
nf-core pipelines take their options as command-line flags; run a pipeline with <code>--help</code> to list them:
 
<syntaxhighlight lang="bash">
nextflow run nf-core/rnaseq --help
</syntaxhighlight>


'''Now the setup is complete'''
The ones you almost always need are a sample sheet and an output directory:


== How to run the pipeline ==
<syntaxhighlight lang="bash">
--input samplesheet.csv --outdir /path/to/output
</syntaxhighlight>


Since the pipelines can take a while to run, it’s best if you use a [https://linuxize.com/post/how-to-use-linux-screen/ screen session]. By using a screen session, Snakemake stays “active” in the shell while it’s running, there’s no risk of the connection going down and Snakemake stopping.
Because pipelines can take a long time, run Nextflow inside a persistent session (screen or tmux) so it keeps running if your connection drops. Then launch the pipeline with your profile:


Start by creating a screen session:
<syntaxhighlight lang="bash">
nextflow run nf-core/rnaseq -profile slurm \
    --input samplesheet.csv --outdir /path/to/output
</syntaxhighlight>


<pre>screen -S &lt;name of session&gt;</pre>
Each process is submitted to SLURM as its own job; follow the progress in your terminal and with the usual tools — see [[Monitoring Jobs]].


You'll need to activate the conda environment again
If the run stops — a failed job, a full disk, a dropped session — fix the cause and rerun the same command with <code>-resume</code>. Nextflow reuses every step that already finished and only repeats what is missing:
<pre>conda activate &lt;name-of-pipeline&gt;</pre>


Then run
<syntaxhighlight lang="bash">
nextflow run nf-core/rnaseq -profile slurm \
    --input samplesheet.csv --outdir /path/to/output -resume
</syntaxhighlight>


<pre>snakemake -np</pre>
This will show you the steps and commands that will be executed. Check the commands and file names to see if there’s any mistake.


If all looks ok, you can now run your pipeline
<!-- TODO: add a Nextflow section for Anunna — how to load or install Nextflow, the SLURM executor configuration (nextflow.config: process.executor = 'slurm'), and a minimal example. -->


<pre>snakemake --profile &lt;name-of-pipeline&gt;</pre>
== See also ==
If everything was set up correctly, the jobs should be submitted and you should be able to see the progress of the pipeline in your terminal.
* [[Python]]
* [[Environment Modules]]
* [[Monitoring Jobs]]
* [[Scheduler Overview (Slurm)]]
* [https://snakemake.github.io/snakemake-plugin-catalog/plugins/executor/slurm.html SLURM executor plugin]
* [https://snakemake.readthedocs.io/en/stable/executing/cli.html Profiles / CLI reference]

Latest revision as of 18:55, 22 July 2026

Workflow engines let you describe a multi-step analysis as a set of rules — which steps depend on which, and how to run each — and then execute the whole pipeline reproducibly, submitting the individual steps to the scheduler for you. The two most common on Anunna are Snakemake and Nextflow.

Using a workflow engine has real advantages on an HPC cluster: steps run as SLURM jobs with the right resources, only the parts that need to run are rerun, and the same pipeline can be shared and reproduced by others.

Snakemake

Snakemake describes a pipeline as a set of rules in a Snakefile. It can submit each rule's work to SLURM and manage the dependencies between steps.

Set up

Snakemake is available in through our modules, for instance, in the 2025 bucket

module load 2025
module load snakemake/9.14.0

If you need additional python packages installed. Condiser creating a virtual environment.


SLURM profile

To let Snakemake submit jobs to SLURM, create a profile. Make a directory for it:

mkdir -p ~/.config/snakemake/my-pipeline

and create a config.yaml inside it that tells Snakemake how to submit jobs, for example:

executor: slurm
jobs: 10
software-deployment-method: conda
default-resources:
  slurm_partition: "main"      # your Anunna partition
  slurm_account: "your_account"
  runtime: 60                  # minutes
  mem_mb: 16000
  cpus_per_task: 16
# per-rule overrides:
# set-resources:
#   big_rule:
#     mem_mb: 64000
#     runtime: 240

Adjust the resources (time, memory, cores) to what your rules need.

Configure and run

Open the pipeline's own config.yaml and set the input and output paths, keeping the variable names already in the file:

OUTDIR: /path/to/output
READS_DIR: /path/to/reads/
ASSEMBLY: /path/to/assembly
PREFIX: output_name

Because pipelines can take a long time, run Snakemake inside a persistent session (screen or tmux) so it keeps running if your connection drops. First do a dry run to check what will happen:

snakemake -np

If the steps and commands look right, run the pipeline with your profile:

snakemake --profile my-pipeline

The jobs are submitted to SLURM and you can follow the progress in your terminal and with the usual tools — see Monitoring Jobs.

Pitfalls

If you run your pipelines with --use-conda, please make sure to point the variable SNAKEMAKE_CONDA_PREFIX to a directory in lustre. For example make sure this line is in your ~/.bash_aliases.

export SNAKEMAKE_CONDA_PREFIX=$myScratch/snakemake/conda

Nextflow

Nextflow is another widely used workflow engine, popular in bioinformatics (for example the nf-core pipelines). It describes a pipeline as a set of processes connected by data channels, and it can submit each process to SLURM just like Snakemake does.


Set up

Nextflow is available from the 2025 bucket. Loading it pulls in Java 25 as a dependency:

module load 2025
module load Nextflow/25.10.2

Check that it works:

nextflow info

If you run pipelines that use containers (all nf-core pipelines do), also load Apptainer:

module load utilities
module load Apptainer

Keep Nextflow out of your home directory

By default Nextflow and Apptainer store their caches in $HOME. On Anunna this will fill your home quota very quickly: nf-core pipelines alone download several gigabytes of reference files and container images. Set these environment variables — for example in your ~/.bash_aliases — to point them at fast, large storage instead:

export NXF_HOME=/path/to/scratch/.nextflow
export NXF_APPTAINER_CACHEDIR=/path/to/scratch/.apptainer-cache
  • NXF_HOME — where Nextflow keeps its own files: downloaded pipeline code (nextflow run nf-core/... caches the pipeline here), plugins, and secrets. Never point this at $HOME.
  • NXF_APPTAINER_CACHEDIR — where container images pulled by Nextflow are stored. Images are single large files and add up fast. Never point this at $HOME.

Make sure both variables point to a location in lustr

SLURM configuration

To make Nextflow submit jobs to SLURM, add a profile to the pipeline's nextflow.config (or to your personal $NXF_HOME/config so it applies to every pipeline you run):

profiles {
  slurm {
    workDir = '/path/to/scratch/work'  // Nextflow's working directory; use fast shared storage
    process {
      executor = 'slurm'
      queue = 'main'                   // your Anunna partition
      clusterOptions = '--account=your_account'
      cpus = 4
      memory = '16 GB'
      time = '1h'

      // per-process overrides, by process name:
      withName: BIG_STEP {
        memory = '64 GB'
        time = '4h'
      }
      // or by label, if the pipeline sets them (nf-core pipelines do):
      withLabel: process_high {
        memory = '64 GB'
        time = '4h'
      }
    }
    executor {
      queueSize = 10                   // maximum number of jobs in SLURM at once
    }
    apptainer {
      enabled = true
    }
  }
}

Adjust the resources (time, memory, cores) to what the pipeline needs.

Configure and run

nf-core pipelines take their options as command-line flags; run a pipeline with --help to list them:

nextflow run nf-core/rnaseq --help

The ones you almost always need are a sample sheet and an output directory:

--input samplesheet.csv --outdir /path/to/output

Because pipelines can take a long time, run Nextflow inside a persistent session (screen or tmux) so it keeps running if your connection drops. Then launch the pipeline with your profile:

nextflow run nf-core/rnaseq -profile slurm \
    --input samplesheet.csv --outdir /path/to/output

Each process is submitted to SLURM as its own job; follow the progress in your terminal and with the usual tools — see Monitoring Jobs.

If the run stops — a failed job, a full disk, a dropped session — fix the cause and rerun the same command with -resume. Nextflow reuses every step that already finished and only repeats what is missing:

nextflow run nf-core/rnaseq -profile slurm \
    --input samplesheet.csv --outdir /path/to/output -resume


See also