R/write_kstructure.R

###
### write_kstructure.R
###
### dependencies: pks, kst, MASS
###

write_kstructure <- function (x, filename, format="SRBT") {
  
  if (inherits(x, "kstructure")) {
    x <- as.binmat(x)
  }
  
  if (!inherits(x, "matrix")) 
    stop(sprintf("%s must be of class %s or %s!",
                 dQuote("x"),
                 dQuote("kstructure"),
                 dQuote("matrix")
    ))
  
  # rownames(x) <- NULL
  # colnames(x) <- NULL
  
  if (format == "CSV") {
    write.csv(x, filename, row.names = FALSE)
  } else {
    con <- file(filename)
    if (is.null(con))
      stop(sprintf("Unable to open file %s.", dQuote(filename)))
    open(con, open="w")
    
    size <- dim(x)
    
    if (format == "SRBT")
      cat("#SRBT v2.0 structure ASCII\n", file=con)
    
    if ((format == "SRBT") | (format == "KST")) {
      cat(sprintf("%d\n", size[2]), file=con)
      cat(sprintf("%d\n", size[1]), file=con)
    }
    
    write.matrix(x, sep="", file=con)
    
    close(con)
  }
}

Try the kstIO package in your browser

Any scripts or data that you put into this service are public.

kstIO documentation built on Feb. 16, 2023, 6:22 p.m.