# How can I assign a default channel to another channel if it’s empty, while retaining its original content when it’s not empty, considering the default value is also a channel?

**URL:** <https://community.seqera.io/t/how-can-i-assign-a-default-channel-to-another-channel-if-it-s-empty-while-retaining-its-original-content-when-it-s-not-empty-considering-the-default-value-is-also-a-channel/1435>\
**Category:** Ask for help\
**Tags:** nextflow, platform\
**Created:** [November 17, 2024, 7:33am UTC](https://community.seqera.io/t/how-can-i-assign-a-default-channel-to-another-channel-if-it-s-empty-while-retaining-its-original-content-when-it-s-not-empty-considering-the-default-value-is-also-a-channel/1435 "2024-11-17T07:33:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kanna\_Dhasan](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/kanna_dhasan/32/1217_2.png) [@Kanna\_Dhasan](https://community.seqera.io/u/Kanna_Dhasan)\
**Post date:** [November 17, 2024, 7:33am UTC](https://community.seqera.io/t/how-can-i-assign-a-default-channel-to-another-channel-if-it-s-empty-while-retaining-its-original-content-when-it-s-not-empty-considering-the-default-value-is-also-a-channel/1435/1 "2024-11-17T07:33:32Z")

</div>

Hi all,  
I’m working on a larger workflow and have encountered a challenge. Here’s the scenario:  
I have a channel v1, which may sometimes be empty. In that case, I want to assign another channel sample (holding [SNP-2024-FT]) to v1. However, if v1 is not empty, it should retain its original value.  
Here’s the code snippet I tried:

```auto
groovy

params.sampleId = "SNP-2024-FT"

workflow {
    input_ch = Channel.of(
        tuple(params.sampleId)
    )
    sample = input_ch.map { it -> [it[0]] }
    sample.view()

    v1 = Channel.empty().ifEmpty(true).map { value ->
        if (value == true) {
            sample
        } else {
            value
        }
    }
    v1.view()
}

```

The issue is that this gives the error:` DataflowBroadcast around DataflowStream[?]`. It seems related to the fact that sample is itself a channel, not a static value, and the `ifEmpty(true)` only works with direct static values.  
Additionally, consider that Channel.empty() is an empty output channel from a process in this context, so the behavior of assigning it can be tricky. When I set a direct value like `ifEmpty("some value")`, it works, but since sample is an output channel, it’s always a channel rather than a direct value.  
Does anyone know how to work around this or handle such cases? Any advice or alternative approaches would be greatly appreciated!  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [November 18, 2024, 2:26am UTC](https://community.seqera.io/t/how-can-i-assign-a-default-channel-to-another-channel-if-it-s-empty-while-retaining-its-original-content-when-it-s-not-empty-considering-the-default-value-is-also-a-channel/1435/2 "2024-11-18T02:26:28Z")

</div>

Hi @Kanna_Dhasan,

I believe the snippet below will be helpful for you.

```Groovy
Channel
  .of(params.sample_id)
  .flatMap()                                                                                                                                                            
  .ifEmpty('Hello')
  .view()

```

Output

 ![Screenshot 2024-11-17 at 23.26.15](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/2X/6/60e40b7f143291290a5bb696ec695774dff1d67f.jpeg)

---

<div class="post-metadata">

**Author:** ![mahesh.binzerpanchal](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mahesh.binzerpanchal/32/253_2.png) [@mahesh.binzerpanchal](https://community.seqera.io/u/mahesh.binzerpanchal)\
**Post date:** [November 18, 2024, 10:26am UTC](https://community.seqera.io/t/how-can-i-assign-a-default-channel-to-another-channel-if-it-s-empty-while-retaining-its-original-content-when-it-s-not-empty-considering-the-default-value-is-also-a-channel/1435/3 "2024-11-18T10:26:17Z")

</div>

Here’s one way to solve the problem of if one channel is empty, use another one.

```nextflow
params.fill = false

workflow {
    ch_something = params.fill ? Channel.of(1..3) : Channel.empty()
    ch_backfill = Channel.of(4..6).toList()
    ch_something.collect().concat(ch_backfill).first().flatMap().view()
}

```

This uses the property that `collect` only emits a list if there’s something in the channel. Use `concat` to make sure the channel contents stay in order. and then use `first()` to take the first list from the concatenated channels. If `ch_something` is empty, there will be nothing, and the list from `ch_backfill` is taken.

---

<div class="post-metadata">

**Author:** ![Kanna\_Dhasan](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/kanna_dhasan/32/1217_2.png) [@Kanna\_Dhasan](https://community.seqera.io/u/Kanna_Dhasan)\
**Post date:** [November 19, 2024, 11:37am UTC](https://community.seqera.io/t/how-can-i-assign-a-default-channel-to-another-channel-if-it-s-empty-while-retaining-its-original-content-when-it-s-not-empty-considering-the-default-value-is-also-a-channel/1435/4 "2024-11-19T11:37:10Z")

</div>

Thank you so much for your response! Just to clarify, `ifEmpty` only accepts direct static values and not channels. However, in my case, if the channel is empty, I want to assign another channel. Here, `sample` is a value channel holding a single value

---

<div class="post-metadata">

**Author:** ![Kanna\_Dhasan](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/kanna_dhasan/32/1217_2.png) [@Kanna\_Dhasan](https://community.seqera.io/u/Kanna_Dhasan)\
**Post date:** [November 19, 2024, 11:38am UTC](https://community.seqera.io/t/how-can-i-assign-a-default-channel-to-another-channel-if-it-s-empty-while-retaining-its-original-content-when-it-s-not-empty-considering-the-default-value-is-also-a-channel/1435/5 "2024-11-19T11:38:50Z")

</div>

@mahesh.binzerpanchal Thank you so much for this workaround! It works exactly as I needed and solves my problem perfectly. I really appreciate your help! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/system/32/2402_2.png) [@system](https://community.seqera.io/u/system)\
**Post date:** [November 26, 2024, 11:39am UTC](https://community.seqera.io/t/how-can-i-assign-a-default-channel-to-another-channel-if-it-s-empty-while-retaining-its-original-content-when-it-s-not-empty-considering-the-default-value-is-also-a-channel/1435/6 "2024-11-26T11:39:16Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
