Nextflow Channels

Nextflow Channels


This is part 5 of 14 of a Introduction to NextFlow.


mkdir channels
cd channels

Value channels

Create a Nextflow file called value_ch.nf; add the following and execute it using nextflow run value_ch.nf:

// Creates an empty value channel.
ch1 = Channel.value('GRCh38')
// Creates a value channel and binds a string to it.
ch2 = Channel.value(['chr1', 'chr2', 'chr3', 'chr4', 'chr5'])
// Creates a value channel and binds a list object to it that will be emitted as a single item.
ch3 = Channel.value(['chr1': 248956422, 'chr2': 242193529, 'chr3': 198295559])
// The value method can only take 1 argument, however, this can be a single list containing several elements.
ch1.view()
ch2.view()
ch3.view()

Output

N E X T F L O W  ~  version 21.04.3
Launching `value_ch.nf` [happy_bell] - revision: a80c27d5b2
GRCh38
[chr1, chr2, chr3, chr4, chr5]
[chr1:248956422, chr2:242193529, chr3:198295559]

Reminder:

  • A List object can be defined by placing the values in square brackets [] separated by a comma.
  • A Map object is similar, but with key:value pairs separated by commas.

Example:

myList = [1776, -1, 33, 99, 0, 928734928763]

myMap = [ p1 : "start", q2 : "end" ]

Queue channel

In Nextflow DSL1, queue channels can only be used once in a workflow, either connecting workflow input to process input, or process output to input for another process. In DSL2, we can use a queue channel multiple times.

What is the difference between a queue and value channel?

  • A queue channel is consumable and can store multiple values.
  • A value channel, also known as a singleton channel, is bound to a single value by definition.

Queue channel factory


Create a Nextflow file called queue_channel.nf; add the following and execute it using nextflow run queue_channel.nf:

// Create a queue channel using the Channel.of factory method
ch1 = Channel.of(1, 2, 3, 4, 5)

// Create a queue channel using the Channel.fromList factory method
ch2 = Channel.fromList(['A', 'B', 'C'])

// Create a queue channel using the Channel.fromPath factory method (adjust the path to an existing file in your system)
ch3 = Channel.fromPath('/path/to/your/file.txt')

ch1.view()
ch2.view()
ch3.view()

Output

N E X T F L O W  ~  version 21.04.3
Launching `queue_channel.nf` [serene_babbage] - revision: 12345678
1
2
3
4
5
A
B
C
/path/to/your/file.txt

1. The of Channel factory

Create a Nextflow file called queue_of.nf; add the following to create a Channel.of method and execute it using nextflow run queue_of.nf:

chromosome_ch = Channel.of('chr1', 'chr3', 'chr5', 'chr7')
chromosome_ch2 = Channel.of(1..22, 'X', 'Y')
chromosome_ch.view()
chromosome_ch2.view()

Output

N E X T F L O W  ~  version 21.04.3
Launching `queue_of.nf` [modest_miescher] - revision: fb9862a33f
chr1
chr3
chr5
chr7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
X
Y
  • The first line in this example creates a variable chromosome_ch. chromosome_ch is a queue channel containing the four values specified as arguments in the of method. The .view operator will print one line per item in a list. Therefore the view operator on the second line will print four lines, one for each element in the channel.
  • Arguments passed to the of method can be of varying types e.g., combinations of numbers, strings, or objects. In the above examples, we have examples of both string and number data types.

2. The fromList Channel factory

Create a new file queue_list.nf; add the following and execute it using nextflow run queue_list.nf:

aligner_list = ['salmon', 'kallisto']

aligner_ch = Channel.fromList(aligner_list)

aligner_ch.view()

Output

N E X T F L O W  ~  version 21.04.3
Launching `queue_list.nf` [admiring_raman] - revision: b2dec22cea
salmon
kallisto

In this example, we first create a list object called aligner_list containing two strings, ‘salmon’ and ‘kallisto’. We then use the Channel.fromList method to create a queue channel named aligner_ch from the aligner_list. Finally, we use the .view() operator to display the content of the channel.

Channel.fromList vs Channel.of In the above example, the channel has two elements. If you had used the Channel.of(list) it would have contained only 1 element [salmon, kallisto] and any operator or process using the channel would run once.


CLICK HERE for an exercise on creating channels from a list Create a Nextflow script `exercise_1.nf` that creates both a `queue` and `value` channel for the following list. Then print the contents of the channels using the view operator. How many lines does the queue and value channel print? ```groovy ids = ['ERR908507', 'ERR908506', 'ERR908505'] ``` Solution ```groovy ids = ['ERR908507', 'ERR908506', 'ERR908505'] queue_ch = Channel.fromList(ids) value_ch = Channel.value(ids) queue_ch.view() value_ch.view() ``` Output ``` N E X T F L O W ~ version 21.04.3 Launching `exercise_1.nf` [silly_hypatia] - revision: 5d8381b7d4 ERR908507 ERR908506 ERR908505 [ERR908507, ERR908506, ERR908505] ``` ``` The queue channel `queue_ch` will print three lines. The value channel `value_ch` will print one line. ``` ##


3. The fromPath Channel factory

Name Description
glob When true, the characters *, ?, [] and {} are interpreted as glob wildcards, otherwise they are treated as literal characters (default: true)
type The type of file paths matched by the string, either file, dir or any (default: file)
hidden When true, hidden files are included in the resulting paths (default: false)
maxDepth Maximum number of directory levels to visit (default: no limit)
followLinks When true, symbolic links are followed during directory tree traversal, otherwise they are managed as files (default: true)
relative When true returned paths are relative to the top-most common directory (default: false)
checkIfExists When true throws an exception if the specified path does not exist in the file system (default: false)

Create a new file queue_path.nf; add the following and nextflow run queue_path.nf:

println("Single File")
read_ch = Channel.fromPath("/workspace/nextflow_tutorial/data/untrimmed_fastq/SRR2584863_1.fastq.gz")
read_ch.view()

println("Glob Syntax")

// We can change the default options for the `fromPath` method to give an error if the file doesn’t exist using the `checkIfExists` parameter.
read_ch = Channel.fromPath( "/workspace/nextflow_tutorial/data/untrimmed_fastq/*.fastq.gz", checkIfExists: true )
read_ch.view()

// **Note The pattern must contain at least a star wildcard character.**

Output

N E X T F L O W  ~  version 21.04.3
Launching `queue_path.nf` [awesome_yalow] - revision: 695fc25ebf
Single File
/workspace/nextflow_tutorial/data/untrimmed_fastq/SRR2584863_1.fastq.gz
Glob Syntax
/workspace/nextflow_tutorial/data/untrimmed_fastq/SRR2589044_

4. The fromFilePairs Channel factory

For example in the /workspace/nextflow_tutorial/data/untrimmed_fastq directory we have three groups of read pairs:

Sample_ID read1 read2
SRR2589044 SRR2589044_1.fastq.gz SRR2589044_2.fastq.gz
SRR2584863 SRR2584863_1.fastq.gz SRR2584863_2.fastq.gz
SRR2584866 SRR2584866_1.fastq.gz SRR2584866_2.fastq.gz

Create a new queue_pairs.nf; add the following and nextflow run queue_pairs.nf:

read_pair_ch = Channel.fromFilePairs("/workspace/nextflow_tutorial/data/untrimmed_fastq/*_{1,2}.fastq.gz", checkIfExists: true)
read_pair_ch.view()

Output

N E X T F L O W  ~  version 21.04.3
Launching `queue_pairs.nf` [nauseous_austin] - revision: eb531c1c8a
[SRR2589044, [nextflow_tutorial/data/untrimmed_fastq/SRR2589044_1.fastq.gz, nextflow_tutorial/data/untrimmed_fastq/SRR2589044_2.fastq.gz]]
[SRR2584863, [nextflow_tutorial/data/untrimmed_fastq/SRR2584863_1.fastq.gz, nextflow_tutorial/data/untrimmed_fastq/SRR2584863_2.fastq.gz]]
[SRR2584866, [nextflow_tutorial/data/untrimmed_fastq/SRR2584866_1.fastq.gz, nextflow_tutorial/data/untrimmed_fastq/SRR2584866_2.fastq.gz]]

This will produce a queue channel, read_pair_ch , containing three elements:

Read more information about the channel factory fromFilePairs here

5. The fromSRA Channel factory

Quick Recap:

  • Channels must be used to import data into Nextflow.
  • Nextflow has two different kinds of channels, queue channels and value channels.
  • Data in value channels can be used multiple times in a workflow.
  • Data in queue channels are consumed when they are used by a process or an operator.
  • Channel factory methods, such as Channel.of, are used to create Channels.
  • Channel factory methods have optional parameters e.g., checkIfExists, that can be used to alter the creation and behavior of a channel.

Complex Multi-map Input for Channels

Quick Recap:

  • Channels must be used to import data into Nextflow.
  • Nextflow has two different kinds of channels, queue channels and value channels.
  • Data in value channels can be used multiple times in workflow.
  • Data in queue channels are consumed when they are used by a process or an operator.
  • Channel factory methods, such as Channel.of, are used to create Channels.
  • Channel factory methods have optional parameters e.g., checkIfExists, that can be used to alter the creation and behaviour of a channel.

Back to:Nextflow Scripting Next:NextFlow Processes