|
| 1 | +# Loading Chemical Reaction Network Models from Files |
| 2 | +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/)). These enables e.g. 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 files to Catalyst `ReactionSystem`s. |
| 3 | + |
| 4 | +## Saving Catalyst models to, and loading them from, Julia files |
| 5 | +Catalyst provides a `save_reaction_system` function, enabling the user to save a `ReactionSystem` to a file. Here we demonstrate it by first creating a simple catalysis network |
| 6 | +```@example file_handling_1 |
| 7 | +using Catalyst |
| 8 | +rn = @reaction_network begin |
| 9 | + kB, S + E --> SE |
| 10 | + kD, SE --> S + E |
| 11 | + kP, SE --> P + E |
| 12 | +end |
| 13 | +``` |
| 14 | +and next saving it to a file |
| 15 | +```@example file_handling_1 |
| 16 | +save_reaction_system("reaction_network.jl", rn) |
| 17 | +``` |
| 18 | +Here, `save_reaction_system`'s first argument is the path to the file where we wish to save it. The second argument is the `ReactionSystem` we wish to save. To load the file, we use Julia's `include` function: |
| 19 | +```@example file_handling_1 |
| 20 | +loaded_rn = include("reaction_network.jl") |
| 21 | +``` |
| 22 | + |
| 23 | +!!! note |
| 24 | + The destination file can be in a folder. E.g. `save_reaction_system("my_folder/reaction_network.jl", rn)` saves the model to the file "reaction_network.jl" in the folder "my_folder". |
| 25 | + |
| 26 | +`include` is used to execute the Julia code from any file. This means that `save_reaction_system` 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, if we print the file we can confirm this: |
| 27 | +```@example file_handling_1 |
| 28 | +println(read("reaction_network.jl", String)) |
| 29 | +rm("reaction_network.jl") # hide |
| 30 | +``` |
| 31 | + |
| 32 | +## Loading and Saving arbitrary Julia variables using Serialization.jl |
| 33 | +Julia provide 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 allow 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 |
| 34 | +```@example file_handling_2 |
| 35 | +using Serialization |
| 36 | +ps = [:kB => 1.0, :kD => 0.1, :kP => 2.0] |
| 37 | +serialize("saved_parameters.jls", ps) |
| 38 | +``` |
| 39 | +Here, we use the extension ".jls" (standing for **J**u**L**ia **S**erialization), however, any can be used. To load a structure, we can then use |
| 40 | +```@example file_handling_2 |
| 41 | +loaded_sol = deserialize("saved_parameters.jls") |
| 42 | +``` |
| 43 | + |
| 44 | +## [Loading .net files usings ReactionNetworkImporters.jl](@id file_loading_rni_net) |
| 45 | +A general-purpose format for storing CRN models are 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](https://en.wikipedia.org/wiki/Repressilator) model stored in the "repressilator.net" file: |
| 46 | +```@example file_handling_3 |
| 47 | +using ReactionNetworkImporters |
| 48 | +cp("../assets/model_files/repressilator.net", "repressilator.net") # hide |
| 49 | +prn = loadrxnetwork(BNGNetwork(), "repressilator.net") |
| 50 | +rm("repressilator.net") # hide |
| 51 | +prn # hide |
| 52 | +``` |
| 53 | +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 ref) of the model. |
| 54 | + |
| 55 | +A parsed reaction network can be provided directly to various problem types for simulation. E.g. here we perform an ODE simulation of our repressilator model: |
| 56 | +```@example file_handling_3 |
| 57 | +using Catalyst, OrdinaryDiffEq, Plots |
| 58 | +tspan = (0.0, 1000.0) |
| 59 | +oprob = ODEProblem(prn, tspan) |
| 60 | +sol = solve(oprob) |
| 61 | +plot(sol) |
| 62 | +``` |
| 63 | + |
| 64 | +!!! note |
| 65 | + It should be noted that .net files supports 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. |
| 66 | + |
| 67 | +A more detailed description of ReactionNetworkImporter's features can be found in its [documentation](https://docs.sciml.ai/ReactionNetworkImporters/stable/). |
| 68 | + |
| 69 | +## Loading SBML files using SBMLImporter.jl and SBMLToolkit.jl |
| 70 | +The Systems Biology Markup Language (SBML) is the most widespread format for representing CRN models. Currently, there exists two different Julia packages, [SBMLImporter.jl](https://github.com/sebapersson/SBMLImporter.jl) and [SBMLToolkit.jl](https://github.com/SciML/SBMLToolkit.jl), both of which are able to load SBML files to Catalyst `ReactionSystem` structures. SBML is able to represent a *very* wide range of model featuresSome of these are not supported by these packages (or Catalyst). Hence, there exist SBML files (typically containing obscure model features such as events with time delays) that currently cannot be loaded into Catalyst models. |
| 71 | + |
| 72 | +SBMLImporter's `load_SBML` function can be used to load SBML files. Here, we load a [Brusselator](https://en.wikipedia.org/wiki/Brusselator) model stored in the "brusselator.xml" file: |
| 73 | +```@example file_handling_4 |
| 74 | +using SBMLImporter |
| 75 | +cp("../assets/model_files/brusselator.xml", "brusselator.xml") # hide |
| 76 | +prn, cbs = load_SBML("brusselator.xml") |
| 77 | +rm("brusselator.xml") # hide |
| 78 | +prn, cbs # hide |
| 79 | +``` |
| 80 | +Here, while [ReactionNetworkImporters generates a `ParsedReactionSystem` only](@ref file_loading_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: |
| 81 | +```@example file_handling_4 |
| 82 | +using Catalyst, OrdinaryDiffEq, Plots |
| 83 | +tspan = (0.0, 1000.0) |
| 84 | +oprob = ODEProblem(prn, tspan) |
| 85 | +sol = solve(oprob; cb=cbs) |
| 86 | +plot(sol) |
| 87 | +``` |
| 88 | + |
| 89 | +A more detailed description of SBMLImporter's features can be found in its [documentation](https://docs.sciml.ai/ReactionNetworkImporters/stable/). |
| 90 | + |
| 91 | +### SBMLImporter and SBMLToolkit |
| 92 | +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). 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). |
| 93 | + |
| 94 | +## Loading models from matrix representation using ReactionNetworkImporters.jl |
| 95 | +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 |
| 96 | +- An $mxn$ substrate matrix (with each species's substrate stoichiometry in each reaction) and an $nxm$ product matrix (with each species's product stoichiometry in each reaction). |
| 97 | + |
| 98 | +Or |
| 99 | +- An $mxn$ complex stoichiometric matrix (...) and a $2mxn$ incidence matrix (...). |
| 100 | + |
| 101 | +The advantage with these forms is that they offer a compact and very general way to represent a large class of CRNs. ReactionNetworkImporters have the functionality for converting matrices of these forms directly into Catalyst `ReactionSystem` models. Instructions on how to do this are available in [ReactionNetworkImporter's documentation](https://docs.sciml.ai/ReactionNetworkImporters/stable/#Loading-a-matrix-representation). |
0 commit comments