使用 os.system 和 snakemake.shell 的区别

Difference between using os.system and snakemake.shell

我对 snakemake 还很陌生,所以我仍然难以组合 shell 命令和 python 代码。

我的解决方案是制作脚本文件,然后在该脚本中执行 shell 命令。

调用 snakemake.shellos.system 执行命令行之间是否存在机械差异?

示例:

sample = ["SRR12845350"]
rule prefetch:
  input:
    "results/Metadata/{sample}.json"
  output:
    "results/SRA/{sample}.sra
  params:
    "prefetch %s -o %s"
  script:
    "scripts/prefetch.py"

prefetch.py是:

from json import load
from snakemake import shell
from os import system

json_file = snakemake.input[0]
prefetch  = snakemake.params[0]
sra_file  = snakemake.output[0]

json    = load(open(json_file))
sra_run = json["RUN_accession"]

shell(prefetch %(sra_run, sra_file))  # option 1
system(prefetch %(sra_run, sra_file))  # option 2

shell 只是一个辅助函数,可以更轻松地从 snakemake 调用 command-line 参数。学习 snakemake 可能会让人不知所措,而学习 Python 的 os.systemsubprocess 的复杂性则不必要地复杂化。 snakemake shell 命令进行了一些健全性检查,设置了一些环境变量,例如该命令可以使用的线程数和其他一些“小”东西,但是 under the hood 只是在您的命令上调用 subprocess.Popen。这两个选项都应该有效,但由于您正在编写 snakemake 包装器,因此使用 shell 可能稍微好一些,因为它设计用于 snakemake。