Commit da50e144 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil
Browse files

document the new functions

(readModel has certainly grown a bit)
parent 9b477ad3
...@@ -31,6 +31,13 @@ function readSBML(fn::String)::Model ...@@ -31,6 +31,13 @@ function readSBML(fn::String)::Model
end end
end end
"""
function getOptionalString(x::VPtr, fn_sym)::Maybe{String}
C-call the SBML function `fn_sym` with a single parameter `x`, interpret the result as a nullable string pointer and return appropriately.
This is used to get notes and annotations and several other things (see `getNotes`, `getAnnotations`)
"""
function getOptionalString(x::VPtr, fn_sym)::Maybe{String} function getOptionalString(x::VPtr, fn_sym)::Maybe{String}
str = ccall(sbml(fn_sym), Cstring, (VPtr,), x) str = ccall(sbml(fn_sym), Cstring, (VPtr,), x)
if str != C_NULL if str != C_NULL
...@@ -43,9 +50,17 @@ end ...@@ -43,9 +50,17 @@ end
getNotes(x::VPtr)::Maybe{String} = getOptionalString(x, :SBase_getNotesString) getNotes(x::VPtr)::Maybe{String} = getOptionalString(x, :SBase_getNotesString)
getAnnotation(x::VPtr)::Maybe{String} = getOptionalString(x, :SBase_getAnnotationString) getAnnotation(x::VPtr)::Maybe{String} = getOptionalString(x, :SBase_getAnnotationString)
"""
function extractModel(mdl::VPtr)::Model
Take the `SBMLModel_t` pointer and extract all information required to make a
valid [`Model`](@ref) structure.
"""
function extractModel(mdl::VPtr)::Model function extractModel(mdl::VPtr)::Model
# get the FBC plugin pointer (FbcModelPlugin_t)
mdl_fbc = ccall(sbml(:SBase_getPlugin), VPtr, (VPtr, Cstring), mdl, "fbc") mdl_fbc = ccall(sbml(:SBase_getPlugin), VPtr, (VPtr, Cstring), mdl, "fbc")
# get the parameters
parameters = Dict{String,Float64}() parameters = Dict{String,Float64}()
for i = 1:ccall(sbml(:Model_getNumParameters), Cuint, (VPtr,), mdl) for i = 1:ccall(sbml(:Model_getNumParameters), Cuint, (VPtr,), mdl)
p = ccall(sbml(:Model_getParameter), VPtr, (VPtr, Cuint), mdl, i - 1) p = ccall(sbml(:Model_getParameter), VPtr, (VPtr, Cuint), mdl, i - 1)
...@@ -54,6 +69,7 @@ function extractModel(mdl::VPtr)::Model ...@@ -54,6 +69,7 @@ function extractModel(mdl::VPtr)::Model
parameters[id] = v parameters[id] = v
end end
# parse out the unit definitions
units = Dict{String,Vector{UnitPart}}() units = Dict{String,Vector{UnitPart}}()
for i = 1:ccall(sbml(:Model_getNumUnitDefinitions), Cuint, (VPtr,), mdl) for i = 1:ccall(sbml(:Model_getNumUnitDefinitions), Cuint, (VPtr,), mdl)
ud = ccall(sbml(:Model_getUnitDefinition), VPtr, (VPtr, Cuint), mdl, i - 1) ud = ccall(sbml(:Model_getUnitDefinition), VPtr, (VPtr, Cuint), mdl, i - 1)
...@@ -78,6 +94,7 @@ function extractModel(mdl::VPtr)::Model ...@@ -78,6 +94,7 @@ function extractModel(mdl::VPtr)::Model
] ]
end end
# parse out compartment names
compartments = [ compartments = [
unsafe_string( unsafe_string(
ccall( ccall(
...@@ -89,13 +106,15 @@ function extractModel(mdl::VPtr)::Model ...@@ -89,13 +106,15 @@ function extractModel(mdl::VPtr)::Model
) for i = 1:ccall(sbml(:Model_getNumCompartments), Cuint, (VPtr,), mdl) ) for i = 1:ccall(sbml(:Model_getNumCompartments), Cuint, (VPtr,), mdl)
] ]
# parse out species
species = Dict{String,Species}() species = Dict{String,Species}()
for i = 1:ccall(sbml(:Model_getNumSpecies), Cuint, (VPtr,), mdl) for i = 1:ccall(sbml(:Model_getNumSpecies), Cuint, (VPtr,), mdl)
sp = ccall(sbml(:Model_getSpecies), VPtr, (VPtr, Cuint), mdl, i - 1) sp = ccall(sbml(:Model_getSpecies), VPtr, (VPtr, Cuint), mdl, i - 1)
sp_fbc = ccall(sbml(:SBase_getPlugin), VPtr, (VPtr, Cstring), sp, "fbc") sp_fbc = ccall(sbml(:SBase_getPlugin), VPtr, (VPtr, Cstring), sp, "fbc") # FbcSpeciesPlugin_t
formula = "" formula = ""
charge = nothing charge = nothing
if sp_fbc != C_NULL if sp_fbc != C_NULL
# if the FBC plugin is present, try to get the chemical formula and charge
if 0 != if 0 !=
ccall(sbml(:FbcSpeciesPlugin_isSetChemicalFormula), Cint, (VPtr,), sp_fbc) ccall(sbml(:FbcSpeciesPlugin_isSetChemicalFormula), Cint, (VPtr,), sp_fbc)
formula = unsafe_string( formula = unsafe_string(
...@@ -121,6 +140,8 @@ function extractModel(mdl::VPtr)::Model ...@@ -121,6 +140,8 @@ function extractModel(mdl::VPtr)::Model
) )
end end
# parse out the flux objectives (these are complementary to the objectives
# that appear in the reactions, see comments lower)
objectives_fbc = Dict{String,Float64}() objectives_fbc = Dict{String,Float64}()
if mdl_fbc != C_NULL if mdl_fbc != C_NULL
for i = 1:ccall(sbml(:FbcModelPlugin_getNumObjectives), Cuint, (VPtr,), mdl_fbc) for i = 1:ccall(sbml(:FbcModelPlugin_getNumObjectives), Cuint, (VPtr,), mdl_fbc)
...@@ -131,7 +152,7 @@ function extractModel(mdl::VPtr)::Model ...@@ -131,7 +152,7 @@ function extractModel(mdl::VPtr)::Model
mdl_fbc, mdl_fbc,
i - 1, i - 1,
) )
# this part seems missing in C API docs...
for j = 1:ccall(sbml(:Objective_getNumFluxObjectives), Cuint, (VPtr,), o) for j = 1:ccall(sbml(:Objective_getNumFluxObjectives), Cuint, (VPtr,), o)
fo = ccall(sbml(:Objective_getFluxObjective), VPtr, (VPtr, Cuint), o, j - 1) fo = ccall(sbml(:Objective_getFluxObjective), VPtr, (VPtr, Cuint), o, j - 1)
objectives_fbc[unsafe_string( objectives_fbc[unsafe_string(
...@@ -141,13 +162,15 @@ function extractModel(mdl::VPtr)::Model ...@@ -141,13 +162,15 @@ function extractModel(mdl::VPtr)::Model
end end
end end
# reactions!
reactions = Dict{String,Reaction}() reactions = Dict{String,Reaction}()
for i = 1:ccall(sbml(:Model_getNumReactions), Cuint, (VPtr,), mdl) for i = 1:ccall(sbml(:Model_getNumReactions), Cuint, (VPtr,), mdl)
re = ccall(sbml(:Model_getReaction), VPtr, (VPtr, Cuint), mdl, i - 1) re = ccall(sbml(:Model_getReaction), VPtr, (VPtr, Cuint), mdl, i - 1)
lb = (-Inf, "") lb = (-Inf, "") # (bound value, unit id)
ub = (Inf, "") ub = (Inf, "")
oc = 0.0 oc = 0.0
# kinetic laws store a second version of the bounds and objectives
kl = ccall(sbml(:Reaction_getKineticLaw), VPtr, (VPtr,), re) kl = ccall(sbml(:Reaction_getKineticLaw), VPtr, (VPtr,), re)
if kl != C_NULL if kl != C_NULL
for j = 1:ccall(sbml(:KineticLaw_getNumParameters), Cuint, (VPtr,), kl) for j = 1:ccall(sbml(:KineticLaw_getNumParameters), Cuint, (VPtr,), kl)
...@@ -187,6 +210,7 @@ function extractModel(mdl::VPtr)::Model ...@@ -187,6 +210,7 @@ function extractModel(mdl::VPtr)::Model
end end
end end
# extract stoichiometry
stoi = Dict{String,Float64}() stoi = Dict{String,Float64}()
add_stoi = add_stoi =
(sr, factor) -> (sr, factor) ->
...@@ -196,6 +220,7 @@ function extractModel(mdl::VPtr)::Model ...@@ -196,6 +220,7 @@ function extractModel(mdl::VPtr)::Model
ccall(sbml(:SpeciesReference_getStoichiometry), Cdouble, (VPtr,), sr) * ccall(sbml(:SpeciesReference_getStoichiometry), Cdouble, (VPtr,), sr) *
factor factor
# reactants and products
for j = 1:ccall(sbml(:Reaction_getNumReactants), Cuint, (VPtr,), re) for j = 1:ccall(sbml(:Reaction_getNumReactants), Cuint, (VPtr,), re)
sr = ccall(sbml(:Reaction_getReactant), VPtr, (VPtr, Cuint), re, j - 1) sr = ccall(sbml(:Reaction_getReactant), VPtr, (VPtr, Cuint), re, j - 1)
add_stoi(sr, -1) add_stoi(sr, -1)
...@@ -217,6 +242,7 @@ function extractModel(mdl::VPtr)::Model ...@@ -217,6 +242,7 @@ function extractModel(mdl::VPtr)::Model
) )
end end
# extract gene products
gene_products = Dict{String,GeneProduct}() gene_products = Dict{String,GeneProduct}()
if mdl_fbc != C_NULL if mdl_fbc != C_NULL
for i = 1:ccall(sbml(:FbcModelPlugin_getNumGeneProducts), Cuint, (VPtr,), mdl_fbc) for i = 1:ccall(sbml(:FbcModelPlugin_getNumGeneProducts), Cuint, (VPtr,), mdl_fbc)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment