Nextflow
概述
Nextflow 是一种用于构建可复现、可移植、可扩展数据流水线的流程语言与运行时。它在生物信息学中占主导,但也适用于任何数据密集型计算。nf-core 是一个社区,在 Nextflow 之上维护生产级 Nextflow 流水线、可复用模块与 nf-core 工具链。
关键理念:
- 数据流编程:流水线是由通道连接的
process任务构成。Nextflow 从数据依赖推断执行顺序与并行度——无需编写显式调度器。 - 一次编写,随处运行:同一流水线通过更改配置/配置文件(而非代码)即可在本地、HPC(SLURM、SGE、LSF、PBS)与云(AWS Batch、Google Batch、Azure Batch、Kubernetes)上运行。
- 可复现性:按任务容器化(Docker/Singularity/Apptainer/Conda/Wave)+
-resume缓存 + 固定的流水线版本。 - DSL2 是现代、必需的语法:模块化的
process/workflow/include定义。
本 skill 同时涵盖运行现有流水线,以及开发你自己的(Nextflow 语言 + nf-core 约定、用 nf-test 测试、配置与部署)。
何时使用本 Skill
在以下场景使用本 skill:
- 运行 nf-core 或自定义 Nextflow 流水线,或调试失败/恢复的运行。
- 编写或修改
.nf脚本、nextflow.config、配置文件或nextflow_schema.json。 - 编写或测试 nf-core 风格模块/子工作流(
main.nf、meta.yml、tests/、nf-test)。 - 配置 executor、容器或资源;扩展到 HPC 或云。
- 构建可复现的科学/生物信息学工作流(即使未指明“Nextflow”)。
- 理解 process、channel、operator、
take/emit、publishDir、ext.args、meta map。
安装
Nextflow 需要 Bash 与 Java 17 或更新(支持 17–25)。用 java -version 验证。
# 安装 Nextflow(自包含启动器)
curl -s https://get.nextflow.io | bash # 创建 ./nextflow
sudo mv nextflow /usr/local/bin/ # 放入 PATH
nextflow info # 验证
# 或经 conda/bioconda(同时获得受管 Java)
conda create -n nf -c bioconda -c conda-forge nextflow nf-core
# nf-core 工具(Python)用于创建/lint/运行 nf-core 资产
pip install nf-core # 或:conda install -c bioconda nf-core
nf-core --version
为可复现性固定引擎:export NXF_VER=24.10.0(仅在需要时使用 [edge] 版本)。对气隙/HPC,见 references/running-pipelines.md(离线模式)与 references/configuration.md。
两种工作路径
确定用户处在哪条路径上——这改变一切:
| 目标 | 从这里开始 |
|---|---|
运行现有流水线(nf-core 或给你的 .nf) |
references/running-pipelines.md |
| 开发新流水线 / 模块 / 子工作流 | references/language.md + references/developing.md |
| 配置 / 扩展(HPC、云、容器、资源) | references/configuration.md + references/containers.md |
| 测试模块/流水线 | references/testing.md |
快速开始
运行 nf-core 流水线
始终先用自带的 test 配置文件做冒烟测试;它使用微小数据,验证你的环境可用。
# 1. 确认设置可用(下载流水线 + 微小测试数据)
nextflow run nf-core/rnaseq -profile test,docker --outdir results
# 2. 真实运行:固定版本(-r),选容器引擎,传入输入
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>固定一个发布版本以保证可复现性。
用 nf-core pipelines launch <name> 以交互式、schema 校验的方式构建命令与 -params-file。见 references/running-pipelines.md。
编写最小流水线
#!/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 # 重跑时加 -resume
完整语言(process、channel、operator、带 take/main/emit 的 DSL2 workflow、module)见 references/language.md。
核心概念一瞥
- Process:运行脚本(默认 Bash)的工作单元。声明
input:、output:、可选directives(资源、容器、publishDir、tag、errorStrategy),以及script:/shell:/exec:块。每个任务在自身隔离的工作目录(work/xx/yy…)中运行。 - Channel:连接 process 的异步队列。Queue channel 是可消费的流;value channel 持有单个可复用值。用
channel.of、channel.fromPath、channel.fromFilePairs、channel.value等工厂创建。 - Operator:转换/组合通道——
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'(支持as别名)暴露 process/workflow 的.nf文件。 - Configuration:
nextflow.config设置params、process指令、executor、容器引擎与命名profiles。选择器withName:/withLabel:针对特定 process。见references/configuration.md。 - meta map(nf-core):在输入/输出元组中,文件旁携带元数据 map(
[ id:'sample1', single_end:false ])的约定,使样本在流水线中保持标记。见references/developing.md。
nf-core 工具 CLI
nf-core 工具(v3+)将子命令分组在 pipelines、modules、subworkflows 下。(nf-core lint 等裸形式仍可用但会警告——优先用分组形式。)
| 命令 | 用途 |
|---|---|
nf-core pipelines list |
列出/搜索 nf-core 流水线(--json、关键字) |
nf-core pipelines create |
从 nf-core 模板搭建新流水线 |
nf-core pipelines launch <name> |
交互式、schema 驱动的命令 + 参数文件 |
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 参数文件 |
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 |
子工作流的相同生命周期 |
完整命令参考、标志与示例:references/nf-core-tools.md。
必要 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> |
管理缓存的远程流水线 |
配置、executor、缓存内部与追踪细节:references/configuration.md。
最佳实践(高价值习惯)
- 始终先
test:真实数据前先用-profile test,docker(或singularity/conda)——快速且能捕获环境问题。 - 固定一切:流水线版本(
-r)、NXF_VER与工具版本(容器)。不要对你将发表的科学运行latest。 - 使用
-resume并理解缓存:任务在输入、脚本或容器变化时重跑。缓存调试见references/configuration.md。 - 通过 config/params-file 参数化,而非硬编码路径。将
params与 profile 保留在nextflow.config中。 - 每个 process 一个容器/conda 环境;绝不依赖主机上安装的工具。
- nf-core 开发:写新模块前先复用现有模块(
nf-core modules install);通过ext.args传递工具标志(而非硬编码在脚本中);始终包含stub:块与 nf-test 测试;提交前运行nf-core pipelines lint与prettier。 - 合理设置资源:用
process_low/medium/high标签与errorStrategy 'retry'配合动态task.attempt扩展,而非一次性巨大请求。 - 编写前向兼容语法:严格语法解析器在 Nextflow 26.04 成为默认。优先小写
channel.of(...)、显式闭包参数({ v -> ... })、所有变量用def、以及emit:命名输出。用nextflow lint检查。
参考文件
需要深入时阅读相关文件——每个都自包含:
references/language.md— DSL2 语言:process、指令、channel、operator、workflow(take/emit)、module、动态资源、错误处理。references/configuration.md—nextflow.config、作用域、profiles、withName/withLabel选择器、executor(local/SLURM/cloud)、缓存/-resume内部、追踪/报告、nextflowCLI。references/containers.md— Docker、Singularity/Apptainer、Podman、Conda、Wave 容器;选择并启用引擎;常见陷阱。references/running-pipelines.md— 查找/运行 nf-core 流水线、samplesheet、参数文件、参考基因组(iGenomes)、离线运行、机构配置、Seqera Platform。references/nf-core-tools.md— 完整nf-coreCLI 参考(pipelines/modules/subworkflows)、标志与工作流。references/developing.md— 编写 nf-core 流水线与模块:模板布局、模块main.nf/meta.yml、meta map、ext.args/modules.config、子工作流、资源标签、lint 与 Harshil 对齐风格。references/testing.md— 模块/子工作流/流水线的 nf-test:测试结构、断言、快照、标签、运行测试、CI。
官方文档:Nextflow https://www.nextflow.io/docs/latest/ · nf-core https://nf-co.re/docs/ · 训练 https://training.nextflow.io/




