Subworkflow with optional input and chained processes

I’m wondering what is the best aproach for doing this:

I have a subworkflow with three steps (processes). Lets call them P1, P2 and P3.

The workflow takes 2 inputs (i1, i2) and i2 may be an empty channel.

P1 is allways executed with i1 as input and produces o1.
If i2 is not empty then P2 is executed with o1 and i2 as inputs and produces o2.
If i2 is not empty then P3 is executed with o2 as input else P3 is executed with o1 as input.

I have tryed several things but I feel I’m overcomplicating everything.

If you don’t know at the beginning of the run if i2 is emtpy or not, the easiest way to implement this would be to first collect and then concat o2 and o1. You can then take the first element of the concatenated channel and flatten it again.

1 Like

OMG!! So easy and so many hours thinking about wrong solutions in my side… Thanks!!

For the sake of completeness the solution would be something like:

workflow w {

take:
  i1
  i2 //This may be an empty channel

main:
  o1 = p1(i1)
  o2 = p2(o1,i2)
  list_o1 = o1.collect()
  list_o2 = o2.collect()
  p3_input = list_o2.concat(list_o1).first()
  p3(p3_input)
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.