

Ở Phần 1, chúng tôi đã xây dựng một pipeline gọi biến thể GATK gồm mười bước bằng bash. Cách làm đó phù hợp với số mẫu ít, nhưng trở nên khó quản lý khi số mẫu tăng lên. Bài này viết lại các bước tương tự thành các process của Nextflow, chạy chúng trong container, rồi kiểm tra — bằng checksum — rằng hai cách hiện thực cho ra kết quả tương đương.
Repository: mã nguồn của bài hướng dẫn này nằm trong repository nf-germline-short-read-variant-calling, với hai thư mục riêng cho bản bash (
bash-gatk) và bản Nextflow (nextflow-gatk).
Mã nguồn của bài hướng dẫn này có trong GitHub repository: https://github.com/nttg8100/nf-germline-short-read-variant-calling/tree/0.1.0
Pipeline bash thuận tiện cho:
Vì thực thi tuần tự, thời gian chạy tăng gần như tuyến tính theo số mẫu: một phép ngoại suy thô cho 1.000 mẫu nằm ở mức hàng chục giờ. Con số này nên được xem như ước lượng chứ không phải benchmark đo được, vì nó phụ thuộc vào phần cứng, độ phủ và phiên bản công cụ.
Nextflow bổ sung:
-resume.Song song hóa theo mẫu là lý do chính giúp thời gian chạy giảm; mức tăng tốc cụ thể phụ thuộc vào số slot tính toán có sẵn chứ không phải một hệ số cố định.
Chạy pipeline bash đầy đủ từ Phần 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.shCác workflow manager như Nextflow, Snakemake và Cromwell chia sẻ một vài ý tưởng cốt lõi.
Một template script được định nghĩa với các placeholder cho biến, và workflow engine điền giá trị thực tế vào lúc chạy.
Ví dụ:
fastqc {{fastq_r1}} {{fastq_r2}}Container cung cấp môi trường tái lập được và kết quả nhất quán giữa các hệ thống.
Ví dụ:
# 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}}Scheduler tối ưu việc dùng tài nguyên bằng cách chạy nhiều task song song trong giới hạn CPU và bộ nhớ. Chúng cũng hỗ trợ chạy trên cluster hoặc cloud, quản lý phụ thuộc giữa các bước và chỉ khởi động bước sau khi đầu vào đã sẵn sàng.
Project Nextflow hoàn chỉnh có trong thư mục nextflow-gatk:
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.tomlMỗi bước được tách thành một module rồi import vào main.nf. Cách này giúp từng bước dễ tái sử dụng hơn.
Tạo file main.nf:
Một process cơ bản cần:
process <PROCESS_NAME>, để định nghĩa process;input, định nghĩa các channel dùng làm đầu vào — có thể là file, string, số hoặc tổ hợp của chúng viết dưới dạng tuple, tương tự dictionary trong Python;output, định nghĩa các channel đầu ra, dùng biểu thức chính quy để thu thập các file được tạo ra;script, template code nội suy các biến đầu vào và cấu hình của process thành câu lệnh thực sự được chạy.#!/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 )}Tạo một thư mục làm việc:
# 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
Một số việc được xử lý tự động ở đây:
work được tạo với thư mục con đặt tên theo hash cho mỗi task, và mỗi task có thể chạy lại độc lập bên ngoài Nextflow.Chuyển vào một thư mục con trong work để xem cấu trúc. Những gì Nextflow tạo ở đó là một bản ghi khép kín của một task:
.command.begin: file đánh dấu thời điểm task bắt đầu..command.err: standard error của process, hữu ích khi debug..command.log: gộp standard output và standard error..command.out: standard output của process..command.run: script wrapper của chính Nextflow; nó xử lý thiết lập môi trường, stage file, chạy container, xử lý lỗi, và cuối cùng chạy script của bạn..command.sh: khối script bạn viết trong process — chính là các câu lệnh khoa học, ví dụ chạy FastQC.Tóm lại:
.command.sh là logic pipeline của bạn..command.run là wrapper tự động của Nextflow..command.out và .command.err hỗ trợ debug.Tất cả các file này cùng tạo thành một audit trail đầy đủ cho tính tái lập và xử lý sự cố. Một ví dụ đầy đủ về cấu trúc thư mục:
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.ymlVề dòng sample1_R2.fastq.gz -> /path/to/data/sample1_R2.fastq.gz:
Một phiên bản rút gọn của .command.run trông như sau:
#!/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_ENTRYTóm lại, wrapper này:
.command.sh thông qua /bin/bash -ue <repo>/tmp/work/.../.command.sh,Như đã nói ở trên, cấu hình Singularity có thể được thêm vào 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 )}Tạo file nextflow.config trong cùng thư mục:
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
Điều gì xảy ra:
container từ định nghĩa process.quay.io/biocontainers/fastqc:0.12.1--hdfd78af_0.work/singularity/).Có thể kiểm chứng thay đổi này trong .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 )}Giữ nguyên nextflow.config; lệnh Singularity không thay đổi. Để dùng Docker, sửa nextflow.config:
docker.enabled = truedocker.autoMounts = trueCó thể xem câu lệnh được sinh ra để thấy Docker áp giới hạn CPU và bộ nhớ như thế nào:
# check docker command quicklycat work/8e/7c7d8e091a9b515adbe0f136e13f93/.command.run|grep dockerKết quả dưới đây cho thấy giới hạn được truyền qua --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.shNhớ dọn thư mục work. Việc thay đổi container, cpus hay memory không làm thay đổi hash của process, nên kết quả đã cache trước đó sẽ tiếp tục được dùng lại.
Trong một pipeline, dữ liệu raw thường được mô tả bằng file CSV gọi là samplesheet. Workflow sau đó đọc CSV và tạo các channel đầu vào.
#!/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 )}Sau đó có thể chạy pipeline với file samplesheet.csv có nội dung:
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.gzChạy lại sẽ thấy hai tuple tương ứng với hai dòng của file đầu vào. Đây là lúc workflow phân bố công việc: hai process FastQC chạy cùng lúc trên máy cục bộ, với giới hạn tài nguyên do Docker áp đặt.
nextflow run main.nf --input samplesheet.csv -resume
Một pipeline là chuỗi công cụ nối tiếp nhau, trong đó output của bước này là input của bước kế tiếp. Hãy mở rộng ví dụ với hai module trimgalore và bwa.
Có gì mới?
trimgalore để trim adapter và bwa để align.trimgalore chạy trực tiếp trên ch_input và phát ra output là reads.trimgalore (TRIM_GALORE.out.reads) trở thành input của bwa, giữ nguyên định dạng tuple..out.<emit_name>.script có thể chứa các câu lệnh def để định nghĩa biến động..collect() để chúng được tái sử dụng ở nhiều process, vì mặc định channel hoạt động như hàng đợi.Cấu trúc dưới đây cho thấy cách nối các process, truyền output giữa các module và quản lý tài nguyên dùng chung trong 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 )}Chạy lại để kiểm tra mọi thứ hoạt động:
nextflow run main.nf --input samplesheet.csv -resume
Phiên bản Nextflow cuối cùng chỉ gồm:
nextflow.config duy nhất,main.nf duy nhất chỉ import và dùng các module.Các cải tiến sâu hơn sẽ được cấu hình trong những bài tiếp theo.
Phần này trình bày cách tách một script duy nhất thành các module/subworkflow để dễ quản lý hơn.
Module nằm trong thư mục modules và được import như một thư viện vào 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'Subworkflow là một chồng nhiều module. Nó có tên để import về sau và khai báo input, output tương tự một 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)}Trong main.nf, subworkflow được import như một module:
include { PREPROCESSING } from './subworkflows/local/preprocessing/main'Ở thời điểm này config chỉ có hai dòng:
docker.enabled = truedocker.autoMounts = trueCó thể mở rộng như sau:
params {}.profile nhóm một tập tham số và thiết lập, nên một lần chạy được cấu hình bằng -profile test,docker, dùng tập tham số test cùng cấu hình docker.manifest giúp ghi lại thông tin về pipeline.timeline, trace và report giúp theo dõi quá trình chạy dễ hơn.check_max và label giúp đặt CPU và bộ nhớ hợp lý. Ví dụ, với chỉ bốn CPU, chạy hai process FastQC cùng lúc là hợp lý; chạy nhiều hơn có thể gây tranh chấp tài nguyên./*======================================================================================== 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 } }}Các module sau đó có thể được cập nhật với:
tag, hiển thị mẫu đang chạy thay vì chỉ số thứ tự của process;label, suy ra CPU và bộ nhớ tự động — trong môi trường công nghiệp, việc tinh chỉnh thủ công thường tốt hơn để tài nguyên được dùng ở mức khoảng 80%;version, để phiên bản công cụ được ghi lại; MULTIQC có thể thu thập các file versions.yml và tạo báo cáo mà không cần ghi chép thủ công cho từng process.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 """}Việc publish output sẽ dễ hơn với cấu hình dưới đây, giúp mọi process có thư mục output viết thường.
process { publishDir = [ path: { "${params.outdir}/${task.process.toLowerCase()}" }, mode: "symlink", enabled: true ]}Workflow dưới đây minh họa các khái niệm Nextflow chuẩn được dùng trong chuỗi bài này. Cần chạy script tải dữ liệu trước để dữ liệu đầu vào và tham chiếu đã sẵn sàng.
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
Ý tưởng khá đơn giản: cùng một phân tích nên cho cùng kết quả bất kể controller là bash hay Nextflow. Một vài điểm cần lưu ý khi so sánh:
md5sum để so sánh nội dung file.Với FastQC, bước đầu tiên, cần loại bỏ timestamp bên trong file ZIP rồi so sánh nội dung:
#!/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 "=========================================="Kết quả được hiển thị dưới đây. Cách so sánh tương tự có thể lặp lại cho các bước còn lại.
Làm việc với một workflow orchestrator sẽ dễ nắm bắt hơn khi hiểu rõ các cơ chế bên dưới: template, container và configuration.





