【问题标题】:Best way to run same rule twice with different params使用不同参数两次运行相同规则的最佳方法
【发布时间】:2018-04-02 03:02:51
【问题描述】:

我正在使用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}"

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

【问题讨论】:

    标签: python bioinformatics snakemake vcf-variant-call-format bcftools


    【解决方案1】:

    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}"
    

    【讨论】:

    • 太好了,这正是我所需要的。在目标规则中使用通配符非常强大。感谢您的帮助!
    • 您甚至不需要将hap_no 重新分配给param。通配符可直接从规则正文访问,在本例中为 wildcards.hap_no
    • @KirillG 是的。但我的一个习惯是把我使用的所有参数放在一个地方以便于阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多