Nextflow Pipelines
Build maintainable DSL2 workflows that are portable across local workstations, HPC schedulers, and cloud executors. Treat reproducibility, validation, and operational failure modes as core design requirements.
Workflow
- Clarify inputs, outputs, sample metadata, scale, executor, and acceptance criteria.
- Separate orchestration from atomic tool processes.
- Model channels with metadata maps and explicit tuple contracts.
- Pin every tool through Conda and a container.
- Configure resources through labels and executor-specific profiles.
- Add stub runs, small synthetic tests, and execution reports.
- Record tool versions and workflow parameters with the outputs.
Project Structure
pipeline/
├── main.nf
├── nextflow.config
├── nextflow_schema.json
├── modules/local/
├── subworkflows/local/
├── conf/
│ ├── base.config
│ └── test.config
├── assets/
├── tests/
└── README.md
Keep processes atomic and reusable. Keep workflow composition in main.nf or subworkflows, and keep environment-specific settings in profiles rather than hard-coded paths.
Minimal DSL2 Pattern
nextflow.enable.dsl = 2
params.input = null
params.outdir = 'results'
process FASTQC {
tag "${meta.id}"
label 'process_low'
conda 'bioconda::fastqc=0.12.1'
container 'quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0'
input:
tuple val(meta), path(reads)
output:
tuple val(meta), path('*.html'), emit: html
path 'versions.yml', emit: versions
script:
"""
fastqc --threads ${task.cpus} ${reads}
cat <<-END_VERSIONS > versions.yml
"${task.process}":
fastqc: \$(fastqc --version | sed 's/FastQC v//')
END_VERSIONS
"""
stub:
"""
touch ${meta.id}_fastqc.html
echo '"${task.process}": {fastqc: "0.12.1"}' > versions.yml
"""
}
workflow {
reads_ch = Channel
.fromFilePairs(params.input, checkIfExists: true)
.map { id, reads -> tuple([id: id, single_end: false], reads) }
FASTQC(reads_ch)
}
Design Rules
- Use DSL2 and named outputs.
- Pass sample identity in a metadata map rather than encoding it in filenames.
- Use tuples to keep metadata and files synchronized.
- Pin exact tool versions in both
conda and container directives.
- Prefer one main tool per process so failures and resource use remain observable.
- Define CPU, memory, and time through labels in configuration.
- Use
publishDir for final artifacts only; treat work/ as an execution cache.
- Add
stub: blocks for fast structural tests.
- Emit
versions.yml from every tool process.
- Avoid institutional paths, credentials, and account identifiers in public configuration.
Resource Configuration
process {
withLabel: process_low {
cpus = 2
memory = 4.GB
time = 2.h
}
errorStrategy = { task.exitStatus in 137..140 ? 'retry' : 'terminate' }
maxRetries = 2
}
profiles {
slurm {
process.executor = 'slurm'
singularity.enabled = true
docker.enabled = false
}
test {
includeConfig 'conf/test.config'
}
}
Cap dynamic retries at an explicit maximum. A retry policy should distinguish resource exhaustion from deterministic tool or input failures.
Validation Checklist
nextflow config resolves without errors.
- A stub run completes:
nextflow run . -profile test -stub-run.
- A small synthetic or public test dataset completes end to end.
- Expected outputs, versions, reports, and provenance files are asserted.
- Resume behavior is tested after an intentional interruption.
- At least one target executor profile is exercised outside a developer laptop.
- Documentation states supported versions, inputs, outputs, limitations, and attribution.
Reference Routing
Read only the references needed for the task:
- Usage guide for common prompts and run commands.
- Best practices for process design, schemas, testing, and nf-core conventions.
- Channel patterns for joins, grouping, branching, and metadata maps.
- Container management for Conda, BioContainers, registries, and custom images.
- Version tracking for robust
versions.yml patterns.
- RNA-seq example for a compact multi-process workflow.
Output Expectations
When creating or reviewing a pipeline, return:
- The proposed file structure and data-flow contract.
- Complete code for the requested files.
- A minimal test command and expected outputs.
- Assumptions, operational limitations, and executor-specific notes.
- Attribution for adapted modules or shared upstream work.
1---2name: nextflow-pipelines3description: Create, review, and improve production-quality bioinformatics pipelines with Nextflow DSL2. Use whenever a task involves Nextflow processes, workflows, channels, nf-core conventions, containers, HPC or cloud executors, resource configuration, testing, provenance, or reproducible pipeline design.4---56# Nextflow Pipelines78Build maintainable DSL2 workflows that are portable across local workstations, HPC schedulers, and cloud executors. Treat reproducibility, validation, and operational failure modes as core design requirements.910## Workflow11121. Clarify inputs, outputs, sample metadata, scale, executor, and acceptance criteria.132. Separate orchestration from atomic tool processes.143. Model channels with metadata maps and explicit tuple contracts.154. Pin every tool through Conda and a container.165. Configure resources through labels and executor-specific profiles.176. Add stub runs, small synthetic tests, and execution reports.187. Record tool versions and workflow parameters with the outputs.1920## Project Structure2122```text23pipeline/24├── main.nf25├── nextflow.config26├── nextflow_schema.json27├── modules/local/28├── subworkflows/local/29├── conf/30│ ├── base.config31│ └── test.config32├── assets/33├── tests/34└── README.md35```3637Keep processes atomic and reusable. Keep workflow composition in `main.nf` or subworkflows, and keep environment-specific settings in profiles rather than hard-coded paths.3839## Minimal DSL2 Pattern4041```nextflow42nextflow.enable.dsl = 24344params.input = null45params.outdir = 'results'4647process FASTQC {48 tag "${meta.id}"49 label 'process_low'5051 conda 'bioconda::fastqc=0.12.1'52 container 'quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0'5354 input:55 tuple val(meta), path(reads)5657 output:58 tuple val(meta), path('*.html'), emit: html59 path 'versions.yml', emit: versions6061 script:62 """63 fastqc --threads ${task.cpus} ${reads}6465 cat <<-END_VERSIONS > versions.yml66 "${task.process}":67 fastqc: \$(fastqc --version | sed 's/FastQC v//')68 END_VERSIONS69 """7071 stub:72 """73 touch ${meta.id}_fastqc.html74 echo '"${task.process}": {fastqc: "0.12.1"}' > versions.yml75 """76}7778workflow {79 reads_ch = Channel80 .fromFilePairs(params.input, checkIfExists: true)81 .map { id, reads -> tuple([id: id, single_end: false], reads) }8283 FASTQC(reads_ch)84}85```8687## Design Rules8889- Use DSL2 and named outputs.90- Pass sample identity in a metadata map rather than encoding it in filenames.91- Use tuples to keep metadata and files synchronized.92- Pin exact tool versions in both `conda` and `container` directives.93- Prefer one main tool per process so failures and resource use remain observable.94- Define CPU, memory, and time through labels in configuration.95- Use `publishDir` for final artifacts only; treat `work/` as an execution cache.96- Add `stub:` blocks for fast structural tests.97- Emit `versions.yml` from every tool process.98- Avoid institutional paths, credentials, and account identifiers in public configuration.99100## Resource Configuration101102```groovy103process {104 withLabel: process_low {105 cpus = 2106 memory = 4.GB107 time = 2.h108 }109110 errorStrategy = { task.exitStatus in 137..140 ? 'retry' : 'terminate' }111 maxRetries = 2112}113114profiles {115 slurm {116 process.executor = 'slurm'117 singularity.enabled = true118 docker.enabled = false119 }120121 test {122 includeConfig 'conf/test.config'123 }124}125```126127Cap dynamic retries at an explicit maximum. A retry policy should distinguish resource exhaustion from deterministic tool or input failures.128129## Validation Checklist130131- `nextflow config` resolves without errors.132- A stub run completes: `nextflow run . -profile test -stub-run`.133- A small synthetic or public test dataset completes end to end.134- Expected outputs, versions, reports, and provenance files are asserted.135- Resume behavior is tested after an intentional interruption.136- At least one target executor profile is exercised outside a developer laptop.137- Documentation states supported versions, inputs, outputs, limitations, and attribution.138139## Reference Routing140141Read only the references needed for the task:142143- [Usage guide](references/usage-guide.md) for common prompts and run commands.144- [Best practices](references/best-practices.md) for process design, schemas, testing, and nf-core conventions.145- [Channel patterns](references/channel-patterns.md) for joins, grouping, branching, and metadata maps.146- [Container management](references/container-management.md) for Conda, BioContainers, registries, and custom images.147- [Version tracking](references/version-tracking.md) for robust `versions.yml` patterns.148- [RNA-seq example](examples/rnaseq.nf) for a compact multi-process workflow.149150## Output Expectations151152When creating or reviewing a pipeline, return:1531541. The proposed file structure and data-flow contract.1552. Complete code for the requested files.1563. A minimal test command and expected outputs.1574. Assumptions, operational limitations, and executor-specific notes.1585. Attribution for adapted modules or shared upstream work.