Search code examples
nextflow

How to separate nested lists by element index in nextflow?


I need to separate a channel with complex list:

The channel contains a nested list like this one: [1,['a','b'],['c','d']],[2,['a','b'],['e','f']]

How to create two new channels so that channel one contains [[1,a,c],[2,a,e]] and channel two - [[1,b,d],[2,b,f]], which basically means separating each original nested list element in two (e.g. [1,[a,b],[c,d]] becomes [1,a,c],[1,b,d]) and then combining by the second inner element ([a,b] in both cases).

So far i have managed to create a very primitive solution:

Channel
       .from([1,['a','b'],['c','d']],[2,['a','b'],['e','f']])
       .multiMap { 
              ch_one: tuple (it[0], it[1][0], it[2][0])
              ch_two: tuple (it[0], it[1][1], it[2][1])
              }     
       .set { to_mix }
to_mix.ch_one.subscribe {println it}
to_mix.ch_two.subscribe {println it}

Is there a better way to do it?


Solution

  • If you're only going to mix the outputs anyway, I think you can just use the transpose operator for this. Note that the index (zero based) of the element to be transposed can be specified using the by parameter. For example:

    workflow {
           
        Channel
           .from([1,['a','b'],['c','d']],[2,['a','b'],['e','f']])
           .transpose()
           .set { transposed }
    
        transposed.view()
    }
    

    Results:

    $ nextflow run main.nf 
    N E X T F L O W  ~  version 23.04.1
    Launching `main.nf` [naughty_agnesi] DSL2 - revision: d3d72e875e
    [1, a, c]
    [1, b, d]
    [2, a, e]
    [2, b, f]