How to sort tuples

Hello, I am grouping a set of samples by sample, which I do by the following:

SAMTOOLS_MERGE.out.bam.mix(CREATEREADNUM.out.readnum).mix(CLIPPER.out.bed)
.groupTuple(by: [0])
.map {
meta,result →
[meta, meta.sample, result[0], result[1], result[2] ]}
.groupTuple(by: [1])
.map {
result →
[[id:result[0].sample[0],sample:result[0].sample[0],single_end:result[0].single_end[0]],result[2],result[3],result[4]]}
.set { ch_bamreadbed }

and I end up with this, which is what I want:

[[id:test1, sample:test1, single_end:false], [test1_signal.bam,test1_background.bam], [test1_signal.readnum.txt, test1_background.readnum.txt], [test1_signal.clip.peakClusters.bed, test1_background.clip.peakClusters.bed]]

However, I would like to ensure that a certain order is always preserved: signal always goes before background (or viceversa). What command could help me with that? I could not find a sort and I am not sure how I would apply it here.

Hi @ramirobarrantes. I have run into something similar before and think that maybe the sort argument of groupTuple() might help. Several sorting criteria are included by default, but you can also create custom sorting criteria if necessary.

Here’s a link to the documentation ( Operators — Nextflow documentation) if you scroll down a bit you should be able to see the sorting options.

Please let me know if this is helpful!
Carson Miller

1 Like

Adding to what @CarsonJM explained, this Nextflow snippet may help you achieve what you’re looking for :wink:

1 Like

@mribeirodantas I will definitely be saving this snipped for future reference :sweat_smile:

1 Like

THis is excellent, thank you!!!

1 Like