Skip to content

Instantly share code, notes, and snippets.

@vsbuffalo
Created February 12, 2015 00:05
Show Gist options
  • Save vsbuffalo/6e78546735bd1006f66f to your computer and use it in GitHub Desktop.
Save vsbuffalo/6e78546735bd1006f66f to your computer and use it in GitHub Desktop.
parseMS <- function(file) {
# simple MS parser
lns <- readLines(file)
header <- lns[1:3]
body <- lns[-c(1:3)]
brks <- body == "//"
groups <- cumsum(brks)
sims <- split(body, groups)
# sanity check
stopifnot(all(sapply(sims, '[[', 1) == "//"))
lapply(seq_along(sims), function(i) {
# drop //
x <- sims[[i]][-1] # -1 to drop //
# parse positions and segsites
pos_str <- unlist(strsplit(sub("positions: (.*) *$", "\\1", x[2]), " "))
pos <- as.numeric(pos_str)
segsites <- as.integer(sub("segsites: (\\d+)", "\\1", x[1]))
haps <- x[-c(1, 2)] # drop header
haps <- haps[-length(haps)] # drop empty line
list(sim=i, position=pos, segsites=segsites, haplotypes=haps)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment