输入参数在 Nextflow 流程中不能用作条件开关

Input param not working as conditional switch in Nextflow processes

我试图将一个参数传递给 Nextflow,我可以用它来打开或关闭进程,但没有用。

此外,当我在日志文件中打印参数时,大小写总是发生变化,这对我来说似乎很奇怪(即,TRUE 变为 true)。鉴于此行为,我已尝试将条件语句设置为匹配“TRUE”或“true”,但似乎都不起作用。

下面是一些代码来说明这个问题。

params.force = "FALSE"
params.in = 1

log.info """\
         Force: $params.force
         """
         .stripIndent()

process tester {
  input:
    val x from params.in

  output:
      stdout testerOut

  when:
    params.force == "TRUE"

  script:
      """
      echo "foo"
      """
  }

  testerOut.view()

如果此文件保存为 testnf 并且通过“nextflow 运行 testnf --force “TRUE””运行,则该过程将不会 运行。输出为:

N E X T F L O W ~ 版本 21.10.0 启动 testnf [soggy_lorenz] - 修订版:a7399aad3c 力:真

[-] 进程 > 测试人员 -

目标是让用户传入关闭或打开某些进程的参数。这似乎是一个常见的用例,但我被卡住了。为任何帮助干杯!

Nextflow 自动将 --force TRUE 参数转换为布尔值,因此只需将其更改为:

params.force = "FALSE"
params.in = 1

log.info """\
         Force: $params.force
         """
         .stripIndent()

process tester {
  input:
    val x from params.in

  output:
      stdout testerOut

  when:
    params.force

  script:
      """
      echo "foo"
      """
  }

  testerOut.view()

输出:

$ ~/nextflow run main.nf
N E X T F L O W  ~  version 21.10.6
Launching `main.nf` [high_hamilton] - revision: cac46af672
Force: FALSE

executor >  local (1)
[1e/5cb38e] process > tester [100%] 1 of 1 ✔
foo

Pallie 正确:

Nextflow automatically converts the --force TRUE param to a boolean

这是因为 Nextflow command-line 参数值 TRUEFALSE(不区分大小写)是特殊的,将 return Boolean.TRUEBoolean.FALSE,分别代表1. When you print (or log) these parameters, you are really just accessing a Boolean的特殊真值:truefalse.

请注意,您的 Nextflow 脚本中定义的参数不会被强制转换。例如,在你的脚本中设置 params.force = "FALSE" 只会给你一个普通的旧 java.lang.String if --force is not specified on the command-line. The problem is that if you do specify --force on the command-line, you'll get a different type: a java.lang.Boolean。解决方案是在脚本中将 params.force 设置为布尔值:

params.force = false

println("Force: ${params.force}")


process test {

    echo true

    when:
    params.force

    """
    echo "foo"
    """
}

一些测试,预期结果:

nextflow run test.nf 
N E X T F L O W  ~  version 21.04.3
Launching `test.nf` [clever_mccarthy] - revision: e7a5148ea1
Force: false
[-        ] process > test -

$ nextflow run test.nf --force
N E X T F L O W  ~  version 21.04.3
Launching `test.nf` [distraught_jepsen] - revision: e7a5148ea1
Force: true
executor >  local (1)
[d0/c9a3d2] process > test [100%] 1 of 1 ✔
foo

$ nextflow run test.nf --force FALSE
N E X T F L O W  ~  version 21.04.3
Launching `test.nf` [astonishing_feynman] - revision: e7a5148ea1
Force: false
[-        ] process > test -

$ nextflow run test.nf --force True
N E X T F L O W  ~  version 21.04.3
Launching `test.nf` [sleepy_sammet] - revision: e7a5148ea1
Force: true
executor >  local (1)
[b5/9fac0f] process > test [100%] 1 of 1 ✔
foo

最后一个示例显示 non-empty 字符串根据 Groovy truth:

被强制转换为 true
$ nextflow run test.nf --force foobar
N E X T F L O W  ~  version 21.04.3
Launching `test.nf` [trusting_hilbert] - revision: e7a5148ea1
Force: foobar
executor >  local (1)
[d1/c1b190] process > test [100%] 1 of 1 ✔
foo