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

时间:2018-04-02 03:02:51

标签: python bioinformatics snakemake bcftools

我使用bcftools consensus从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}"

虽然这段代码有效,但似乎应该有一种更好,更pythonic的方式来只使用一条规则来做到这一点。是否可以将其合并为一个规则,或者我当前的方法是最好的方法?

1 个答案:

答案 0 :(得分:2)

rule all中对单倍型1和2使用通配符。 See here了解有关通过rule all

添加目标的详情
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}"