4

如何在 nextflow 中执行 try catch?

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

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

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 忽略这个错误并继续,但我宁愿学习如何做一个正确的 try catch。

4

1 回答 1

4

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]
于 2021-02-10T02:31:23.257 回答