Skip to content

Commit

Permalink
Add support for more reaction attributes to writeSBML
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Jun 22, 2022
1 parent 68d5f0d commit 9a01866
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 41 deletions.
122 changes: 81 additions & 41 deletions src/writesbml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@ const WRITESBML_PKG_DEFAULT_LEVEL = 3
const WRITESBML_PKG_DEFAULT_VERSION = 1
const WRITESBML_PKG_DEFAULT_PKGVERSION = 2

function get_parameter_ptr!(parameter_ptr::VPtr, id::String, parameter::Parameter)::VPtr
ccall(sbml(:Parameter_setId), Cint, (VPtr, Cstring), parameter_ptr, id)
isnothing(parameter.name) || ccall(
sbml(:Parameter_setName),
Cint,
(VPtr, Cstring),
parameter_ptr,
parameter.name,
)
isnothing(parameter.value) || ccall(
sbml(:Parameter_setValue),
Cint,
(VPtr, Cdouble),
parameter_ptr,
parameter.value,
)
isnothing(parameter.units) || ccall(
sbml(:Parameter_setUnits),
Cint,
(VPtr, Cstring),
parameter_ptr,
parameter.units,
)
isnothing(parameter.constant) || ccall(
sbml(:Parameter_setConstant),
Cint,
(VPtr, Cint),
parameter_ptr,
Cint(parameter.constant),
)
return parameter_ptr
end

function get_parameter_ptr(id::String, parameter::Parameter)::VPtr
parameter_ptr = ccall(
sbml(:Parameter_create),
VPtr,
(Cuint, Cuint),
WRITESBML_DEFAULT_LEVEL,
WRITESBML_DEFAULT_VERSION,
)
return get_parameter_ptr!(parameter_ptr, id, parameter)
end

function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
# Create the model pointer
model = ccall(sbml(:SBMLDocument_createModel), VPtr, (VPtr,), doc)
Expand All @@ -25,43 +69,8 @@ function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr

# Add parameters
for (id, parameter) in mdl.parameters
parameter_t = ccall(
sbml(:Parameter_create),
VPtr,
(Cuint, Cuint),
WRITESBML_DEFAULT_LEVEL,
WRITESBML_DEFAULT_VERSION,
)
ccall(sbml(:Parameter_setId), Cint, (VPtr, Cstring), parameter_t, id)
isnothing(parameter.name) || ccall(
sbml(:Parameter_setName),
Cint,
(VPtr, Cstring),
parameter_t,
parameter.name,
)
isnothing(parameter.value) || ccall(
sbml(:Parameter_setValue),
Cint,
(VPtr, Cdouble),
parameter_t,
parameter.value,
)
isnothing(parameter.units) || ccall(
sbml(:Parameter_setUnits),
Cint,
(VPtr, Cstring),
parameter_t,
parameter.units,
)
isnothing(parameter.constant) || ccall(
sbml(:Parameter_setConstant),
Cint,
(VPtr, Cint),
parameter_t,
Cint(parameter.constant),
)
res = ccall(sbml(:Model_addParameter), Cint, (VPtr, VPtr), model, parameter_t)
parameter_ptr = get_parameter_ptr(id, parameter)
res = ccall(sbml(:Model_addParameter), Cint, (VPtr, VPtr), model, parameter_ptr)
!iszero(res) &&
@warn "Failed to add parameter \"$(id)\": $(OPERATION_RETURN_VALUES[res])"
end
Expand Down Expand Up @@ -330,11 +339,42 @@ function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
true,
)
end
# TODO: add kinetic parameters
# TODO: add lower bound
# TODO: add upper bound
if !isempty(reaction.kinetic_parameters) || !isnothing(reaction.kinetic_math)
kinetic_law_ptr =
ccall(sbml(:Reaction_createKineticLaw), VPtr, (VPtr,), reaction_ptr)
for (id, parameter) in reaction.kinetic_parameters
error()
parameter_ptr =
ccall(sbml(:KineticLaw_createParameter), VPtr, (VPtr,), kinetic_law_ptr)
get_parameter_ptr!(parameter_ptr, id, parameter)
end
isnothing(reaction.kinetic_math) || ccall(
sbml(:KineticLaw_setMath),
Cint,
(VPtr, VPtr),
kinetic_law_ptr,
get_astnode_ptr(reaction.kinetic_math),
)
end
if !isnothing(reaction.lower_bound) || !isnothing(reaction.upper_bound)
reaction_fbc_ptr =
ccall(sbml(:SBase_getPlugin), VPtr, (VPtr, Cstring), reaction_ptr, "fbc")
isnothing(reaction.lower_bound) || ccall(
sbml(:FbcReactionPlugin_setLowerFluxBound),
Cint,
(VPtr, Cstring),
reaction_fbc_ptr,
reaction.lower_bound,
)
isnothing(reaction.upper_bound) || ccall(
sbml(:FbcReactionPlugin_setUpperFluxBound),
Cint,
(VPtr, Cstring),
reaction_fbc_ptr,
reaction.upper_bound,
)
end
# TODO: add gene product association
# TODO: add kinetic math
isnothing(reaction.notes) || ccall(
sbml(:SBase_setNotesString),
Cint,
Expand Down
76 changes: 76 additions & 0 deletions test/data/Dasgupta2020-written.xml
Original file line number Diff line number Diff line change
Expand Up @@ -414,31 +414,107 @@ response to activation both in 2D and in 3D</p>
<listOfProducts>
<speciesReference species="N" stoichiometry="1" constant="true"/>
</listOfProducts>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci> Cell </ci>
<apply>
<ci> R4_flux </ci>
<ci> k2 </ci>
<ci> P </ci>
</apply>
</apply>
</math>
</kineticLaw>
</reaction>
<reaction id="R3" name="R3" reversible="false">
<listOfProducts>
<speciesReference species="P" stoichiometry="1" constant="true"/>
</listOfProducts>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci> Cell </ci>
<apply>
<ci> R3_flux </ci>
<ci> k_1 </ci>
<ci> N </ci>
</apply>
</apply>
</math>
</kineticLaw>
</reaction>
<reaction id="R6" name="R6" reversible="false">
<listOfReactants>
<speciesReference species="N" stoichiometry="1" constant="true"/>
</listOfReactants>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci> Cell </ci>
<ci> k3 </ci>
<ci> N </ci>
</apply>
</math>
</kineticLaw>
</reaction>
<reaction id="R1" name="R1" reversible="false">
<listOfReactants>
<speciesReference species="P" stoichiometry="1" constant="true"/>
</listOfReactants>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>