bashgroovynextflownextflow-tower

Nextflow: How to define a environment var in process A and call this env var in process B?


I am new to the Nextflow. I want to define a environment a in the process A:

process define_env {
output:
env var_a
"""
echo var_a="test" > var_file.txt
source var_file.txt
"""
}

And then I want to call environment var_a in the following process:

process call_var {
"""
echo This is a $var_a.
"""
}

//And run two processes
workflow {
define_env()
call_var()
}

It reports the error: No such variable: var_a. Could you please help me to figure out how to fix it?

Thanks a lot!


Solution

  • Every processing environment is different, so to my knowledge environmental variables will need to be defined for each process you use.

    If you're creating the file var_file.txt, you can output the file using path(var_file.txt) in the output declaration. In the process call_var, you can then use path(var_file.txt) in the input declaration but would need to amend your workflow declaration so that call_var takes the output of the first process.

    process define_env {
      output:
      path(var_file.txt), emit: vars
    
      script:
      """
      echo var_a="test" > var_file.txt
      source var_file.txt
      """
    }
    
    process call_var {
      input:
      path(var_file)
    
      script:
      """
      source ${var_file}
      echo This is a $var_a.
      """
    }
    
    //And run two processes
    workflow {
      define_env()
      call_var(define_env.out.vars)
    }
    

    An alternative if you are using the same env variable in all processes would be to define it in the config file and export it as an environmental variable in each process (i.e., export ${params.global_variable}=test in each process. You can even use the beforeScript directive to ensure it's read into the environment in each process.