我正在尝试编写一个规则,它从不同的目录中获取两个文件,并将规则的输出放在与下面文件结构相同的目录中:
DIR_A
dir1
file1.clean.vcf
dir2
file2.clean.vcf
dir3
file1.output.vcf
file2.output.vcf
到目前为止,我已尝试使用glob_wildcards:
(DIR,NAME) = glob_wildcards("DIR_A/{dir}/{name}.clean.vcf")
input: expand("DIR_A/{dir}/{name}.clean.vcf", dir=DIR, name=NAME)
output: "DIR_A/dir3/{name}.output.vcf
但它会引发错误:
MissingInputException in line 80 of DIR_A:
Missing input files for rule convert_output:
DIR_A/dir1/file2.clean.vcf
DIR_A/dir2/file1.clean.vcf
将zip添加到输入:
input: expand("DIR_A/{dir}/{name}.clean.vcf", zip, dir=DIR, name=NAME)
如果$ snakemake -s snakefile -n(干运行):
rule conv_output:
input: DIR_A/dir1/file1.clean.vcf, DIR_A/file2/file2.clean.vcf
这可以被snakemake接受并防止出现上述错误,但现在file1.clean.vcf和file2.clean.vcf都是规则的输入,但{name}通配符使规则每个文件运行一次。这最终成为一个文件到一个文件,而不是我正在寻找的一对一文件。
有没有办法设置它,所以我可以得到规则conv_output的输出作用于每个文件然后将输出放在dir3?任何帮助将不胜感激!!
答案 0 :(得分:2)
使用python,将输入vcf sample / filename配对到其路径,然后使用它在snakemake规则中指定输入路径。下面的示例适用于问题中给出的目录结构。
from pathlib import Path
def pair_name_to_infiles():
# get all *.clean.vcf files recursively under DIR_A
vcf_path = Path('DIR_A').glob('**/*.clean.vcf')
# pair vcf name to infile path using a dictionary
vcf_infiles_dict = {}
for f in vcf_path:
vcf_name = f.name.replace('.clean.vcf', '')
vcf_infiles_dict[vcf_name] = str(f)
return vcf_infiles_dict
# using function written in python code, map vcf name to their infile path
vcf_infiles_dict = pair_name_to_infiles()
rule all:
input:
expand('DIR_A/dir3/{vcf_name}.output.vcf', vcf_name=vcf_infiles_dict.keys())
rule foo:
input:
lambda wildcards: vcf_infiles_dict[wildcards.vcf_name]
output:
'DIR_A/dir3/{vcf_name}.output.vcf'
shell:
'touch {output}'