在nextflow和groovy中,以扩展名结尾的文件中的For循环

hxzsmxv2  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(71)

我想写一个简单的nextflow管道,我将在一个工作多个任务一个接一个。
以下是我目前的档案:

// 1. Define the input directory
params.input_dir = "path1/path2/path3/Data"

// 2. Define the output directory
params.output_dir = "path1/path2/path3/Output"

// 3. Define the process using nextflow.enable.dsl=2

process RUN_KRAKEN2 {

    publishDir params.output_dir, mode: 'copy'

    // 3.2 Define the output file
    output:
    path("Kraken2_iterations.check")

    // 3.3 Define the script
    script:
    """
    echo "Making directory"
    mkdir -p ${params.output_dir}
    for (file in file("$params.input_dir").list()) { # for each file in params.input_dir
     if (file.endsWith(".m8")) { #if it ends with *.m8
         #then do this task
         task1
         #and finaly run this last task
         task2
     }

    """
}

// 4. Run the workflow
workflow {
    RUN_KRAKEN2()
}

但这段代码中有一些问题。我真的是Nextflow的新手,尤其是Groovy。我试图对每一行进行评论,以解释我想要实现的目标。如果有人能正确地编写代码,那将是惊人的。
此外,正如你所看到的,我定义了一个名为“Kraken2_iterations.check”的输出文件。我想在脚本结束时创建这样一个文件,在处理完每个*.m8文件之后。有人有主意吗?我已经想到使用变量' n=0 ',然后在每次迭代中将' n '递增1。然后,我可以使用像'if n < count(files ending with *.m8) '这样的' if '语句,但我不确定如何在Groovy中实现它。

4ioopgfo

4ioopgfo1#

您可以使用Groovy函数eachFileRecurse(...)为特定文件类型的所有文件搜索一个文件夹。这个答案有一个例子:在Groovy中递归列出与特定文件类型匹配的所有文件

  • 更新-
    我得到了这个项目结构:
├── data
│   ├── data.m8
│   └── input
│       ├── in.m8
│       └── out.m8
├── find-files
│   └── main.nf
└── nextflow

main.nf:

#!/usr/bin/env nextflow

process RUN_KRAKEN2 {

    script:
    def out = ''
    new File('.').eachFileRecurse(groovy.io.FileType.FILES) {
        if(it.name.endsWith('.m8')) {
            println it
            out += it
        }
    }

    """
    touch done
    echo ${out} >> done
    """
}

workflow {
    RUN_KRAKEN2()
}

在执行时:

./nextflow run find-files/

在工作目录(fx. work/27/e1994e62df22bb5c7cb0ec3ef1f2cd/done)包含找到的文件:

./data/data.m8./data/input/in.m8./data/input/out.m8

相关问题