| Title: | 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. |
| Authors: | Pedro Baltazar [aut, cre, cph] |
| Maintainer: | Pedro Baltazar <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-12-01 09:56:51 UTC |
| Source: | https://github.com/pedrobtz/zusbml |
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.
sbml_check(m)sbml_check(m)
m |
An |
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.
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) sbml_check(m)m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) sbml_check(m)
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.
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)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)
m |
An |
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.
A data frame.
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) sbml_species(m) sbml_reactions(m)$reactants[[1]]m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) sbml_species(m) sbml_reactions(m)$reactants[[1]]
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.
sbml_derivs(m, outputs = character(0), parms = NULL)sbml_derivs(m, outputs = character(0), parms = NULL)
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 |
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.
A function with attributes y0, parms, and for models with
events rootfunc and events.
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"))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"))
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().
sbml_math(x)sbml_math(x)
x |
An expression from a model, such as |
A character vector with one line of R code per expression, NA
for absent math.
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) sbml_math(sbml_reactions(m)$kinetic_law[[1]])m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) sbml_math(sbml_reactions(m)$kinetic_law[[1]])
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.
sbml_odes(m) sbml_initial_state(m)sbml_odes(m) sbml_initial_state(m)
m |
An |
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.
sbml_odes() returns a list:
states: a data frame with the state's name, the model symbol it
belongs to, and its kind ("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, as sbml_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 its id, trigger,
initial_value, persistent, priority and delay expressions
(NULL when absent), use_values_from_trigger_time, and
assignments, a named list of expressions by target.
sbml_initial_state() returns a named numeric vector.
zusbml_unsupported for algebraic rules (the model is then a DAE),
the delay csymbol and fast reactions, each naming the element.
zusbml_derive_error for an assignment cycle, a missing value, or a
name the model does not define, naming the ids involved in its ids
field.
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) eq <- sbml_odes(m) eq$rhs sbml_initial_state(m)m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) eq <- sbml_odes(m) eq$rhs sbml_initial_state(m)
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.
sbml_read(x, ignore_packages = FALSE)sbml_read(x, ignore_packages = FALSE)
x |
A file path, a |
ignore_packages |
A document that uses SBML Level 3 packages ( |
An sbml_model.
zusbml_parse_error for a malformed document: an unknown element or
attribute in the SBML namespace, a missing required attribute, or a
value of the wrong type. Its path field names the element, for
example model/listOfSpecies/species[3], and attribute the attribute.
zusbml_unsupported for SBML packages, Level 1, and Level 2 before
Version 4.
zusbml_arg_error for an argument of the wrong type, a missing file or
a directory.
path <- system.file("extdata", "decay.xml", package = "zusbml") m <- sbml_read(path) m sbml_species(m)path <- system.file("extdata", "decay.xml", package = "zusbml") m <- sbml_read(path) m sbml_species(m)
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.
sbml_to_rxode2(m, events = c("refuse", "drop"))sbml_to_rxode2(m, events = c("refuse", "drop"))
m |
An |
events |
|
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.
A single string of rxode2 model code.
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) cat(sbml_to_rxode2(m))m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) cat(sbml_to_rxode2(m))
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.
sbml_write(m, path = NULL)sbml_write(m, path = NULL)
m |
An |
path |
A file to write, or |
The document is written as text, escaping attribute values and names.
The path, invisibly, or the document as a string when path is
NULL.
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.
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) path <- tempfile(fileext = ".xml") sbml_write(m, path) identical(sbml_read(path), m)m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) path <- tempfile(fileext = ".xml") sbml_write(m, path) identical(sbml_read(path), m)
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.
sbml_write_json(m, path = NULL)sbml_write_json(m, path = NULL)
m |
An |
path |
A file to write, or |
The path, invisibly, or the JSON as a string when path is NULL.
m <- sbml_read(system.file("extdata", "decay.xml", package = "zusbml")) cat(substr(sbml_write_json(m), 1, 200))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
zusbml_info()zusbml_info()
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).
zusbml_info()zusbml_info()