4

I have a nextflow script that creates a variable from a text file, and I need to pass the value of that variable to a command line order (which is a bioconda package). Those two processes happen inside the "script" part. I have tried to call the variable using the '$' symbol without any results, I think because using that symbol in the script part of a nextflow script is for calling variables defined in the input part.

To make myself clearer, here is a code sample of what I'm trying to achieve:

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 
    """
}

By doig this the error I recieve is:

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

Is there any way to call the variable parameter inside the script without Nextflow interpreting it as an input file? Thanks in advance!

Cris Tuñí
  • 107
  • 1
  • 9

2 Answers2

6

The documentation re the script block is useful here:

Since Nextflow uses the same Bash syntax for variable substitutions in strings, you need to manage them carefully depending on if you want to evaluate a variable in the Nextflow context - or - in the Bash environment execution.

One solution is to escape your shell (Bash) variables by prefixing them with a back-slash (\) character, like in the following example:

process foo {

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

Another solution is to instead use a shell block, where dollar ($) variables are managed by your shell (Bash interpreter), while exclamation mark (!) variables are handled by Nextflow. For example:

process bar {

    echo true

    input:
    val greeting from 'Hello', 'Hola', 'Bonjour'

    shell:
    '''
    echo 123 > number.txt
    parameter="$(cat number.txt)"
    echo "!{greeting} parameter ${parameter}"
    '''
}
Steve
  • 51,466
  • 13
  • 89
  • 103
  • 1
    Thank you for this detailed answer! It worked perfectly, both of the options :) – Cris Tuñí Mar 11 '21 at 10:58
  • 1
    how would I have to edit / add the output to forward the value of parameter into a value channel? – tristan Dec 08 '21 at 10:00
  • 2
    @tristan https://stackoverflow.com/questions/70274226/how-to-call-a-forward-the-value-of-a-variable-created-in-the-script-in-nextflow – Steve Dec 08 '21 at 14:17
-2

declare "parameter" in the top 'params' section.

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

and call "nextflow run" with "--parameter 87678"

Pierre
  • 34,472
  • 31
  • 113
  • 192
  • 2
    A workflow parameter won't help if the value that's required needs to be computed on the fly like in the example. – Steve Mar 11 '21 at 03:24