将 `publishDir` 的路径传递给 NextFlow Processes

Pass path for `publishDir` to NextFlow Processes

我正在使用 NextFlowDSL2 语法。我想将最终结果路径定义为命令行参数。然而,据我所知,NextFlow 进程无法识别 directive 范围内的 input 变量(参见下面的最小示例)。

终极问题:如何将“基本目录”传递给一个进程,然后该进程可以附加到该路径(例如,publishDir("${results_dir}/MY_PROC_RESULTS/", mode = "copy"))?如果这不是最佳方法,也欢迎其他建议。

非常感谢任何帮助!

这是一个最小的例子:

MY_WORKFLOW.nf

/*
 * Make this pipeline a nextflow 2 implementation
 */
nextflow.enable.dsl=2

include {SUB_WORKFLOW_WF} from './SUB_WORKFLOW_WF_PROCS.nf'

/*
 * Define the directory to publish final results in.
 */
params.results_dir = "${projectDir}/results"

results_dir = params.results_dir

workflow{
    SUB_WORKFLOW_WF(results_dir)
}

SUB_WORKFLOW_WF_PROCS.nf

workflow SUB_WORKFLOW_WF {
    take:
        results_dir

    main:
        println results_dir // This works
        MY_PROC(results_dir)
}

process MY_PROC {
    /*
     * Workflow fails with ERROR 1 if the following line is included. Fails
     * with ERROR 2 if the following line is excluded.
     */
    println $results_dir
    publishDir("${results_dir}/MY_PROC_RESULTS/", mode = "copy")

    input:
        path(results_dir)

    script:
        """
        echo "Hello"
        """
}

结果

错误 1

No such variable: $results_dir

 -- Check script './SUB_WORKFLOW_WF_PROCS.nf' at line: 11 or see '.nextflow.log' file for more details

错误 2

Error executing process > 'SUB_WORKFLOW_WF:MY_PROC'

Caused by:
  Not a valid PublishDir entry [org.codehaus.groovy.runtime.GStringImpl] null/MY_PROC_RESULTS

您收到第一个错误是因为全局范围内没有“$results_dir”变量。第二个错误有点神秘,但实际上只是一个语法错误。 'mode' 声明只需要一个冒号,就像一个 Groovy 映射来分隔键和值:

publishDir("${results_dir}/MY_PROC_RESULTS/", mode: "copy")

因此,与其传递变量,不如在任何 'include' 声明之前在脚本开头定义管道参数。然后这些参数将从包含的上下文中继承。例如:

MY_WORKFLOW.nf

nextflow.enable.dsl=2

params.publish_dir = './results'

include { SUB_WORKFLOW_WF } from './SUB_WORKFLOW_WF_PROCS.nf'


workflow{
    
    SUB_WORKFLOW_WF()
}

SUB_WORKFLOW_WF_PROCS.nf

workflow SUB_WORKFLOW_WF {

    MY_PROC()
}

process MY_PROC {
    
    publishDir(
        path: "${params.publish_dir}/MY_PROC_RESULTS",
        mode: 'copy',
    )

    output:
    path "proc_results.txt"

    """
    touch "proc_results.txt"
    """
}

结果:

$ nextflow run ./MY_WORKFLOW.nf 
N E X T F L O W  ~  version 21.04.3
Launching `./MY_WORKFLOW.nf` [shrivelled_montalcini] - revision: d31b4930b9
executor >  local (1)
[2b/08e223] process > SUB_WORKFLOW_WF:MY_PROC [100%] 1 of 1 ✔
$ find ./results/MY_PROC_RESULTS/proc_results.txt 
./results/MY_PROC_RESULTS/proc_results.txt