5.4 Channels and Operators

There are different types of channels:

  • A queue channel is a non-blocking unidirectional FIFO (First In, First Out) queue which connects two processes or operators.
  • A value channel a.k.a. singleton channel is bound to a single value and it can be read unlimited times without consuming its content.

An operator is a method that reshapes or connects different channels applying some rules.

We can write a very simple Nextflow script: save the following piece of code in the test0.nf file:

#!/usr/bin/env nextflow

// This is a comment

/*
 * This is a block of comments
 */

// This is needed for activating the new DLS2
nextflow.enable.dsl=2


//Let's create a channel from string values

str = Channel.from('hello', 'hola', 'bonjour')

/*
* Let's print that channel using the operator view()
* https://www.nextflow.io/docs/latest/operator.html#view
*/
str.view()

Once the file is saved, execute it with:


nextflow run test0.nf

N E X T F L O W  ~  version 20.07.1
Launching `test0.nf` [agitated_avogadro] - revision: 61a595c5bf
hello
hola
bonjour

As you can see the Channel is just a collection of values, but it can also be a collection of file paths.
Let’s create three empty files with the touch command:

touch aa.txt bb.txt cc.txt

And let’s create another script (test2.nf) and save the following code in it:

#!/usr/bin/env nextflow

nextflow.enable.dsl=2

/*
* Let's create the channel `my_files`
* using the method fromPath
*/

Channel
    .fromPath( "*.txt" )
    .set {my_files}

// We can use the view() operator again to see the content of channel "my_files"
my_files.view()

We can now execute test2.nf:

nextflow run test2.nf

N E X T F L O W  ~  version 20.07.1
Launching `test2.nf` [condescending_hugle] - revision: f513c0fac3
/home/ec2-user/git/CoursesCRG_Containers_Nextflow_May_2021/nextflow/aa.txt
/home/ec2-user/git/CoursesCRG_Containers_Nextflow_May_2021/nextflow/bb.txt
/home/ec2-user/git/CoursesCRG_Containers_Nextflow_May_2021/nextflow/cc.txt

Once executed, we can see that a folder named work is generated: Nextflow stores in this folder the intermediate files generated by the processes.