Skip to content

Commit

Permalink
read and write gzipped SBML
Browse files Browse the repository at this point in the history
Not super important, as libSBML can usually handle compressed model
file. However, in the homebrew/brewsci build of SBML, the gzip-support
is disables due to a conflict of libSBML and zlib:

brewsci/homebrew-bio#1672

The change here, used R's build-in gz-file in/out support to read/write
gzipped SBML files.

Note: It is a little less efficient than the libSBML gz-support. When
zlib-connection in homebrew's libsbml is back, the lines added in this
commit could be removed again.
  • Loading branch information
Waschina committed Jun 8, 2024
1 parent 0caf489 commit 94be9d5
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@
#' @import Matrix
#' @export
readSBMLmod <- function(file_path) {
is_compressed <- FALSE

file_path <- normalizePath(file_path)

if(grepl("\\.gz$",file_path)) {
is_compressed <- TRUE
fileconn <- gzfile(file_path)
tmpfile <- tempfile(fileext = ".xml")
writeLines(readLines(fileconn), tmpfile)
close(fileconn)
sbmldoc <- readSBMLfile(tmpfile)
} else {
sbmldoc <- readSBMLfile(file_path)
}

# Pointers to SBML document and model (libSBML objects)
sbmldoc <- readSBMLfile(normalizePath(file_path))
modelPtr <- getModelObj(sbmldoc)

#---------------#
Expand Down Expand Up @@ -78,6 +91,9 @@ readSBMLmod <- function(file_path) {
})
gpr_sbo <- getGeneProductSBOTerms(modelPtr)

if(is_compressed)
file.remove(tmpfile)

return(
new("ModelOrg",
mod_id = mod_id,
Expand Down Expand Up @@ -144,6 +160,13 @@ sboterm2int <- function(sbo) {
#' @export
writeSBMLmod <- function(model, file_path = NULL) {

compress <- FALSE
if(grepl("\\.gz$",file_path)) {
compress <- TRUE
out_file <- file_path
file_path <- gsub("\\.gz$","", file_path)
}

if(is.null(file_path))
file_path <- paste0(model@mod_id, ".xml")

Expand Down Expand Up @@ -272,6 +295,16 @@ writeSBMLmod <- function(model, file_path = NULL) {
obj_coef = model@obj_coef
)

if(compress) {
input_conn <- file(file_path, "r")
output_conn <- gzfile(out_file, "w")
file_content <- readLines(input_conn)
writeLines(file_content, output_conn)
close(input_conn)
close(output_conn)
file.remove(file_path)
}

return(out)
}

Expand Down

0 comments on commit 94be9d5

Please sign in to comment.