nextflow icon indicating copy to clipboard operation
nextflow copied to clipboard

ifEmpty operator doesn't return expected value (value channel)

Open onuryukselen opened this issue 3 months ago • 5 comments

Expected behavior and actual behavior

I anticipate receiving the file() block when the channel is empty. However, it looks like it returns queue channel and it gets consumed after one job submission. Are there any other way to check whether the channel is empty?

Steps to reproduce the problem

Here is the example code to reproduce the problem:

ch = channel.from( 1, 3, 5, 7 )
ch_empty_file = file("$baseDir/NO_FILE")
secondChannel = channel.empty()
testChn= secondChannel.ifEmpty(ch_empty_file)

process test {

input:
   val(a)
   file(b)

   script:
   """
   """
}

process test2 {

input:
  val(a)
  file(b)

  script:
  """
  """
}

workflow {
test (ch,ch_empty_file)
test2(ch,testChn)
}

Program output

N E X T F L O W ~ version 23.10.1 Launching /opt/run1372/test.nf [exotic_swartz] DSL2 - revision: 663225ff37 [1b/66845b] Submitted process > test (1) [8c/7fb0e1] Submitted process > test (2) [41/eda731] Submitted process > test2 (1) [f5/470f5e] Submitted process > test (3) [ac/f0ad1a] Submitted process > test (4)

It executes test2 only once. I was expecting 4 test2 jobs similar to test process.

Environment

  • Nextflow version: [23.10.1]
  • Java version: [openjdk 18.0.2-ea 2022-07-19]
  • Operating system: [Linux]
  • Bash version: (5.1.16)

onuryukselen avatar Apr 26 '24 21:04 onuryukselen

Hello, have you been able to replicate the issue? Do you think my expectation about the ifEmpty operator is correct?

onuryukselen avatar May 06 '24 15:05 onuryukselen

It turns out the docs are incorrect. They say that ifEmpty returns a value channel but actually it always returns a queue channel. I will review the operator return types more carefully and update the docs

bentsherman avatar May 06 '24 16:05 bentsherman

I see. Could you please suggest another way to check whether the channel is empty or not? I want to return Value Channel if channel is empty.

onuryukselen avatar May 06 '24 16:05 onuryukselen

I recommend that you explicitly combine the process inputs instead of relying on the implicit value channel behavior:

process test2 {
  input:
  tuple val(a), file(b)

  script:
  """
  """
}

workflow {
  test (ch,ch_empty_file)
  test2( ch.combine(testChn) )
}

bentsherman avatar May 06 '24 17:05 bentsherman

My goal is creating optional input for a process, so "combine" operator might not work for some cases. For example if secondChannel is empty, I want to use file("$baseDir/NO_FILE") so that process will work even secondChannel is empty.

Similar to https://nextflow-io.github.io/patterns/optional-input/ but I need to check channel. So is there any way to write condition like this:

testChn= secondChannel.ifEmpty("empty")
if (testChn.value == "empty"){
  testChn = file("$baseDir/NO_FILE")
}

onuryukselen avatar May 06 '24 19:05 onuryukselen