Generate C Code outside of R
GenerateCodeInSHELL.Rmd
The github repository of SBtabVFGEN
(systems biology table vector field generator) also includes an Rscript
called sbtab_to_vfgen
. This exists for convenience and ease
of automation.
The model probably exists as a git repository (or another version control system). Perhaps it uses github-actions or something similar.
When the model changes, it is convenient to also immediately change the C source code.
This can be done directly from the command line, like this:
sbtab_to_vfgen *.tsv # no conservation law analysis
sbtab_to_vfgen --cla *.tsv # with conservation law analysis
sbtab_to_vfgen *.ods *.xlsx # for an ods file, or xlsx file
Here, we didn’t have to launch R explicitly; the commands still run in R. If github actions are used, R continues to be a dependency in the build environment.
See also PATH variable explanations, about how to call scripts from any location.
This script creates the .vf
, .xml
, and .mod
files, as well as the optional ConservationLaws.{h5,RDS}
(an hdf5 file, and an RDS file), with the conservation laws; just like
from within R.
To generate the c source code we use the
sh/ode.sh
script from RPN-derivative:
Compile:
gcc -shared -fPIC -O2 -o myModel.so myModel_gvf.c
The _gvf
suffix is not mandatory, this is how
vfgen gsl
calls such files.
R code can be created as well (pick some combination of options):
This does not need to be compiled, it is plain R code for deSolve.
Generated Code
The files generated in this manner only need to be re-created if you change the model structurally. A change in initial conditions, or parameter values will result in largely the same code, except for the function that returns the default initial conditions and default parameter values.
Once you have myModel_gvf.c
you can either convert it to
a shared library yourself, or use the checkModel
function
included in uqsa
:
The simulator will guess the name ./myModel.so
if you
supply modelName="myModel"
, for other locations, attach the
path to modelName
as a comment.
GNU make
All of these can be automated further with a
Makefile
:
.PHONY: all
model = myModel
all: $(model).so $(model).R
%.R: %.tar.gz
ode -R --maxima $^ > $@
%_gvf.c: %.tar.gz
ode -C --maxima $^ > $@
%.vf %.tar.gz %.zip: *.tsv
sbtab_to_vfgen $^
%.so: %_gvf.c
gcc -shared -fPIC -O2 -o $@ $^
Calling make
with no arguments in the project directory
will then re build the model if anything has changed in the tsv
files.
for Open Document Spreadsheets
A very similar file, for models stored as .ods
spreadsheets, and using the prefix function of GNU make:
.PHONY: all
all: $(addprefix myModel, .so .R)
%.R: %.tar.gz
ode -R --maxima $^ > $@
%_gvf.c: %.tar.gz
ode -C --maxima $^ > $@
%.vf %.tar.gz: %.ods
sbtab_to_vfgen $^
%.so: %_gvf.c
gcc -shared -fPIC -O2 -o $@ $^