/***************************
Annotation pipeline config
----------------------------
Default for all environments
****************************/

// Global parameters
params {
	// Compulsory input
	genome              = null
	gff                 = null
	rnaseq              = null
	genome_bam          = null
	ncbi_query_email    = null
	taxon_id            = null

	//Optional input
	protein             = null
	lineage             = null
	genetic_code        = 1
	stranding           = "RF"

	// Output
	pdf                 = "AnnoAudit_Report.pdf"
	outdir              = "${launchDir}"
	tracedir            = "${params.outdir}/pipeline_info"
	publish_dir_mode    = 'copy'

	// Database
	odb_version         = "odb12"
	busco_database      = null
	oma_database        = null
	ref_protein         = null
	ncbi_query_count    = 100000
	ncbi_query_batch    = 1000

	// Conditioning option
	rnaseq_single       = false
	run_blast           = false
	query_ncbi_prot     = true
	cds_only            = "False"

	// Define max resource, need to be changed before running 
	max_cores           = Runtime.runtime.availableProcessors()
	max_memory          = '300.GB'
    max_cpus            = 36 // or Runtime.runtime.availableProcessors()
    max_time            = '24.h'

	// Cache dir
	tmpdir              = "${launchDir}/tmpdir"
    singularityCacheDir = "${launchDir}/singularity"

	// Others
	help                = false
	profile             = false
}

includeConfig 'conf/base.config'

profiles {

	// Executors
	
	local {
		executor {
			name = "local"
			cpus = params.max_cores
			memory = params.max_memory
		}
	}

	slurm {
        executor {
            name = "slurm"
		}
		process.cache = "lenient"
	}

	// Engines
	
	docker {
		docker.enabled         = true
		docker.useEmulation    = true
		conda.enabled          = false
		singularity.enabled    = false
		apptainer.enabled      = false
		includeConfig 'conf/container.config'
    }
	singularity {
		singularity.enabled    = true
		singularity.autoMounts = true
		singularity.cacheDir   = params.singularityCacheDir
		conda.enabled          = false
		docker.enabled         = false
		apptainer.enabled      = false
		includeConfig 'conf/container.config'
    }
	apptainer {
        apptainer.enabled      = true
		apptainer.cacheDir     = params.singularityCacheDir
    	singularity.enabled    = false
        conda.enabled          = false
        docker.enabled         = false
		includeConfig 'conf/container.config'
    }

	// Standard pipeline

	standard {
		executor {
            name = "slurm"
        }
		singularity {
			enabled = true
			autoMounts = true
			runOptions = "-B /env:/env -B /mnt:/mnt"
			includeConfig 'conf/container.config'
		}
	}
}

// Capture exit codes from upstream processes when pipeping
process.shell = ['/bin/bash', '-euo', 'pipefail']

def trace_timestamp = new java.util.Date().format('yyyy-MM-dd_HH-mm-ss')
timeline {
	enabled = true
	file    = "${params.tracedir}/timeline_${trace_timestamp}.html"
}
report {
	enabled = true
	file    = "${params.tracedir}/report_${trace_timestamp}.html"
}
trace {
	enabled = true
	file    = "${params.tracedir}/trace_${trace_timestamp}.txt"
}
dag {
	enabled = true
	file    = "${params.tracedir}/dag_${trace_timestamp}.html"
}

// Need to be changed
manifest {
	name            = 'AnnotAudit'
	author          = """Genoscope"""
	description     = """Annotation auditor for evaluate genome annotation"""
	mainScript      = 'main.nf'
	nextflowVersion = '!>=23.04.0'
	version         = '1.0.0'
}

// Load DSL2 module config
includeConfig 'conf/modules.config'

// Function to ensure that resource requirements don't go beyond
// a maximum limit
def check_max(obj, type) {
    if (type == 'memory') {
        try {
            if (obj.compareTo(params.max_memory as nextflow.util.MemoryUnit) == 1)
                return params.max_memory as nextflow.util.MemoryUnit
            else
                return obj
        } catch (all) {
            println "   ### ERROR ###   Max memory '${params.max_memory}' is not valid! Using default value: $obj"
            return obj
        }
    } else if (type == 'time') {
        try {
            if (obj.compareTo(params.max_time as nextflow.util.Duration) == 1)
                return params.max_time as nextflow.util.Duration
            else
                return obj
        } catch (all) {
            println "   ### ERROR ###   Max time '${params.max_time}' is not valid! Using default value: $obj"
            return obj
        }
    } else if (type == 'cpus') {
        try {
            return Math.min( obj, params.max_cpus as int )
        } catch (all) {
            println "   ### ERROR ###   Max cpus '${params.max_cpus}' is not valid! Using default value: $obj"
            return obj
        }
    }
}
