首页 > 解决方案 > 如何确定要在工作流程中间展开的文件列表?

问题描述

在某些时候,我的 snakemake 工作流程会创建一个文件 ovlp.txt,我事先不知道它将包含多少行。然后我希望将此文件拆分为 25000 行的较小文件,这些文件都称为 ovlp_split{s}.txt。为此,我有规则拆分(见下文)。接下来,将使用规则 predict(见下文)处理每个文件,该规则将所有文件 ovlp_split{s}.txt 作为输入。最后,所有文件都在下面的规则 merge_predict 中一起处理。如您所见,我需要多次扩展所有拆分的输入或输出文件列表。但是,由于我不知道文件 ovlp.txt 的大小,直到它是由较早的规则创建的,所以我不知道会有多少拆分。我该如何解决这个问题?

我希望这足够清楚,如果有部分不是,请告诉我。

谢谢!

马琳

rule split:
    input:
        'ovlp.txt'
    output:
        expand('ovlp_split{s}.txt', s=splits)
    run:
        shell('split -l '+str(lines_per_file)+' -a 4 --numeric-suffixes=1 --additional-suffix=.txt ovlp.txt ovlp_split')

rule predict:
    input:
        'ovlp_split{s}.txt',
    output:
        'ovlp_predict_split{s}.txt',
    run:
        (command that processes the file)

rule merge_predict:
    input:
        expand('ovlp_predict_split{s}.txt', s=splits)
    output:
        'ovlp_merged.txt'
    run:
        (command that processed the separate files and creates one final output file)

标签: parameterssnakemake

解决方案


看来您需要使用检查点。也许沿着这些思路:

rule all:
    input:
        'ovlp_merged.txt',

checkpoint split:
    input:
        'ovlp.txt'
    output:
        splitd= directory('ovlp_split'),
    shell:
        r"""
        mkdir {output.splitd}
        split -l 3 -a 4 --numeric-suffixes=1 --additional-suffix=.txt ovlp.txt {output.splitd}/
        """

rule predict:
    input:
        'ovlp_split/{s}.txt',
    output:
        'ovlp_predict_split{s}.txt',
    shell:
        r"""
        cp {input} {output}
        """

def aggregate_split(wildcards):
    chkp_done = checkpoints.split.get().output.splitd
    chkp_output = sorted(glob_wildcards(os.path.join(chkp_done, "{s}.txt")).s)
    return expand('ovlp_predict_split{s}.txt', s= chkp_output)

rule merge_predict:
    input:
        aggregate_split,
    output:
        'ovlp_merged.txt'
    shell:
        r"""
        cat {input} > {output}
        """

推荐阅读