| Title: | Read, Write and Derive Equations from SBML Models |
| Version: | 0.1.0 |
| Description: | Reads Systems Biology Markup Language (SBML) core documents, Level 3 Versions 1 and 2 and Level 2 Versions 4 and 5, into one R object with a data frame per element kind. Writes that object back as valid Level 3 Version 2. Derives the states, initial values, right-hand sides, assignments and events that an ordinary differential equation solver needs, following the specification at https://sbml.org/documents/specifications/. Model math becomes R expressions that are evaluated without access to the rest of R, and constructs the package does not support are refused rather than approximated. |
| License: | MIT + file LICENSE |
| Imports: | zuxml |
| Encoding: | UTF-8 |
| Language: | en-US |
| Suggests: | deSolve, parallel, testthat (≥ 3.0.0), withr, zujson |
| Enhances: | rxode2 |
| Config/testthat/edition: | 3 |
| URL: | https://pedrobtz.github.io/zusbml/, https://github.com/pedrobtz/zusbml |
| BugReports: | https://github.com/pedrobtz/zusbml/issues |
| Config/roxygen2/version: | 8.1.0 |
| NeedsCompilation: | no |
| Packaged: | 2026-11-12 14:45:58 UTC; pbtz |
| Author: | Pedro Baltazar [aut, cre, cph] |
| Maintainer: | Pedro Baltazar <pedrobtz@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-12-04 17:00:02 UTC |
zusbml: Read, Write and Derive Equations from SBML Models
Description
Reads Systems Biology Markup Language (SBML) core documents, Level 3 Versions 1 and 2 and Level 2 Versions 4 and 5, into one R object with a data frame per element kind. Writes that object back as valid Level 3 Version 2. Derives the states, initial values, right-hand sides, assignments and events that an ordinary differential equation solver needs, following the specification at https://sbml.org/documents/specifications/. Model math becomes R expressions that are evaluated without access to the rest of R, and constructs the package does not support are refused rather than approximated.
Scope
zusbml reads SBML Level 3 Versions 1 and 2 core, and Level 2 Versions 4
and 5 mapped to the Level 3 meaning, and writes Level 3 Version 2.
Derivation covers reactions, assignment and rate rules, initial
assignments, function definitions, rateOf, conversion factors,
compartments that change size, and events with delays, priorities and
persistence.
It refuses, with a zusbml_unsupported error naming the construct:
SBML Level 3 packages (
comp,fbc,layout,qual, ...), unlesssbml_read(ignore_packages = TRUE);Level 1 and Level 2 Versions 1 to 3;
in derivation, algebraic rules (a DAE), the
delaycsymbol (a delay differential equation) and fast reactions;in
sbml_to_rxode2(), events, unless dropped on request.
It does not check units, validate against libSBML's full rule set, or
simulate on its own: sbml_derivs() hands the equations to deSolve.
Author(s)
Maintainer: Pedro Baltazar pedrobtz@gmail.com [copyright holder]
Authors:
Pedro Baltazar pedrobtz@gmail.com [copyright holder]
See Also
Useful links:
Report bugs at https://github.com/pedrobtz/zusbml/issues
Check a model's structure
Description
Checks the structural rules derivation relies on: ids are unique, references resolve, a symbol is set by at most one rule (or by reactions and not also by a rule), rules and events do not change constants, and assignment rules do not form a cycle. This is not libSBML's full rule set.
Usage
sbml_check(m)
Arguments
m |
An |
Value
A data frame with one row per finding: rule ("duplicate_id",
"unknown_reference", "multiple_definitions", "constant_target" or
"assignment_cycle"), the id concerned, and a message. No rows when
the model passes. For an sbml_model, sbml_check() never raises; it
raises zusbml_arg_error for anything else.
Examples
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml"))
sbml_check(m)
Model components
Description
Each accessor returns one data frame of an sbml_model, one row per
element. Scalar attributes are columns, named in snake_case after the
SBML attribute (initialAmount is initial_amount); optional attributes
that are absent are NA. Math is a list column. Every table also has
metaid, sbo_term, and notes and annotation as XML text.
Usage
sbml_compartments(m)
sbml_species(m)
sbml_parameters(m)
sbml_reactions(m)
sbml_rules(m)
sbml_events(m)
sbml_functions(m)
sbml_initial_assignments(m)
sbml_constraints(m)
sbml_unit_definitions(m)
Arguments
m |
An |
Details
sbml_reactions() holds the species references (reactants, products,
modifiers) as data frames and kinetic_law as a list with math and
local_parameters. sbml_events() holds trigger, priority, delay
and assignments. sbml_rules() has a type column: "assignment",
"rate" or "algebraic". sbml_functions() adds fun, each lambda
as an R function; see sbml_math() for how math is represented.
Value
A data frame.
Examples
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml"))
sbml_species(m)
sbml_reactions(m)$reactants[[1]]
A derivative function for deSolve
Description
sbml_derivs() turns a model into a derivative function for
deSolve::ode(), which calls it with time, state and parameters by
position. The function carries what deSolve needs as attributes:
y0, the states at time 0 after any events that fire then; parms, the
constants; and, for a model with events, rootfunc and events, which
run the events with the full SBML semantics: delays, persistence,
priorities, useValuesFromTriggerTime, and events triggered by other
events at the same time.
Usage
sbml_derivs(m, outputs = character(0), parms = NULL)
Arguments
m |
An |
outputs |
Model symbols to report as extra columns of the solver output, such as species defined by assignment rules or reaction rates. |
parms |
A named numeric vector of constants to use instead of the
model's values, or |
Details
The function keeps the event queue between calls. Use it for one integration starting at time 0; calling it again at time 0 restarts the queue.
To change constants, pass them in parms here rather than editing the
parms attribute: initial assignments and events at time 0 that depend
on them are then reflected in y0. The function and its events always
use the parms the solver passes.
Value
A function with attributes y0, parms, and for models with
events rootfunc and events.
Examples
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml"))
f <- sbml_derivs(m)
deSolve::ode(attr(f, "y0"), seq(0, 2, by = 0.5), f, attr(f, "parms"))
Show model math as R code
Description
Model math is held as R language objects: SBML's MathML operators become
the matching R calls (<power/> is ^, <ln/> is log(),
<piecewise> is nested if), the time csymbol is the name time,
and lambdas are function expressions. Operators without an exact base R
equivalent keep their SBML name: quotient(), rem(), implies(),
sec() and the other reciprocal trigonometric functions, rateOf() and
delay().
Usage
sbml_math(x)
Arguments
x |
An expression from a model, such as |
Value
A character vector with one line of R code per expression, NA
for absent math.
Examples
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml"))
sbml_math(sbml_reactions(m)$kinetic_law[[1]])
Derive the equations of a model
Description
sbml_odes() turns a model into what an ODE solver needs: the states,
the right-hand side of each, the assignments evaluated before the
right-hand sides at every time, and the constants.
sbml_initial_state() gives the states' values at time 0.
Usage
sbml_odes(m)
sbml_initial_state(m)
Arguments
m |
An |
Details
Species whose symbol is a concentration are integrated as concentrations,
except in a compartment whose size can change, where the amount is
integrated (as the state .amount.<id>) and the concentration is an
assignment. Reaction ids are assignments holding the reaction's rate.
Function definitions are inlined, local parameters become constants named
.local.<reaction>.<id>, and rateOf() is replaced by the right-hand
side it refers to.
Value
sbml_odes() returns a list:
-
states: a data frame with the state'sname, the modelsymbolit belongs to, and itskind("species","compartment", ...); -
rhs: a named list of expressions, the time derivative of each state; -
assignments: a named list of expressions in evaluation order; -
constants: a named numeric vector of the constants' values; -
initial: the states' values at time 0, assbml_initial_state(); -
species: for each species, whether its symbol is an amount and its compartment, to convert between amounts and concentrations; -
events: one list per event with itsid,trigger,initial_value,persistent,priorityanddelayexpressions (NULLwhen absent),use_values_from_trigger_time, andassignments, a named list of expressions by target.
sbml_initial_state() returns a named numeric vector.
Errors
-
zusbml_unsupportedfor algebraic rules (the model is then a DAE), thedelaycsymbol and fast reactions, each naming the element. -
zusbml_derive_errorfor an assignment cycle, a missing value, or a name the model does not define, naming the ids involved in itsidsfield.
Examples
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml"))
eq <- sbml_odes(m)
eq$rhs
sbml_initial_state(m)
Read an SBML document
Description
Reads an SBML core document into an sbml_model: Level 3 Versions 1 and
2, and Level 2 Versions 4 and 5, which are mapped to their Level 3
meaning. Every element carries the attributes Level 3 defines; optional
values without a default read as NA.
notes and annotation on the listOf* containers are dropped, so
sbml_write() cannot write them back.
Usage
sbml_read(x, ignore_packages = FALSE)
Arguments
x |
A file path, a |
ignore_packages |
A document that uses SBML Level 3 packages ( |
Value
An sbml_model.
Errors
-
zusbml_parse_errorfor a malformed document: an unknown element or attribute in the SBML namespace, a missing required attribute, or a value of the wrong type. Itspathfield names the element, for examplemodel/listOfSpecies/species[3], andattributethe attribute. -
zusbml_unsupportedfor SBML packages, Level 1, and Level 2 before Version 4. -
zusbml_arg_errorfor an argument of the wrong type, a missing file or a directory.
Examples
path <- system.file("extdata", "decay.xml", package = "zusbml")
m <- sbml_read(path)
m
sbml_species(m)
Write a model as rxode2 text
Description
Prints the derived equations (sbml_odes()) in the model syntax of the
rxode2 package: constants and assignments as x = ...;, initial
conditions as x(0) = ...; and right-hand sides as d/dt(x) = ...;. The
text is self-contained. Initial conditions are numbers computed from the
model's constants, so an initial value that depends on a constant does
not follow a parameter you pass to rxode2; edit the text instead.
Usage
sbml_to_rxode2(m, events = c("refuse", "drop"))
Arguments
m |
An |
events |
|
Details
rxode2 text cannot express SBML events, which change states at triggered
times, so a model with events is refused unless events = "drop", which
writes the continuous part only. Names rxode2 cannot take,
ids starting with _ and those it reserves such as time or param, are
written with a .u prefix: _J0 becomes .u_J0.
Value
A single string of rxode2 model code.
Examples
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml"))
cat(sbml_to_rxode2(m))
Write a model as SBML
Description
Writes an sbml_model as an SBML Level 3 Version 2 core document,
whatever level and version it was read from. Attributes that are NA
are left out; notes, annotation and constraint messages are written as
they were read; math is written as MathML. Reading the result gives back
an identical model for any Level 3 Version 2 input.
Usage
sbml_write(m, path = NULL)
Arguments
m |
An |
path |
A file to write, or |
Details
The document is written as text, escaping attribute values and names.
Value
The path, invisibly, or the document as a string when path is
NULL.
Errors
zusbml_write_error for math with no MathML equivalent, naming the R
call, for text with characters XML 1.0 does not allow, for notes,
annotation or messages that are not well-formed XML, and for a fast
reaction, which Level 3 Version 2 cannot express. zusbml_arg_error when
m is not a model or path is neither NULL nor a single string.
Examples
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml"))
path <- tempfile(fileext = ".xml")
sbml_write(m, path)
identical(sbml_read(path), m)
Write a model as JSON
Description
Writes the sbml_model object as JSON with the zujson package, for use
without XML: one array of records per element kind, math as R code (as
sbml_math() shows it), notes and annotation as XML text.
Usage
sbml_write_json(m, path = NULL)
Arguments
m |
An |
path |
A file to write, or |
Value
The path, invisibly, or the JSON as a string when path is NULL.
Examples
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml"))
cat(substr(sbml_write_json(m), 1, 200))
What this build of zusbml reads and writes
Description
What this build of zusbml reads and writes
Usage
zusbml_info()
Value
A list: version, the package version; read, the SBML levels
and versions sbml_read() accepts; write, the level and version
sbml_write() produces; and packages, the SBML Level 3 packages
supported (none: they are refused or, on request, ignored).
Examples
zusbml_info()