Snakemake中输入函数的并行输出

时间:2018-12-19 14:21:33

标签: python function lambda wildcard snakemake

你好Snakemake社区,

在Snakemake中正确定义一个功能并在 params 部分中调用它时,我遇到了很多麻烦。该函数的输出是一个列表,我的目标是将列表的每一项用作shell命令的参数。换句话说,我想使用不同的参数并行运行同一外壳命令的多个作业

这是功能:

import os, glob
def get_scontigs_names(wildcards):
   scontigs = glob.glob(os.path.join("reference", "Supercontig*"))
   files = [os.path.basename(s) for s in scontigs]
   return name

输出是一个看起来像这样的列表:

['Supercontig0', 'Supercontig100', 'Supercontig2', ...]

Snakemake规则是:

rule all:
    input:
        "updated/all_supercontigs.sorted.vcf.gz"
rule update_vcf:
    input:
        len="genome/genome_contigs_len_cumsum.txt",
        vcf="filtered/all.vcf.gz"
    output:
        cat="updated/all_supercontigs.updated.list"
    params:
        scaf=get_scontigs_names
    shell:
        """
        python 3.7 scripts/update_genomic_reg.py -len {input.len} -vcf {input.vcf} -scaf {params.scaf}
        ls updated/*.updated.vcf.gz > {output.cat}
        """

此代码是不正确的,因为当我调用{params.scaf}时,列表中的所有项目都已加载到shell命令中。当前的shell命令如下:

python 3.7 scripts/update_genomic_reg.py -len genome/genome_contigs_len_cumsum.txt -vcf filtered/all.vcf.gz -scaf Supercontig0 Supercontig100 Supercontig2 ...

我想要得到的是:*

python 3.7 scripts/update_genomic_reg.py -len genome/genome_contigs_len_cumsum.txt -vcf filtered/all.vcf.gz -scaf Supercontig0

python 3.7 scripts/update_genomic_reg.py -len genome/genome_contigs_len_cumsum.txt -vcf filtered/all.vcf.gz -scaf Supercontig100

以此类推。

我尝试在函数中使用wildcards,但是我没有给它正确的属性。

关于输入函数和通配符,还有snakemake文档,有好几篇文章,但我无法真正将它们应用于我的案例。 有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

那下面呢?请注意,您的get_scontigs_names不使用通配符。

import os, glob

def get_scontigs_names():
   scontigs = glob.glob(os.path.join("reference", "Supercontig*"))
   files = [os.path.basename(s) for s in scontigs]
   name = [i.split('_')[0] for i in files]
   return name

supercontigs= get_scontigs_names()

rule all:
    input:
        "updated/all_supercontigs.sorted.vcf.gz"

rule update_vcf:
    input:
        len="genome/genome_contigs_len_cumsum.txt",
        vcf="filtered/all.vcf.gz",
    output:
        upd= "updated/{supercontig}.updated.vcf.gz",
    shell:
        r"""
        python 3.7 scripts/update_genomic_reg.py -len {input.len} \
            -vcf {input.vcf} -scaf {wildcards.supercontig}
        """

rule list_updated: 
    input:
        expand("updated/{supercontig}.updated.vcf.gz", supercontig= supercontigs),
    output:
        "updated/all_supercontigs.sorted.vcf.gz",
    shell:
        r"""
        ls {input} > {output}
        """

答案 1 :(得分:0)

我发现了受@dariober启发的问题的解决方案。

rule all:
input:
    "updated/all_supercontigs.updated.list"

import os, glob

def get_scontigs_names(wildcards):
    scontigs = glob.glob(os.path.join("reference", "Supercontig*"))
    files = [os.path.basename(s) for s in scontigs]
    name = [i.split('_')[0] for i in files]
    return name

rule update_vcf:
    input:
        len="genome/genome_contigs_len_cumsum.txt",
        vcf="filtered/all.vcf.gz"
    output:
        vcf="updated/all_{supercontig}.updated.vcf.gz"
    params:
        py3=config["modules"]["py3"],
        scaf=get_scontigs_names
    shell:
        """
        {params.py3} scripts/update_genomic_reg.py -len {input.len} -vcf 
        {input.vcf} -scaf {wildcards.supercontig}
        """


rule list_updated:
    input:
        expand("updated/all_{supercontig}.updated.vcf.gz", supercontig = 
        supercontigs)
    output:
        "updated/all_supercontigs.updated.list"
    shell:
        """
        ls {input} > {output}
        """