groovynextflow

Dynamic output files from list variable in nextflow


I have a very simple nextflow process, it takes as an input a list of files (can be 1 or 2) and what I want is out output a list of files equal to the input with a modification:

process filtering{

        input:
        tuple val(acc),  path(fastqlist)

        output:
        tuple val(acc), path(filtered_fastqlist)

        script:
            def filtered_fastqlist = (fastqlist.size() == 2) ? ["fastp_"+fastqlist[0],"fastp_"+fastqlist[1]] : ["fastp_"+fastqlist[0]]

            """
            touch  ${filtered_fastqlist[0]}
            touch  ${filtered_fastqlist[1]}
            """
}

When running this, nextflow gives me the error NOTE: Missing output file(s) 'filtered_fastqlist' expected by process. It seemss somehow that filtered_fastq is being used as a literal, instead of a groovy array. (I know I could just use a wildcard), but is there anyway to dynamically pass a list of values from a variable to a an output channel?


Solution

  • The def keyword makes the variable context inaccessible (different scope) to the output block. Check the snippet below for another example. Try adding def to run into the same error you did.

    process filtering{
      input:
      tuple val(acc),  path(fastqlist)
    
      output:
      tuple val(acc), path(my_str)
    
      script:
      my_str = fastqlist.find { it.toString().endsWith('2.txt') }
      """
      """
    }
    
    workflow {
      Channel
        .of(['A', [file('A1.txt'), file('A2.txt'), file('A3.txt')]],
            ['B', [file('B1.txt'), file('B2.txt'), file('B3.txt')]])
        | filtering
        | view
    }
    

    Outputs with and without the def keyword. enter image description here