channel - 检查nextflow channel 是否为空

标签 channel nextflow

我正在尝试找出如何检查 channel 是否为空。

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

.....
.....

// 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.

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

我尝试了一些查看文档以及 google groups 和 gitter 上的线程的方法。试图将其设置为空,但随后它提示我试图使用该 channel 两次。放置 create().close()

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

如有任何建议/反馈,我们将不胜感激。预先感谢您!

马吕斯

最佳答案

最好避免尝试检查 channel 是否为空。如果您的 channel 可能为空,并且您需要在 channel 中使用默认值,则可以使用 ifEmpty运营商提供一份。请注意,单个值隐式为 value channel 。我认为您需要的是:

myDefaultInputFile = file(params.defaultInputProcessTwo)

chInputProcessTwo = chNewInputForProcessTwo.ifEmpty(myDefaultInputFile)

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

关于channel - 检查nextflow channel 是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64042860/

相关文章:

go - 如果 sync.WaitGroup 类型的 Wait() 方法阻塞,因此不是异步的,为什么要使用它?

matlab - 如何在 MATLAB 中读取 2 channel 音频文件

pipeline - 如何将流程输出发送到 Nextflow 中的多个 channel ?

workflow - 限制 Nextflow 工作流程中的单个进程的数量

nextflow - 如何根据映射到染色体的读取将bam文件拆分为单独的bams

go - 如何从按特定顺序执行的 N 个 goroutine 中收集值?

select - 当涉及多个 channel 时,select 如何工作?

go - 合并两个关闭 channel

Nextflow:输出不是 "found",尽管设置了 publishDir

下一个流 : Is it possible to tranform a queue channel to a value channel?