Nextflow "flatten" analogous to "unnest" in dplyr

Viewed 142

Nextflow, for now not using DSL2.


data =  [
    [1, 'a', ['a1', 'a2']],
    [2, 'b', ['b1', 'b2', 'b3']],
    [3, 'c', ['c1']]
]

ch = Channel.from(data)

Is there any way to get from this to a channel with a total of 6 rows, with contents

[
    [1, 'a', 'a1'],
    [1, 'a', 'a2'],
    [2, 'b', 'b1'],
    [2, 'b', 'b2'],
    [2, 'b', 'b3'],
    [3, 'c', 'c1']
]

Basically, unnesting the third item per tuple in the channel. I imagine it could work by first repeating the non-nested items for the length of the nested list and then flattening the result, but I don't know how I would do this.

1 Answers

You can use the transpose operator for this:

data =  [
    [1, 'a', ['a1', 'a2']],
    [2, 'b', ['b1', 'b2', 'b3']],
    [3, 'c', ['c1']]
]

Channel.from(data).transpose().view()

Results:

[1, a, a1]
[1, a, a2]
[2, b, b1]
[2, b, b2]
[2, b, b3]
[3, c, c1]
Related