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.
SBML, the Systems Biology Markup Language, is how
models of biochemical networks, pharmacokinetics and cell signaling are
shared: hundreds of them are curated in
BioModels. But an SBML file describes
reactions between species in compartments, and a solver wants dX/dt
for each state. The translation, with amounts against concentrations,
compartments that change size, conversion factors and events, is where
hand-written versions go wrong. The R packages that read SBML either
need the libSBML system library or are no longer maintained.
zusbml reads an SBML document into one R object, writes it back as SBML, and derives from it the equations a solver needs, with nothing but R installed.
sbml_odes() gives the states, their
right-hand sides, the assignments and the events as R expressions;
sbml_derivs() turns them into a function for
deSolve, with SBML’s
event semantics (delays, priorities, persistence) included.delay, fast reactions or SBML packages, and are refused by
name rather than approximated. 1,028 of the supported cases also pass
when read from their Level 2 Version 5 file.system() or the rest of R.Once zusbml is on CRAN, install it with:
install.packages("zusbml")
The development version is on GitHub:
# install.packages("pak")
pak::pak("pedrobtz/zusbml")
library(zusbml)
The package comes with a small model of a drug taken by mouth: absorbed
from the gut into the central compartment, eliminated from there, and
dosed a second time at hour 12. sbml_read() takes a file, a URL or the
XML itself.
m <- sbml_read(system.file("extdata", "dosing.xml", package = "zusbml"))
m
#> <sbml_model 'dosing'> SBML Level 3 Version 2
#> compartments 2
#> species 2
#> parameters 3
#> reactions 2
#> events 1
Each kind of element is a data frame. The drug in the gut is an amount; the drug in the central compartment is a concentration.
species <- sbml_species(m)
species[, c("id", "compartment", "initial_amount", "has_only_substance_units")]
#> id compartment initial_amount has_only_substance_units
#> 1 drug_gut gut 100 TRUE
#> 2 drug central 0 FALSE
sbml_odes() derives the equations. The concentration’s right-hand side
is divided by the volume of its compartment, and each reaction id stands
for its rate.
eq <- sbml_odes(m)
eq$rhs
#> $drug_gut
#> -absorption
#>
#> $drug
#> (absorption - elimination)/central
eq$assignments
#> $absorption
#> ka * drug_gut
#>
#> $elimination
#> ke * drug * central
sbml_derivs() gives deSolve what it needs, including the event: the
drug in the gut goes back up at hour 12.
f <- sbml_derivs(m)
deSolve::ode(
attr(f, "y0"), times = c(0, 6, 12, 18, 24), func = f, parms = attr(f, "parms"),
method = "lsodar", rootfunc = attr(f, "rootfunc"), events = attr(f, "events")
)
#> time drug_gut drug
#> 1 0 1.000000e+02 0.000000
#> 2 6 2.478742e-01 3.733943
#> 3 12 1.000006e+02 1.133898
#> 4 18 2.478759e-01 4.075490
#> 5 24 6.144424e-04 1.236769
sbml_write() writes SBML Level 3 Version 2, and reading it back gives
the same model.
identical(sbml_read(sbml_write(m)), m)
#> [1] TRUE
sbml_to_rxode2() writes the equations as
rxode2 model text. rxode2
text cannot express SBML events, so the dose at hour 12 is refused
unless you ask for it to be dropped.
cat(sbml_to_rxode2(m, events = "drop"))
#> # dosing, written by zusbml from SBML Level 3 Version 2
#> gut = 1;
#> central = 10;
#> ka = 1;
#> ke = 0.20000000000000001;
#> dose = 100;
#> drug_gut(0) = 100;
#> drug(0) = 0;
#> absorption = ka * drug_gut;
#> elimination = ke * drug * central;
#> d/dt(drug_gut) = -absorption;
#> d/dt(drug) = (absorption - elimination)/central;
Functions are named sbml_*(): sbml_read() and sbml_write() for
documents, an accessor per element kind, and sbml_odes(),
sbml_derivs() and sbml_to_rxode2() for equations.
?zusbml
lists what is supported and what is refused.zusbml follows the SBML Level 3 Version 2 Core specification and reads Level 2 Versions 4 and 5 by mapping them to it. It is checked against the SBML Test Suite and curated models from BioModels, neither of which it ships. libSBML is the reference implementation; zusbml does not use it.