# Using workflow outputs to publish files from a nested map channel

**URL:** <https://community.seqera.io/t/using-workflow-outputs-to-publish-files-from-a-nested-map-channel/2428>\
**Category:** Ask for help\
**Tags:** nextflow\
**Created:** [October 27, 2025, 1:28pm UTC](https://community.seqera.io/t/using-workflow-outputs-to-publish-files-from-a-nested-map-channel/2428 "2025-10-27T13:28:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_D](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/nick_d/32/2125_2.png) [@Nick\_D](https://community.seqera.io/u/Nick_D)\
**Post date:** [October 27, 2025, 1:28pm UTC](https://community.seqera.io/t/using-workflow-outputs-to-publish-files-from-a-nested-map-channel/2428/1 "2025-10-27T13:28:00Z")

</div>

I have the following workflow:

```auto
#!/usr/bin/env nextflow
nextflow.enable.dsl=2

process ALIGN {

  input:
  val(sample)

  output:
  tuple val(sample), path("*.bam")

  script:
  """
  touch ${sample}-1.bam
  touch ${sample}-2.bam
  """
}

process CALL_VARIANTS {

  output:
  path ("*.vcf.gz")

  script:
  """
  touch final.vcf.gz
  """
}

workflow {

  # Run processes.
  Channel.of('SAMPLE1', 'SAMPLE2')
  | ALIGN

  CALL_VARIANTS()

  # Create output manifest channel.
  ALIGN.out
  .map { sample, bam ->
    [
      sample: sample,
      bam: bam
    ]
  }
  .collect()
  .merge(CALL_VARIANTS.out) { per_sample, jointcall -> [per_sample: per_sample, jointcall: jointcall]}
  .dump(pretty: true, tag: 'results_ch')
  .set { results_ch }
}

```

When I run `nextflow run main.nf -dump-channels results_ch`, I get the following channel dump:

```auto
[DUMP: results_ch] {
    "per_sample": [
        {
            "sample": "SAMPLE1",
            "bam": [
              "/path/to/SAMPLE1-1.bam",
              "/path/to/SAMPLE1-2.bam"
            ]
        },
        {
            "sample": "SAMPLE2",
            "bam": [
              "/path/to/SAMPLE2-1.bam",
              "/path/to/SAMPLE2-2.bam"
            ]
        }
    ],
    "jointcall": "/path/to/final.vcf.gz"
}

```

I would like to use this channel in workflow outputs to create a single json manifest/index file, and to publish outputs to the create target directories. However, this nested structure presents a problem when attempting to publish outputs because the `result.per_sample.bam >> "bam/"` statement expects each entry in the list to be a filepath, not another list:

```auto
workflow {

  ...workflow continued from above...

  publish:
  results = results_ch
}

output {
  results {
    path { result ->
      result.per_sample.bam >> "bam/"
    }
    index {
      path "manifest.json"
    }
  }
}

```

```auto
ERROR ~ Cannot cast object '["/path/to/SAMPLE1-1.bam", "/path/to/SAMPLE1-2.bam"]' with class 'java.util.ArrayList' to class 'java.nio.file.Path' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.nio.file.Path(sun.nio.fs.UnixPath, sun.nio.fs.UnixPath)

```

The problem is that `path { result -> result.per_sample.bam >> "bam/" }` yields this output structure:

```auto
[
  [
    "/path/to/SAMPLE1-1.bam",
    "/path/to/SAMPLE1-2.bam"
  ],
  [
    "/path/to/SAMPLE2-1.bam",
    "/path/to/SAMPLE2-2.bam"
  ]
]

```

instead of the structure I need, which is:

```auto
[
  "/path/to/SAMPLE1-1.bam",
  "/path/to/SAMPLE1-2.bam"
]
[
  "/path/to/SAMPLE2-1.bam",
  "/path/to/SAMPLE2-2.bam"
]

```

In the main body of the workflow, I would use the `flatMap()` operator to obtain independent lists from a list of lists:

```auto
results_ch
.flatMap { result ->
  result.per_sample.bam
}

```

```auto
[
  "/path/to/SAMPLE1-1.bam",
  "/path/to/SAMPLE1-2.bam"
]
[
  "/path/to/SAMPLE2-1.bam",
  "/path/to/SAMPLE2-2.bam"
]

```

but I don’t think I can use `flatMap()` in the output block. Is there another way to achieve this?

---

<div class="post-metadata">

**Author:** ![bentsherman](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bentsherman/32/52_2.png) [@bentsherman](https://community.seqera.io/u/bentsherman)\
**Post date:** [October 27, 2025, 5:22pm UTC](https://community.seqera.io/t/using-workflow-outputs-to-publish-files-from-a-nested-map-channel/2428/2 "2025-10-27T17:22:39Z")

</div>

It looks like you are doing `result.per_sample.bam` but this is not correct because `per_sample` is a list of maps where each map has a `bam` property

You can use the `collectMany` method for lists, it works the same way as `flatMap:`

```auto
result.per_sample.collectMany { s -> s.bam } >> "bam/"

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Nater](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/alexander_nater/32/552_2.png) [@Alexander\_Nater](https://community.seqera.io/u/Alexander_Nater)\
**Post date:** [October 28, 2025, 9:37am UTC](https://community.seqera.io/t/using-workflow-outputs-to-publish-files-from-a-nested-map-channel/2428/3 "2025-10-28T09:37:05Z")

</div>

As a side note, the use of `merge` here is a disaster waiting to happen.

---

<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 4, 2025, 9:37am UTC](https://community.seqera.io/t/using-workflow-outputs-to-publish-files-from-a-nested-map-channel/2428/4 "2025-11-04T09:37:32Z")

</div>

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