/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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_max( 1    * task.attempt, 'cpus'   ) }
    memory = { check_max( 6.GB * task.attempt, 'memory' ) }
    time   = { check_max( 4.h  * task.attempt, 'time'   ) }
    shell  = ['/bin/bash', '-euo', 'pipefail']

    // memory errors which should be retried. otherwise error out
    errorStrategy = { task.exitStatus in ((130..145) + 104) ? 'retry' : 'finish' }
    maxRetries    = 1
    maxErrors     = '-1'

    // Process-specific resource requirements
    // See https://www.nextflow.io/docs/latest/config.html#config-process-selectors
    withLabel:process_single {
        cpus          = { check_max( 1                  , 'cpus'    ) }
        memory        = { check_max( 6.GB * task.attempt, 'memory'  ) }
        time          = { check_max( 4.h  * task.attempt, 'time'    ) }
    }
    withLabel:process_low {
        cpus          = { check_max( 2     * task.attempt, 'cpus'    ) }
        memory        = { check_max( 12.GB * task.attempt, 'memory'  ) }
        time          = { check_max( 4.h   * task.attempt, 'time'    ) }
    }
    withLabel:process_medium {
        cpus          = { check_max( 6     * task.attempt, 'cpus'    ) }
        memory        = { check_max( 36.GB * task.attempt, 'memory'  ) }
        time          = { check_max( 8.h   * task.attempt, 'time'    ) }
    }
    withLabel:process_high {
        cpus          = { check_max( 12    * task.attempt, 'cpus'    ) }
        memory        = { check_max( 72.GB * task.attempt, 'memory'  ) }
        time          = { check_max( 16.h  * task.attempt, 'time'    ) }
    }
    withLabel:process_long {
        time          = { check_max( 20.h  * task.attempt, 'time'    ) }
    }
    withLabel:process_high_memory {
        memory        = { check_max( 200.GB * task.attempt, 'memory' ) }
    }
    withLabel:error_ignore {
        errorStrategy = 'ignore'
    }
    withLabel:error_retry {
        errorStrategy = 'retry'
        maxRetries    = 2
    }
    withName: 'UNZIP.*|UNTAR.*|TABIX.*|BUILD_INTERVALS|CREATE_INTERVALS_BED|CUSTOM_DUMPSOFTWAREVERSIONS|VCFTOOLS|BCFTOOLS.*|SAMTOOLS_INDEX' {
        cpus            = { check_max( 1 * task.attempt, 'cpus' ) }
        memory          = { check_max( 1.GB * task.attempt, 'memory' ) }
    }
    withName: 'FASTQC'{
        cpus            = { check_max( 4 * task.attempt, 'cpus' ) }
        memory          = { check_max( 4.GB * task.attempt, 'memory' ) }
    }
    withName: 'FASTP'{
        cpus            = { check_max( 12 * task.attempt, 'cpus' ) }
        memory          = { check_max( 4.GB * task.attempt, 'memory' ) }
    }
    withName: 'BWAMEM1_MEM|BWAMEM2_MEM' {
        cpus            = { check_max( 24 * task.attempt, 'cpus' ) }
        memory          = { check_max( 30.GB * task.attempt, 'memory' ) }
    }
    withName: 'GATK4_MARKDUPLICATES|GATK4_MARKDUPLICATESSPARK' {
        cpus           = { check_max( 6 * task.attempt, 'cpus' ) }
        memory         = { check_max( 30.GB * task.attempt, 'memory' ) }
    }
    withName:'GATK4_APPLYBQSR|GATK4_APPLYBQSR_SPARK|GATK4_BASERECALIBRATOR|GATK4_BASERECALIBRATOR_SPARK|GATK4_GATHERBQSRREPORTS'{
        cpus            = { check_max( 2 * task.attempt, 'cpus' ) }
        memory          = { check_max( 4.GB * task.attempt, 'memory' ) }
    }
    withName:'MOSDEPTH'{
        cpus            = { check_max( 4 * task.attempt, 'cpus' ) }
        memory          = { check_max( 4.GB * task.attempt, 'memory' ) }
    }
    withName:'STRELKA.*|MANTA.*' {
        cpus            = { check_max( 10 * task.attempt, 'cpus' ) }
        memory          = { check_max( 8.GB * task.attempt, 'memory' ) }
    }
    withName:'SAMTOOLS_CONVERT'{
        memory          = { check_max( 4.GB * task.attempt, 'memory' ) }
    }
    withName:'GATK4_MERGEVCFS'{
        cpus            = { check_max( 2 * task.attempt, 'cpus' ) }
        memory          = { check_max( 4.GB * task.attempt, 'memory' ) }
    }
    withName: 'MULTIQC' {
        cpus            = { check_max( 4 * task.attempt, 'cpus' ) }
        memory          = { check_max( 12.GB * task.attempt, 'memory' ) }
    }
}
