使用Snakemake时通过--singularity-args绑定目录失败求助
Singularity容器运行Snakemake的路径挂载问题
问题背景
原始数据存放在工作目录外,容器内无法访问,数据路径:/data/projects/pipe_test/Fastqs/
简化Snakefile代码
singularity: "/home/AD/username/scripts/test.sif" rule fastqc: shell: """ ls Fastqs > fastqs.txt 2>&1"""
执行命令(在Snakefile所在目录运行)
snakemake -c1 --use-singularity --singularity-args '--bind /data/projects/pipe_test/Fastqs/:$PWD/Fastqs/'
报错信息
Error in rule fastqc: jobid: 0 shell: ls Fastqs > fastqs.txt 2>&1 (one of the commands exited with non-zero exit code; note that snakemake uses bash strict mode!)
已尝试的操作
- 省略
--singularity-args冒号后的部分:目录能挂载,但容器内路径是源路径而非Fastqs - 使用
--bind /data/projects/pipe_test/Fastqs/:/Fastqs/:目录被挂载到容器根目录 - 直接运行
singularity shell --bind /data/projects/pipe_test/Fastqs/:$PWD/Fastqs test.sif:可正确挂载到指定路径并访问数据
期望效果
运行Snakemake时,原始数据能出现在容器的./Fastqs路径中,请问哪里操作错了?
问题原因与解决办法
问题核心是$PWD的解析时机不对:你在命令行里写的$PWD是被宿主机的Shell解析的,但Snakemake启动容器时,容器内的工作目录可能和你预期的不一致——Snakemake默认会挂载宿主机工作目录,但容器内的实际工作路径可能不是宿主机的$PWD,或者命令行里的$PWD没有正确传递到容器挂载规则中。
解决办法1:用绝对路径指定容器内挂载目标
先在终端执行echo $PWD得到宿主机当前工作目录的绝对路径(比如/home/AD/username/pipe_run),然后修改命令:
snakemake -c1 --use-singularity --singularity-args '--bind /data/projects/pipe_test/Fastqs/:/home/AD/username/pipe_run/Fastqs/'
解决办法2:通过环境变量传递挂载规则
绕开Shell解析变量的问题,用SINGULARITY_BIND环境变量挂载:
SINGULARITY_BIND="/data/projects/pipe_test/Fastqs/:$PWD/Fastqs/" snakemake -c1 --use-singularity
解决办法3:在Snakefile里直接指定挂载参数
把挂载规则写进Snakefile的singularity指令里,避免命令行变量解析的坑:
singularity: "/home/AD/username/scripts/test.sif, --bind /data/projects/pipe_test/Fastqs/:./Fastqs/" rule fastqc: shell: """ ls Fastqs > fastqs.txt 2>&1"""
之后直接运行:
snakemake -c1 --use-singularity
补充说明
为什么直接用singularity shell能成功?因为此时$PWD是宿主机当前目录,且singularity shell默认把这个路径作为容器内的工作目录;但Snakemake启动容器时,可能会把容器内的工作目录切换到任务临时目录或其他路径,导致你用$PWD指定的挂载目标在容器内不存在。
内容的提问来源于stack exchange,提问作者tretol
相关产品推荐
相关产品推荐

