Marius
Marius

Reputation: 1060

check if nextflow channel is empty

I am trying to figure out how to check if a channel is empty or not.

For instance, I have two processes. The first process runs only if a combination of parameters/flags are set and if so, checks also if its input file from another process (input via a channel) is not empty, then it creates a new input file for a second process (to eventually replace the default one). As a simplified example:

.....
.....

// create the channel here to force nextflow to wait for the first process
_chNewInputForProcessTwo = Channel.create()

process processOne {
  when:
    params.conditionOne && parameters.conditionTwo
  input:
      file inputFile from _channelUpstreamProcess
 output:
      file("my.output.file") into _chNewInputForProcessTwo
  script:
    """
    # check if we need to produce new input for second process (i.e., input file not empty)
    if [ -s ${inputFIle} ]
      then
          <super_command_to_generate_new_fancy_input_for_second_process> > "my.output.file" 
      else
          echo "No need to create new input"
    fi
   """
}

// and here I would like to check if new input was generated or leave the "default" one
_chInputProcessTwo = Channel.from(_chNewInputForProcessTwo).ifEmpty(Channel.value(params.defaultInputProcessTwo))

process secondProcess {
  input:
      file inputFile from _chInputProcessTwo
......
......
etc.

When I try running with this approach it fails because the channel _chNewInputForProcessTwo contains DataflowQueue(queue=[]) therefore, not being actually empty.

I've tried several things looking at the documentation and the threads on google groups and on gitter. trying to set it to empty, but then it complains i am trying to use the channel twice. putting create().close(), etc.

Is there a clean/reasonable way to do this? I could do it using a value channel and have the first process output some string on the stdout to be picked up and checked by the second process, but that seems pretty dirty to me.

Any suggestions/feedback is appreciated. Thank you in advance!

Marius

Upvotes: 3

Views: 4720

Answers (1)

Steve
Steve

Reputation: 54502

Best to avoid trying to check if the channel is empty. If your channel could be empty and you need a default value in your channel, you can use the ifEmpty operator to supply one. Note that a single value is implicitly a value channel. I think all you need is:

myDefaultInputFile = file(params.defaultInputProcessTwo)

chInputProcessTwo = chNewInputForProcessTwo.ifEmpty(myDefaultInputFile)

Also, calling Channel.create() is usually unnecessary.

Upvotes: 3

Related Questions