Channels
Overview
Tutorial: 15 min
- Objectives:
Learn how to use channels in NextFlow.
A channel in Nextflow is a mechanism for passing data between processes. It acts as a queue that allows processes to communicate asynchronously and facilitates parallel execution.
1process sayHello {
2
3 publishDir 'results', mode: 'copy'
4
5 input:
6 val message
7
8 output:
9 path 'output.txt'
10
11 script:
12 """
13 echo '$message' > output.txt
14 """
15}
16
17params.message = '¡Buenos días!'
18
19workflow {
20
21 // create a channel for inputs
22 message_ch = Channel.of('Hello Channels!')
23
24 // emit a greeting
25 sayHello(message_ch)
26}
Explanation
There are a variety of channel factories in NextFlow. The most basic channel factory, called Channel.of, creates a channel containing a single value.
Run the following workflow:
1nextflow run 4_input.nf
We can upgrade the workflow to accept multiple input values.
1workflow {
2 message_ch = Channel.of('Hello','Bonjour','Holà')
3 sayHello(message_ch)
4}
Explanation
By default, the ANSI logging system overwrites logs from multiple executions of the same process on a single line. As a result, only one process appears in the log, even if multiple are running. To display all process logs separately, use -ansi-log false.
Explanation
Inside the work/ directory, separate output.txt files are created for different inputs, ensuring each execution has its own result. However, in the results/ directory, only one output.txt file is present because the filename is hardcoded. When using publishDir, subsequent outputs overwrite the previous ones unless unique filenames or alternative modes are used.
Unique Output Files
Instead of hardcoding the output filename, we can generate unique filenames for different inputs when using publishDir.
1nextflow run 6_multiple_outputs.nf -ansi-log false
Key Points
A channel enables asynchronous data transfer between processes, supporting parallel execution.
Channel.of allows workflows to accept multiple inputs dynamically.
Using -ansi-log false prevents log overwriting and displays all process logs separately.
Unique filenames in publishDir prevent output overwrites in the results/ directory.