用途
Nextflow 是一种工作流语言与运行时,用于构建可复现、可移植、可扩展的数据流水线。它在生物信息学领域占据主导地位,但也适用于任何数据密集型计算。nf-core 是一个社区项目,在 Nextflow 之上整理生产级流水线、可复用模块以及 nf-core 工具链。
本技能同时覆盖两类工作:运行已有的 nf-core 或自定义 Nextflow 流水线,以及开发自己的流水线(Nextflow 语言 + nf-core 约定、使用 nf-test 测试、配置与部署)。
主要能力
- 数据流编程:流水线是由 channel 连接的 process 任务。Nextflow 根据数据依赖自动推断执行顺序与并行度,无需编写显式调度器。
- 一次编写,随处运行:同一流水线通过修改配置/profile 即可在本地、HPC(SLURM、SGE、LSF、PBS)和云端(AWS Batch、Google Batch、Azure Batch、Kubernetes)运行,无需改动代码。
- 可复现性:每任务容器(Docker/Singularity/Apptainer/Conda/Wave)+ -resume 缓存 + 固定流水线版本。
- DSL2 是现代且必需的语法:模块化的 process/workflow/include 定义。
核心概念:
- Process:执行脚本(默认 Bash)的工作单元,声明 input:、output:,可选指令(resources、container、publishDir、tag、errorStrategy)以及 script:/shell:/exec: 块。每个任务在独立的工作目录(work/xx/yy…)中运行。
- Channel:连接 process 的异步队列。队列 channel 是可消费的流;值 channel 保存单个可复用值。通过 channel.of、channel.fromPath、channel.fromFilePairs、channel.value 等工厂创建。
- Operator:转换/组合 channel,如 map、filter、collect、groupTuple、join、combine、mix、flatten、branch、multiMap、splitCsv、view、set。
- Workflow:组合 process。DSL2 workflow 可声明 take:(输入)、main:(逻辑)、emit:(命名输出),并可作为子工作流被 include。未命名的 workflow {} 是入口点。
- Module:通过 include { NAME } from './path' 暴露 process/workflow 的 .nf 文件(支持 as 别名)。
- Configuration:nextflow.config 设置 params、process 指令、executor、容器引擎和命名 profile。withName:/withLabel: 选择器可定位特定 process。
- meta map(nf-core):在输入/输出元组中随文件携带元数据映射([ id:'sample1', single_end:false ])的约定,使样本在流水线中保持标记。
使用方式
环境要求:Nextflow 需要 Bash 和 Java 17 或更新版本(支持 17–25)。用 java -version 验证。
安装 Nextflow(自包含启动器): curl -s https://get.nextflow.io | bash # creates ./nextflow sudo mv nextflow /usr/local/bin/ # put on PATH nextflow info # verify
或通过 conda/bioconda(同时获得受管理的 Java): conda create -n nf -c bioconda -c conda-forge nextflow nf-core
nf-core 工具(Python),用于创建/lint/运行 nf-core 资产: uv pip install nf-core # or: conda install -c bioconda nf-core nf-core --version
为可复现性固定引擎:export NXF_VER=24.10.0(仅在需要时使用 [edge] 版本)。
运行 nf-core 流水线:先用内置 test profile 做冒烟测试,它使用极小数据并验证环境是否正常。
1. Confirm setup works (downloads pipeline + tiny test data)
nextflow run nf-core/rnaseq -profile test,docker --outdir results
2. Real run: pin a revision (-r), pick a container engine, pass inputs
nextflow run nf-core/rnaseq -r 3.14.0
-profile docker
--input samplesheet.csv
--genome GRCh38
--outdir results
-resume
-profile(单破折号)选择内置配置 profile,可用逗号组合,如 test,docker。容器/基础设施 profile(docker、singularity、conda)互斥,只能选一个。--input、--genome、--outdir(双破折号)是流水线参数。nf-core 流水线接收 samplesheet CSV,而非零散文件。-resume 复用上次运行的缓存结果。-r <version> 固定发布版本以保证可复现性。
编写最小流水线: #!/usr/bin/env nextflow
process SAYHELLO { tag "$greeting" publishDir "results", mode: 'copy'
input:
val greeting
output:
path "${greeting}.txt"
script:
"""
echo '$greeting world' > ${greeting}.txt
"""
}
workflow { channel.of('hello', 'bonjour', 'hola') | SAYHELLO }
nextflow run main.nf # add -resume on reruns
nf-core tools CLI(v3+)将子命令分组在 pipelines、modules、subworkflows 下:
- nf-core pipelines list:列出/搜索 nf-core 流水线(--json、keywords)
- nf-core pipelines create:从 nf-core 模板搭建新流水线
- nf-core pipelines launch <name>:交互式、schema 驱动的运行命令 + params 文件
- nf-core pipelines download <name>:下载流水线 + 容器以供离线/HPC 使用
- nf-core pipelines lint:按 nf-core 标准 lint 流水线(在仓库根目录运行)
- nf-core pipelines schema build:通过 Web GUI 构建/编辑 nextflow_schema.json
- nf-core pipelines create-params-file <name>:生成带文档的 YAML params 文件
- nf-core pipelines bump-version / sync:升级版本 / 与模板更新同步
- nf-core modules list/info/install/update/remove:管理 nf-core/modules 中的模块
- nf-core modules create / lint / test:编写、lint 和 nf-test 模块
- nf-core modules patch / bump-versions:修补已安装模块 / 升级工具版本
- nf-core subworkflows install/create/lint/test:子工作流的相同生命周期
常用 nextflow CLI:
- nextflow run <pipeline> -profile <p> --outdir <dir>:运行流水线(路径、.nf 或 user/repo)
- -resume:复用先前运行的缓存结果
- -r <rev>:运行特定 git 版本/标签/分支
- -params-file params.yml:从 YAML/JSON 提供参数
- -c custom.config:叠加额外配置文件
- -with-report -with-trace -with-timeline -with-dag flow.html:执行报告、trace、时间线、DAG
- -stub-run:仅运行 stub: 块(干跑管道)
- nextflow log:查看过往运行
- nextflow clean -f -before <run>:删除旧的 work/ 数据
- nextflow pull / drop / list / info <repo>:管理缓存的远程流水线
适用场景
当用户想要:运行 nf-core 或自定义 Nextflow 流水线,或调试失败/续跑运行;编写或修改 .nf 脚本、nextflow.config、profiles 或 nextflow_schema.json;编写或测试 nf-core 风格的模块/子工作流(main.nf、meta.yml、tests/、nf-test);配置执行器、容器或资源,扩展到 HPC 或云端;构建可复现的科学/生物信息学工作流(即使未提及“Nextflow”);理解 process、channel、operator、take/emit、publishDir、ext.args、meta map。
限制与注意事项
- 版本相关细节可能过时,需对照已安装版本的官方文档确认。
- 不得编造结果、参数、标识符、引用或统计。若无法运行或验证,应明确说明。
- 不要超出统计支持范围解释生物学意义;报告多重检验校正。
- 涉及花钱、占用共享 GPU 或云配额、触碰个人或患者数据、或不可撤销的操作前,应先询问。
- 若输入之间的基因组构建或标识符不匹配,应停止并先协调(liftover、ID 映射)。
- 若指令中的函数、标志或端点在已安装版本中缺失,应查阅该版本自身文档(help()、--help、官方文档),适配并告知用户,绝不臆造 API。
- 若必需输入、标识符或参数不明确,应询问用户,或在运行前明确说明假设。
官方文档:Nextflow https://www.nextflow.io/docs/latest/ · nf-core https://nf-co.re/docs/ · 培训 https://training.nextflow.io/