python - Snakemake 规则中的全局变量

标签 python snakemake

我正在创建一个简单的 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/

相关文章:

python - 将具有多个函数的 python 脚本转换为 SnakeMake 工作流程

python - snakemake 集群脚本 ImportError snakemake.utils

python - 如何按字母顺序完全排序 JSON 树

python - 我定义了一个 Complex 类。我该怎么做这样 "9+Complex"

python - GridSearchCV 在管道中将 fit_params 传递给 XGBRegressor 产生 "ValueError: need more than 1 value to unpack"

python - 如何将函数拟合到 3D numpy 数组?

python - 如何在 snakemake 的扩展函数参数中使用通配符?

Snakemake - 删除工作流程生成的所有非输出文件

snakemake - 尝试为工作流 [Snakemake] 创建目录时出现 ChildIOException

python - scipy stats 几何平均返回 NaN