Pablo
Pablo

Reputation: 1003

How to enumerate files in channel to use `collectFile`

I am trying to enumerate files in a Channel to rename them before using collectFile:

files.flatten().merge(Channel.fromList([1, 2, 3, 4])).collectFile(storeDir: "$SCRATCH/intermediate") {
    item -> ["data${item[1]}.csv", item[0].text]
}

But the latest documentation says that the merge operator for channels is deprecated, but does not point to any alternative that should be used. What can I use instead of merge?

Upvotes: 1

Views: 908

Answers (1)

Steve
Steve

Reputation: 54502

The migration notes say to use the join operator instead. If your inputs were lists, you could do something like:

def indexedChannel( items ) {
    return Channel.from( items.withIndex() ).map { item, idx -> tuple( idx, item ) }
}

ch1 = indexedChannel( [ 15, 20, 21 ] )
ch2 = indexedChannel( [ 'a', 'b', 'c' ] )
ch3 = indexedChannel( [ 1, 2, 3 ] )

ch1
    .join( ch2 )
    .join( ch3 )
    .view()

Results:

[0, 15, a, 1]
[1, 20, b, 2]
[2, 21, c, 3]

However, the merging/joining of two channels is unnecessary to enumerate. Just use the map operator:

def c = 1
Channel
    .fromPath( './data/*.txt' )
    .map { tuple( it, c++ ) }
    .collectFile(storeDir: "$SCRATCH/intermediate") { fn, count ->
        ["data${count}.csv", fn.text]
    }

Upvotes: 2

Related Questions