Ahkam
Ahkam

Reputation: 13

How to subset a nextflow channel using another channel containing only one unknown value

I have a channel which emit as follows:

[[A, B, C, D], 1]

[[A, B, C, D], 2]

[[A, B, C], 3]

[[A, B, V], 4]

[[A], 5]

[[A1, B1, D], 7]

[[A1, B1, D], 8]

I have another parameter defined by the user. The parameter could be a single number or List of numbers. For example, 4 or [4,7,8]. Based on the user parameter value, I want to subset the channel matching the values of the above channel and pass it to a downstream process. For example, the subsetted channel must only retain [[A, B, V], 4] from the channel, if user input is 4. Similarly, if user input is [7, 8], I would like to retain only [[A1, B1, D], 7] [[A1, B1, D], 8] in the above channel. I tried using combine, filter and branch operator but couldn't get the desired results.

first_ch.combine(par_ch)
    .branch{ a, b, c, d  ->
        controls: b == d
            return [a, b]
        others: true
            return [a, b]
            }
    .view { }

However, the following works:

first_ch.combine(par_ch)
    .filter{ it[1] == '4' }

But when it is passed as params or channel, I couldn't able to get the desired result.

Upvotes: 1

Views: 46

Answers (1)

Steve
Steve

Reputation: 54552

I think you can just use the join operator for this, for example:

params.input = '4,7,8'


workflow {

    def input_string = params.input.toString()

    par_ch = Channel.fromList(
        input_string.tokenize(',')*.toInteger()
    )

    first_ch = Channel.of(
        [['A', 'B', 'C', 'D'], 1],
        [['A', 'B', 'C', 'D'], 2],
        [['A', 'B', 'C'], 3],
        [['A', 'B', 'V'], 4],
        [['A'], 5],
        [['A1', 'B1', 'D'], 7],
        [['A1', 'B1', 'D'], 8],
    )

    first_ch
        .map { keys, val -> tuple( val, keys ) }
        .join( par_ch )
        .map { val, keys -> tuple( keys, val ) }
        .view()
}

Results:

$ nextflow run main.nf --input 7,8

 N E X T F L O W   ~  version 24.10.4

Launching `main.nf` [gloomy_pike] DSL2 - revision: 8fa7a5ddbc

[[A1, B1, D], 7]
[[A1, B1, D], 8]

Upvotes: 1

Related Questions