【问题标题】:snakemake temporary directoriessnakemake 临时目录
【发布时间】:2020-02-10 16:07:25
【问题描述】:

snakemake 删除所有标记为临时的输出文件,但如果输出是如下所示的目录,则不对文件执行任何操作:

rule all:
    input:
        'final.txt',

checkpoint split_big_file:
    input: 'bigfile.txt'
    output: temp(directory('split_files'))
    shell: 'mkdir -p {output} ; split -l 5000 -d -e bigfile.txt {output}/part_'

rule copy_small_files:
    input: 'split_files/part_{num}'
    output: temp('copy_files/part_{num}.txt')
    shell: 'cp -f {input} {output}'

def aggregate_input(wildcards):
    '''
    aggregate the file names of the random number of files
    generated at the scatter step
    '''
    checkpoint_output = checkpoints.split_big_file.get(**wildcards).output[0]
    print(checkpoint_output)
    agg_inp = expand('copy_files/part_{num}.txt', num=glob_wildcards('split_files/part_{num}').num)
    print(agg_inp)
    return agg_inp

rule merge_small_files:
    input: aggregate_input
    output: 'final.txt'
    shell: 'cat {input} > {output}'

当我使用包含数千行的bigfile.txt 运行上面显示的代码时,一切运行正常,但split_files 目录不为空。

$ wc -l final.txt 
  61177 final.txt
$ wc -l bigfile.txt 
  61177 bigfile.txt
$ ls copy_files/
$ ls split_files/
  part_00  part_01  part_02  part_03  part_04  
  part_05  part_06  part_07  part_08  part_09  
  part_10  part_11  part_12

我想看的:

  1. copy_files 目录也应该删除(但显然因为snakemake无法确定该目录中是否存在与snakemake无关的其他文件,默认情况下不会删除目录)
  2. split_files 目录的内容(最好是目录本身;参见上面的第 1 点)应该被删除。

【问题讨论】:

    标签: python snakemake


    【解决方案1】:

    我无法重新创建它:

    rule all:
        input:
            "a.txt"
    
    rule first:
        output:
            temp(directory("dir1"))
        shell:
            "mkdir {output}; touch {output}/a.txt; sleep 5"
    
    rule second:
        input:
            "dir1"
        output:
            "a.txt"
        shell:
            "touch {output}"
    

    你用的是什么版本的snakemake? output_dir 可能是在 rule all 下列出的吗? Snakemake 假设您想要的输出是您的第一条规则的输入(可能是rule all)。所以它不会删除这些文件,从rule all 下删除output_dir 将解决这个问题。

    但我只是猜测,因为您没有提供minimal reproducible example

    编辑

    嗯...应该可以!以下是我可以提出的两个非理想解决方案:

    我们可以骗过snakemake再次重新评估DAG,然后像这样删除文件夹,但不确定文件是否足够早地删除(文件可能非常大)。

    rule merge_small_files:
        input: aggregate=aggregate_input, dummy='split_files'
        output: 'final.txt'
        shell: 'cat {input.aggregate} > {output}'
    

    或者只是复制后删除文件,但最后你会得到一个空文件夹:

    rule copy_small_files:
        input: 'split_files/part_{num}'
        output: temp('copy_files/part_{num}.txt')
        shell: 'cp -f {input} {output}; rm {input}'
    

    您当然可以将这两种解决方案结合起来,两全其美,但是不幸的是,看起来并不是很漂亮:(

    【讨论】:

    • > 但是我只是在猜测,因为您没有提供最小的可重现示例——足够公平;我现在添加了一个更好的例子。
    • @vkkodali,我不在办公室,我更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2011-06-08
    • 2016-01-03
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 2011-03-27
    相关资源
    最近更新 更多