2

我试图弄清楚如何检查通道是否为空。

例如,我有两个进程。第一个进程仅在设置了参数/标志的组合时运行,如果是,则还检查来自另一个进程的输入文件(通过通道输入)是否不为空,然后它为第二个进程创建一个新的输入文件(以最终替换默认的)。作为一个简化的例子:

.....
.....

// create the channel here to force nextflow to wait for the first process
_chNewInputForProcessTwo = Channel.create()

process processOne {
  when:
    params.conditionOne && parameters.conditionTwo
  input:
      file inputFile from _channelUpstreamProcess
 output:
      file("my.output.file") into _chNewInputForProcessTwo
  script:
    """
    # check if we need to produce new input for second process (i.e., input file not empty)
    if [ -s ${inputFIle} ]
      then
          <super_command_to_generate_new_fancy_input_for_second_process> > "my.output.file" 
      else
          echo "No need to create new input"
    fi
   """
}

// and here I would like to check if new input was generated or leave the "default" one
_chInputProcessTwo = Channel.from(_chNewInputForProcessTwo).ifEmpty(Channel.value(params.defaultInputProcessTwo))

process secondProcess {
  input:
      file inputFile from _chInputProcessTwo
......
......
etc.

当我尝试使用这种方法运行时,它会失败,因为通道_chNewInputForProcessTwo包含DataflowQueue(queue=[])因此,实际上并不是空的。

我已经尝试了几件事来查看文档以及 google 组和 gitter 上的线程。试图将其设置为空,但随后它抱怨我试图使用该频道两次。放create().close()

有没有一种干净/合理的方法来做到这一点?我可以使用值通道来做到这一点,并让第一个进程输出一些字符串stdout以供第二个进程拾取和检查,但这对我来说似乎很脏。

任何建议/反馈表示赞赏。先感谢您!

马吕斯

4

1 回答 1

1

最好避免尝试检查通道是否为空。如果您的频道可能为空,并且您需要在频道中设置默认值,则可以使用ifEmpty运算符来提供一个。请注意,单个值隐含地是一个值通道。我想你需要的只是:

myDefaultInputFile = file(params.defaultInputProcessTwo)

chInputProcessTwo = chNewInputForProcessTwo.ifEmpty(myDefaultInputFile)

Channel.create()此外,通常不需要调用。

于 2020-09-24T15:07:45.390 回答