snakemake:从数组写入文件

时间:2019-04-20 19:24:48

标签: snakemake

我有一个数组xx = [1,2,3],我想使用Snakemake来创建(空)文件1.txt, 2.txt, 3.txt的列表。

这是我使用的Snakefile:

xx = [1,2,3]
rule makefiles:
    output: expand("{f}.txt", f=xx)
    run:
        with open(output, 'w') as file:
            file.write('blank')

但是,我没有在文件夹中看到三个新的闪亮文本文件,而是看到错误消息:

expected str, bytes or os.PathLike object, not OutputFiles

不确定我做错了什么。

1 个答案:

答案 0 :(得分:1)

迭代output获取文件名,然后写入文件名。参见relevant documentation here

rule makefiles:
    output: expand("{f}.txt", f=xx)
    run:
        for f in output:
            with open(f, 'w') as file:
                file.write('blank')

通过rule all中的defining target files重写上述规则以进行并行化:

rule all:
    expand("{f}.txt", f=xx)

rule makefiles:
    output: 
        "{f}.txt"
    run:
        with open(output[0], 'w') as file:
           file.write('blank')
相关问题