Skip to content

Instantly share code, notes, and snippets.

@sa-lee
Created May 10, 2016 02:12
Show Gist options
  • Save sa-lee/530db6cb53976c27e648b9ab4500252c to your computer and use it in GitHub Desktop.
Save sa-lee/530db6cb53976c27e648b9ab4500252c to your computer and use it in GitHub Desktop.
Running structure in R in parallel
library(parallel)
library(stringr)
# prelim setup
structure_binary <- "/path/to/structure"
n.cores <- 8
# number of Ks to try
tryK <- 1L:20L
n.runs <- 1L:20L
# output files (will be appended by out_f from structure)
out_files <- outer(n.runs, tryK, function(x,y) paste0("str_run_", str_pad(x, width = 2, pad = 0), "_K_", str_pad(y, width = 2, pad = 0), ".out")
log_files <- gsub("out", "log", out_files)
# create function to run structure, assumes that mainparams/extraparams file is in same path as current wd in Rscript
run_structure <- function(out_file, log_file) {
k <- as.integer(str_extract(out_file, "[0-9]{2}\\b"))
return(system(paste(structure_binary, "-K", k, "-o", out_file, "&>", log_file)))
}
mcmapply(run_structure, out_files, log_files, mc.cores = n.cores)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment