我正在创建一个简单的 snakemake 管道,其中包含 Snakefile 中的全局变量。在我的规则中调用的 Python 脚本中使用这些全局变量的推荐方法是什么?
我目前正在使用此处描述的 argparse 命令行参数( Snakemake: pass command line arguments to scripts ),但我想知道是否有更好的方法。
最佳答案
传递变量
如果在 Snakefile 中指定了变量,那么它可以通过 params
传递。 .例如,
蛇皮
# global variable to use
FOO = 100
rule test:
input: "a.in"
output: "a.out"
params:
foo=FOO # pass the variable value as 'foo'
script: "scripts/test.py"
脚本/test.py #!/usr/bin/env python
# access the variable through the `snakemake` object
print(snakemake.params.foo)
见 Snakemake documentation on external Python scripts .补充评论
请注意,通常我发现在
config.yaml
中放置一个像上面例子一样的变量是更好的做法。反而。这有助于集中可调参数,提供单点配置以供重用。尽管可用 snakemake.config
在外部脚本中,我仍然更喜欢将配置值显式传递为 params
,以便明确哪些规则取决于哪些配置值。例子
config.yaml
foo: 100
蛇皮configfile: "config.yaml"
rule test:
input: "a.in"
output: "a.out"
params:
foo=config["foo"]
script: "scripts/test.py"
脚本/test.py #!/usr/bin/env python
# access the variable through the `snakemake` object
print(snakemake.params.foo)
覆盖配置参数如果在
config.yaml
中提供了该值,然后还可以(可选)在 CLI 中覆盖它:snakemake --config foo=150
见 documentation on configuration parameters .
关于python - Snakemake 规则中的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68763547/