Chapter 6 Operators

Nextflow operators are methods that allow you to connect channels to each other or to transform values emitted by a channel applying some user provided rules. There is a large number of operators that can be seen here. We go through some of them!

6.1 Collect

The collect operator collects all the items emitted by a channel to a List and return the resulting object as a sole emission.

For example,

mzMLFiles = Channel.fromPath( '/crex/proj/uppmax2024-2-11/metabolomics/mzMLData/*.mzML' )
mzMLFiles.collect().view()

The above code, will get the files from the path and emit them all at once. You can try this and compare the results to when you don’t use collect.

Create a file called main_11.nf

nano main_11.nf

Copy the above code to it. Save the file (Ctrl+o enter) and exit (Ctrl+x) Now run

nextflow main_11.nf

This operator can also be used inside the process. For example,

mzMLFiles = Channel.fromPath( '/crex/proj/uppmax2024-2-11/metabolomics/mzMLData/*.mzML' )
process featureFinder {
debug true

input:
file x

""" 
echo $x
"""  
}

workflow {
    featureFinder(mzMLFiles.collect()) // #1
}
  1. By using collect(), the process will gather all the files in mzMLFiles channel and do the operation for this collection of files.

Run this example and compare the results to when you don’t use collect. What is the difference? How many times the process will be run if you use and don’t use collect?

6.2 flatten

The flatten operator transforms a channel in such a way that every item of type Collection or Array is flattened so that each single entry is emitted separately by the resulting channel.

for example,

Channel
    .from( [1,[2,3]], 4, [5,[6]] )
    .flatten()
    .view()

Try to run this example and compare the results to collect!