Channel enumerate

Hello,

Maybe it is something available in the underlying Groovy language, but not something I could find through a quick web search.

For my use case, I need to be able to “enumerate” a channel, ie simply get the index/position of the value in the channel along with the value itself:

words = channel.of('hello', 'ciao')
words.enumerate()

> (1, 'hello')
> (2, 'ciao')

Any pointer would be appreciated!

Theo

Hi @TheoG,

Good question - there’s no enumerate(), but Seqera Co-Scientist gave me a quick snippet to get to the behaviour you’re after:

words = channel.of('hello', 'ciao')

words
    .collect()                                // → single ordered list ['hello','ciao']
    .flatMap { items -> items.withIndex(1) }  // → [['hello',1], ['ciao',2]]
    .map { value, index -> [index, value] }   // → [1,'hello'], [2,'ciao']
    .view { index, value -> "($index, $value)" }

Note that this will consume the channel. Also .collect() waits for the whole channel to be in place before running.

I hope that helps!

Phil

Thanks @ewels , that is perfect!

To complement the already complete answer for others (and future self :joy: ), the withIndex method comes from Groovy, documentation here Iterable (Groovy JDK enhancements) . Basically, the use is withIndex(offset) on an interable, and returns (value, index) in order, with index starting at offset.

withIndex() is documented here in the Nextflow standard library. You may find equivalent methods in the Groovy docs, but Groovy methods and types are not guaranteed to be supported by Nextflow, so make sure to consult the Nextflow docs first.

One caveat to Phil’s answer: if you want to track the index alongside each channel value, you should apply withIndex() at a point where the ordering is consistent. Doing collect() -> withIndex() on a channel will give you a different ordering on every run. So in your example, you would need to find something upstream of words that is ordered (e.g. a samplesheet that you just loaded as a list of rows) and add the index there.

Ha! Thanks Ben for pointing to this page, much appreciated!

A note here: this is the first time I encounter this documentation page, despite spending quite some time searching. I kindly suggest some SEO, as an example, this page does not appear (at least on my setup) when googling nextflow "withIndex".

Thanks for the precisions. For my use case (disambiguating outputs), I don’t need to enforce ordering, but the precision could indeed be important for others.