/*
 * -------------------------------------------------
 *  nf-core/crisprvar Nextflow config file
 * -------------------------------------------------
 * Default config options for all environments.
 * Cluster-specific config options should be saved
 * in the conf folder and imported under a profile
 * name here.
 */

// Global default params, used in configs
params {

  container = 'czbiohub/crisprvar:latest' // Container slug. Stable releases should specify release tag!

  help = false
  reads = "data/*{1,2}.fastq.gz"
  readPaths = false
  hdr = false
  nhej = false
  adapters = false
  excel = false
  samplesheet = "data/samplesheet.csv"
  adapters = false

  singleEnd = false
  outdir = './results'
  trim_pattern = false

  // Trimming
  clip_r1 = 0
  clip_r2 = 0
  three_prime_clip_r1 = 0
  three_prime_clip_r2 = 0
  trim_nextseq = 0
  pico = false
  saveTrimmed = false

  tracedir = "${params.outdir}/pipeline_info"
  clusterOptions = false
  awsqueue = false
  awsregion = 'eu-west-1'

  debug = false
  skipQC = false
  skipFastQC = false

  custom_config_version = 'master'
  custom_config_base = "https://raw.githubusercontent.com/nf-core/configs/${params.custom_config_version}"
}


// Load base.config by default for all pipelines
includeConfig 'conf/base.config'

// Load nf-core custom profiles from different Institutions
try {
  includeConfig "${params.custom_config_base}/nfcore_custom.config"
} catch (Exception e) {
  System.err.println("WARNING: Could not load nf-core/config profiles: ${params.custom_config_base}/nfcore_custom.config")
}

profiles {

  conda { process.conda = "$baseDir/environment.yml" }
  docker {
    docker.enabled = true
    process.container = params.container
  }
  singularity {
    singularity.enabled = true
    process.container = {"shub://${params.container.replace('nfcore', 'nf-core')}"}
  }
  awsbatch {
    includeConfig 'conf/awsbatch.config'
    includeConfig 'conf/igenomes.config'
  }
  test_nhej {
    includeConfig 'conf/test_nhej.config'
  }
  test_hdr {
    includeConfig 'conf/test_hdr.config'
  }
  test_excel {
    includeConfig 'conf/test_excel.config'
  }
  debug { process.beforeScript = 'echo $HOSTNAME' }
  none {
    // Don't load any config (for use with custom home configs)
  }

}

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

timeline {
  enabled = true
  file = "${params.tracedir}/nf-core/crisprvar_timeline.html"
}
report {
  enabled = true
  file = "${params.tracedir}/nf-core/crisprvar_report.html"
}
trace {
  enabled = true
  file = "${params.tracedir}/nf-core/crisprvar_trace.txt"
}
dag {
  enabled = true
  file = "${params.tracedir}/nf-core/crisprvar_dag.svg"
}

manifest {
  name = 'nf-core/crisprvar'
  author = 'Project author name (use a comma to separate multiple names).'
  homePage = 'https://github.com/nf-core/crisprvar'
  description = 'Run CRISPResso on genome editing experiments'
  mainScript = 'main.nf'
  nextflowVersion = '>=0.32.0'
  version = '1.0dev'
}

// 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
    }
  }
}
