我正在使用一些带有snakemake的python脚本来自动化工作流程。这些脚本接受命令行参数,虽然我可以用snakemake.input[0]、snakemake.output[0]等替换它们,但我不愿意这样做,因为我也希望能够在 Snakemake 之外使用它们。
解决这个问题的一种自然方法(我一直在做的)是将它们作为 ashell而不是script. 然而,当我这样做时,依赖图就被破坏了;我更新了脚本,DAG 认为不需要重新运行任何内容。
有没有办法将命令行参数传递给脚本但仍将它们作为脚本运行?
编辑:一个例子
我的Python脚本
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-o", type=str)
args = parser.parse_args()
with open(args.o, "w") as file:
file.write("My file output")
Run Code Online (Sandbox Code Playgroud)
我的蛇形档案
rule some_rule:
output: "some_file_name.txt"
shell: "python my_script.py -o {output}"
Run Code Online (Sandbox Code Playgroud)
根据@troy-comi 的评论,我一直在做以下事情——虽然有点黑客行为——但正是我想要的。我将脚本定义为 Snakemake 规则的输入,这实际上也有助于提高可读性。典型的规则(这不是完整的 MWE)可能看起来像
rule some_rule:
input:
files=expand("path_to_files/f", f=config["my_files"]),
script="scripts/do_something.py"
output: "path/to/my/output.txt"
shell: "python {input.script} -i {input.files} -o {output}"
Run Code Online (Sandbox Code Playgroud)
当我修改脚本时,它会触发重新运行;它是可读的;并且它不需要我插入snakemake.output[0]我的 python 脚本(使得它们很难在此工作流程之外回收)。