library(optparse)

# ------------------------------------------------------------------------------
# defaults
# ------------------------------------------------------------------------------

default.max.inter.t <- 1
default.nperm <- 99
default.ncores <- 1
# ------------------------------------------------------------------------------
# parsing arguments
# ------------------------------------------------------------------------------

options <- list (
  
  make_option(
    opt_str = c("-c", "--ncores"),
    dest    = "ncores",
    type    = "integer",
    default = default.ncores,
    help    = paste0("number of CPU cores to use, defaults to ", default.ncores),
    metavar = "4"
  ),
  
  make_option(
    opt_str = c("-m", "--max.inter.t"),
    dest    = "max.inter.t",
    type    = "integer",
    default = default.max.inter.t,
    help    = "Max number of interactions for traits",
    metavar = "4"
  ),
  
  make_option(
    opt_str = c("-n", "--chunkn"),
    dest    = "chunkn",
    type    = "integer",
    default = 1,
    help    = "How many chunks?",
    metavar = "4"
  ),
  
  make_option(
    opt_str = c("-i", "--chunk.i"),
    dest    = "chunk.i",
    type    = "integer",
    default = NA,
    help    = "Which chunk?",
    metavar = "4"
  ), 
  make_option(
    opt_str = c("-p", "--nperm"),
    dest    = "nperm",
    type    = "integer",
    default = default.nperm,
    help    = "How many permutations?",
    metavar = "4"
  ), 
  make_option(
    opt_str = c("-s", "--start.round"),
    dest    = "start.round",
    type    = "integer",
    default = NA,
    help    = "Which start round?",
    metavar = "4"
  ), 
  make_option(
    opt_str = c("-x", "--relax.round"),
    dest    = "relax.round",
    type    = "integer",
    default = NA,
    help    = "After what round should the selection of new best combos stop being based on c.i.?",
    metavar = "4"
  ), 
  make_option(
    opt_str = c("-e", "--exclude.na"),
    dest    = "exclude.na",
    type    = "logical",
    default = F,
    help    = "should species with ANY nas in traits be excluded? (and relative covers recalculated?)",
    metavar = "4"
  )
)

parser <- OptionParser(
  usage       = "Rscript %prog [options] data dt_beals header output",
  option_list = options,
  description = "\nan awesome R script",
  epilogue    = "use with caution, the awesomeness might slap you in the face!"
)

cli <- parse_args(parser, positional_arguments = 5)

# ------------------------------------------------------------------------------
# assign a few shortcuts
# ------------------------------------------------------------------------------

species.path   <- cli$args[1]
traits.path  <- cli$args[2]
output       <- cli$args[3]
myfunction   <- cli$args[4]
combinations   <- cli$args[5]
start.round    <- cli$options$start.round
relax.round    <- cli$options$relax.round
max.inter.t    <- cli$options$max.inter.t
chunkn         <- cli$options$chunkn
chunk.i        <- cli$options$chunk.i
nperm          <- cli$options$nperm
ncores         <- cli$options$ncores
exclude.na     <- cli$options$exclude.na

# ------------------------------------------------------------------------------
# actual program
# ------------------------------------------------------------------------------

source("01b_MesobromionCluster.R")
Mesobromion(species.path, traits.path, output, myfunction, combinations, start.round, relax.round, max.inter.t, chunkn, chunk.i, nperm, ncores, exclude.na)