如何让Snakemake规则以local-storage-prefix作为工作目录运行?
Snakemake本地规则工作目录配置问题解决
问题描述
我使用Snakemake 8.20.3版本,已在配置中设置default-storage-provider: fs及local-storage-prefix: /wrk-vakka/users/$USER/local。但运行test_lf本地规则时,生成的touched_file.txt文件位于Snakefile所在目录,而非指定的local-storage-prefix目录。请问如何让shell规则将local-storage-prefix作为其shell脚本的工作目录?
解决方法
方法1:显式指定规则工作目录
方式A:使用workdir参数
直接在规则中通过workdir指定目标路径,还可以用{env[USER]}自动获取当前用户,替代配置里的$USER变量:
rule test_lf: localrules: test_lf workdir: "/wrk-vakka/users/{env[USER]}/local" shell: "touch touched_file.txt"
方式B:在shell脚本内手动切换目录
如果不想修改规则结构,也可以在shell命令里先切换目录再执行操作:
rule test_lf: localrules: test_lf shell: "cd /wrk-vakka/users/$USER/local && touch touched_file.txt"
方法2:引用配置文件中的路径变量
如果已经在配置文件(如config.yaml)里定义了local-storage-prefix,可以直接在规则中读取配置值,后续修改目录只需改配置文件:
configfile: "config.yaml" rule test_lf: localrules: test_lf workdir: config["local-storage-prefix"] shell: "touch touched_file.txt"
额外注意事项
local-storage-prefix默认作用是存储提供者的文件路径映射,并非直接设置shell规则的工作目录,所以必须手动指定工作目录才能让生成文件落到目标路径。- 若目标目录可能不存在,可在shell命令中添加目录创建逻辑:
"mkdir -p /wrk-vakka/users/$USER/local && cd $_ && touch touched_file.txt"
内容的提问来源于stack exchange,提问作者Sebastian Schmidt
相关产品推荐
相关产品推荐

