【问题标题】:How to call a variable created in the script in Nextflow?如何在 Nextflow 中调用脚本中创建的变量?
【发布时间】:2021-03-11 11:17:17
【问题描述】:

我有一个从文本文件创建变量的 nextflow 脚本,我需要将该变量的值传递给命令行命令(这是一个 bioconda 包)。这两个过程发生在“脚本”部分中。我尝试使用“$”符号调用变量而没有任何结果,我认为是因为在 nextflow 脚本的脚本部分中使用该符号是为了调用在输入部分中定义的变量。

为了让自己更清楚,这里是我想要实现的代码示例:

params.gz_file = '/path/to/file.gz'
params.fa_file = '/path/to/file.fa'
params.output_dir = '/path/to/outdir'

input_file = file(params.gz_file)
fasta_file = file(params.fa_file)

process foo {
    //publishDir "${params.output_dir}", mode: 'copy',

    input:
    path file from input_file
    path fasta from fasta_file

    output:
    file ("*.html")

    script:
    """
    echo 123 > number.txt
    parameter=`cat number.txt`
    create_report $file $fasta --flanking $parameter 
    """
}

通过 doig,我收到的错误是:

Error executing process > 'foo'
Caused by:
  Unknown variable 'parameter' -- Make sure it is not misspelt and defined somewhere in the script before using it

有没有办法在脚本中调用变量parameter 而无需 Nextflow 将其解释为输入文件?提前致谢!

【问题讨论】:

    标签: bash bioinformatics nextflow


    【解决方案1】:

    script block 的文档在这里很有用:

    由于 Nextflow 使用相同的 Bash 语法进行变量替换 字符串,您需要根据需要仔细管理它们 在 Nextflow 上下文中评估变量 - 或 - 在 Bash 中 环境执行。

    一种解决方案是通过在它们前面加上反斜杠 (\) 字符来转义您的 shell (Bash) 变量,如下例所示:

    process foo {
    
        script:
        """
        echo 123 > number.txt
        parameter="\$(cat number.txt)"
        echo "\${parameter}"
        """
    }
    

    另一种解决方案是改用shell block,其中美元($)变量由您的shell(Bash 解释器)管理,而感叹号(!)变量由Nextflow 处理。例如:

    process bar {
    
        echo true
    
        input:
        val greeting from 'Hello', 'Hola', 'Bonjour'
    
        shell:
        '''
        echo 123 > number.txt
        parameter="$(cat number.txt)"
        echo "!{greeting} parameter ${parameter}"
        '''
    }
    

    【讨论】:

    【解决方案2】:

    在顶部的“参数”部分声明“参数”。

    params.parameter="1234"
    (..)
    script:
    """
    (...)
    create_report $file $fasta --flanking ${params.parameter} 
    (...)
    """
    (...)
    

    并使用“--parameter 87678”调用“nextflow run”

    【讨论】:

    • 如果需要像示例中那样动态计算所需的值,则工作流参数将无济于事。
    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    相关资源
    最近更新 更多