在 Snakemake 中重用规则
Reuse of a rule in Snakemake
有没有办法在 snakemake 中重用规则,仅更改 params
?
例如:
rule job1:
...
params:
reference = "path/to/ref1"
...
rule job2:
input: rules.job1.output
...
params:
reference = "path/to/ref2"
job1
和 job2
规则做同样的事情,但我需要连续调用它们并且必须修改 reference
参数。它为非常相似的任务生成大量代码。
我试着为这一步做一个子工作流程,主要的Snakefile更具可读性。但是子流程代码还是重复了。
有什么想法或建议吗?我错过了什么吗?
编辑
更具体地说,job2必须在job1之后执行,使用后者的输出。
如果规则相同,您可以在输出文件的命名中使用通配符。这样,相同的规则将被执行两次:
references = ["ref1", "ref2"]
rule all:
input: expand("my_output_{ref}", ref=references)
rule job:
input: "my_input"
output: "my_output_{ref}"
params: ref = "path/to/{ref}"
shell: "... {params.ref} {output}"
希望这对您有所帮助,如果没有,您能否让您的问题更具体一些?
编辑
好的,可以使用 python 函数为规则定义自定义输入。也许你可以朝这个方向发展。请参阅此工作示例:
references = ["ref1", "ref2"]
rule all:
input: expand("my_output_{ref}", ref=references)
def rule_input(wildcards):
if (wildcards.ref == "ref1"):
input = "my_first_input"
elif (wildcards.ref == "ref2"):
input = "my_output_ref1"
return(input)
rule job:
input: rule_input
output: "my_output_{ref}"
params: ref = "path/to/{ref}"
shell: "echo input: {input} ; echo output: {output} ; touch {output}"
Snakemake 不支持规则之间的继承。因此,如果您不能为您的情况使用通配符,一种选择是使用委托。您将两个规则的常量部分预定义为变量,然后从规则体中引用这些。
另一种选择可能是 input/params 函数(参见文档和教程)和只有一个规则定义。这样,您可以拥有完全不同的输入文件,遵循一些基于例如值的逻辑。一个通配符。
有没有办法在 snakemake 中重用规则,仅更改 params
?
例如:
rule job1:
...
params:
reference = "path/to/ref1"
...
rule job2:
input: rules.job1.output
...
params:
reference = "path/to/ref2"
job1
和 job2
规则做同样的事情,但我需要连续调用它们并且必须修改 reference
参数。它为非常相似的任务生成大量代码。
我试着为这一步做一个子工作流程,主要的Snakefile更具可读性。但是子流程代码还是重复了。
有什么想法或建议吗?我错过了什么吗?
编辑
更具体地说,job2必须在job1之后执行,使用后者的输出。
如果规则相同,您可以在输出文件的命名中使用通配符。这样,相同的规则将被执行两次:
references = ["ref1", "ref2"]
rule all:
input: expand("my_output_{ref}", ref=references)
rule job:
input: "my_input"
output: "my_output_{ref}"
params: ref = "path/to/{ref}"
shell: "... {params.ref} {output}"
希望这对您有所帮助,如果没有,您能否让您的问题更具体一些?
编辑
好的,可以使用 python 函数为规则定义自定义输入。也许你可以朝这个方向发展。请参阅此工作示例:
references = ["ref1", "ref2"]
rule all:
input: expand("my_output_{ref}", ref=references)
def rule_input(wildcards):
if (wildcards.ref == "ref1"):
input = "my_first_input"
elif (wildcards.ref == "ref2"):
input = "my_output_ref1"
return(input)
rule job:
input: rule_input
output: "my_output_{ref}"
params: ref = "path/to/{ref}"
shell: "echo input: {input} ; echo output: {output} ; touch {output}"
Snakemake 不支持规则之间的继承。因此,如果您不能为您的情况使用通配符,一种选择是使用委托。您将两个规则的常量部分预定义为变量,然后从规则体中引用这些。 另一种选择可能是 input/params 函数(参见文档和教程)和只有一个规则定义。这样,您可以拥有完全不同的输入文件,遵循一些基于例如值的逻辑。一个通配符。