【问题标题】:Try catch in Nextflow processes在 Nextflow 流程中尝试 catch
【发布时间】:2021-02-12 07:29:49
【问题描述】:

如何在 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。

【问题讨论】:

    标签: groovy nextflow


    【解决方案1】:

    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]
    

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 2012-02-26
      • 2017-02-13
      • 2015-10-04
      • 1970-01-01
      • 2010-12-25
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多