Collecting output from different processes to run another process

Hi,
I am quite new to nextflow. I am trying to parse a list of outputs from other processes to a new process (basically recycling the process). I thought it would be possible to do it like this, however I keep getting errors. Can someone maybe help me further.

#!/usr/bin/env nextflow

process foo {
    output:
    path("foo.txt"), emit: foo

    script:
    """
    echo "foo" > foo.txt
    """
}

process bar {
    output:
    path("bar.txt"), emit: bar

    script:
    """
    echo "bar" > bar.txt
    """
}

process baz {
    output:
    path("baz.txt"), emit: baz

    script:
    """
    echo "baz" > baz.txt
    """
}

process add_some {
    publishDir "/home/zeddafir/scripts/nextflow/test_next/", mode: 'copy', overwrite: true

    input:
    path(x)

    output:
    path("${x.baseName}_some.txt"), emit: some

    script:
    """
    echo "some" >> ${x}

    cp ${x} ${x.baseName}_some.txt
    """
}

workflow {
    // Run processes and collect their outputs
    foo()
    bar()
    baz()

    // Collect outputs from processes
    all_ch = Channel.from(foo.out, bar.out, baz.out)

    // Run process add_some on each output
    add_some(all_ch)
}

Hi @zaka-edd

Next time, please share what errors you’re running into.
The first thing that caught my attention is that you’re using the Channel.from channel factory. As you can see in the official documentation, the from channel factory is deprecated. You should use Channel.of or Channel.fromList instead.

The second thing is that you shouldn’t have a channel of a channel. At the same time, you want a channel to contain the outputs of all previous processes. You need to use a channel operator for that. Instead of

    // Collect outputs from processes
    all_ch = Channel.from(foo.out.foo, bar.out.bar, baz.out.baz)

    // Run process add_some on each output
    add_some(all_ch)

You should do

    // Collect outputs from processes
    all_ch = foo.out.concat(bar.out, baz.out)

    // Run process add_some on each output
    add_some(all_ch)

Having it this way, you’ll probably see an output similar to the one below:

CleanShot 2024-07-23 at 15.43.06@2x

:stop_sign: Another tip: Avoid absolute paths. Instead of publishDir "/home/zeddafir..." have publishDir "test_next".

1 Like

Hi @mribeirodantas,

Thank you for clarifying the issue. Your input was really helpful :smile:

1 Like

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