

In Part 1, we built a ten-step GATK variant calling pipeline in bash. That approach works well for small batches of samples, but it becomes awkward when the number of samples grows. This post rewrites the same steps as Nextflow processes, runs them inside containers, and then checks — with checksums — that the two implementations produce equivalent output.
Repository: the code for this tutorial lives in the nf-germline-short-read-variant-calling repository, with separate directories for the bash (
bash-gatk) and Nextflow (nextflow-gatk) implementations.
The code for this tutorial is available in the GitHub repository: https://github.com/nttg8100/nf-germline-short-read-variant-calling/tree/0.1.0
The bash pipeline is convenient for:
Because execution is serial, wall-clock time grows roughly linearly with the number of samples: a rough extrapolation for 1,000 samples is on the order of tens of hours. That figure should be read as an estimate rather than a measured benchmark, since it depends on hardware, coverage and tool versions.
Nextflow adds:
-resume.Parallelising across samples is the main reason the wall-clock time drops; the exact speed-up depends on the number of available compute slots rather than on a fixed factor.
Run the complete bash pipeline from Part 1:
# Clone and setup repository (first time only)git clone git@github.com:nttg8100/nf-germline-short-read-variant-calling.git -b 0.1.0cd nf-germline-short-read-variant-calling
# Install dependenciespixi shell --manifest-path bash-gatk
# Download test databash scripts/download_data.sh
# Run pipelinebash bash-gatk/gatk_pipeline.shWorkflow managers such as Nextflow, Snakemake and Cromwell share a few core ideas.
A script template is defined with placeholders for variables, and the workflow engine fills in the actual values at runtime.
Example:
fastqc {{fastq_r1}} {{fastq_r2}}Containers provide a reproducible environment and consistent results across systems.
Example:
# Pull container imagesingularity pull quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0
# Run FastQC inside the containersingularity run fastqc_0.12.1--hdfd78af_0.sif fastqc {{fastq_r1}} {{fastq_r2}}Schedulers optimise resource usage by running multiple tasks in parallel while respecting CPU and memory limits. They also support execution on clusters or cloud platforms, manage dependencies, and start downstream steps only when their inputs are ready.
The final Nextflow project is available in the nextflow-gatk directory:
nextflow-gatk├── main.nf├── modules│ ├── bwa_mem.nf│ ├── fastqc.nf│ ├── gatk_applybqsr.nf│ ├── gatk_baserecalibrator.nf│ ├── gatk_collectmetrics.nf│ ├── gatk_genotypegvcfs.nf│ ├── gatk_haplotypecaller.nf│ ├── gatk_markduplicates.nf│ ├── samtools_sort.nf│ └── trim_galore.nf├── nextflow.config├── pixi.lock├── pixi.tomlEach step is split into a module that is imported into main.nf. This makes the individual steps easier to reuse.
Create main.nf:
A basic process needs:
process <PROCESS_NAME>, that defines the process;input, which defines the channels used as inputs — a file, a string, a number or a combination of them written as a tuple, similar to a dictionary in Python;output, which defines the output channels, using regular expressions to collect the produced files;script, the code template that interpolates the input variables and process configuration into the command that is actually run.#!/usr/bin/env nextflownextflow.enable.dsl=2 // enable DSL2// Define the module as templateprocess FASTQC { input: tuple val(meta), path(reads) // input variable, example: [sample, [/path/read1.fq, /path/read2.fq ]]
output: tuple val(meta), path("*.html") // output variable, example: [sample, [/path/read1.html, /path/read2.html ]]
script: // template that has been mentioned above """ # Quality Control with FastQC # This generates HTML and ZIP files with quality metrics fastqc $reads """}
// You need to define the workflow to call to tool to runworkflow { // Prepare the channel which later is allow the workflow to know how to get input, output to run ch_input = Channel.of([ params.sample , [file(params.fastq_r1), file(params.fastq_r2)] ]) // For file, we simply add with file() // ch_input.view() // print statement that you can view what it is, ex: [sample1, [/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_1.fastq.gz, /nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_2.fastq.gz]] // Run workflow FASTQC ( ch_input )}Create a working folder:
# env setupmkdir -p tempcd temppixi initpixi add nextflow singularitypixi shellpixi add fastqc# add the workflow, check folder structuretree .├── main.nf├── pixi.lock└── pixi.toml
# now you can run itnextflow run main.nf \ --sample sample1 \ --fastq_r1 https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_1.fastq.gz \ --fastq_r2 https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_2.fastq.gz
# not only nextflow, but many tool will help you to resume your work, ignore re-run if it was ran before.# with nextflow, it can be easy done by adding `-resume`nextflow run main.nf -resume \ --sample sample1 \ --fastq_r1 https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_1.fastq.gz \ --fastq_r2 https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_2.fastq.gz
Several things are handled automatically here:
work folder is created with a hashed subfolder per task, and each task can be re-run independently outside Nextflow.Change into a work subfolder to inspect its structure. What Nextflow creates there is a self-contained record of one task:
.command.begin: marker file recording when the task started..command.err: standard error of the process, useful when debugging..command.log: combined standard output and standard error..command.out: standard output of the process..command.run: Nextflow's own wrapper script; it handles environment setup, file staging, container execution, error handling, and finally runs your script..command.sh: the script block you wrote in the process — the actual scientific commands, for example running FastQC.Summary:
.command.sh is your pipeline logic..command.run is Nextflow's automation wrapper..command.out and .command.err help with debugging.Together these files form a complete audit trail for reproducibility and troubleshooting. A complete example folder listing:
drwxrwxr-x 2 user user 4096 Feb 18 15:32 .drwxrwxr-x 3 user user 4096 Feb 18 15:32 ..-rw-rw-r-- 1 user user 0 Feb 18 15:32 .command.begin-rw-rw-r-- 1 user user 1750 Feb 18 15:32 .command.err-rw-rw-r-- 1 user user 1868 Feb 18 15:32 .command.log-rw-rw-r-- 1 user user 118 Feb 18 15:32 .command.out-rw-rw-r-- 1 user user 3758 Feb 18 15:32 .command.run ← Wrapper script-rw-rw-r-- 1 user user 347 Feb 18 15:32 .command.sh ← Your actual script-rw-rw-r-- 1 user user 1 Feb 18 15:32 .exitcode-rw-rw-r-- 1 user user 598807 Feb 18 15:32 sample1_R1_fastqc.html-rw-rw-r-- 1 user user 352734 Feb 18 15:32 sample1_R1_fastqc.ziplrwxrwxrwx 1 user user 113 Feb 18 15:32 sample1_R1.fastq.gz -> /path/to/data/sample1_R1.fastq.gz-rw-rw-r-- 1 user user 582761 Feb 18 15:32 sample1_R2_fastqc.html-rw-rw-r-- 1 user user 326969 Feb 18 15:32 sample1_R2_fastqc.ziplrwxrwxrwx 1 user user 113 Feb 18 15:32 sample1_R2.fastq.gz -> /path/to/data/sample1_R2.fastq.gz-rw-rw-r-- 1 user user 50 Feb 18 15:32 versions.ymlAbout the line sample1_R2.fastq.gz -> /path/to/data/sample1_R2.fastq.gz:
An abridged .command.run looks like this:
#!/bin/bash### ---### name: 'FASTQC (1)'### outputs:### - '*.html'### - '*.zip'### ...set -eset -uNXF_DEBUG=${NXF_DEBUG:=0}; [[ $NXF_DEBUG > 1 ]] && set -xNXF_ENTRY=${1:-nxf_main}
# Sleep for a given number of seconds (fallback to 1s if error)nxf_sleep() { sleep $1 2>/dev/null || sleep 1;}
# Generate a timestamp in millisecondsnxf_date() { local ts=$(date +%s%3N); if [[ ${#ts} == 10 ]]; then echo ${ts}000 elif [[ $ts == *%3N ]]; then echo ${ts/\%3N/000} elif [[ $ts == *3N ]]; then echo ${ts/3N/000} elif [[ ${#ts} == 13 ]]; then echo $ts else echo "Unexpected timestamp value: $ts"; exit 1 fi}
# Print sorted environment variables, masking AWS credentialsnxf_env() { echo '============= task environment =============' env | sort | sed "s/\(.*\)AWS\(.*\)=\(.\{6\}\).*/\1AWS\2=\3xxxxxxxxxxxxx/" echo '============= task output =================='}
# Kill a process and all its child processesnxf_kill() { declare -a children while read P PP;do children[$PP]+=" $P" done < <(ps -e -o pid= -o ppid=)
kill_all() { [[ $1 != $$ ]] && kill $1 2>/dev/null || true for i in ${children[$1]:=}; do kill_all $i; done }
kill_all $1}
# Create a temporary directorynxf_mktemp() { local base=${1:-/tmp} mkdir -p "$base" if [[ $(uname) = Darwin ]]; then mktemp -d $base/nxf.XXXXXXXXXX else TMPDIR="$base" mktemp -d -t nxf.XXXXXXXXXX fi}
# Copy files/directories recursively, preserving symlinksnxf_fs_copy() { local source=$1 local target=$2 local basedir=$(dirname $1) mkdir -p $target/$basedir cp -fRL $source $target/$basedir}
# Move files/directories recursivelynxf_fs_move() { local source=$1 local target=$2 local basedir=$(dirname $1) mkdir -p $target/$basedir mv -f $source $target/$basedir}
# Rsync files/directories recursivelynxf_fs_rsync() { rsync -rRl $1 $2}
# Copy files using rclonenxf_fs_rclone() { rclone copyto $1 $2/$1}
# Copy files using fcpnxf_fs_fcp() { fcp $1 $2/$1}
# Handler for process exit: sets exit code and exitson_exit() { local last_err=$? local exit_status=${nxf_main_ret:=0} [[ ${exit_status} -eq 0 && ${nxf_unstage_ret:=0} -ne 0 ]] && exit_status=${nxf_unstage_ret:=0} [[ ${exit_status} -eq 0 && ${last_err} -ne 0 ]] && exit_status=${last_err} printf -- $exit_status > <repo>/tmp/work/4b/957bdd32e8e03d575e17185ef4f09a/.exitcode set +u exit $exit_status}
# Handler for termination signals: kills child processeson_term() { set +e [[ "$pid" ]] && nxf_kill $pid}
# Launch the main command scriptnxf_launch() { /bin/bash -ue <repo>/tmp/work/4b/957bdd32e8e03d575e17185ef4f09a/.command.sh}
# Stage input files (create symlinks in work directory)nxf_stage() { true # stage input files rm -f test_1.fastq.gz rm -f test_2.fastq.gz ln -s <repo>/tmp/work/stage-8a00ef45-80ce-4033-9494-354f1e68968c/a7/c6160733f5332acde30841c8d7cfb6/test_1.fastq.gz test_1.fastq.gz ln -s <repo>/tmp/work/stage-8a00ef45-80ce-4033-9494-354f1e68968c/2e/f8faa73d537d3b8bc792454dc541de/test_2.fastq.gz test_2.fastq.gz}
# Unstage output files (placeholder, does nothing)nxf_unstage_outputs() { true}
# Unstage control files (placeholder, does nothing)nxf_unstage_controls() { true}
# Unstage outputs and controls after main executionnxf_unstage() { if [[ ${nxf_main_ret:=0} == 0 ]]; then (set -e -o pipefail; (nxf_unstage_outputs | tee -a .command.out) 3>&1 1>&2 2>&3 | tee -a .command.err) nxf_unstage_ret=$? fi nxf_unstage_controls}
# Main entry point: sets up traps, environment, stages files, runs command, unstages outputsnxf_main() { trap on_exit EXIT trap on_term TERM INT USR2 trap '' USR1
[[ "${NXF_CHDIR:-}" ]] && cd "$NXF_CHDIR" NXF_SCRATCH='' [[ $NXF_DEBUG > 0 ]] && nxf_env touch <repo>/tmp/work/4b/957bdd32e8e03d575e17185ef4f09a/.command.begin set +u set -u [[ $NXF_SCRATCH ]] && cd $NXF_SCRATCH export NXF_TASK_WORKDIR="$PWD" nxf_stage
set +e (set -o pipefail; (nxf_launch | tee .command.out) 3>&1 1>&2 2>&3 | tee .command.err) & pid=$! wait $pid || nxf_main_ret=$? nxf_unstage}
# Run the entry point (default: nxf_main)$NXF_ENTRYIn short, the wrapper:
.command.sh through /bin/bash -ue <repo>/tmp/work/.../.command.sh,As mentioned above, a Singularity configuration can be added to the template:
#!/usr/bin/env nextflownextflow.enable.dsl=2 // enable DSL2// Define the module as templateprocess FASTQC { container 'quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0' // NEW CONFIG
input: tuple val(meta), path(reads) // input variable, example: [sample, [/path/read1.fq, /path/read2.fq ]]
output: tuple val(meta), path("*.html") // output variable, example: [sample, [/path/read1.html, /path/read2.html ]]
script: """ # Quality Control with FastQC # This generates HTML and ZIP files with quality metrics fastqc $reads """}
workflow { ch_input = Channel.of([ params.sample , [file(params.fastq_r1), file(params.fastq_r2)] ]) // Run workflow FASTQC ( ch_input )}Create the file nextflow.config in the same folder:
singularity.enabled = truesingularity.autoMounts = truenextflow run main.nf -resume \ --sample sample1 \ --fastq_r1 https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_1.fastq.gz \ --fastq_r2 https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_2.fastq.gz
What happens:
container directive from the process definition.quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0.work/singularity/).The change can be verified in .command.run:
nxf_launch() { set +u; env - PATH="$PATH" ${TMP:+SINGULARITYENV_TMP="$TMP"} ${TMPDIR:+SINGULARITYENV_TMPDIR="$TMPDIR"} ${NXF_TASK_WORKDIR:+SINGULARITYENV_NXF_TASK_WORKDIR="$NXF_TASK_WORKDIR"} singularity exec --no-home --pid -B <repo>/tmp/work <repo>/tmp/work/singularity/quay.io-biocontainers-fastqc-0.12.1--hdfd78af_0.img /bin/bash -c "cd $NXF_TASK_WORKDIR; /bin/bash -ue <repo>/tmp/work/87/28135f97ffa37daf6192aaf824dedb/.command.sh"}#!/usr/bin/env nextflownextflow.enable.dsl=2 // enable DSL2// Define the module as templateprocess FASTQC { container 'quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0' cpus 2 // NEW CONFIG memory "4.GB" // NEW CONFIG input: tuple val(meta), path(reads) // input variable, example: [sample, [/path/read1.fq, /path/read2.fq ]]
output: tuple val(meta), path("*.html") // output variable, example: [sample, [/path/read1.html, /path/read2.html ]]
script: """ # Quality Control with FastQC # This generates HTML and ZIP files with quality metrics fastqc $reads """}
workflow { ch_input = Channel.of([ params.sample , [file(params.fastq_r1), file(params.fastq_r2)] ]) // Run workflow FASTQC ( ch_input )}Keep the nextflow.config as it is; the Singularity command does not change. To use Docker instead, change nextflow.config:
docker.enabled = truedocker.autoMounts = trueThe generated command can be inspected to see how Docker applies the CPU and memory limits:
# check docker command quicklycat work/8e/7c7d8e091a9b515adbe0f136e13f93/.command.run|grep dockerThe output below shows the CPU and memory limits being passed as --cpu-shares 2048 --memory 4096m:
docker run -i --cpu-shares 2048 --memory 4096m -e "NXF_TASK_WORKDIR" -v <repo>/tmp/work:<repo>/tmp/work -w "$NXF_TASK_WORKDIR" --name $NXF_BOXID quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0 /bin/bash -ue <repo>/tmp/work/8e/7c7d8e091a9b515adbe0f136e13f93/.command.shRemember to clean the work directory. Changing container, cpus or memory does not change the process hash, and previously cached results will otherwise be reused.
In a pipeline, the raw data is usually described by a CSV file called a samplesheet. The workflow then reads the CSV and builds the input channels.
#!/usr/bin/env nextflownextflow.enable.dsl=2 // enable DSL2// Define the module as templateprocess FASTQC { container 'quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0' cpus 2 // NEW CONFIG memory "4.GB" // NEW CONFIG input: tuple val(meta), path(reads) // input variable, example: [sample, [/path/read1.fq, /path/read2.fq ]]
output: tuple val(meta), path("*.html") // output variable, example: [sample, [/path/read1.html, /path/read2.html ]]
script: """ # Quality Control with FastQC # This generates HTML and ZIP files with quality metrics fastqc $reads """}
workflow { Channel .fromPath(params.input) .splitCsv(header: true) .map { row -> def meta = [:] // create empty list for metadata meta.id = row.sample // it map value of row as sample id, add to meta list // Support both new format (with lane) and old format (without lane) meta.lane = row.lane // require to have lane, for combining data later if one sample is sequenced by more than one lanes def reads = [] // create empty list for files reads.add(file(row.fastq_1)) // add R1 fq reads.add(file(row.fastq_2)) // add R2 fq return [ meta, reads ] } .set { ch_input } ch_input.view() // View channel here // Run workflow FASTQC ( ch_input )}The pipeline can then be run with a samplesheet.csv whose content is:
sample,lane,fastq_1,fastq_2sample1,1,https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_1.fastq.gz,https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_2.fastq.gzsample2,1,https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_1.fastq.gz,https://raw.githubusercontent.com/nf-core/test-datasets/modules/data/genomics/homo_sapiens/illumina/fastq/test_2.fastq.gzRunning again shows two tuples matching the two rows of the input file. This is the point at which the workflow distributes work: two FastQC processes run at the same time on the local machine, with resource limits enforced by Docker.
nextflow run main.nf --input samplesheet.csv -resume
A pipeline is a chain of tools in which the output of one step feeds the next. Let us extend the example with trimgalore and bwa modules.
What changed?
trimgalore for adapter trimming and bwa for alignment.trimgalore runs directly on ch_input and emits its output as reads.trimgalore (TRIM_GALORE.out.reads) becomes the input of bwa, keeping the tuple format..out.<emit_name>.script block can contain def statements to define variables dynamically..collect() makes them reusable across processes, since channels behave as queues by default.The structure below shows how processes are chained, how outputs are passed between modules, and how shared resources are managed in Nextflow.
#!/usr/bin/env nextflownextflow.enable.dsl=2 // enable DSL2// Define the module as templateprocess FASTQC { container 'quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0' cpus 2 // NEW CONFIG memory "4.GB" // NEW CONFIG input: tuple val(meta), path(reads) // input variable, example: [sample, [/path/read1.fq, /path/read2.fq ]]
output: tuple val(meta), path("*.html") // output variable, example: [sample, [/path/read1.html, /path/read2.html ]]
script: """ # Quality Control with FastQC # This generates HTML and ZIP files with quality metrics fastqc $reads """}
process TRIM_GALORE { container 'quay.io/biocontainers/trim-galore:0.6.10--hdfd78af_0' cpus 2 // NEW CONFIG memory "4.GB" // NEW CONFIG input: tuple val(meta), path(reads)
output: tuple val(meta), path("*_val_{1,2}.fq.gz"), emit: reads tuple val(meta), path("*_trimming_report.txt"), emit: log
script: """ # Adapter Trimming and Quality Filtering trim_galore \\ --paired \\ --quality 20 --length 50 --fastqc \\ --cores $task.cpus \\ ${reads} """}
process BWA_MEM { container 'quay.io/biocontainers/mulled-v2-fe8faa35dbf6dc65a0f7f5d4ea12e31a79f73e40:219b6c272b25e7e642ae3ff0bf0c5c81a5135ab4-0' cpus 2 // NEW CONFIG memory "4.GB" // NEW CONFIG
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
script: def args = task.ext.args ?: '-M' def prefix = task.ext.prefix ?: "${meta.id}" def read_group = "@RG\\tID:${meta.id}\\tSM:${meta.id}\\tPL:ILLUMINA\\tLB:${meta.id}_lib"
""" # Read Alignment with BWA-MEM bwa mem \\ -t $task.cpus \\ $args \\ -R "${read_group}" \\ $reference \\ ${reads[0]} ${reads[1]} \\ | samtools view -Sb - > ${prefix}_aligned.bam """}
workflow { Channel .fromPath(params.input) .splitCsv(header: true) .map { row -> def meta = [:] // create empty list for metadata meta.id = row.sample // it map value of row as sample id, add to meta list // Support both new format (with lane) and old format (without lane) meta.lane = row.lane // require to have lane, for combining data later if one sample is sequenced by more than one lanes def reads = [] // create empty list for files reads.add(file(row.fastq_1)) // add R1 fq reads.add(file(row.fastq_2)) // add R2 fq return [ meta, reads ] } .set { ch_input }
// Run workflow FASTQC ( ch_input )
TRIM_GALORE ( ch_input )
ch_reference = Channel.fromPath(params.reference).collect() ch_reference_fai = Channel.fromPath(params.reference + ".fai").collect() ch_reference_dict = Channel.fromPath(params.reference_dict).collect() ch_bwa_index = Channel.fromPath(params.bwa_index+".{amb,ann,bwt,pac,sa}").collect()
BWA_MEM ( TRIM_GALORE.out.reads, // use output of trim_galore as input for bwa_mem ch_reference, ch_reference_fai, ch_reference_dict, ch_bwa_index )}Run the workflow again to check that everything works:
nextflow run main.nf --input samplesheet.csv -resume
The final Nextflow version contains only:
nextflow.config,main.nf that imports and uses the modules.Further improvements are configured in the next posts.
This section shows how a single script can be split into modules and subworkflows that are easier to manage.
Modules live in a modules folder and are imported like a library into main.nf:
// Include modulesinclude { FASTQC } from './modules/fastqc'include { TRIM_GALORE } from './modules/trim_galore'include { BWA_MEM } from './modules/bwa_mem'include { SAMTOOLS_SORT } from './modules/samtools_sort'include { GATK_MARKDUPLICATES } from './modules/gatk_markduplicates'include { GATK_BASERECALIBRATOR } from './modules/gatk_baserecalibrator'include { GATK_APPLYBQSR } from './modules/gatk_applybqsr'include { GATK_COLLECTMETRICS } from './modules/gatk_collectmetrics'include { GATK_HAPLOTYPECALLER } from './modules/gatk_haplotypecaller'include { GATK_GENOTYPEGVCFS } from './modules/gatk_genotypegvcfs'A subworkflow stacks several modules. It has a name that can be imported later and declares inputs and outputs similar to a process:
#!/usr/bin/env nextflow
/*======================================================================================== PREPROCESSING SUBWORKFLOW======================================================================================== Steps 1-8: QC, Trimming, Alignment, Deduplication, BQSR, Quality Metrics========================================================================================*/
include { FASTP } from '../../../modules/local/fastp/trim/main'include { BWAMEM2_INDEX } from '../../../modules/local/bwa/index/main'include { BWA_MEM2 } from '../../../modules/local/bwa/mem2/main'include { SAMTOOLS_SORT } from '../../../modules/local/samtools/sort/main'include { SAMTOOLS_MERGE } from '../../../modules/local/samtools/merge/main'include { GATKSPARK_MARKDUPLICATES } from '../../../modules/local/gatkspark/markduplicates/main'include { GATKSPARK_BASERECALIBRATOR } from '../../../modules/local/gatkspark/baserecalibrator/main'include { GATKSPARK_APPLYBQSR } from '../../../modules/local/gatkspark/applybqsr/main'include { GATK_COLLECTMETRICS } from '../../../modules/local/gatk/collectmetrics/main'
workflow PREPROCESSING {
take: reads_ch // channel: [ val(meta), [ path(read1), path(read2) ] ] ref_fasta // path: reference FASTA ref_fai // path: reference FAI ref_dict // path: reference dict bwa2_index // channel: Optional BWA index files index_bwa2_reference // channel: Optional BWA index files dbsnp_vcf // path: dbSNP VCF dbsnp_tbi // path: dbSNP TBI known_indels_vcf // path: known indels VCF known_indels_tbi // path: known indels TBI
main: ch_versions = Channel.empty()
// Check if BWA index needs to be generated // If empty, generate it; otherwise use provided index if (index_bwa2_reference){ BWAMEM2_INDEX(ref_fasta) ch_versions = ch_versions.mix(BWAMEM2_INDEX.out.versions) bwa2_index_ch = BWAMEM2_INDEX.out.index } else{ bwa2_index_ch = Channel.fromPath(bwa2_index) } // // STEP 1: Adapter Trimming, Quality Filtering, and QC with fastp // FASTP ( reads_ch ) ch_versions = ch_versions.mix(FASTP.out.versions)
// // STEP 2: Read Alignment with BWA-MEM2 // BWA_MEM2 ( FASTP.out.reads, ref_fasta, ref_fai, ref_dict, bwa2_index_ch ) ch_versions = ch_versions.mix(BWA_MEM2.out.versions)
// // STEP 3: Sort BAM file // SAMTOOLS_SORT ( BWA_MEM2.out.bam ) ch_versions = ch_versions.mix(SAMTOOLS_SORT.out.versions)
// // STEP 4: Merge lanes per sample (if multiple lanes exist) // SAMTOOLS_SORT.out.bam .map { meta, bam -> 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 -> return [ metas[0], bams ] } .set { ch_bams_to_merge }
SAMTOOLS_MERGE ( ch_bams_to_merge ) ch_versions = ch_versions.mix(SAMTOOLS_MERGE.out.versions)
// // STEP 5: Mark Duplicates with GATK Spark // GATKSPARK_MARKDUPLICATES ( SAMTOOLS_MERGE.out.bam ) ch_versions = ch_versions.mix(GATKSPARK_MARKDUPLICATES.out.versions)
// // STEP 6: Base Quality Score Recalibration - Generate table // GATKSPARK_BASERECALIBRATOR ( GATKSPARK_MARKDUPLICATES.out.bam.join(GATKSPARK_MARKDUPLICATES.out.bai), ref_fasta, ref_fai, ref_dict, dbsnp_vcf, dbsnp_tbi, known_indels_vcf, known_indels_tbi ) ch_versions = ch_versions.mix(GATKSPARK_BASERECALIBRATOR.out.versions)
// // STEP 7: Apply BQSR // GATKSPARK_APPLYBQSR ( GATKSPARK_MARKDUPLICATES.out.bam .join(GATKSPARK_MARKDUPLICATES.out.bai) .join(GATKSPARK_BASERECALIBRATOR.out.table), ref_fasta, ref_fai, ref_dict ) ch_versions = ch_versions.mix(GATKSPARK_APPLYBQSR.out.versions)
// // STEP 8: Collect Alignment Metrics // GATK_COLLECTMETRICS ( GATKSPARK_APPLYBQSR.out.bam.join(GATKSPARK_APPLYBQSR.out.bai), ref_fasta, ref_fai, ref_dict ) ch_versions = ch_versions.mix(GATK_COLLECTMETRICS.out.versions)
emit: bam = GATKSPARK_APPLYBQSR.out.bam // channel: [ val(meta), path(bam) ] bai = GATKSPARK_APPLYBQSR.out.bai // channel: [ val(meta), path(bai) ] alignment_summary = GATK_COLLECTMETRICS.out.alignment_summary // channel: [ val(meta), path(metrics) ] insert_metrics = GATK_COLLECTMETRICS.out.insert_metrics // channel: [ val(meta), path(metrics) ] versions = ch_versions // channel: path(versions.yml)}In main.nf, the subworkflow is imported like a module:
include { PREPROCESSING } from './subworkflows/local/preprocessing/main'At this point the config has only two lines:
docker.enabled = truedocker.autoMounts = trueIt can be extended as follows:
params {} block.profile groups a set of parameters and settings so that a run is configured with -profile test,docker, using the test parameter set together with the docker configuration.manifest block documents the pipeline.timeline, trace and report makes execution easier to monitor.check_max and label help set CPUs and memory sensibly. With only four CPUs, for example, running two FastQC processes at a time is reasonable; starting more may cause resource contention./*======================================================================================== GATK Variant Calling Pipeline - Nextflow Config========================================================================================*/
// Global default paramsparams { // Input/output options input = null outdir = 'results_nextflow'
// Direct input parameters (alternative to samplesheet) sample = 'sample1' fastq_r1 = null fastq_r2 = null
// Reference files reference = '../reference/genome.fasta' dbsnp = '../reference/dbsnp_146.hg38.vcf.gz' known_indels = '../reference/mills_and_1000G.indels.vcf.gz'
// Resource limits max_cpus = 4 max_memory = '15.GB' max_time = '24.h'
// Help help = false}
// Modulesprocess { publishDir = [ path: { "${params.outdir}/${task.process.toLowerCase()}" }, mode: "symlink", enabled: true ]}
// Process configurationprocess { // Default settings shell = ['/bin/bash', '-euo', 'pipefail']
// Error handling errorStrategy = { task.exitStatus in [143,137,104,134,139] ? 'retry' : 'finish' } maxRetries = 1 maxErrors = '-1'
// Process labels withLabel: process_low { cpus = { check_max( 2 * task.attempt, 'cpus' ) } memory = { check_max( 2.GB * task.attempt, 'memory' ) } time = { check_max( 4.h * task.attempt, 'time' ) } }
withLabel: process_medium { cpus = { check_max( 4 * task.attempt, 'cpus' ) } memory = { check_max( 4.GB * task.attempt, 'memory' ) } time = { check_max( 8.h * task.attempt, 'time' ) } }
withLabel: process_high { cpus = { check_max( 4 * task.attempt, 'cpus' ) } memory = { check_max( 16.GB * task.attempt, 'memory' ) } time = { check_max( 16.h * task.attempt, 'time' ) } }}
// Execution profilesprofiles { docker { docker.enabled = true docker.userEmulation = true singularity.enabled = false podman.enabled = false shifter.enabled = false charliecloud.enabled = false }
singularity { singularity.enabled = true singularity.autoMounts = true docker.enabled = false podman.enabled = false shifter.enabled = false charliecloud.enabled = false } test { params.sample = 'sample1' params.fastq_r1 = '../data/sample1_R1.fastq.gz' params.fastq_r2 = '../data/sample1_R2.fastq.gz' params.reference = '../reference/genome.fasta' params.dbsnp = '../reference/dbsnp_146.hg38.vcf.gz' params.known_indels = '../reference/mills_and_1000G.indels.vcf.gz' params.max_cpus = 4 params.max_memory = '15.GB' }}
// Manifestmanifest { name = 'GATK Variant Calling Pipeline' author = 'River' homePage = 'https://github.com/your-repo/gatk-variant-calling' description = 'GATK best practices variant calling pipeline' mainScript = 'main.nf' nextflowVersion = '!>=22.10.0' version = '1.0.0'}
// Function to ensure that resource requirements don't go beyond maximum limitdef 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: Max memory '${params.max_memory}' is not valid! Using default value: $obj" return obj } } else if (type == 'time') { try { if (obj.compareTo(params.max_time as nextflow.util.Duration) == 1) return params.max_time as nextflow.util.Duration else return obj } catch (all) { println "WARNING: Max time '${params.max_time}' is not valid! Using default value: $obj" return obj } } else if (type == 'cpus') { try { return Math.min( obj, params.max_cpus as int ) } catch (all) { println "WARNING: Max cpus '${params.max_cpus}' is not valid! Using default value: $obj" return obj } }}The modules can then be updated with:
tag, which shows which sample is currently running instead of only a process number;label, which derives CPU and memory requests automatically — in industrial settings it is often better to tune these by hand so that around 80% of the requested resources are actually used;version, so that tool versions are tracked; MULTIQC can collect the versions.yml files and produce a report without per-process bookkeeping.process FASTQC { tag "$meta.id" label 'process_medium'
// Use BioContainers FastQC image // Works with both Docker and Singularity (no root required) container 'quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0'
input: tuple val(meta), path(reads)
output: tuple val(meta), path("*.html"), emit: html tuple val(meta), path("*.zip") , emit: zip 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}"
// Determine if single-end or paired-end def input_files = reads instanceof List ? reads.join(' ') : reads
""" # Quality Control with FastQC # This generates HTML and ZIP files with quality metrics fastqc \\ $args \\ --threads $task.cpus \\ $input_files
# Create versions file cat <<-END_VERSIONS > versions.yml "${task.process}": fastqc: \$(fastqc --version | sed 's/FastQC v//') END_VERSIONS """}Publishing output is easier with the configuration below, which gives every process a lowercase output folder.
process { publishDir = [ path: { "${params.outdir}/${task.process.toLowerCase()}" }, mode: "symlink", enabled: true ]}The workflow below illustrates the standard Nextflow concepts used throughout this series. The download script has to be run first so that the input and reference data are in place.
git clone git@github.com:nttg8100/nf-germline-short-read-variant-calling.git -b 0.1.0cd nf-germline-short-read-variant-calling
# Install dependenciespixi shell --manifest-path bash-gatk
# Download test databash scripts/download_data.sh
# Run pipelinecd nextflow-gatknextflow run main.nf -profile docker
The idea is simple: the same analysis should give the same result regardless of the controller (bash or Nextflow). A few points help when comparing:
md5sum to compare file contents.For FastQC, the first step, the timestamps inside the ZIP file can be removed before comparing the contents:
#!/bin/bash## Validation Script: Compare Bash and Nextflow Outputs# This script validates that the Nextflow pipeline produces identical results to the bash pipeline#
set -euo pipefail
echo "=========================================="echo "Pipeline Migration Validation"echo "Comparing Bash vs Nextflow (Step 1: FastQC)"echo "=========================================="echo
# DirectoriesBASH_DIR="bash-gatk/results/qc/sample1"NEXTFLOW_DIR="nextflow-gatk/results_nextflow/qc/sample1"
echo "=== File Comparison ==="echo
# Compare all HTML filesfor file in ${BASH_DIR}/*.html; do fname=$(basename "$file") if [[ -f "${NEXTFLOW_DIR}/$fname" ]]; then BASH_MD5=$(md5sum "$file" | cut -d' ' -f1) NEXTFLOW_MD5=$(md5sum "${NEXTFLOW_DIR}/$fname" | cut -d' ' -f1) if [ "$BASH_MD5" == "$NEXTFLOW_MD5" ]; then echo " ✓ PASS: $fname" echo " MD5: $BASH_MD5" else echo " ✗ FAIL: $fname" echo " Bash MD5: $BASH_MD5" echo " Nextflow MD5: $NEXTFLOW_MD5" fi else echo " ✗ FAIL: $fname (missing in Nextflow)" fidoneecho
echo "Comparing ZIP files (content only, ignoring timestamps):"
# Compare all ZIP files (ignore archive timestamps, check actual content)for file in ${BASH_DIR}/*.zip; do fname=$(basename "$file") if [[ -f "${NEXTFLOW_DIR}/$fname" ]]; then BASE_NAME=$(basename "$file" .zip) BASH_EXTRACT="/tmp/bash_${BASE_NAME}" NF_EXTRACT="/tmp/nextflow_${BASE_NAME}" rm -rf "$BASH_EXTRACT" "$NF_EXTRACT" mkdir -p "$BASH_EXTRACT" "$NF_EXTRACT" unzip -q "$file" -d "$BASH_EXTRACT" unzip -q "${NEXTFLOW_DIR}/$fname" -d "$NF_EXTRACT" if diff -r "$BASH_EXTRACT" "$NF_EXTRACT" > /dev/null 2>&1; then echo " ✓ PASS: $fname (content identical)" else echo " ✗ FAIL: $fname (content differs)" fi FASTQC_DATA="${BASE_NAME}/fastqc_data.txt" if [ -f "$BASH_EXTRACT/$FASTQC_DATA" ] && [ -f "$NF_EXTRACT/$FASTQC_DATA" ]; then BASH_DATA_MD5=$(md5sum "$BASH_EXTRACT/$FASTQC_DATA" | cut -d' ' -f1) NF_DATA_MD5=$(md5sum "$NF_EXTRACT/$FASTQC_DATA" | cut -d' ' -f1) echo " fastqc_data.txt MD5: $BASH_DATA_MD5" if [ "$BASH_DATA_MD5" == "$NF_DATA_MD5" ]; then echo " ✓ Core data file identical" else echo " ✗ Data file content differs" fi fi else echo " ✗ FAIL: $fname (missing in Nextflow)" fidone
echoecho "=== Summary ==="echo "HTML files: Byte-for-byte identical ✓"echo "ZIP files: Content identical (timestamps differ as expected) ✓"echoecho "=== Conclusion ==="echo "✓ VALIDATION SUCCESSFUL"echo "The Nextflow pipeline produces scientifically equivalent outputs to the bash pipeline."echo "Differences in ZIP file MD5 checksums are due to embedded timestamps,"echo "which is expected behavior and does not affect scientific reproducibility."echo "=========================================="The output is shown below. The same comparison can be repeated for the remaining steps.
Working with a workflow orchestrator is more tractable once the mechanisms behind it are understood: template, container and configuration.





