Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created October 25, 2013 06:02
Show Gist options
  • Save explodecomputer/7150052 to your computer and use it in GitHub Desktop.
Save explodecomputer/7150052 to your computer and use it in GitHub Desktop.
Read and write GRM from GCTA
readGRM <- function(rootname)
{
bin.file.name <- paste(rootname, ".grm.bin", sep="")
n.file.name <- paste(rootname, ".grm.N.bin", sep="")
id.file.name <- paste(rootname, ".grm.id", sep="")
cat("Reading IDs\n")
id <- read.table(id.file.name, colClasses="character")
n <- dim(id)[1]
cat("Reading GRM\n")
bin.file <- file(bin.file.name, "rb")
grm <- readBin(bin.file, n=n*(n+1)/2, what=numeric(0), size=4)
close(bin.file)
cat("Reading N\n")
n.file <- file(n.file.name, "rb")
N <- readBin(n.file, n=n*(n+1)/2, what=numeric(0), size=4)
close(n.file)
cat("Creating data frame\n")
l <- list()
for(i in 1:n)
{
l[[i]] <- 1:i
}
col1 <- rep(1:n, 1:n)
col2 <- unlist(l)
grm <- data.frame(id1=col1, id2=col2, N=N, grm=grm)
ret <- list()
ret$grm <- grm
ret$id <- id
return(ret)
}
writeGRM <- function(grm, rootname)
{
bin.file.name <- paste(rootname, ".grm.bin", sep="")
n.file.name <- paste(rootname, ".grm.N.bin", sep="")
id.file.name <- paste(rootname, ".grm.id", sep="")
write.table(grm$id[,1:2], id.file.name, row=F, col=F, qu=F)
n <- dim(id)[1]
cat("Writing GRM\n")
bin.file <- file(bin.file.name, "wb")
writeBin(grm$grm$grm, bin.file, size=4)
close(bin.file)
cat("Writing N\n")
n.file <- file(n.file.name, "wb")
writeBin(grm$grm$N, n.file, size=4)
close(n.file)
}
grm <- readGRM("filename")
writeGRM(grm, "filename2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment