Skip to content

Commit 64f3df6

Browse files
authored
Merge pull request #1441 from sebapersson/sbmlimporter
Update SBMLImporter tutorial
2 parents 4d1d324 + 1315737 commit 64f3df6

3 files changed

Lines changed: 43 additions & 48 deletions

File tree

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a"
3737
OrdinaryDiffEqVerner = "79d7bb75-1356-48c1-b8c0-6832512096c2"
3838
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
3939
QuasiMonteCarlo = "8a4e6c94-4038-4cdc-81c3-7e6ffdb2a71b"
40+
ReactionNetworkImporters = "b4db0fb7-de2a-5028-82bf-5021f5cfa881"
41+
SBMLImporter = "210efffb-c3c8-456d-a807-6f55560b12fe"
4042
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
4143
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
4244
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

docs/pages.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pages = Any[
1515
"model_creation/conservation_laws.md",
1616
"model_creation/parametric_stoichiometry.md",
1717
"model_creation/functional_parameters.md",
18-
# "model_creation/model_file_loading_and_export.md", Wait for new SBMImporter and ReactionNetworkImporters versions
18+
"model_creation/model_file_loading_and_export.md",
1919
"model_creation/model_visualisation.md",
2020
"model_creation/reactionsystem_content_accessing.md",
2121
"model_creation/chemistry_related_functionality.md",

docs/old_files/unused_tutorials/model_file_loading_and_export.md renamed to docs/src/model_creation/model_file_loading_and_export.md

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pkg.add("SBMLImporter")
1616
</details>
1717
```
1818
\
19-
19+
2020
Catalyst stores chemical reaction network (CRN) models in `ReactionSystem` structures. This tutorial describes how to load such `ReactionSystem`s from, and save them to, files. This can be used to save models between Julia sessions, or transfer them from one session to another. Furthermore, to facilitate the computation modelling of CRNs, several standardised file formats have been created to represent CRN models (e.g. [SBML](https://sbml.org/)). This enables CRN models to be shared between different software and programming languages. While Catalyst itself does not have the functionality for loading such files, we will here (briefly) introduce a few packages that can load different file types to Catalyst `ReactionSystem`s.
2121

2222
## [Saving Catalyst models to, and loading them from, Julia files](@id model_file_import_export_crn_serialization)
@@ -46,33 +46,34 @@ cc_loaded # hide
4646
Here, `include` is used to execute the Julia code from any file. This means that `save_reactionsystem` actually saves the model as executable code which re-generates the exact model which was saved (this is the reason why we use the ".jl" extension for the saved file). Indeed, we can confirm this if we check what is printed in the file:
4747
```
4848
let
49+
# Serialised using Catalyst version v1.12.5.
4950
5051
# Independent variable:
51-
@parameters t
52+
@independent_variables t
5253
5354
# Parameters:
54-
ps = @parameters kB kD kP
55+
ps = @parameters k₁ k₂ k₃
5556
5657
# Species:
57-
sps = @species S(t) E(t) SE(t) P(t)
58+
sps = @species S(t) C(t) S₁C(t) S₂(t) CP(t) P(t)
5859
5960
# Reactions:
6061
rxs = [
61-
Reaction(kB, [S, E], [SE], [1, 1], [1]),
62-
Reaction(kD, [SE], [S, E], [1], [1, 1]),
63-
Reaction(kP, [SE], [P, E], [1], [1, 1])
62+
Reaction(k₁, [S₁, C], [S₁C], [1, 1], [1]),
63+
Reaction(k₂, [S₁C, S₂], [CP], [1, 1], [1]),
64+
Reaction(k₃, [CP], [C, P], [1], [1, 1])
6465
]
6566
6667
# Declares ReactionSystem model:
67-
rs = ReactionSystem(rxs, t, sps, ps; name = Symbol("##ReactionSystem#12592"))
68+
rs = ReactionSystem(rxs, t, sps, ps; name = Symbol("##ReactionSystem#288"))
6869
complete(rs)
6970
7071
end
7172
```
7273
!!! note
7374
The code that `save_reactionsystem` prints uses [programmatic modelling](@ref programmatic_CRN_construction) to generate the written model.
7475

75-
In addition to transferring models between Julia sessions, the `save_reactionsystem` function can also be used or print a model to a text file where you can easily inspect its components.
76+
In addition to transferring models between Julia sessions, the `save_reactionsystem` function can also be used to print a model to a text file where you can easily inspect its components.
7677

7778
## [Loading and saving arbitrary Julia variables using Serialization.jl](@id model_file_import_export_julia_serialisation)
7879
Julia provides a general and lightweight interface for loading and saving Julia structures to and from files that it can be good to be aware of. It is called [Serialization.jl](https://docs.julialang.org/en/v1/stdlib/Serialization/) and provides two functions, `serialize` and `deserialize`. The first allows us to write a Julia structure to a file. E.g. if we wish to save a parameter set associated with our model, we can use
@@ -92,55 +93,56 @@ loaded_sol # hide
9293
A general-purpose format for storing CRN models is so-called .net files. These can be generated by e.g. [BioNetGen](https://bionetgen.org/). The [ReactionNetworkImporters.jl](https://github.com/SciML/ReactionNetworkImporters.jl) package enables the loading of such files to Catalyst `ReactionSystem`. Here we load a [Repressilator](@ref basic_CRN_library_repressilator) model stored in the "repressilator.net" file:
9394
```julia
9495
using ReactionNetworkImporters
95-
prn = loadrxnetwork(BNGNetwork(), "repressilator.net")
96+
rn = loadrxnetwork(BNGNetwork(), "repressilator.net")
9697
```
97-
Here, .net files not only contain information regarding the reaction network itself, but also the numeric values (initial conditions and parameter values) required for simulating it. Hence, `loadrxnetwork` generates a `ParsedReactionNetwork` structure, containing all this information. You can access the model as `prn.rn`, the initial conditions as `prn.u0`, and the parameter values as `prn.p`. Furthermore, these initial conditions and parameter values are also made [*default* values](@ref dsl_advanced_options_default_vals) of the model.
98-
99-
A parsed reaction network's content can then be provided to various problem types for simulation. E.g. here we perform an ODE simulation of our repressilator model:
98+
Here, .net files not only contain information regarding the reaction network itself, but also the numeric values (initial conditions and parameter values) required for simulating it. The `loadrxnetwork` function loads the model as a normal `ReactionSystem` structure, but saves the initial conditions and parameter values as [*default* values](@ref dsl_advanced_options_default_vals) that are automatically accounted for in simulations. The loaded model can be provided to various problem types for simulation. E.g. here we perform an ODE simulation of our repressilator model:
10099
```julia
101100
using Catalyst, OrdinaryDiffEqDefault, Plots
102101
tspan = (0.0, 10000.0)
103-
oprob = ODEProblem(prn.rn, Float64[], tspan, Float64[])
102+
oprob = ODEProblem(rn, Float64[], tspan, Float64[])
104103
sol = solve(oprob)
105104
plot(sol; idxs = [:mTetR, :mLacI, :mCI])
106105
```
107106
![Repressilator Simulation](../assets/repressilator_sim_ReactionNetworkImporters.svg)
108107

109-
Note that, as all initial conditions and parameters have default values, we can provide empty vectors for these into our `ODEProblem`.
110-
108+
Note that, as all initial conditions and parameters have default values, we can provide empty vectors for these into our `ODEProblem`. To load these values from the model, the `get_u0_map(rn)` and `get_parameter_map(rn)` functions can be used.
111109

112110
!!! note
113111
It should be noted that .net files support a wide range of potential model features, not all of which are currently supported by ReactionNetworkImporters. Hence, there might be some .net files which `loadrxnetwork` will not be able to load.
114112

115113
A more detailed description of ReactionNetworkImporter's features can be found in its [documentation](https://docs.sciml.ai/ReactionNetworkImporters/stable/).
116114

117-
## [Loading SBML files using SBMLImporter.jl and SBMLToolkit.jl](@id model_file_import_export_sbml)
118-
The Systems Biology Markup Language (SBML) is the most widespread format for representing CRN models. Currently, there exist two different Julia packages, [SBMLImporter.jl](https://github.com/sebapersson/SBMLImporter.jl) and [SBMLToolkit.jl](https://github.com/SciML/SBMLToolkit.jl), that are able to load SBML files to Catalyst `ReactionSystem` structures. SBML is able to represent a *very* wide range of model features, with both packages supporting most features. However, there exist SBML files (typically containing obscure model features such as events with time delays) that currently cannot be loaded into Catalyst models.
115+
## [Loading SBML files using SBMLImporter.jl](@id model_file_import_export_sbml)
116+
The Systems Biology Markup Language (SBML) is the most widespread format for representing CRN models. Here, the [SBMLImporter.jl](https://github.com/sebapersson/SBMLImporter.jl) package is able to load SBML files to Catalyst `ReactionSystem` structures. SBML is able to represent a *very* wide range of model features, with SBMLImporter supporting most features. However, there exist SBML files (typically containing obscure model features such as events with time delays) that currently cannot be loaded into Catalyst models.
119117

120-
SBMLImporter's `load_SBML` function can be used to load SBML files. Here, we load a [Brusselator](@ref basic_CRN_library_brusselator) model stored in the "brusselator.xml" file:
118+
SBMLImporter's `load_SBML` function can be used to load SBML files. Here, we load a
119+
[Brusselator](@ref basic_CRN_library_brusselator) model stored in the "brusselator.xml"
120+
file:
121121
```julia
122122
using SBMLImporter
123-
prn, cbs = load_SBML("brusselator.xml", massaction = true)
123+
rn, cbs = load_SBML("brusselator.xml", massaction = true)
124124
```
125-
Here, while [ReactionNetworkImporters generates a `ParsedReactionSystem` only](@ref model_file_import_export_sbml_rni_net), SBMLImporter generates a `ParsedReactionSystem` (here stored in `prn`) and a [so-called `CallbackSet`](https://docs.sciml.ai/DiffEqDocs/stable/features/callback_functions/#CallbackSet) (here stored in `cbs`). While `prn` can be used to create various problems, when we simulate them, we must also supply `cbs`. E.g. to simulate our brusselator we use:
125+
126+
`load_SBML` returns two outputs: a `ReactionSystem` (`rn`) and a `CallbackSet` (`cbs`). The `CallbackSet` contains SBML events and callbacks generated from any SBML `piecewise` expressions. While `rn` can be used to create problems, when we simulate them, we must also supply `cbs`. The SBML file also contains simulation initial condition and parameter values. These are saved within the `rn` and can be extracted using the `get_u0_map` and `get_parameter_map` functions. Here we simulate the loaded brusselator model, providing `u0`, `ps`, and `cb` as described.
126127
```julia
127128
using Catalyst, OrdinaryDiffEqDefault, Plots
128129
tspan = (0.0, 50.0)
129-
oprob = ODEProblem(prn.rn, prn.u0, tspan, prn.p)
130+
u0 = get_u0_map(rn)
131+
ps = get_parameter_map(rn)
132+
oprob = ODEProblem(rn, u0, tspan, ps)
130133
sol = solve(oprob; callback = cbs)
131134
plot(sol)
132135
```
136+
133137
![Brusselator Simulation](../assets/brusselator_sim_SBMLImporter.svg)
134138

135-
Note that, while ReactionNetworkImporters adds initial condition and species values as default to the imported model, SBMLImporter does not do this. These must hence be provided to the `ODEProblem` directly.
139+
!!! note
140+
While ReactionNetworkImporters saves parameters (`ps`) and species values (`u0`) as default to the imported model, SBMLImporter does not do this. These must hence be explicitly loaded through `get_u0_map` and `get_parameter_map` and then provided to the `ODEProblem`.
136141

137142
A more detailed description of SBMLImporter's features can be found in its [documentation](https://sebapersson.github.io/SBMLImporter.jl/stable/).
138143

139144
!!! note
140-
The `massaction = true` option informs the importer that the target model follows mass-action principles. When given, this enables SBMLImporter to make appropriate modifications to the model (which are important for e.g. jump simulation performance).
141-
142-
### [SBMLImporter and SBMLToolkit](@id model_file_import_export_package_alts)
143-
Above, we described how to use SBMLImporter to import SBML files. Alternatively, SBMLToolkit can be used instead. It has a slightly different syntax, which is described in its [documentation](https://github.com/SciML/SBMLToolkit.jl), and does not support as wide a range of SBML features as SBMLImporter. A short comparison of the two packages can be found [here](https://github.com/sebapersson/SBMLImporter.jl?tab=readme-ov-file#differences-compared-to-sbmltoolkit). Generally, while they both perform well, we note that for *jump simulations* SBMLImporter is preferable (its way for internally representing reaction event enables more performant jump simulations).
145+
The `massaction = true` option informs the importer that the target model follows mass-action principles. When given, this enables SBMLImporter to make appropriate modifications to the model (which are important for jump simulation performance).
144146

145147
## [Loading models from matrix representation using ReactionNetworkImporters.jl](@id model_file_import_export_matrix_representations)
146148
While CRN models can be represented through various file formats, they can also be represented in various matrix forms. E.g. a CRN with $m$ species and $n$ reactions (and with constant rates) can be represented with either
@@ -155,32 +157,23 @@ The advantage of these forms is that they offer a compact and very general way t
155157
---
156158
## [Citations](@id petab_citations)
157159
If you use any of this functionality in your research, [in addition to Catalyst](@ref doc_index_citation), please cite the paper(s) corresponding to whichever package(s) you used:
158-
```
160+
```bibtex
159161
@software{2022ReactionNetworkImporters,
160162
author = {Isaacson, Samuel},
161163
title = {{ReactionNetworkImporters.jl}},
162164
howpublished = {\url{https://github.com/SciML/ReactionNetworkImporters.jl}},
163165
year = {2022}
164166
}
165167
```
166-
```
167-
@software{2024SBMLImporter,
168-
author = {Persson, Sebastian},
169-
title = {{SBMLImporter.jl}},
170-
howpublished = {\url{https://github.com/sebapersson/SBMLImporter.jl}},
171-
year = {2024}
172-
}
173-
```
174-
```
175-
@article{LangJainRackauckas+2024,
176-
url = {https://doi.org/10.1515/jib-2024-0003},
177-
title = {SBMLToolkit.jl: a Julia package for importing SBML into the SciML ecosystem},
178-
title = {},
179-
author = {Paul F. Lang and Anand Jain and Christopher Rackauckas},
180-
pages = {20240003},
181-
journal = {Journal of Integrative Bioinformatics},
182-
doi = {doi:10.1515/jib-2024-0003},
183-
year = {2024},
184-
lastchecked = {2024-06-02}
168+
```bibtex
169+
@article{PEtabBioinformatics2025,
170+
title={PEtab.jl: advancing the efficiency and utility of dynamic modelling},
171+
author={Persson, Sebastian and Fr{\"o}hlich, Fabian and Grein, Stephan and Loman, Torkel and Ognissanti, Damiano and Hasselgren, Viktor and Hasenauer, Jan and Cvijovic, Marija},
172+
journal={Bioinformatics},
173+
volume={41},
174+
number={9},
175+
pages={btaf497},
176+
year={2025},
177+
publisher={Oxford University Press}
185178
}
186179
```

0 commit comments

Comments
 (0)