/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    sanger-tol/curationpretext 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.
----------------------------------------------------------------------------------------
*/

cleanup {
    enabled = true
}

process {

    // TODO nf-core: Check the defaults for all processes
    cpus   = { check_max( 1    * task.attempt, 'cpus'   ) }
    memory = { check_max( 6.GB * task.attempt, 'memory' ) }
    time   = { check_max( 4.h  * task.attempt, 'time'   ) }

    errorStrategy = { task.exitStatus in ((130..145) + 104) ? 'retry' : 'finish' }
    maxRetries    = 2
    maxErrors     = '-1'

    // IN CASES WHERE THERE IS ONE HIC FILE THIS WILL NEED ALMOST NOTHING
    withName:SAMTOOLS_MERGE {
        cpus    = { check_max( 16        * 1,               'cpus'  ) }
        memory  = { check_max( 50.GB     * task.attempt,    'memory') }
    }

    withName: '.*:.*:LONGREAD_COVERAGE:(MINIMAP2_ALIGN|MINIMAP2_ALIGN_SPLIT)' {
        cpus    = { check_max( 16       * 1, 'cpus'      ) }
        memory  = { check_max( 1.GB     * ( reference.size() < 2e9 ? 50 : Math.ceil( ( reference.size() / 1e+9 ) * 20 ) * Math.ceil( task.attempt * 1 ) ) , 'memory') }
    }

    withName: CRAM_FILTER_ALIGN_BWAMEM2_FIXMATE_SORT {
        cpus    = { check_max( 16      * 1                                                 , 'cpus'   ) }
        memory  = { check_max( 15.GB * Math.ceil( meta2.ref_size / 1e+9 )  * task.attempt, 'memory' ) }
    }

    withName: CRAM_FILTER_MINIMAP2_FILTER5END_FIXMATE_SORT {
        cpus    = { check_max( 16      * 1                                                 , 'cpus'   ) }
        memory  = { check_max( 15.GB * Math.ceil( meta2.ref_size / 1e+9 )  * task.attempt, 'memory' ) }
    }

    withName: PRETEXT_GRAPH {
        memory  = { check_max( 128.MB   * Math.ceil( task.attempt * 1.5 ), 'memory' ) }
    }

    withName: PRETEXTMAP_STANDRD{
        cpus    = { check_max( 16      * 1, 'cpus'   ) }
        memory  = { check_max( 3.GB    * task.attempt, 'memory' ) }
    }

    withName: PRETEXTMAP_HIGHRES {
        cpus    = { check_max( 20      * task.attempt, 'cpus'   ) }
        memory  = { check_max( 16.GB   * task.attempt, 'memory' ) }
    }

    withName: SNAPSHOT_SRES {
        cpus    = { check_max( 1        * task.attempt, 'cpus'   ) }
        memory  = { check_max( 1.GB     * task.attempt, 'memory' ) }
    }

    withName: SNAPSHOT_HRES {
        cpus    = { check_max( 1      * task.attempt, 'cpus'   ) }
        memory  = { check_max( 20.GB   * task.attempt, 'memory' ) }
    }

    withName: BWAMEM2_INDEX {
        memory = { check_max( 1.GB * Math.ceil( 28 * fasta.size() / 1e+9 ) * task.attempt, 'memory' ) }
    }

    withName: MINIMAP2_INDEX {
        cpus   = { check_max( 2    * task.attempt, 'cpus'    ) }
        memory = { check_max( 1.GB * Math.ceil( 30 * fasta.size() / 1e+9 ) * task.attempt, 'memory' ) }
    }

    withName: GAP_LENGTH {
        cpus   = { check_max( 1                         , 'cpus'    ) }
        memory = { check_max( 100.MB    * task.attempt  , 'memory'  ) }
    }

    withName: GENERATE_CRAM_CSV {
        cpus   = { check_max( 2                         , 'cpus'    ) }
    }

    // Process-specific resource requirements
    // NOTE - Please try and re-use the labels below as much as possible.
    //        These labels are used and recognised by default in DSL2 files hosted on nf-core/modules.
    //        If possible, it would be nice to keep the same label naming convention when
    //        adding in your local modules too.
    // 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:CUSTOM_DUMPSOFTWAREVERSIONS {
        cache = false
    }
}
