并行执行具有相同输入和单个参数值范围的 snakemake 规则
Parallel execution of a snakemake rule with same input and a range of values for a single parameter
我正在将 bash 脚本转换为 snakemake,我想并行化我之前使用 for 循环处理的步骤。我 运行ning 遇到的问题是,snakemake 不是 运行ning 并行进程,而是最终尝试 运行 一个包含所有参数的进程,但失败了。
我的原始 bash 脚本 运行 是一个程序多次用于参数 K
的值范围。
for num in {1..3}
do
structure.py -K $num --input=fileprefix --output=fileprefix
done
有多个以 fileprefix
开头的输入文件。每个 运行 有两个主要输出,例如对于 K=1,它们是 fileprefix.1.meanP
、fileprefix.1.meanQ
。我的config和snakemake文件如下
配置:
cat config.yaml
infile: fileprefix
K:
- 1
- 2
- 3
蛇妖:
configfile: 'config.yaml'
rule all:
input:
expand("output/{sample}.{K}.{ext}",
sample = config['infile'],
K = config['K'],
ext = ['meanQ', 'meanP'])
rule structure:
output:
"output/{sample}.{K}.meanQ",
"output/{sample}.{K}.meanP"
params:
prefix = config['infile'],
K = config['K']
threads: 3
shell:
"""
structure.py -K {params.K} \
--input=output/{params.prefix} \
--output=output/{params.prefix}
"""
这是用 snakemake --cores 3
执行的。当我只使用一个线程时,问题仍然存在。
我希望 K 的每个值都能得到上述输出,但 运行 失败并出现此错误:
RuleException:
CalledProcessError in line 84 of Snakefile:
Command ' set -euo pipefail; structure.py -K 1 2 3 --input=output/fileprefix \
--output=output/fileprefix ' returned non-zero exit status 2.
File "Snakefile", line 84, in __rule_Structure
File "snake/lib/python3.6/concurrent/futures/thread.py", line 56, in run
当我将 K 设置为单个值时,例如 K = ['1']
,一切正常。所以问题似乎是 {params.K}
在执行 shell 命令时被扩展到 K 的所有值。我今天开始自学 snakemake,效果非常好,但我遇到了困难。
您需要从通配符中检索 -K
的参数,而不是从配置文件中。配置文件将简单地 return 您的可能值列表,它是一个普通的 python 字典。
configfile: 'config.yaml'
rule all:
input:
expand("output/{sample}.{K}.{ext}",
sample = config['infile'],
K = config['K'],
ext = ['meanQ', 'meanP'])
rule structure:
output:
"output/{sample}.{K}.meanQ",
"output/{sample}.{K}.meanP"
params:
prefix = config['invcf'],
K = config['K']
threads: 3
shell:
"structure.py -K {wildcards.K} "
"--input=output/{params.prefix} "
"--output=output/{params.prefix}"
请注意,这里还有更多需要改进的地方。例如,规则 structure
没有定义任何输入文件,尽管它使用了一个。
我正在将 bash 脚本转换为 snakemake,我想并行化我之前使用 for 循环处理的步骤。我 运行ning 遇到的问题是,snakemake 不是 运行ning 并行进程,而是最终尝试 运行 一个包含所有参数的进程,但失败了。
我的原始 bash 脚本 运行 是一个程序多次用于参数 K
的值范围。
for num in {1..3}
do
structure.py -K $num --input=fileprefix --output=fileprefix
done
有多个以 fileprefix
开头的输入文件。每个 运行 有两个主要输出,例如对于 K=1,它们是 fileprefix.1.meanP
、fileprefix.1.meanQ
。我的config和snakemake文件如下
配置:
cat config.yaml
infile: fileprefix
K:
- 1
- 2
- 3
蛇妖:
configfile: 'config.yaml'
rule all:
input:
expand("output/{sample}.{K}.{ext}",
sample = config['infile'],
K = config['K'],
ext = ['meanQ', 'meanP'])
rule structure:
output:
"output/{sample}.{K}.meanQ",
"output/{sample}.{K}.meanP"
params:
prefix = config['infile'],
K = config['K']
threads: 3
shell:
"""
structure.py -K {params.K} \
--input=output/{params.prefix} \
--output=output/{params.prefix}
"""
这是用 snakemake --cores 3
执行的。当我只使用一个线程时,问题仍然存在。
我希望 K 的每个值都能得到上述输出,但 运行 失败并出现此错误:
RuleException:
CalledProcessError in line 84 of Snakefile:
Command ' set -euo pipefail; structure.py -K 1 2 3 --input=output/fileprefix \
--output=output/fileprefix ' returned non-zero exit status 2.
File "Snakefile", line 84, in __rule_Structure
File "snake/lib/python3.6/concurrent/futures/thread.py", line 56, in run
当我将 K 设置为单个值时,例如 K = ['1']
,一切正常。所以问题似乎是 {params.K}
在执行 shell 命令时被扩展到 K 的所有值。我今天开始自学 snakemake,效果非常好,但我遇到了困难。
您需要从通配符中检索 -K
的参数,而不是从配置文件中。配置文件将简单地 return 您的可能值列表,它是一个普通的 python 字典。
configfile: 'config.yaml'
rule all:
input:
expand("output/{sample}.{K}.{ext}",
sample = config['infile'],
K = config['K'],
ext = ['meanQ', 'meanP'])
rule structure:
output:
"output/{sample}.{K}.meanQ",
"output/{sample}.{K}.meanP"
params:
prefix = config['invcf'],
K = config['K']
threads: 3
shell:
"structure.py -K {wildcards.K} "
"--input=output/{params.prefix} "
"--output=output/{params.prefix}"
请注意,这里还有更多需要改进的地方。例如,规则 structure
没有定义任何输入文件,尽管它使用了一个。