Python Snakemake中输入函数的并行输出

Python Snakemake中输入函数的并行输出,python,function,lambda,wildcard,snakemake,Python,Function,Lambda,Wildcard,Snakemake,您好,蛇人社区 在Snakemake中正确定义函数,并在参数部分调用它,我遇到了很多麻烦。函数的输出是一个列表,我的目标是将列表中的每一项用作shell命令的参数。换句话说,我希望使用不同的参数并行运行同一shell命令的多个作业 这就是功能: import os, glob def get_scontigs_names(wildcards): scontigs = glob.glob(os.path.join("reference", "Supercontig*")) files

您好,蛇人社区

在Snakemake中正确定义函数,并在参数部分调用它,我遇到了很多麻烦。函数的输出是一个列表,我的目标是将列表中的每一项用作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',…]

毒蛇规则是:

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脚本/update\u genomic\u reg.py-len genome/genome\u contigs\u len\u cumsum.txt-vcf filtered/all.vcf.gz-scaf Supercontig0 Supercontig100 Supercontig2…

我想要的是::

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

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

等等

我曾尝试在函数中使用
通配符
,但未能为其提供正确的属性

有几篇关于输入函数和通配符以及snakemake文档的帖子,但我无法将它们真正应用到我的案例中。
谁能帮我一下吗?

下面这个怎么样?请注意,
get\u scontigs\u name
没有使用通配符

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

我从@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}
        """

您是否考虑过在一个规则内运行多个作业的
xargs
parallel
?由于这一行
supercontigs=get\u scontigs\u names()
,这个建议并没有真正起作用,但我通过将规则分成两部分(见下文)解决了这个问题。@SamPer-的确,根据下面的答案编辑代码。很好,您找到了解决方案。但我仍然不明白将
get\u scontigs\u names
作为规则中的输入函数的意义,因为它的输出不依赖于通配符。就个人而言,我觉得这使得编写代码有点做作,但我可能错了。(另外,为了可读性,我将把
import
语句放在脚本的顶部)我还没有真正理解如何在输入函数中使用
通配符。有人有这样一个简单明了的例子吗?