Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用不同参数运行同一规则两次的最佳方法_Python_Bioinformatics_Snakemake_Vcf Variant Call Format_Bcftools - Fatal编程技术网

Python 使用不同参数运行同一规则两次的最佳方法

Python 使用不同参数运行同一规则两次的最佳方法,python,bioinformatics,snakemake,vcf-variant-call-format,bcftools,Python,Bioinformatics,Snakemake,Vcf Variant Call Format,Bcftools,我正在使用bcftools共识从vcf文件中提取单倍型。 给定输入文件: A.sorted.bam B.sorted.bam 将创建以下输出文件: A.hap1.fna A.hap2.fna B.hap1.fna B.hap2.fna 我现在有两条规则来做这件事。它们的区别仅在于输出文件和shell命令中的数字1和2。代码: rule consensus1: input: vcf="variants/phased.vcf.gz", tbi="varia

我正在使用
bcftools共识
从vcf文件中提取单倍型。 给定输入文件:

A.sorted.bam
B.sorted.bam
将创建以下输出文件:

A.hap1.fna
A.hap2.fna
B.hap1.fna
B.hap2.fna
我现在有两条规则来做这件事。它们的区别仅在于输出文件和shell命令中的数字1和2。代码:

rule consensus1:
    input:
        vcf="variants/phased.vcf.gz",
        tbi="variants/phased.vcf.gz.tbi",
        bam="alignments/{sample}.sorted.bam"
    output:
        "haplotypes/{sample}.hap1.fna"
    params:
        sample="{sample}"
    shell:
        "bcftools consensus -i -s {params.sample} -H 1 -f {reference_file} {input.vcf} > {output}"

rule consensus2:
    input:
        vcf="variants/phased.vcf.gz",
        tbi="variants/phased.vcf.gz.tbi",
        bam="alignments/{sample}.sorted.bam"
    output:
        "haplotypes/{sample}.hap2.fna"
    params:
        sample="{sample}"
    shell:
        "bcftools consensus -i -s {params.sample} -H 2 -f {reference_file} {input.vcf} > {output}"

虽然这段代码可以工作,但似乎应该有一种更好、更具python风格的方法,只使用一条规则就可以做到这一点。是否可以将其折叠为一个规则,或者我当前的方法是最好的方法?

规则所有
中对单倍型1和2使用通配符。要了解有关通过
规则所有添加目标的更多信息

reference_file = "ref.txt"

rule all:
    input:
        expand("haplotypes/{sample}.hap{hap_no}.fna",
                   sample=["A", "B"], hap_no=["1", "2"])

rule consensus1:
    input:
        vcf="variants/phased.vcf.gz",
        tbi="variants/phased.vcf.gz.tbi",
        bam="alignments/{sample}.sorted.bam"
    output:
        "haplotypes/{sample}.hap{hap_no}.fna"
    params:
        sample="{sample}",
        hap_no="{hap_no}"
    shell:
        "bcftools consensus -i -s {params.sample} -H {params.hap_no} \
               -f {reference_file} {input.vcf} > {output}"

太好了,这正是我需要的。在目标规则中使用通配符是非常强大的。谢谢你的帮助!您甚至不需要将
hap\u no
重新分配给
param
。通配符可以直接从规则体访问,在本例中,通配符为
通配符.hap_no
@KirillG True。但我的习惯是将我使用的所有参数保存在一个地方,以便于阅读。