# How To emit nextflow subworkflow outputs ?!

**URL:** <https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576>\
**Category:** Ask for help\
**Created:** [March 19, 2024, 9:14am UTC](https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576 "2024-03-19T09:14:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Firas\_Zemzem](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/firas_zemzem/32/892_2.png) [@Firas\_Zemzem](https://community.seqera.io/u/Firas_Zemzem)\
**Post date:** [March 19, 2024, 9:14am UTC](https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576/1 "2024-03-19T09:14:52Z")

</div>

Hi Folks , I Am trying to write nextflow subworkflow : for :  
1- Trimming  
2- Aligninig trimmed Reads  
And unfortunately I didn’t know how to emit Trimmed reads which derived from sub-workflow n°1 to be used into the next sub-workflow n°2 ( Alignment )  
Any Advice !  
**main.nf**  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/961bf0c1cc211a4795cb214f898a01070942c2ec.png)

* * *

**Trimmming Subworkflow**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/e65bd2bfbbc534ea8aa0dad20b13ea5d53eb3063.png)

Here my Triming **subworkflowoutputs** 👇

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/9aeb4a2b8f6273f4341945416264bbc6d9ca0655.png)

* * *

**Alignment Subworkflow**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/840836293ef3f4b4cce954162a107a712d528512.png)  
**ERROR**  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/8661fc8ad12daebd5eedbe9df2bd05a6606a0185.png)

---

<div class="post-metadata">

**Author:** ![Firas\_Zemzem](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/firas_zemzem/32/892_2.png) [@Firas\_Zemzem](https://community.seqera.io/u/Firas_Zemzem)\
**Post date:** [March 19, 2024, 9:21am UTC](https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576/2 "2024-03-19T09:21:55Z")

</div>

@mribeirodantas what do suggest please 🤔

---

<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:** [March 19, 2024, 2:57pm UTC](https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576/3 "2024-03-19T14:57:56Z")

</div>

You have to call the `TRIMMED` subworkflow so that there’s an output channel to be consumed by the `Alignment` process. It’s what the error message is saying. Shouldn’t you have `trimmedReads` where you typed `TRIMMED.out.trimmedR`?

---

<div class="post-metadata">

**Author:** ![Firas\_Zemzem](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/firas_zemzem/32/892_2.png) [@Firas\_Zemzem](https://community.seqera.io/u/Firas_Zemzem)\
**Post date:** [March 19, 2024, 3:08pm UTC](https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576/4 "2024-03-19T15:08:26Z")

</div>

Yes I already create that channel but . Alignment subworkfkow don’t recognise it  
I want to align Trim.outputs ( process under TRIMMD Subworkfkow)

**TRIMMED SUBWORKFKOW**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/e65bd2bfbbc534ea8aa0dad20b13ea5d53eb3063.png)

**Alignment subworkfkow**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/840836293ef3f4b4cce954162a107a712d528512.png)

---

<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:** [March 19, 2024, 4:46pm UTC](https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576/5 "2024-03-19T16:46:49Z")

</div>

Pictures aren’t the best way to share code. Please, share code blocks so I can know if these are all in the same file, for example. In the code you showed, I don’t see the subworkflows being called.

---

<div class="post-metadata">

**Author:** ![Firas\_Zemzem](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/firas_zemzem/32/892_2.png) [@Firas\_Zemzem](https://community.seqera.io/u/Firas_Zemzem)\
**Post date:** [March 21, 2024, 4:39pm UTC](https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576/6 "2024-03-21T16:39:31Z")

</div>

@mribeirodantas No they are not in the same file, They are organised like that :  
├── data  
│ ├── SRR519926\_1.fastq  
│ ├── SRR519926\_2.fastq  
│ ├── SRR519930\_1.fastq  
│ └── SRR519930\_2.fastq  
├── main.nf  
├── modules  
│ ├── Alignment.nf  
│ ├── bwa\_index.nf  
│ ├── FASTQC.nf  
│ └── TRIM.nf  
├── Ref\_genome  
│ └── Ecoli.fa  
├── samplemain.csv  
└── subworkflows  
├── alignmentsub.nf  
├── RawQC\_sub.nf  
└── Trim&QC\_Sub.nf  
**Main.nf** 👇

```auto
#!/usr/bin/env nextflow

// Params 
params.reads = "$projectDir/samplemain.csv"
params.reference = "$projectDir/Ref_genome/*.{fasta,fa}"
params.outdir ="$projectDir/outdir"
params.cpus = 3

//Channels

reads = Channel.fromPath(params.reads, checkIfExists: true)
               .splitCsv(header: true)
               .map { row -> tuple(row.sampleId, file(row.read1), file(row.read2)) } 

Channel.fromPath(params.reference)
       .first()
       .set {ref_file}       

      
// Sub-Workflows 

// Raw Reads QC :: nextflow 

include { RAW_QC } from "./subworkflows/RawQC_sub.nf" 

workflow Raw {
 	RAW_QC (reads) 
 	} 

// Trimming + QC check 

include { TRIMMED } from "./subworkflows/Trim&QC_Sub.nf"  

workflow Trim { 
	TRIMMED (reads) 
	}          
    
// Mapping to Reference 
 
include { ALIGNMENT } from "./subworkflows/alignmentsub.nf"
 
workflow Align { 
	ALIGNMENT (ref_file,TRIMMED)
	}

```

**Trimming Sub-Workflow**

```auto
/*
========================================================================================
   Trimming Sub-Workflow
========================================================================================
*/

include {Trimming} from '../modules/TRIM.nf' // Check raw reads Quality 
include {QC_Trim} from '../modules/TRIM.nf' // Multiqc for raw reads
include {Multiqc_Trim} from '../modules/TRIM.nf' // Multiqc for raw reads

reads = Channel.fromPath(params.reads, checkIfExists: true)
               .splitCsv(header: true)
               .map { row -> tuple(row.sampleId, file(row.read1), file(row.read2)) } 

workflow TRIMMED {

    take:
        reads

    main:
       Trimming(reads)    
       QC_Trim(Trimming.out.triMmed)  
       Multiqc_Trim(QC_Trim.out.collect()) 
    
   emit: 
      trimmedR = Trimming.out.triMmed.collect()
      trimmedR.view()
}

```

**Alignment Sub-Workflow**

```auto
/*
========================================================================================
  Alignment Sub-Workflow
========================================================================================
*/

include {index } from '../modules/bwa_index.nf' // Index the reference
include {Alignment	} from '../modules/Alignment.nf' // Alignment based-reference 
include {TRIMMED	} from '../subworkflows/Trim&QC_Sub.nf' // Trimming subworkflow 
  
  
/* channels */

Channel.fromPath(params.reference)
    .first()
    .set { ref_file }
    

workflow ALIGNMENT { 
    take:
        ref
        trimmedReads
    main :
    index(ref_file)
    Alignment(ref_file,index.out.indexes,TRIMMED.out.trimmedR)

}

```

**ERROR**

```auto
nextflow run main.nf -entry Align
N E X T F L O W ~ version 23.04.1
Launching `main.nf` [pedantic_jang] DSL2 - revision: 8bc364eb64
Access to 'TRIMMED.out' is undefined since the workflow 'TRIMMED' has not been invoked before accessing the output attribute

 -- Check script './subworkflows/alignmentsub.nf' at line: 27 or see '.nextflow.log' file for more details

```

---

<div class="post-metadata">

**Author:** ![Adam\_Talbot](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/adam_talbot/32/62_2.png) [@Adam\_Talbot](https://community.seqera.io/u/Adam_Talbot)\
**Post date:** [April 4, 2024, 11:38am UTC](https://community.seqera.io/t/how-to-emit-nextflow-subworkflow-outputs/576/7 "2024-04-04T11:38:10Z")

</div>

`trimmedReads` isn’t a variable you can access everywhere, you need to use the output of TRIMMED in the input of ALIGNMENT. Here I have combined you examples to demonstrate this. See the anonymous workflow at the bottom:

```auto
include {Trimming } from '../modules/TRIM.nf' // Check raw reads Quality 
include {QC_Trim } from '../modules/TRIM.nf' // Multiqc for raw reads
include {Multiqc_Trim} from '../modules/TRIM.nf' // Multiqc for raw reads
include {index } from '../modules/bwa_index.nf' // Index the reference
include {Alignment } from '../modules/Alignment.nf' // Alignment based-reference 
include {TRIMMED } from '../subworkflows/Trim&QC_Sub.nf' // Trimming subworkflow 
  

workflow TRIMMED {

    take:
        reads

    main:
       Trimming(reads)    
       QC_Trim(Trimming.out.triMmed)  
       Multiqc_Trim(QC_Trim.out.collect()) 
    
   emit: 
      trimmedR = Trimming.out.triMmed.collect()
}

workflow ALIGNMENT { 
    take:
        ref
        trimmedReads
    main:
        index(ref_file)
        Alignment(ref_file,index.out.indexes,TRIMMED.out.trimmedR)

}

workflow {
    reads = Channel.fromPath(params.reads, checkIfExists: true)
               .splitCsv(header: true)
               .map { row -> tuple(row.sampleId, file(row.read1), file(row.read2)) } 

    ref_file = Channel.fromPath(params.reference)
                .first()

    TRIMMED(reads)
    ALIGNMENT(ref_file, TRIMMED.out.trimmedR)
}

```
