Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Nextflow in Neurodesk

Run this notebook

Author: Steffen Bollmann

Date: 13 Feb 2026

License:

MIT License

Note: If this notebook uses neuroimaging tools from Neurocontainers, those tools retain their original licenses. Please see Neurodesk citation guidelines for details.

Citation and Resources

Workflow engine

  • Di Tommaso, P., Chatzou, M., Floden, E. W., Barja, P. P., Palumbo, E., & Notredame, C. (2017). Nextflow enables reproducible computational workflows. Nature Biotechnology, 35(4), 316-319. Di Tommaso et al. (2017)

Tools included in this workflow

FSL - Brain Extraction Tool (BET)

  • M. Jenkinson, C.F. Beckmann, T.E. Behrens, M.W. Woolrich, S.M. Smith. FSL. NeuroImage, 62:782-90, 2012

  • Smith S. M. (2002). Fast robust automated brain extraction. Human brain mapping, 17(3), 143–155. Smith (2002)

Dataset from OSF

Introduction

Nextflow is a workflow engine that lets you write data-driven computational pipelines. It handles parallelism, file staging, and error recovery so you can focus on the analysis logic.

Why use Nextflow for neuroimaging?

  • Automatically runs independent subjects in parallel

  • Tracks which steps have finished so you can resume failed runs with -resume

  • Works the same way on your laptop, an HPC cluster, or in the cloud

  • Large ecosystem of ready-made pipelines at nf-core

This notebook teaches Nextflow from scratch through three progressively complex examples:

  1. A “hello world” pipeline

  2. Brain extraction on a single subject

  3. A multi-subject pipeline with quality-control summary

Setup

Load FSL via the Neurodesk module system.

['fsl/6.0.7.16']

      N E X T F L O W
      version 25.10.4 build 11173
      created 10-02-2026 15:17 UTC 
      cite doi:10.1038/nbt.3820
      http://nextflow.io

Download test data

We download a T1-weighted MRI scan from the TOMCAT dataset on OSF, then create a copy as a simulated second subject for the multi-subject demo later.

sub-01.nii already exists, skipping download
Done.
-rw-rw-r-- 1 jovyan jovyan 88M Apr 30 01:37 ./data/sub-01.nii
%%bash
# Create a simulated second subject by copying sub-01
if [ ! -f ./data/sub-02.nii ]; then
    cp ./data/sub-01.nii ./data/sub-02.nii
    echo "Created sub-02.nii"
else
    echo "sub-02.nii already exists"
fi

ls -lh ./data/

Hello Nextflow

Every Nextflow pipeline is a .nf script containing processes (units of work) and a workflow that wires them together using channels (asynchronous data queues).

Let’s start with the simplest possible pipeline.

Overwriting hello.nf
Nextflow 26.04.0 is available - Please consider updating your version to it

 N E X T F L O W   ~  version 25.10.4

Launching `hello.nf` [high_dijkstra] DSL2 - revision: 59446397d2

[-        ] SAY_HELLO -

executor >  local (3)
[4c/d3d62e] SAY_HELLO (1) | 0 of 3

executor >  local (3)
[4c/d3d62e] SAY_HELLO (1) | 3 of 3 ✔
Bonjour from Nextflow!

Hello from Nextflow!

Hola from Nextflow!


What just happened?

  1. Channel.of(...) created a channel with three items

  2. Nextflow launched the SAY_HELLO process three times — once per item, potentially in parallel

  3. Each execution ran in its own isolated work/ subdirectory

Let’s peek inside the work directory to see how Nextflow organises task execution:

=== work directory structure ===
Example task dir: work/28/2c794f65480a7ec87e99b983907443

--- .command.sh (the actual script that ran) ---
#!/bin/bash -ue
echo "Hello from Nextflow!"

--- .command.log (stdout/stderr) ---
Hello from Nextflow!

Core concepts

ConceptDescription
ProcessA unit of work with defined inputs, outputs, and a script. Runs in an isolated directory.
ChannelAn asynchronous queue that connects processes. Data flows through channels.
WorkflowThe top-level block that creates channels and wires processes together.
publishDirCopies output files from the work directory to a permanent results folder.
paramsPipeline parameters that can be set on the command line with --name value.

Nextflow automatically handles:

  • Parallelism: If a channel has N items, the process runs N times (potentially concurrently)

  • File staging: Input files are symlinked into each task’s work directory

  • Resumability: Use -resume to skip already-completed tasks after a failure

Single-subject brain extraction

Now let’s do something useful: run FSL’s bet (Brain Extraction Tool) on a single T1w image via Nextflow.

This introduces:

  • path inputs (file handling)

  • params for configurable settings

  • publishDir to save outputs to a results folder

Overwriting bet_single.nf
Nextflow 26.04.0 is available - Please consider updating your version to it

 N E X T F L O W   ~  version 25.10.4

Launching `bet_single.nf` [marvelous_picasso] DSL2 - revision: 0e8f469548

[-        ] BET -

executor >  local (1)
[7f/e48fe7] BET (1) | 0 of 1

executor >  local (1)
[7f/e48fe7] BET (1) | 0 of 1

executor >  local (1)
[7f/e48fe7] BET (1) | 1 of 1 ✔

total 18M
-rw-rw-r-- 1 jovyan jovyan 18M Apr 30 01:42 sub-01_brain.nii.gz

Key points:

  • Channel.fromPath(...) creates a channel from a file path

  • Inside the script block, ${t1w} refers to the staged input file

  • publishDir copies the outputs matching '*_brain.*' to our results folder

  • We could override any parameter from the command line, e.g. nextflow run bet_single.nf --frac 0.3

Multi-subject pipeline

Real neuroimaging studies have multiple subjects. Nextflow makes this easy — we just put multiple files into a channel and Nextflow fans out automatically.

This pipeline has two processes:

  1. BET — runs brain extraction per subject (parallel fan-out)

  2. QC_SUMMARY — collects all results and generates a summary table (runs once after all BET tasks finish)

Overwriting bet_multi.nf
Nextflow 26.04.0 is available - Please consider updating your version to it

 N E X T F L O W   ~  version 25.10.4

Launching `bet_multi.nf` [gloomy_jepsen] DSL2 - revision: ef713f98e8

[-        ] BET -

[-        ] BET        -
[-        ] QC_SUMMARY -

[-        ] BET        | 0 of 1
[-        ] QC_SUMMARY -

executor >  local (1)
[58/8ec1f6] BET (1)    | 0 of 1
[-        ] QC_SUMMARY -

executor >  local (2)
[58/8ec1f6] BET (1)    | 1 of 1 ✔
[97/777caa] QC_SUMMARY | 0 of 1

executor >  local (2)
[58/8ec1f6] BET (1)    | 1 of 1 ✔
[97/777caa] QC_SUMMARY | 1 of 1 ✔

executor >  local (2)
[58/8ec1f6] BET (1)    | 1 of 1 ✔
[97/777caa] QC_SUMMARY | 1 of 1 ✔

=== Output files ===
total 19M
-rw-rw-r-- 1 jovyan jovyan  18M Apr 30 01:42 sub-01_brain.nii.gz
-rw-rw-r-- 1 jovyan jovyan 324K Apr 30 01:42 sub-01_brain_mask.nii.gz

=== QC Summary ===
subject	brain_volume_voxels
sub-01	7879179

What’s new here?

  • Channel.fromPath('data/sub-*.nii') picks up both sub-01.nii and sub-02.nii

  • Nextflow runs BET twice in parallel (one per subject)

  • .collect() gathers all per-subject outputs into a single list and passes it to QC_SUMMARY

  • QC_SUMMARY runs once, after all BET tasks complete, and generates a combined table

This fan-out/collect pattern is the foundation of most neuroimaging Nextflow pipelines.

Visualize results

Use ipyniivue to visualize results. Enable the first line to see the comparison.

Loading...

Next steps

You now know the core Nextflow patterns. Here are some ways to extend what you’ve learned:

  • -resume: Add this flag to skip already-completed tasks when re-running a pipeline after a failure or parameter change

  • nextflow.config: Move parameters, executor settings (local/SLURM/PBS), and resource limits (CPUs, memory) into a separate config file

  • Containers: Nextflow can pull and run Docker/Singularity containers per process — set container in a process or config

  • nf-core: Browse nf-co.re for production-grade neuroimaging pipelines and community best practices

  • More modalities: Extend the glob pattern (params.inputs) to pick up T2w, FLAIR, or functional data

Dependencies and environment capture

  • Using the package watermark to document system environment and software versions used in this notebook, alongside the Neurodesktop version extracted from the JUPYTER_IMAGE or NEURODESKTOP_VERSION environment variables.

Last updated: 2026-04-30T01:42:50.458562+00:00

Python implementation: CPython
Python version       : 3.13.13
IPython version      : 9.12.0

Compiler    : GCC 14.3.0
OS          : Linux
Release     : 6.8.0-106-generic
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

ipyniivue: 2.4.4

Neurodesktop version: 2026-04-28
References
  1. Di Tommaso, P., Chatzou, M., Floden, E. W., Barja, P. P., Palumbo, E., & Notredame, C. (2017). Nextflow enables reproducible computational workflows. Nature Biotechnology, 35(4), 316–319. 10.1038/nbt.3820
  2. Smith, S. M. (2002). Fast robust automated brain extraction. Human Brain Mapping, 17(3), 143–155. 10.1002/hbm.10062
  3. Thomas Shaw, & Steffen Bollmann. (2020). Dataset for Towards Optimising MRI Methods for ChAracterisation of Tissue (TOMCAT). 10.17605/OSF.IO/BT4EZ