5.8 EXERCISE 2

  • Change the pipeline for producing files instead of standard output:
    • You can write another process to handle the fact that you have a list in the workflow2 (workflow second_pipeline).
    • You need also to specify within the workflow what to output using the emit keyword.
Answer


#!/usr/bin/env nextflow

nextflow.enable.dsl=2

str = Channel.from('hello', 'hola', 'bonjour')

process printHello {

   tag { "${str_in}" }

   input:        
   val str_in

   output:        
   path("${str_in}.txt")

   script:        
   """
   echo ${str_in} in Italian is ciao > ${str_in}.txt
   """
}

process printHello2 {

   tag { "${str_in}" }

   input:        
   val str_in

   output:        
   path("cheers.txt")

   script:
   """
   echo ${str_in.join(', ')} in Italian are ciao > cheers.txt
   """
}

/*
 * A workflow can be named as a function and receive an input using the take keyword
 */

workflow first_pipeline {
    take: str_input
    main:
    out = printHello(str_input)
    emit: out
}


/*
 * You can re-use the previous processes an combine as you prefer
 */

workflow second_pipeline {
    take: str_input
    main:
    out = printHello2(str_input.collect())
    emit: out
}

/*
 * You can then invoke the different named workflows in this way
 * passing the same input channel `str` to both  
 */

workflow {
    out1 = first_pipeline(str)
    out2 = second_pipeline(str)
}
  • Change the pipeline to use only one process to handle both the cases (either one element or a list).
    You can choose the elements from a list using the positional keys (i.e. list[0], list[1], etc…)
Answer


#!/usr/bin/env nextflow

nextflow.enable.dsl=2

str = Channel.from('hello', 'hola', 'bonjour')

process printHello {

   tag { "${str_in}" }

   input:        
   val str_in

   output:        
   path("${str_in[0]}.txt")

   script:        
   """
   echo ${str_in} in Italian is ciao > ${str_in[0]}.txt
   """
}

/*
 * A workflow can be named as a function and receive an input using the take keyword
 */

workflow first_pipeline {
    take: str_input
    main:
    out = printHello(str_input)
    emit: out
}


/*
 * You can re-use the previous processes an combine as you prefer
 */

workflow second_pipeline {
    take: str_input
    main:
    out = printHello(str_input.collect())
    emit: out
}

/*
 * You can then invoke the different named workflows in this way
 * passing the same input channel `str` to both  
 */

workflow {
    out1 = first_pipeline(str)
    out2 = second_pipeline(str)
}