This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to select or subset process outputs in Nextflow DSL2?

I have a DSL2 Nextflow workflow. I would like to use just the outputs named "paired.fq.gz" ( index 0 and 2 in the tuple) in downstream processes.

Is there a way to filter or select a subset of the process outputs?

    process trimreads {

        input:
        tuple val(pair_id), path(reads)

        output:
        set pair_id, path("trimmed_${pair_id}_R{1,2}_paired.fq.gz"),
        path("trimmed_${pair_id}_R{1,2}_unpaired.fq.gz")

        script:
        """
        #!/usr/bin/env bash
        trimmomatic ...
        """
    }

workflow {
   ...
    trimmed_reads_ch = trimreads(read_pairs_ch)

    // selected_trimmed_reads_ch = filter trimmed_reads_ch?

    fastqc_ch = fastqc(...)
dsl2 nextflow

How about declaring multiple output channels? You can even name them to easily reuse them later:

output:
tuple val(pair_id), path("trimmed_${pair_id}_R{1,2}_paired.fq.gz"), emit: paired
tuple val(pair_id), path("trimmed_${pair_id}_R{1,2}_unpaired.fq.gz"), emit: unpaired, optional: true

2 answers

use the map operator https://www.nextflow.io/docs/latest/operator.html#map

trimmed_reads_ch = trimreads(read_pairs_ch)

pair_id_unmapped_channel = trimmed_reads_ch.map{T->[T[0],T[2]]}

Thank you. This worked great!

Check the code below:

process FOO {
  input:
    val id
  output:
    tuple val(id), path("${id}_paired.fq.gz"), emit: paired
    tuple val(id), path("${id}_unpaired.fq.gz"), emit: unpaired, optional: true
  script:
  """
    echo ${id} > ${id}_paired.fq.gz
    echo ${id} > ${id}_unpaired.fq.gz
  """
}

workflow {
  Channel
    .of(1..5)
    .set { ch }
  FOO(ch).paired.view()
}

It outputs:

N E X T F L O W  ~  version 22.10.3
Launching `ex4.nf` [astonishing_einstein] DSL2 - revision: 3f012d70fa
executor >  local (5)
[2f/e395a9] process > FOO (3) [100%] 5 of 5 ✔
[1, /Users/mribeirodantas/work/74/497cebcf0cbe44316c52d877302762/1_paired.fq.gz]
[2, /Users/mribeirodantas/work/2f/127b25707472a7780416bea05b042c/2_paired.fq.gz]
[4, /Users/mribeirodantas/work/94/5106f1547fd087979003be47787c3c/4_paired.fq.gz]
[5, /Users/mribeirodantas/work/aa/886d44ebdd497b6ff43ae5c56fc2a2/5_paired.fq.gz]
[3, /Users/mribeirodantas/work/2f/e395a9283e532c1ef9fc7bac30242a/3_paired.fq.gz]

This is the easiest way to do it. Depending on your case, there are channel operators that can help you better such as filter, branch, take, buffer and so on.

Log in to answer this question.