# npdro::createSimulation2() example for gwas simulation
if (!require("devtools")) install.packages("devtools")
library(devtools)
install_github("insilico/npdro")  
library(npdro)

## Set Working Directory to file directory - RStudio approach
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

gen.gwas = function(samples, vars, batch_size=1000){
  for (x in 1:ceiling(samples/batch_size)){
      curr_batch <- samples - (batch_size*(x-1))
      batch_gen <- ifelse(curr_batch > batch_size, batch_size, curr_batch)
      data <- createSimulation2(data.type = "discrete", avg.maf = 0.2, 
                    sim.type = "mainEffect",
                    pct.train = 1.0, 
                    pct.imbalance=round(runif(n = 1, min = 0.1, max = 0.9),2), 
                    main.bias = 0.4, pct.signals = 0.2,
                    num.samples = batch_gen,
                    num.variables = vars)
      
        rownames(data$train) <- as.integer(rownames(data$train))+
          (batch_size*(x-1))
      
      if (x == 1){
        write.table(data$train, "artif_gwas.csv", row.names = TRUE)
      } else{
          write.table(data$train, "artif_gwas.csv", row.names = TRUE, 
                    append = TRUE, col.names = FALSE)
      }
  }
  return(data$train)
}

gen.gwas(1000000, 10000)

