如何用 nextflow 技能把 Nextflow 流水线配置并扩展到 HPC/SLURM 集群运行
【免费下载链接】scientific-agent-skillsTurn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 190,000+ scientists worldwide. 165 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.项目地址: https://gitcode.com/GitHub_Trending/cl/scientific-agent-skills
如果你的任务是让一条现成的 Nextflow 或 nf-core 流水线(例如nf-core/rnaseq)从本地笔记本搬到 HPC/SLURM 集群上跑,scientific-agent-skills仓库中的 nextflow 技能 给出了完整的配置与扩展路径:同一份流水线代码不改一行,只通过nextflow.config、profile 和 executor 配置切换运行环境。该技能的核心参考文档包括 configuration.md、running-pipelines.md 和 containers.md。
开始前的前提条件(均来自技能文档):
- Bash 和 Java 17 或更高版本(17–25 均受支持);
- 一个可用的 SLURM 集群账号,以及共享文件系统(HPC 场景下容器引擎通常选 Singularity/Apptainer);
- 每次运行只启用一个容器引擎,
docker/singularity/conda等基础设施 profile 互斥。
确认环境:Java 17、Nextflow 引擎与 nf-core 工具
先用java -version确认 Java 版本。Nextflow 提供自包含启动器,无需构建环境:
# Install Nextflow (self-contained launcher) curl -s https://get.nextflow.io | bash # creates ./nextflow sudo mv nextflow /usr/local/bin/ # put on PATH nextflow info # verify其中sudo mv一步会把刚下载的可执行文件移动到系统 PATH,需要管理员权限;如果你的集群登录节点不允许安装,也可以走 conda:
conda create -n nf -c bioconda -c conda-forge nextflow nf-core创建/校验 nf-core 资产时还需要 nf-core 工具(Python 版):
uv pip install nf-core # or: conda install -c bioconda nf-core nf-core --version为了可复现性,技能文档建议固定引擎版本:export NXF_VER=24.10.0(只有确有需要才使用 edge 版本)。
冒烟测试:用 test profile 验证集群环境
正式跑数据前,先用流水线自带的testprofile 冒烟测试——它使用极小的内置数据集,用来证明你的环境(引擎版本、容器运行时、调度器连通性)没有问题:
nf-core pipelines list rna # 关键词搜索流水线 nf-core pipelines info rnaseq # 查看单条流水线的参数、samplesheet、输出# 集群上用 singularity 引擎做冒烟测试 nextflow run nf-core/rnaseq -r 3.14.0 -profile test,singularity --outdir test_results几个文档中明确的约束:
-profile用逗号组合多个 profile,顺序有意义(后定义的覆盖前面的);容器引擎 profile 三选一:docker(本地/CI)、singularity(HPC)、conda(最后手段)。nextflow run nf-core/rnaseq会自动从远端拉取流水线到~/.nextflow/assets;也可以先用nextflow pull nf-core/rnaseq预取,再用-r固定 tag。- 冒烟测试失败时,先看登录节点上
singularity/apptainer是否可用、能否访问共享盘,这决定了后面NXF_SINGULARITY_CACHEDIR怎么设。
配置 SLURM executor
executor 负责把任务映射到计算资源。默认是local(当前机子进程),切到集群只需在配置中改 executor。configuration.md 给出的 SLURM 示例:
process { executor = 'slurm' queue = 'compute' // SLURM partition clusterOptions = '--account=lab123' } executor { queueSize = 200 // max jobs queued at once submitRateLimit = '10/1min' // throttle submissions perCpuMemAllocation = true // emit --mem-per-cpu instead of --mem (some clusters require this) }queue对应 SLURM 分区(partition),clusterOptions把额外参数(如计费账号)原样传给 sbatch;queueSize限制同时排队任务数,submitRateLimit控制提交速率,避免打爆调度器;perCpuMemAllocation = true让 Nextflow 输出--mem-per-cpu而不是--mem,文档注明有些集群要求前者。
配置文件的加载顺序(优先级从低到高):$NXF_HOME/config(即~/.nextflow/config)→ 项目目录的nextflow.config(脚本所在目录)→ 启动目录(当前工作目录)的nextflow.config→ 每个-c custom.config(可重复指定)。CLI 的--param/-params-file覆盖配置中的 params。因此站点专属配置建议单独写成文件,用-c site.config叠加,而不是去改流水线自带的配置。
也可以把 executor 设置收进一个具名 profile,便于命令行切换:
profiles { slurm { process.executor = 'slurm' process.queue = 'compute' } }可选分支:机构配置。nf-core/configs 仓库为很多 HPC 系统提供了现成 profile(executor、队列、容器缓存、资源上限都配好)。如果你的机构在名单里,直接-profile crick,singularity这类方式使用即可,Nextflow 会从中枢仓库自动拉取;离线环境可用--custom_config_base指向本地/私有配置仓库。
配置 Singularity/Apptainer 容器引擎
HPC 上最常见的引擎是 Singularity/Apptainer(无 root、共享文件系统)。containers.md 给出配置:
singularity { enabled = true autoMounts = true // auto-bind host paths cacheDir = '/shared/singularity' // or set NXF_SINGULARITY_CACHEDIR }- Nextflow 首次使用时会自动把 Docker 镜像转成 SIF 并缓存;在集群上务必设置共享的
cacheDir或环境变量NXF_SINGULARITY_CACHEDIR,让所有作业复用已拉取的镜像,否则会反复转换/拉取。 - 如果
autoMounts没绑到你要的路径,用runOptions = '-B /scratch'显式绑定。 - Apptainer(Singularity 更名后)在
apptainerscope 下使用同样的选项。 - 一次只启用一个容器引擎,同时启用两个会导致报错或行为异常。
按 process 调整资源:withLabel 与 withName
集群上任务被杀的最常见原因是资源申请不当。Nextflow 用withName:/withLabel:选择器针对特定 process 设置资源,这正是 nf-core 为各工具设定资源的机制(nf-core 把所有withName块集中在conf/modules.config):
process { // by resource label withLabel: 'process_low' { cpus = 2; memory = 6.GB; time = 4.h } withLabel: 'process_medium' { cpus = 6; memory = 36.GB; time = 8.h } withLabel: 'process_high' { cpus = 12; memory = 72.GB; time = 16.h } // by process name (regex / fully-qualified WORKFLOW:SUB:PROCESS) withName: 'FASTQC' { cpus = 4 } withName: '.*:ALIGN' { container = 'quay.io/biocontainers/bwa:0.7.17--hed695b0_7' ext.args = '-M' // injected into the script as task.ext.args publishDir = [ path: { "${params.outdir}/bam" }, mode: 'copy' ] } }优先级是withName>withLabel> 通用process设置。技能文档的最佳实践也建议:用process_low/medium/high标签给资源“分级”,配合errorStrategy 'retry'和按task.attempt递增的动态资源,而不是一次性申请超大资源。
提交真实运行
环境验证通过后,正式运行的标准模式是:固定版本、选一个容器引擎、传 samplesheet、指定输出目录、加-resume:
nextflow run nf-core/rnaseq \ -r 3.14.0 \ # pin release for reproducibility -profile slurm,singularity \ --input samplesheet.csv \ # the samples to process --outdir results \ # where results go (required by nf-core) -resume # reuse cache on reruns- nf-core 流水线的输入是CSV samplesheet(
--input),不是散装文件;列因流水线而异,RNA-seq 的典型格式:
sample,fastq_1,fastq_2,strandedness CONTROL_REP1,s3://.../ctrl_1.fastq.gz,s3://.../ctrl_2.fastq.gz,auto TREAT_REP1,/data/treat_1.fastq.gz,/data/treat_2.fastq.gz,auto单端数据把fastq_2留空即可;路径可以是本地或 S3/GCS/https,Nextflow 会自动 stage。参数校验(nf-schema/nf-validation插件)会在列或值错误时快速失败并给出明确报错。
- 参数传递的优先级:config 文件 →
-params-file→ 命令行--参数(后者覆盖前者)。非简单场景建议用 params file 保证可复现、可审查:
nf-core pipelines create-params-file nf-core/rnaseq # generate documented YAML nextflow run nf-core/rnaseq -profile slurm,singularity -params-file params.yml --outdir results也可以交互式构建命令:nf-core pipelines launch nf-core/rnaseq会按nextflow_schema.json逐项校验并写出可复用的nf-params.json。
参考基因组:很多流水线接受--genome <KEY>(如GRCh38)并从 AWS iGenomes 自动拉取参考。文档提醒 AWS iGenomes 的注释明显过时(人 GTF 约 Ensembl release 75 / 2015),且其 GRCh38 来自 NCBI 而非软掩码 Ensembl 组装;需要新参考时自行提供--fasta/--gtf,加--save_reference保留自建索引。
可选分支:离线 / 隔离集群
如果计算节点不能访问外网,running-pipelines.md 给出离线路径——在联网机器上打包流水线和 SIF 镜像,再转移到集群:
# On a connected machine: bundle pipeline + configs + containers nf-core pipelines download nf-core/rnaseq \ --revision 3.14.0 \ --container-system singularity \ # pre-convert images to SIF --compress none \ --outdir nf-core-rnaseq # Transfer the folder, then on the offline machine: export NXF_OFFLINE=true export NXF_SINGULARITY_CACHEDIR=/shared/sif nextflow run nf-core-rnaseq/3_14_0 -profile singularity --input <你的samplesheet.csv> --outdir results其中--input后的占位值需替换为你自己的 samplesheet 路径。离线运行还要预先本地准备好参考基因组并设置相应--*_index/igenomes_base参数,固定所有插件版本。
验证配置、监控运行与排查
配置是否正确生效,文档给出两个不实际跑任务的检查命令:
nextflow config -profile slurm,singularity # print the resolved configuration nextflow inspect nf-core/rnaseq # resolve per-process containers without running运行中:每次运行会打印实时任务表格;完整日志在启动目录的.nextflow.log。定位失败任务时,在报错信息中找到该任务的 work 目录,检查里面的.command.sh、.command.out、.command.err、.exitcode。
资源画像:给运行加上报告标记,事后根据实际用量调整请求值:
nextflow run nf-core/rnaseq -profile slurm,singularity \ -with-report -with-trace -with-timeline -with-dag flow.html-with-trace生成逐任务的 TSV(cpu、mem、time、status),-with-report生成按任务的 HTML 资源报告,文档明确说用途是“profile resource usage and right-size requests”。
文档列出的常见失败与对应处理:
- 内存不足(exit 137)→ 用
withName/withLabel或自定义配置提高内存; - 输入列缺失 → 修 samplesheet;
- 容器拉取失败 → 检查引擎/profile 和缓存目录;
- Java/Nextflow 版本不对 → 设置
NXF_VER并检查nextflow info。
缓存未命中的调试(任务本应命中却重跑):
nextflow log <run> -f hash,name,status,workdir文档列出的常见原因:输入文件时间戳/内容变化、脚本被编辑、容器 tag 不同、输入顺序不确定、闭包变量未声明(用def)、或显式cache false。缓存按 work 目录隔离:删除work/或改动-w/workDir都会失去缓存。修复问题后用-resume重跑,避免重算已成功任务。
清理注意:nextflow clean -f -before <run_name>会删除指定运行之前的work/数据,执行前确认要清的运行;集群共享盘配额紧张时才需要用。
限制与下一步
- 一次运行只能启用一个容器引擎;组合 profile 时避免多个 profile 对同一选项设置冲突值(legacy 解析器按配置定义顺序应用 profile,26.04 起的 strict 解析器按 CLI 顺序应用)。
- 配置里
queue指的是 SLURM 分区,不要和executor.queueSize(并发任务数上限)混淆。 - 需要 Web 仪表盘监控时,文档指出的下一步是 Seqera Platform:配置
tower.enabled = true与TOWER_ACCESS_TOKEN,或运行时加-with-tower。
【免费下载链接】scientific-agent-skillsTurn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 190,000+ scientists worldwide. 165 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.项目地址: https://gitcode.com/GitHub_Trending/cl/scientific-agent-skills
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考