

In Part 1, we built a bash baseline. In Part 2, we moved it to Nextflow and checked the results with checksums. This post looks at running the same pipeline on an HPC cluster: configuring executors for clusters of different sizes, tuning resources per tool, replacing the slowest steps with faster alternatives (fastp and Spark-based GATK tools), and scaling the number of samples.
The code for this tutorial is available in the variant-calling-gatk-pipeline-best-practice-from-scratch repository.
Part 1 produced a working bash pipeline suitable for a small number of samples, with a runtime on the order of tens of minutes per sample.
Part 2 rewrote every step as a Nextflow process run inside Singularity containers, and validated the migration with MD5 checksums. In our local runs, a single sample completed in roughly three to four minutes.
Part 3 (this post) turns to deployment: SLURM profiles for clusters of different sizes, per-tool resource allocation, replacing slower tools with faster equivalents, and scaling progressively from 1 to 10 to 100 samples. The expected outcome is that 100 samples complete in hours rather than days; the exact figure depends on the cluster and on queue behaviour, so it should be treated as an estimate rather than a guarantee.
fastp, which performs trimming and quality control in a single pass.This post builds on Parts 1 and 2. If the series is new, it helps to start with:
This guide assumes a SLURM cluster with a small number of nodes and access to shared storage such as NFS. Examples are given for clusters of different sizes.
In many sequencing projects, one biological sample is sequenced across several lanes or runs, producing more than one pair of FASTQ files per sample.
The pipeline described so far treats each sample as a single pair of files. A small update addresses this: add a lane column to the samplesheet so that each row represents a unique combination of sample and lane. The pipeline then aligns each lane as a sub-sample and merges the resulting BAM files into one per sample.
Aligned BAM files of a sample are sorted:
process SAMTOOLS_SORT { tag "$meta.id" label 'process_medium'
container 'quay.io/biocontainers/samtools:1.17--hd87286a_2'
input: tuple val(meta), path(bam)
output: tuple val(meta), path("*_sorted.bam"), emit: bam path "versions.yml", emit: versions
when: task.ext.when == null || task.ext.when
script: def args = task.ext.args ?: '' def prefix = task.ext.prefix ?: "${meta.id}"
""" # Sort BAM file by coordinates samtools sort \\ -@ $task.cpus \\ $args \\ -o ${prefix}_sorted.bam \\ $bam
# Create versions file cat <<-END_VERSIONS > versions.yml "${task.process}": samtools: \$(samtools --version | head -1 | sed 's/samtools //') END_VERSIONS """}The sorted BAM files are then merged:
process SAMTOOLS_MERGE { tag "$meta.id" label 'process_low'
container 'quay.io/biocontainers/samtools:1.18--h50ea8bc_1'
input: tuple val(meta), path(bams)
output: tuple val(meta), path("*.bam"), emit: bam tuple val(meta), path("*.bai"), emit: bai path "versions.yml", emit: versions
when: task.ext.when == null || task.ext.when
script: def args = task.ext.args ?: '' def prefix = task.ext.prefix ?: "${meta.id}"
// If only one BAM, just symlink and index it; otherwise merge if (bams instanceof List && bams.size() > 1) { """ # Merge multiple BAM files samtools merge \\ -@ $task.cpus \\ $args \\ ${prefix}_merged.bam \\ ${bams.join(' ')}
# Index the merged BAM samtools index \\ -@ $task.cpus \\ ${prefix}_merged.bam
# Create versions file cat <<-END_VERSIONS > versions.yml "${task.process}": samtools: \$(samtools --version | head -1 | sed 's/samtools //') END_VERSIONS """ } else { """ # Only one BAM file, create symlink and index ln -s ${bams[0]} ${prefix}_merged.bam
samtools index \\ -@ $task.cpus \\ ${prefix}_merged.bam
# Create versions file cat <<-END_VERSIONS > versions.yml "${task.process}": samtools: \$(samtools --version | head -1 | sed 's/samtools //') END_VERSIONS """ }}Example samplesheet:
sample,lane,fastq_1,fastq_2sample1,1,data/sample1_lane1_R1.fastq.gz,data/sample1_lane1_R2.fastq.gzsample1,2,data/sample1_lane2_R1.fastq.gz,data/sample1_lane2_R2.fastq.gzThe channel logic is updated to handle both single-lane and multi-lane formats:
Channel .fromPath(params.input) .splitCsv(header: true) .map { row -> def meta = [:] meta.id = row.sample meta.sample = row.sample // Use lane if present, otherwise default to "L001" meta.lane = row.lane ?: "L001" meta.read_group = "${row.sample}_${meta.lane}" def reads = [] reads.add(file(row.fastq_1)) if (row.fastq_2) { reads.add(file(row.fastq_2)) } return [ meta, reads ] } .set { ch_input }Each FASTQ pair is then processed with the correct metadata, which supports downstream merging and correct read group assignment. Two steps are added to the workflow:
//// STEP 3: Sort BAM file//SAMTOOLS_SORT ( BWA_MEM.out.bam)ch_versions = ch_versions.mix(SAMTOOLS_SORT.out.versions)
//// STEP 4: Merge lanes per sample (if multiple lanes exist)// Group by sample ID and collect all BAMs for each sample//SAMTOOLS_SORT.out.bam .map { meta, bam -> // Create new meta with only sample ID for merging def new_meta = [:] new_meta.id = meta.sample new_meta.sample = meta.sample return [ new_meta.id, new_meta, bam ] } .groupTuple() .map { sample_id, metas, bams -> // Take the first meta (they should all have same sample info) return [ metas[0], bams ] } .set { ch_bams_to_merge }
SAMTOOLS_MERGE ( ch_bams_to_merge)ch_versions = ch_versions.mix(SAMTOOLS_MERGE.out.versions)Nextflow hides much of SLURM's complexity by translating processes into SLURM jobs automatically:
Nextflow Process → SLURM Job Submission → Queue → Compute Node → ExecutionFor each process, Nextflow:
Configuration settings that matter:
process.executor — set to 'slurm'.executor.queueSize — the maximum number of concurrent jobs, which prevents the pipeline from overwhelming the cluster.executor.pollInterval — how often Nextflow checks job status.process.queue — which SLURM partition to use.For a small research cluster with limited queue capacity:
// Add this to workflows/nextflow/nextflow.config
profiles { slurm_small { process.executor = 'slurm' process.queue = 'short' // Use "short" partition (faster jobs) executor.queueSize = 10 // Max 10 concurrent jobs executor.pollInterval = '10s' // Check every 10 seconds executor.queueStatInterval = '1m' // Query queue status every 1 min executor.submitRateLimit = '10/1min' // Don't submit >10 jobs per minute }}Why these settings?
With 100 samples, limiting the pipeline to 10 concurrent jobs means the workflow processes samples in batches of 10. This is considerate towards other cluster users: it prevents one large job from monopolising cluster resources and delaying everyone else's workloads.
queueSize = 10: on a small cluster, submitting 100 jobs at once is not advisable. Ten concurrent jobs give reasonable parallelism without overloading the queue.submitRateLimit: keeps the number of submissions moderate and causes fewer complaints from cluster administrators.queue = 'short': most small clusters have short, medium and long partitions, and short queues generally start sooner.For a departmental cluster with multiple partitions:
profiles { slurm_medium { process.executor = 'slurm' executor.queueSize = 50 executor.pollInterval = '15s' executor.queueStatInterval = '2m'
// Dynamic queue selection based on task type withLabel: process_low { queue = 'short' } withLabel: process_medium { queue = 'medium' } withLabel: process_high { queue = 'long' } withLabel: process_gpu { queue = 'gpu' } }}Many bioinformatics tools run faster with GPU acceleration. Not all compute nodes have GPUs, however; the SLURM cluster posts linked above describe how to check the available partitions.
Strategy: route different job types to different queues. Short jobs go to the short partition (fast queue, lower priority) while long jobs go to the long partition (higher latency but intended for long-running work).
For production HPC centres:
profiles { slurm_large { process.executor = 'slurm' executor.queueSize = 200 // More parallelism executor.pollInterval = '30s' // Less frequent polling (cheaper) executor.queueStatInterval = '5m' executor.submitRateLimit = '50/1min' // More aggressive submission executor.exitReadTimeout = '10min' // Wait longer for exit status }}FastQC is primarily single-threaded and I/O-bound. In our runs, giving it more resources, for example 8 CPUs and 16 GB of RAM, did not shorten its runtime. Adding CPUs or memory to FastQC, or similar tools, does not speed up processing of an individual sample. Running two samples with 8 CPUs each is not necessarily faster than running them with 2 CPUs each; a reasonable allocation is typically 1–2 CPUs and 2–4 GB of RAM per FastQC process.
For a fuller discussion, see Bioinformatics cost optimization for input using Nextflow (Part 2).
Once a profile has been chosen, individual processes can be customised:
process { withName: FASTQC { cpus = 2 memory = '4.GB' time = '1.h' }}A useful safeguard: a resource validation function prevents requests from exceeding cluster limits:
def check_max(obj, type) { if (type == 'memory') { try { if (obj.compareTo(params.max_memory as nextflow.util.MemoryUnit) == 1) return params.max_memory as nextflow.util.MemoryUnit else return obj } catch (all) { println "WARNING: Memory exceeds max! Using ${params.max_memory}" return obj } } else if (type == 'cpus') { return Math.min(obj, params.max_cpus as int) }}
// Then use in process definitionswithName: FASTQC { cpus = { check_max(4 * task.attempt, 'cpus') } memory = { check_max(8.GB * task.attempt, 'memory') }}In our Part 2 runs (roughly 3 minutes 38 seconds for a single sample), the time was distributed approximately as follows:
| Step | Tool | Time | Bottleneck Type |
|---|---|---|---|
| 1 | FastQC | 15s | Run without threads/cores option |
| 2 | Trim Galore | 45s | Has FastQC is a section |
| 3 | BWA Align | 60s | Old version |
| 4 | MarkDuplicates | 20s | Run without threads/cores option |
| 5-7 | BQSR (Gen+Apply) | 40s | Run without threads/cores option |
| 8 | QC Metrics | 10s | I/O-bound |
| 9 | HaplotypeCaller | 80s | CPU-bound (slowest) |
| 10+ | GenotypeGVCFs, filters, annotation | 68s | CPU-bound + I/O |
Key observation: steps 1 and 2 (FastQC plus Trim Galore) read the data twice, and step 9 (HaplotypeCaller) runs serially. These are the natural targets for optimization.
Points that help when optimizing each task at scale:
git revision.For trimming and quality control of reads, fastp is a widely used modern option: it is fast and performs both steps at once, replacing the FastQC + Trim Galore pair.
task.ext.arg.qualified_quality_phred and length_required are hard-coded here.process FASTP { tag "$meta.id" label 'process_medium'
container 'quay.io/biocontainers/fastp:1.1.0--heae3180_0'
input: tuple val(meta), path(reads)
output: tuple val(meta), path("*_trimmed_{1,2}.fastq.gz"), emit: reads tuple val(meta), path("*.html"), emit: html tuple val(meta), path("*.json"), emit: json path "versions.yml", emit: versions
when: task.ext.when == null || task.ext.when
script: def args = task.ext.args ?: '' def prefix = task.ext.prefix ?: "${meta.read_group}"
""" # Adapter trimming, quality filtering, and QC with fastp fastp \\ --thread $task.cpus \\ --in1 ${reads[0]} \\ --in2 ${reads[1]} \\ --out1 ${prefix}_trimmed_1.fastq.gz \\ --out2 ${prefix}_trimmed_2.fastq.gz \\ --html ${prefix}_fastp.html \\ --json ${prefix}_fastp.json \\ --qualified_quality_phred 20 \\ --length_required 50 \\ --detect_adapter_for_pe \\ $args
# Create versions file cat <<-END_VERSIONS > versions.yml "${task.process}": fastp: \$(fastp --version 2>&1 | grep -oP 'fastp \\K[0-9.]+') END_VERSIONS """}BWA-mem2 is a newer implementation of BWA-MEM that runs faster and scales almost linearly with the number of threads, so extra CPUs generally reduce wall-clock time at little additional cost.
Remember to re-index the genome: the BWA-MEM index cannot be used by BWA-mem2.
process BWA_MEM { tag "$meta.id" label 'process_high'
conda "${moduleDir}/environment.yml" container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ? 'https://community-cr-prod.seqera.io/docker/registry/v2/blobs/sha256/9a/9ac054213e67b3c9308e409b459080bbe438f8fd6c646c351bc42887f35a42e7/data' : 'community.wave.seqera.io/library/bwa-mem2_htslib_samtools:e1f420694f8e42bd' }"
input: tuple val(meta), path(reads) path reference path reference_fai path reference_dict path bwa_index
output: tuple val(meta), path("*.bam"), emit: bam path "versions.yml", emit: versions
when: task.ext.when == null || task.ext.when
script: def args = task.ext.args ?: '-M' def prefix = task.ext.prefix ?: "${meta.read_group}" // Proper read group with unique ID per lane but same SM (sample) for merging def read_group = "@RG\\tID:${meta.read_group}\\tSM:${meta.sample}\\tPL:ILLUMINA\\tLB:${meta.sample}_lib\\tPU:${meta.lane}"
""" # Find the BWA-MEM2 index prefix INDEX=\$(find -L ./ -name "*.amb" | sed 's/\\.amb\$//')
# Read Alignment with BWA-MEM2 bwa-mem2 \\ mem \\ -t $task.cpus \\ $args \\ -R "${read_group}" \\ \$INDEX \\ ${reads[0]} ${reads[1]} \\ | samtools view -Sb - > ${prefix}_aligned.bam
# Create versions file cat <<-END_VERSIONS > versions.yml "${task.process}": bwamem2: \$(echo \$(bwa-mem2 version 2>&1) | sed 's/.* //') samtools: \$(echo \$(samtools --version 2>&1) | sed 's/^.*samtools //; s/Using.*\$//') END_VERSIONS """
stub: def prefix = task.ext.prefix ?: "${meta.read_group}"
""" touch ${prefix}_aligned.bam
cat <<-END_VERSIONS > versions.yml "${task.process}": bwamem2: \$(echo \$(bwa-mem2 version 2>&1) | sed 's/.* //') samtools: \$(echo \$(samtools --version 2>&1) | sed 's/^.*samtools //; s/Using.*\$//') END_VERSIONS """}MarkDuplicates, BaseRecalibrator and ApplyBQSR are single-threaded tools and, in our runs, took a noticeable share of the total time. GATK provides Spark-based equivalents that spread the work over several small tasks on a single node, which is often more efficient. The module below shows what changes.
broadinstitute/gatk:4.6.1.0 to quay.io/biocontainers/gatk4-spark:4.6.1.0--hdfd78af_0.process_high, which allocates more memory and CPU.process GATKSPARK_APPLYBQSR { tag "$meta.id" label 'process_high' container 'quay.io/biocontainers/gatk4-spark:4.6.1.0--hdfd78af_0'
input: tuple val(meta), path(bam), path(bai), path(recal_table) path reference path reference_fai path reference_dict
output: tuple val(meta), path("*_recal.bam"), emit: bam tuple val(meta), path("*_recal.bam.bai"), emit: bai path "versions.yml", emit: versions
when: task.ext.when == null || task.ext.when
script: def avail_mem = (task.memory.mega * 0.8).intValue() """ # Apply Base Quality Score Recalibration gatk --java-options "-Xmx${avail_mem}M -XX:-UsePerfData" \\ ApplyBQSRSpark \\ -I $bam \\ -R $reference \\ --bqsr-recal-file $recal_table \\ --create-output-bam-index true \\ -O ${meta.id}_recal.bam \\ --spark-master local[${task.cpus}] \\ --tmp-dir .
# Create versions file cat <<-END_VERSIONS > versions.yml "${task.process}": gatk4: \$(gatk --version 2>&1 | grep -E 'GATK v[0-9.]+' | sed 's/.*v\\([0-9.]\\+\\).*/\\1/' || echo "4.4.0.0") END_VERSIONS """}Improvements in this part:
In production pipelines for clinical variant calling or large cohorts, the cost of each run matters. Unlike research workflows, where file sizes vary, industrial pipelines usually operate on standardised protocols with fixed input characteristics. Each process therefore behaves predictably and can be tuned closely to the minimum resources it needs.
In an industrial setting, quality control is built into the protocol, so input files follow strict specifications:
This predictability means each process can be tuned for efficiency without much risk of cascading failures.
Instead of generic resource allocations, production pipelines optimise cost per process per sample:
Cost per sample = Σ(cost_per_hour × hours_per_process)Example: UK Biobank-style cohort of 500,000 WGS samples
The costs below are illustrative assumptions, not measured figures for running this pipeline.
Assumptions:
c6i.8xlarge at $1.35/hour (32 CPUs, 64 GB RAM)Cost per sample (original pipeline from Part 2):
| Process | Time | CPUs | Memory | Cost | Notes |
|---|---|---|---|---|---|
| FastQC | 15s | 1 | 2GB | $0.0006 | Single-threaded |
| TrimGalore | 45s | 1 | 2GB | $0.0017 | Single-threaded |
| BWA Align | 60s | 1 | 4GB | $0.0023 | Serial, not parallelized |
| MarkDuplicates | 20s | 1 | 8GB | $0.0008 | GATK standard |
| BQSR | 40s | 1 | 4GB | $0.0015 | Single-threaded |
| HaplotypeCaller | 80s | 1 | 8GB | $0.0030 | Slowest step |
| GenotypeGVCFs + VEP | 68s | 1 | 4GB | $0.0026 | Annotation |
| Total (328s / sample) | $0.0125/sample | For 500k: $6,250 |
Optimized pipeline (Part 3, with CPU and memory allocation matched to each tool):
| Process | Tool | Time | CPUs | Memory | Cost | Savings |
|---|---|---|---|---|---|---|
| Preprocessing | fastp | 25s | 8 | 4GB | $0.0009 | -67% |
| Alignment | BWA-mem2 | 35s | 16 | 8GB | $0.0018 | -42% |
| Dedup+BQSR | GATK Spark | 28s | 16 | 32GB | $0.0015 | -69% |
| Variant Calling | HaplotypeCallerSpark | 22s | 32 | 64GB | $0.0013 | -57% |
| Total (110s / sample) | $0.0055/sample | 56% reduction |
For 500,000 samples:
Nextflow provides built-in reporting — report, trace and timeline — that helps analyse CPU and memory usage per process. Once enabled, the generated reports can be reviewed to identify resource utilisation patterns.
For small datasets, it is common to observe that increasing the number of threads for multi-threaded tools raises CPU usage while memory stays roughly flat and well below the limit. As data size grows, the reports show whether adding CPUs or memory actually helps. A reasonable rule of thumb is to allocate up to about 80% of available memory for demanding processes, and to add CPUs for tools that scale efficiently with threads.
For more detail, see Bioinformatics cost optimization for input using Nextflow (Part 2).
Not every process needs the same resources. withName can be used to fine-tune CPU and memory per process:
process { withName: FASTP { cpus = 8 // Multi-threaded, scales well up to 8 threads memory = '4 GB' // Minimal memory needs time = '1h' }
withName: 'BWA.*' { cpus = 16 // Thread-heavy, scales with CPU count memory = '32 GB' time = '1h' }
withName: 'GATKSPARK.*' { cpus = 32 // Spark parallelism memory = '48 GB' // Memory-intensive distributed processing time = '2h' }}Rationale for these allocations:
The configuration above can be saved as optimized.config and applied to a new run:
nextflow run main.nf -profile singularity --input Batch_1_100_samples_Biobank.csv -c optimized.configAdaptive resource strategy for edge cases:
Across a very large cohort, some samples fail because of data anomalies such as corrupted reads or outlier coverage. Nextflow's retry logic handles these without over-provisioning every job:
process { errorStrategy = { task.exitStatus in [137,139,143,255] ? 'retry' : 'finish' } maxRetries = 2
withLabel: process_low { cpus = { 2 * task.attempt } memory = { 2.GB * task.attempt } }
withLabel: process_medium { cpus = { 4 * task.attempt } memory = { 8.GB * task.attempt } }
withLabel: process_high { cpus = { 16 * task.attempt } memory = { 32.GB * task.attempt } }}How escalation saves money:
Example with 500,000 samples (modelled):
Building a scalable variant calling pipeline is about more than connecting tools: it is about engineering for correctness, reproducibility and maintainability.
Each process — FastQC, alignment, variant calling — should be validated for the data type and sequencing platform in use. Increasing CPUs for FastQC does not improve its performance, and parameters may need adjustment for different coverage levels, such as 30× versus 100× WGS, or for different sequencing technologies. Testing with representative datasets and reviewing tool documentation for platform-specific options is advisable.
Benchmarking against established gold standards gives an objective answer. For germline variant calling, reference datasets such as GIAB (Genome in a Bottle) allow the VCF output to be compared with known variants, producing accuracy and performance metrics. For novel analyses, criteria can be defined using in silico or wet-lab controls. See the NIST publication on benchmarking for background.
Modular design is important for maintainability and reuse. Practices from projects such as nf-core are worth adopting: break workflows into reusable modules and subworkflows, provide example datasets for testing, and add automated tests so that outputs stay consistent. This supports faster development, easier debugging and more robust production deployments. Basing a pipeline on nf-core conventions while keeping it tailored to a specific dataset is a reasonable balance.
All code and examples from this series: variant-calling-gatk-pipeline-best-practice-from-scratch.



