/*
 * -------------------------------------------------
 *  nf-core/sarek Nextflow base config file
 * -------------------------------------------------
 * A 'blank slate' config file, appropriate for general
 * use on most high performance compute environments.
 * Assumes that all software is installed and available
 * on the PATH. Runs in `local` mode - all jobs will be
 * run on the logged in environment.
 */

process {
  cpus = {check_resource(params.cpus * task.attempt)}
  memory = {check_resource((params.singleCPUMem as nextflow.util.MemoryUnit) * task.attempt)}
  time = {check_resource(24.h * task.attempt)}
  shell = ['/bin/bash', '-euo', 'pipefail']

  errorStrategy = {task.exitStatus in [143,137,104,134,139] ? 'retry' : 'finish'}
  maxErrors = '-1'
  maxRetries = 3

  withLabel:cpus_1 {
    cpus = {check_resource(1)}
  }
  withLabel:cpus_2 {
    cpus = {check_resource(2)}
  }
  withLabel:cpus_4 {
    cpus = {check_resource(4)}
  }
  withLabel:cpus_8 {
    cpus = {check_resource(8)}
  }
  withLabel:cpus_16 {
    cpus = {check_resource(16)}
  }
  withLabel:cpus_max {
    cpus = {params.max_cpus}
  }

  withLabel:memory_singleCPU_2_task {
    memory = {check_resource((params.singleCPUMem as nextflow.util.MemoryUnit) * 2 * task.attempt)}
  }
  withLabel:memory_singleCPU_task_sq {
    memory = {check_resource((params.singleCPUMem as nextflow.util.MemoryUnit) * task.attempt * task.attempt)}
  }

  withLabel:memory_max {
    memory = {params.max_memory}
  }

  withName:ConcatVCF {
    // For unknown reasons, ConcatVCF sometimes fails with SIGPIPE
    // (exit code 141). Rerunning the process will usually work.
    errorStrategy = {task.exitStatus == 141 ? 'retry' : 'terminate'}
  }
  withName:FastQCBAM {
    errorStrategy = {task.exitStatus == 143 ? 'retry' : 'ignore'}
  }
  withName:FastQCFQ {
    errorStrategy = {task.exitStatus == 143 ? 'retry' : 'ignore'}
  }
  withName:MapReads {
    memory = {check_resource(60.GB * task.attempt)}
    time = {check_resource(48.h * task.attempt)}
  }
  withName:MultiQC {
    errorStrategy = {task.exitStatus == 143 ? 'retry' : 'ignore'}
  }
  withName:Snpeff {
    container = {(params.annotation_cache && params.snpEff_cache) ? 'nfcore/sarek:2.5.1' : "nfcore/sareksnpeff:2.5.1.${params.genome}"}
    errorStrategy = {task.exitStatus == 143 ? 'retry' : 'ignore'}
  }
  withLabel:VEP {
    container = {(params.annotation_cache && params.vep_cache) ? 'nfcore/sarek:2.5.1' : "nfcore/sarekvep:2.5.1.${params.genome}"}
    errorStrategy = {task.exitStatus == 143 ? 'retry' : 'ignore'}
  }
}
