在 Nextflow 进程中尝试捕获

问题描述

如何在 nextflow 中执行 try catch?

我目前正在编写一个管道,其中我正在执行的 bash 命令可能会在某些条件下以退出代码 1 退出。这使我的管道陷入停顿。我现在想使用 try catch 子句来定义一些替代行为,以防发生这种情况。

我尝试过以常规方式执行此操作,但似乎不起作用:

process align_kallisto {

    publishDir "${params.outdir}/kallisto",mode: 'copy',saveAs:{ filename -> "${name}_abundance.tsv" }   

    input:
    tuple val(name),file(fastq) from fq_kallisto.dump(tag: 'kallisto fq')
    file(index) from kallisto_index.collect().dump(tag: 'kallisto index')

    output:
    file("output/abundance.tsv") into kallisto_quant

    // this can throw an exit 1 status
    try {
        """
        kallisto quant -i ${index} --bias --single --fr-stranded -o output --plaintext \
          --fragment-length ${params.frag_length} --sd ${params.frag_deviation} ${fastq}
        """
    } 
    // if this happens catch and do something else
    catch (Exception e) {
        println("Exception: ${e} for $name")
        """
        // execute some alternative command
        """
    }

}

有什么建议吗?

我可以告诉 nextflow 忽略这个错误并继续,但我更愿意学习如何进行正确的尝试捕获。

解决方法

AFAIK 无法使用 try/catch 块处理流程定义中的错误。与其尝试捕获导致退出状态 1 的所有场景,您能否更好地定义这些条件并在尝试执行流程之前处理它们?例如,如果提供了一个空的 FASTQ 文件(或一个 FASTQ 文件,其读取次数不足以满足您的进程的要求)作为输入,这导致退出状态 1,则过滤掉这些文件的预处理命令可能是在这里很有用。

但如果无法更好地定义您的命令产生退出状态 1 或任何非零退出状态的条件,您可以通过将 errorStrategy 'ignore' 附加到您的流程定义中来忽略它们,就像您建议的那样.以下是如何获得“成功”和“失败”输出的示例,以便对其进行适当处理:

nextflow.enable.dsl=2

process test {

    errorStrategy 'ignore'

    input:
    tuple val(name),path(fastq)

    output:
    tuple val(name),path("output/abundance.tsv")

    """
    if [ "${fastq.baseName}" == "empty" ]; then
        exit 1
    fi
    mkdir output
    touch output/abundance.tsv
    """
}

workflow {

    fastqs = Channel.fromFilePairs( './data/*.fastq',size: 1 )

    test(fastqs) \
        .join(fastqs,remainder: true) \
        .branch { name,abundance,fastq_tuple ->
            failed: abundance == null
                return tuple( name,*fastq_tuple )
            succeeded: true
                return tuple( name,abundance )
        } \
        .set { results }

    results.failed.view { "failed: $it" }
    results.succeeded.view { "success: $it" }
}

运行:

mkdir data
touch data/nonempty.fastq
touch data/empty.fastq

nextflow run -ansi-log false test.nf

结果:

N E X T F L O W  ~  version 20.10.0
Launching `test.nf` [suspicious_newton] - revision: b883179718
[08/60a99f] Submitted process > test (1)
[42/358d60] Submitted process > test (2)
[08/60a99f] NOTE: Process `test (1)` terminated with an error exit status (1) -- Error is ignored
success: [nonempty,/home/user/working/stackoverflow/66119818/work/42/358d60bd7ac2cd8ed4dd7aef665d62/output/abundance.tsv]
failed: [empty,/home/user/working/stackoverflow/66119818/data/empty.fastq]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...