/*
 * -------------------------------------------------
 *  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.single_cpu_mem 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

  // Process-specific resource requirements
  // See https://www.nextflow.io/docs/latest/config.html#config-process-selectors

  withLabel:process_low {
    cpus = { check_resource(2)}
    memory = { check_resource( 14.GB * task.attempt) }
    time = { check_resource( 6.h * task.attempt) }
  }
  withLabel:process_medium {
    cpus = { check_resource(6)}
    memory = { check_resource( 42.GB * task.attempt) }
    time = { check_resource( 8.h * task.attempt) }
  }
  withLabel:process_high {
    cpus = { check_resource(12)}
    memory = { check_resource( 84.GB * task.attempt) }
    time = { check_resource( 10.h * task.attempt) }
  }
  withLabel:process_long {
    time = { check_resource( 20.h * task.attempt) }
  }

  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.single_cpu_mem as nextflow.util.MemoryUnit) * 2 * task.attempt)}
  }
  withLabel:memory_singleCPU_task_sq {
    memory = {check_resource((params.single_cpu_mem as nextflow.util.MemoryUnit) * task.attempt * task.attempt)}
  }
  withLabel:memory_max {
    memory = {params.max_memory}
  }

  withName:get_software_versions {
    cache = false
  }

  withLabel:concat_vcf {
    // For unknown reasons, ConcatVCF sometimes fails with SIGPIPE
    // (exit code 141). Rerunning the process will usually work.
    errorStrategy = {task.exitStatus == 141 ? 'retry' : 'terminate'}
  }
  withLabel:FastQC {
    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.7' : "nfcore/sareksnpeff:2.7.${params.genome}"}
    errorStrategy = {task.exitStatus == 143 ? 'retry' : 'ignore'}
  }
  withLabel:VEP {
    container = {(params.annotation_cache && params.vep_cache) ? 'nfcore/sarek:2.7' : "nfcore/sarekvep:2.7.${params.genome}"}
    errorStrategy = {task.exitStatus == 143 ? 'retry' : 'ignore'}
  }
}
