'NextFlow: How to fail if channel is empty ( .ifEmpty() )

I'd like for my NextFlow pipeline to fail if a specific channel is empty because, as is, the pipeline will continue as though nothing is wrong, but the process depending on the channel never starts. The answer to a related post states that we generally shouldn't check if a channel is empty, but I'm not sure how else to handle this.

The issue I'm having in the below example is that it always fails, but the process is called if I comment out the .ifEmpty() statement.

Here's a basic example:

/*
 * There are .cram files in this folder
 */
params.input_sample_folder = 'path/to/folder/*' 
samples = Channel.fromPath(params.input_sample_folder, checkIfExists: true)
                 .filter( ~/.*(\.sam|\.bam|\.cram)/ )
                 .ifEmpty( exit 1,
                  "ERROR: Did not find any samples in ${params.input_sample_folder})

workflow{
    PROCESS_SAMPLES( samples )
}

Ultimate questions:

  1. My guess is that the channel does not fill immediately. Is that true? If so, when does it fill?
  2. How should I handle this situation? I want to fail if the channel doesn't get populated. e.g., I was surprised to learn that the channel remains empty if I only provide a folder path without a glob/wildcard character (/path/to/folder/; no * or *.cram, etc.). I don't think I can handle it in the process itself, because the process never gets called if the channel is legitimately empty.

Really appreciate your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source