## ----setup, include = FALSE--------------------------------------------------- knitr::opts_chunk$set(collapse = TRUE, comment = "#>") has_desolve <- requireNamespace("deSolve", quietly = TRUE) ## ----library------------------------------------------------------------------ library(r2sbml) ex <- function(f) system.file("examples", f, package = "r2sbml") # convertReactions() reports progress on the console; keep only the warnings convert <- function(file, format = "R", out = tempfile()) { tryCatch({ invisible(capture.output(convertReactions(file, out, format = format))) cat("no warning\n") }, warning = function(w) cat(conditionMessage(w), "\n")) invisible(out) } sbml_file <- function(body) { f <- tempfile(fileext = ".xml") writeLines(sprintf(' %s ', body), f) f } ## ----algebraic-linear--------------------------------------------------------- linear <- sbml_file(' k0.9') out <- convert(linear, "mrgsolve") grep("k = ", readLines(out), value = TRUE) ## ----dae-works, eval = has_desolve-------------------------------------------- out <- convert(ex("sbmlalgebraicrules.xml")) env <- new.env(); source(out, local = env) d <- as.data.frame(deSolve::daspk(y = env$InitialAmounts, dy = env$InitialDerivatives, times = seq(0, 10, by = 2), res = env$massBalances, parms = env$parameters)) max(abs(d$E + d$ES - d$E_total)) # constraint residual, ~1e-13 ## ----dae-warns---------------------------------------------------------------- convert(ex("sbmlalgebraicrules.xml"), "mrgsolve") ## ----volume-rule-------------------------------------------------------------- volume <- function(rule) sbml_file(sprintf(' %s', rule)) time <- 'time' growing <- volume(sprintf('1g%s', time)) out <- convert(growing) grep("dA_dt = ", readLines(out), value = TRUE) # V = 1 + g t, so dV/dt = g ## ----volume-rule-warns-------------------------------------------------------- capped <- volume(sprintf('51 g%s', time)) convert(capped) ## ----delay-------------------------------------------------------------------- out <- convert(ex("sbmldelay.xml")) grep("delay", readLines(out), value = TRUE) ## ----delay-ubiquity----------------------------------------------------------- convert(ex("sbmldelay.xml"), "ubiquity") ## ----events------------------------------------------------------------------- out <- convert(ex("sbmlevent.xml")) grep("events", readLines(out), value = TRUE) ## ----events-inspect----------------------------------------------------------- getEventMath(getModel(ex("sbmlevent.xml"))) ## ----maths-------------------------------------------------------------------- maths <- sbml_file(' 2x 3x ') out <- convert(maths) grep("^ +[ab] = ", readLines(out), value = TRUE) out <- convert(maths, "mrgsolve") grep("^double [ab] = ", readLines(out), value = TRUE) ## ----undefined---------------------------------------------------------------- undefined <- sbml_file(' ') convert(undefined) ## ----reserved----------------------------------------------------------------- reserved <- sbml_file(' Inf3') out <- tempfile() msg <- capture.output(convertReactions(reserved, out, format = "Julia")) grep("Renamed", msg, value = TRUE) ## ----species-ic--------------------------------------------------------------- model <- getModel(ex("sbmlsimple.xml")) getSpeciesIC(model) ## ----species-ic-vs-converted-------------------------------------------------- out <- convert(ex("sbmlsimple.xml")) grep("^ +E = [0-9]", readLines(out), value = TRUE) # 5e-07 = 5e-21 / 1e-14 ## ----species-table------------------------------------------------------------ getSpeciesTable(model)[, c("ID", "InitialConcentration", "InitialAmount")] ## ----species-names------------------------------------------------------------ getSpeciesNames(model) getCmtNames(model) ## ----stopping, error = TRUE--------------------------------------------------- try({ getRuleMath(model) # sbmlsimple.xml has no rules })