Build C and R source for a model
shortMakeSharedLibrary.Rmd
This is a short summary of how to make models for the simulator in the rgsl package:
Steps | tl;dr | |
---|---|---|
1 | convert SBtab to ODE | biology => math |
2 | convert the ODE to sources | math => code |
3 | convert sources to shared library | code => .so
|
The GSL solvers that rgsl uses will load this .so
file (myModel.so
).
RPN-derivative calculates derivatives or offloads that work on maxima or yacas, install them if you want to use the --maxima
or --yacas
options.
Note: SBtab is a kind of content, but several file-types can store it (see Summary section below).
If you are on MAC, please install the homebrew package manager to install essential software that everyone should have (a C compiler, pkg-config, etc.). We recommend that you do not use official apple software like xcode.
Create a Vector Field for ODE solvers
Here we assume that you wrote your own model in SBtab. For testing you can also download one of our example models. All code here is meant to be run in a terminal with a POSIX shell (bash, zsh, dash, ash) should all work (this is not usually pre-installed on windows).
Please be aware that some of the scripts use awk
and sed
, which may differ on different platforms (there is one true awk, GNU gawk
, mawk
, etc.). They differ in options they accept and regular expressions they understand.
We further assume that the format is a collection of tsv files.
The SBtabVFGEN package contains a file called sbtab_to_vfgen
(it is a script, an RScript, R needed). Copy it into the directory of the model (or alias itm or symlink it, whatever you prefer)
./sbtab_to_vfgen *.tsv
Afterwards, the directory should also contain a .vf
file as well as an sbml .xml
file and a zip file (with essentially the same content as the vf file).
Make Source Code for your Model
To convert the ordinary differential equations into code for solvers, we need to calculate the appropriate expressions for the jacobian. To do these calculations you should either download the contents of the sh
directory in the icpm-kth/RPN-derivative, or clone the entire repository (and optionally make install
).
If you want to run the conversion from ODE to code from any directory, the easiest way is to set an alias: so, here we alias
the ode.sh
script. An alternative is to write out the path to it every time, or set a variable with the full path (the script loads other files so doesn’t work if copied without the others). Here we assume that the repository clone lies in your $HOME
directory.
alias ode.sh='~/RPN-derivative/sh/ode.sh'
ode.sh -C --maxima myModel.vf > myModel_gvf.c
[ -f myModel_gvf.c ] && gcc -shared -fPIC -O2 -o myModel.so myModel_gvf.c
Now you have a .c
source file and a shared library .so
file (unless the model has errors, or the model is too complex for this translation).
Summary
Here you can find code blocks for each storage format.
SBtab as TSV
This is a summary of the code in the previous section:
alias sbtab_to_vfgen='~/SBtabVFGEN/sbtab_to_vfgen'
alias ode.sh='~/RPN-derivative/sh/ode.sh'
./sbtab_to_vfgen *.tsv
ode.sh -C myModel.vf > myModel_gvf.c
ode.sh -R myModel.vf > myModel.R
[ -f myModel_gvf.c ] && gcc -shared -fPIC -O2 -o myModel.so myModel_gvf.c
SBtab as XLSX
Here the same procedure applies, but we alias sbtab_to_vfgen
rather than copying it this time. Also, this time we use yacas to calculate the derivatives, rather than maxima. We create both C code and R code this time.
alias sbtab_to_vfgen='~/SBtabVFGEN/sbtab_to_vfgen'
alias ode.sh='~/RPN-derivative/sh/ode.sh'
./sbtab_to_vfgen myModel.xlsx
ode.sh -C --yacas myModel.vf > myModel_gvf.c
ode.sh -R --yacas myModel.vf > myModel.R
[ -f myModel_gvf.c ] && gcc -shared -fPIC -O2 -o myModel.so myModel_gvf.c
SBtab as ODS
We repeat the same procedure as with XLSX, but the model name is a variable, and the C compiler is whatever target cc
links to:
modelName='myModel'
alias sbtab_to_vfgen='~/SBtabVFGEN/sbtab_to_vfgen'
alias ode.sh='~/RPN-derivative/sh/ode.sh'
./sbtab_to_vfgen ${modelName}.ods
ode.sh -C --yacas ${modelName}.vf > ${modelName}_gvf.c
ode.sh -R --yacas ${modelName}.vf > ${modelName}.R
[ -f ${modelName}_gvf.c ] && cc -shared -fPIC -O2 -o ${modelName}.so ${modelName}_gvf.c
Comments
Now you should have C sources and a shared library, or error messages about the SBtab content (if the files contain errors).
The simulator can use the shared library directly. But take into account that ${modelName}.so
is machine specific and will not work if you copy it onto a completely different kind of machine (it may work if the two are similar enough).
Please take time to open and inspect the C sources and try to spot obvious mistakes.
RPN-derivative
The RPN-derivative package can also work without maxima or yacas, but needs to be compiled for this to work. Do this in the RPN-derivative’s root directory:
make
make test
sudo make install
If you get error messages about pkg-config
not being found, install it (you are probably on MAC: brew install pkg-config
). If you get error messages about missing directories, make them (mkdir /path/to/missing/directory
).