if 语句 select 输入块中带有 nextflow 的通道

if statement to select a channel in input block with nextflow

我目前正在编写我的第一个 nextflow 管道,我需要 运行 参数函数中的不同进程。

事实上,我想在一个过程中,select输入来自的通道。 我已经这样测试过:

process foo{

  input:
  if(params.bar && params.bar2)
  {
    file reads from channel1.flatten()
  }
  else
  {
    file reads from channel_2.flatten()
  }
 
output:
  publishDir "$params.output_dir"
  file "output_file" into channel_3

  """
  my command line
  """

我收到这个错误,我不明白为什么。

No such variable: reads

有没有办法做这样的事情? 谢谢!

这是一个有点奇怪的错误,但基本上你只需要确保你的 input 声明 follows/matches 所需的语法:

input:
  <input qualifier> <input name> [from <source channel>] [attributes]

一个解决方案可能是使用 ternary operator 替换您的 if/else 分支,例如:

ch1 = Channel.of( 'hello', 'world' )
ch2 = Channel.of( 1, 3, 5, 7, 9 )

params.foo = false
params.bar = false


process test {

    echo true

    input:
    val myval from ( params.foo && params.bar ? ch1 : ch2 )

    """
    echo -n "${myval}"
    """
}

结果:

$ nextflow run script.nf
N E X T F L O W  ~  version 21.04.3
Launching `script.nf` [shrivelled_stone] - revision: 7b3f3a51df
executor >  local (5)
[3b/fafa5e] process > test (2) [100%] 5 of 5 ✔
1
5
9
7
3

$ nextflow run script.nf --foo --bar
N E X T F L O W  ~  version 21.04.3
Launching `script.nf` [irreverent_mahavira] - revision: 7b3f3a51df
executor >  local (2)
[d2/09d418] process > test (1) [100%] 2 of 2 ✔
world
hello


请注意,新的 DSL 2 将通道输入与流程声明分离,这可能有助于保持内容的可读性,尤其是在条件或操作语句更复杂的情况下。例如:

nextflow.enable.dsl=2

params.foo = false
params.bar = false


process test {

    echo true

    input:
    val myval 

    """
    echo -n "${myval}"
    """
}

workflow {

    ch1 = Channel.of( 'hello', 'world' )
    ch2 = Channel.of( 1, 3, 5, 7, 9 )

    if( params.foo && params.bar ) {
        test( ch1 )
    } else {
        test( ch2 )
    }
}

结果:

$ nextflow run script.nf
N E X T F L O W  ~  version 21.04.3
Launching `script.nf` [nauseous_pare] - revision: e1c4770ff1
executor >  local (5)
[36/49d8da] process > test (4) [100%] 5 of 5 ✔
9
1
3
5
7

$ nextflow run script.nf --foo --bar
N E X T F L O W  ~  version 21.04.3
Launching `script.nf` [goofy_euler] - revision: e1c4770ff1
executor >  local (2)
[56/e635e8] process > test (2) [100%] 2 of 2 ✔
world
hello