环境搭建:从 Python 到容器的完整依赖链
这章其实挺关键的。你想想看,不管是 Snakemake 还是 Nextflow,跑不起来十有八九是环境没整对。我个人习惯先把 Python 环境和容器工具链搭好,再装 workflow 引擎,这样后面遇到的坑会少很多。
Python 与包管理器
Python 的版本挺讲究。我建议统一用 Python 3.8 以上,别折腾 2.7 了。为啥?Snakemake 7.x 和 Nextflow 的 Python 包装器都需要 3.8+。你可以用系统自带的 Python,但我更推荐用 Miniconda 来管理,因为后面还要装一堆生物信息学工具。
# 直接装 Miniconda(Linux 环境)
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda3
eval "$($HOME/miniconda3/bin/conda shell.bash hook)"
conda init
装完后检查一下 Python 版本:
python --version # 应该显示 3.8 以上
Conda 环境隔离
Conda 的好处就是每条工作流都能有自己的小天地。我一般这么干:
# 创建一个干净的 snakemake 环境
conda create -n snakemake_env python=3.9
conda activate snakemake_env
conda install -c bioconda -c conda-forge snakemake
# 创建一个 nextflow 环境(其实 Nextflow 本身不需要 Python,但它的包装库需要)
conda create -n nextflow_env python=3.10
conda activate nextflow_env
pip install nf-core
# Nextflow 推荐直接装到系统 PATH 里
curl -s https://get.nextflow.io | bash
chmod +x nextflow
sudo mv nextflow /usr/local/bin/
记住,Conda 环境的迁移很方便。我把常用环境的 yaml 文件都存着,换机器时直接 conda env create -f env.yaml 就完事。
Docker 容器化
Docker 是 Nextflow 的主力容器引擎。Snakemake 也能用,但兼容性稍微差一点。我在项目中遇到过因为 Docker 版本太老导致容器层冲突,后来统一升级到了 20.10+。
# 安装 Docker(Ubuntu 为例)
sudo apt update
sudo apt install docker.io
sudo systemctl enable --now docker
# 把用户加到 docker 组(免 sudo)
sudo usermod -aG docker $USER
newgrp docker
docker run hello-world 能正常输出就算对了。Nextflow 默认用 Docker 拉取镜像。你可以在 nextflow.config 里指定:
docker.enabled = true
docker.registry = 'my-registry.company.com'
Singularity 在 HPC 中的部署
HPC 集群一般不让用 Docker(安全原因),但 Singularity(现在叫 Apptainer)是标准方案。Singularity 兼容 Docker 镜像,直接把 .sif 文件丢到集群共享存储就行。
# 安装 SingularityCE(社区版)
VERSION=4.0.1
wget https://github.com/sylabs/singularity/releases/download/v${VERSION}/singularity-ce_${VERSION}-$(lsb_release -sc)_amd64.deb
sudo dpkg -i singularity-ce_${VERSION}-*.deb
# 或者用源码编译
git clone --recursive https://github.com/sylabs/singularity.git
cd singularity
./mconfig
make -C builddir
sudo make -C builddir install
我踩过的一个坑:Singularity 默认 cache 目录在 $HOME/.singularity,如果 home 空间小,跑大型流程时磁盘直接爆满。后来我改了系统环境变量:
export SINGULARITY_CACHEDIR=/scratch/$USER/singularity_cache
export SINGULARITY_TMPDIR=/scratch/$USER/singularity_tmp
Snakemake 和 Nextflow 都支持 Singularity。在 Snakemake 的 Snakefile 里:
singularity: "docker://biocontainers/salmon:v1.4.0"
在 Nextflow 里直接指定 -with-singularity 或用 profile:
nextflow run main.nf -profile cluster -with-singularity /path/to/container.sif
环境依赖全貌
下面这张图我把整个依赖链画出来了,从底层操作系统到上层工作流引擎。每种颜色代表一层,箭头表示「依赖」。你可以对照着检查自己的环境缺了什么。
拿我自己的经验来说,一开始只装了个 Python 和 pip,后面跑 Nextflow 容器化流程时就傻了 —— 没装 Docker。又回头补课。所以建议你按这张图一层层夯实,别跳级。
快速验证清单
你可以用下面这张表自检。确认每一项都打了勾,再往下走。
| 组件 | 验证命令 | 预期输出 | 常见问题 |
|---|---|---|---|
| Python | python --version |
3.8 或更高 | 系统 Python 版本过旧 |
| Conda | conda --version |
显示版本号 | conda 命令未找到 / 未初始化 |
| Docker | docker run hello-world |
Hello from Docker! | 权限不够(用户不在 docker 组) |
| Singularity | singularity --version |
4.x 或 3.x | 安装在 HPC 登录节点时需要管理员 |
brew 或 go install 都行,具体可以看官方文档的「user install」一节。我试过,没问题。嗯,环境搭好了,下节就能装 nextflow 和 snakemake 跑第一个 hello 流程了。这里我就不多说了。