首页 > 解决方案 > Snakemake中输入函数的并行输出

问题描述

您好 Snakemake 社区,

我在 Snakemake 中正确定义一个函数并在params部分中调用它时遇到了很多麻烦。该函数的输出是一个列表,我的目标是使用列表中的每个项目作为 shell 命令的参数。换句话说,我想用不同的参数并行运行同一个 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 命令如下所示:

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在函数内部使用,但我没有给它正确的属性。

有几篇关于输入函数和通配符以及蛇形文档的帖子,但我无法真正将它们应用到我的案例中。有人可以帮我解决这个问题吗?

标签: pythonfunctionlambdawildcardsnakemake

解决方案


下面这个呢?请注意,您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}
        """

推荐阅读