From 94be9d5575c65d699d5f216c9e3c001314d5029d Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 8 Jun 2024 22:55:47 +0200 Subject: [PATCH] read and write gzipped SBML 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: https://github.com/brewsci/homebrew-bio/pull/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. --- R/io.R | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/R/io.R b/R/io.R index 082d85e..c70591a 100644 --- a/R/io.R +++ b/R/io.R @@ -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) #---------------# @@ -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, @@ -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") @@ -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) }