Search code examples
workflownextflow

Is there a way to provide a default value for an optional input channel?


I link to be able to do something like this:

workflow XXX {
   take:
       a
       b default ""
   main:
       if (b == "") {
          println "a is ${a} and b is unset"
       } else {
          println "a is ${a} and b is ${b}"
       }

}

However the code does not compile... what is the closest valid nextflow to that?


Solution

  • Workflows can declare one or more input channels using the take keyword. However, there is currently no way to modify the channels in the declaration. This usually isn't a problem though, because you can of course transform values emitted by a channel either upstream or downstream of the workflow.

    I think if your workflow sometimes requires an additional input channel, then just have that workflow define that additional input channel. Then, when you call the workflow you can define a default value for the channel in the usual way - i.e. using the ifEmpty operator. For example:

    nextflow.enable.dsl=2
    
    workflow test {
       
       take:
         input_values
         input_files
       
       main:
         input_files.view()
    }
    
    workflow {
    
       foobarbaz = Channel.of( 'foo', 'bar', 'baz' )
       text_files = Channel.fromPath( '/path/*.txt' ).ifEmpty( file('./default.txt') )
    
       test( foobarbaz, text_files )
    }
    

    Alternatively, define a single input channel and use a parameter to supply a default value:

    workflow test {
         
       take:
         input_values
    
       main:
    
         if( params.text_file ) {
         
           do_something( params.text_file )
    
         } else {
    
           do_something_else()
         }
    }