groovyexitnextflow

How to print (using view) and then exit in nextflow?


I am debugging a workflow, and cannot quickly find a way to execute view (to print the diagnostic output) and then exit (quit) the workflow.

If I use System.exit(0) or exit(0) in the code snippet below, it exits without printing the output from the view operator above.

And error and return additionally generate an error message.

If I do not use System.exit(0) or exit(0), etc operators, view prints the output OK. But then the execution continues for a few more minutes. I would like the execution to stop right then and there.

My current workaround is to use control+c to stop Nextflow from the command line. I am looking for a programmatic way to do this.

Nextflow script is executed like so:

time nextflow -quiet run /path/to/script_name.nf ...

The relevant chunk of code is:

workflow foo {
    take: input_data
    main:
        input_data.view { it -> "foo: input_data: ${it}" }
        // System.exit(0)
        // exit(0)
        // error("Exit1") // ERROR ~ Cannot invoke method getParent() on null object
        // return // ERROR ~ Cannot invoke method getParent() on null object
        // return(0) // ERROR ~ Cannot invoke method getParent() on null object
        
        // Code that I do not want to execute.

These did not help:


Solution

  • I think this is because exit(0) is a java level command that will hard stop everything regardless of what is running (i.e., it will exit before the asynchronous view{} is completed).

    I found in a quick test that return can work, otherwise, putting everything within if statements and have a params.diagnostics flag to only proceed with processes given the correct flag is a slightly more specific approach.

    Here is how return worked for me:

    nextflow.enable.dsl=2
    
    params.run_step = true
    
    workflow {
        input_ch = Channel.from("a", "b", "c")
        foo(input_ch)
    }
    
    workflow foo {
        take:
        input_data
    
        main:
        input_data.view { it -> "foo: input_data: ${it}" }
        return
        input_data.view { it -> "foo2: input_data: ${it}" }
    }
    

    gives the output

    nextflow run example.nf
    N E X T F L O W  ~  version 23.05.0-edge
    Launching `example.nf` [astonishing_ekeblad] DSL2 - revision: f60292630e
    foo: input_data: a
    foo: input_data: b
    foo: input_data: c
    

    But when I remove the return it gives:

    nextflow run example.nf
    N E X T F L O W  ~  version 23.05.0-edge
    Launching `example.nf` [elated_planck] DSL2 - revision: 5c657dee81
    foo2: input_data: a
    foo: input_data: a
    foo: input_data: b
    foo: input_data: c
    foo2: input_data: b
    foo2: input_data: c