Unverified Commit 81d112ec authored by Miroslav Kratochvil's avatar Miroslav Kratochvil Committed by GitHub
Browse files

Merge pull request #50 from LCSB-BioCore/mk-merge-49

merge cleaned-up #49
parents e56c5eda e24275c8
...@@ -5,12 +5,12 @@ This package provides a straightforward way to load model- and ...@@ -5,12 +5,12 @@ This package provides a straightforward way to load model- and
simulation-relevant information from SBML files. simulation-relevant information from SBML files.
The library provides a single function [`readSBML`](@ref) to load a The library provides a single function [`readSBML`](@ref) to load a
[`Model`](@ref): [`SBML.Model`](@ref):
```julia ```julia
julia> using SBML julia> using SBML
julia> mdl = readSBML("Ec_core_flux1.xml") julia> mdl = readSBML("Ec_core_flux1.xml")
Model() SBML.Model()
julia> mdl.compartments julia> mdl.compartments
2-element Array{String,1}: 2-element Array{String,1}:
......
...@@ -12,6 +12,6 @@ include("utils.jl") ...@@ -12,6 +12,6 @@ include("utils.jl")
sbml = (sym::Symbol) -> dlsym(SBML_jll.libsbml_handle, sym) sbml = (sym::Symbol) -> dlsym(SBML_jll.libsbml_handle, sym)
export SBMLVersion, export SBMLVersion,
readSBML, Model, UnitPart, Species, Reaction, getS, getLBs, getUBs, getOCs readSBML, getS, getLBs, getUBs, getOCs
end # module end # module
...@@ -75,11 +75,11 @@ function get_optional_double(x::VPtr, is_sym, get_sym)::Maybe{Float64} ...@@ -75,11 +75,11 @@ function get_optional_double(x::VPtr, is_sym, get_sym)::Maybe{Float64}
end end
""" """
function readSBML(fn::String)::Model function readSBML(fn::String)::SBML.Model
Read the SBML from a XML file in `fn` and return the contained `Model`. Read the SBML from a XML file in `fn` and return the contained `SBML.Model`.
""" """
function readSBML(fn::String)::Model function readSBML(fn::String)::SBML.Model
doc = ccall(sbml(:readSBML), VPtr, (Cstring,), fn) doc = ccall(sbml(:readSBML), VPtr, (Cstring,), fn)
try try
n_errs = ccall(sbml(:SBMLDocument_getNumErrors), Cuint, (VPtr,), doc) n_errs = ccall(sbml(:SBMLDocument_getNumErrors), Cuint, (VPtr,), doc)
...@@ -138,12 +138,12 @@ end ...@@ -138,12 +138,12 @@ end
"""" """"
function extractModel(mdl::VPtr)::Model function extractModel(mdl::VPtr)::SBML.Model
Take the `SBMLModel_t` pointer and extract all information required to make a Take the `SBMLModel_t` pointer and extract all information required to make a
valid [`Model`](@ref) structure. valid [`SBML.Model`](@ref) structure.
""" """
function extractModel(mdl::VPtr)::Model function extractModel(mdl::VPtr)::SBML.Model
# get the FBC plugin pointer (FbcModelPlugin_t) # 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")
...@@ -157,14 +157,14 @@ function extractModel(mdl::VPtr)::Model ...@@ -157,14 +157,14 @@ function extractModel(mdl::VPtr)::Model
end end
# parse out the unit definitions # parse out the unit definitions
units = Dict{String,Vector{UnitPart}}() units = Dict{String,Vector{SBML.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)
id = get_string(ud, :UnitDefinition_getId) id = get_string(ud, :UnitDefinition_getId)
units[id] = [ units[id] = [
begin begin
u = ccall(sbml(:UnitDefinition_getUnit), VPtr, (VPtr, Cuint), ud, j - 1) u = ccall(sbml(:UnitDefinition_getUnit), VPtr, (VPtr, Cuint), ud, j - 1)
UnitPart( SBML.UnitPart(
unsafe_string( unsafe_string(
ccall( ccall(
sbml(:UnitKind_toString), sbml(:UnitKind_toString),
......
...@@ -11,14 +11,14 @@ const Maybe{X} = Union{Nothing,X} ...@@ -11,14 +11,14 @@ const Maybe{X} = Union{Nothing,X}
Part of a measurement unit definition that corresponds to the SBML definition Part of a measurement unit definition that corresponds to the SBML definition
of `Unit`. For example, the unit "per square megahour", Mh^(-2), is written as: of `Unit`. For example, the unit "per square megahour", Mh^(-2), is written as:
UnitPart("second", # base SI unit, this says we are measuring time SBML.UnitPart("second", # base SI unit, this says we are measuring time
-2, # exponent, says "per square" -2, # exponent, says "per square"
6, # log-10 scale of the unit, says "mega" 6, # log-10 scale of the unit, says "mega"
1/3600) # second-to-hour multiplier 1/3600) # second-to-hour multiplier
Compound units (such as "volt-amperes" and "dozens of yards per ounce") are Compound units (such as "volt-amperes" and "dozens of yards per ounce") are
built from multiple `UnitPart`s; see the definition of field `units` in built from multiple `UnitPart`s; see the definition of field `units` in
[`Model`](@ref). [`SBML.Model`](@ref).
""" """
struct UnitPart struct UnitPart
kind::String kind::String
......
""" """
function getS(m::Model; zeros=spzeros)::Tuple{Vector{String},Vector{String},AbstractMatrix{Float64}} function getS(m::SBML.Model; zeros=spzeros)::Tuple{Vector{String},Vector{String},AbstractMatrix{Float64}}
Extract the vector of species (aka metabolite) identifiers, vector of reaction Extract the vector of species (aka metabolite) identifiers, vector of reaction
identifiers, and the (dense) stoichiometry matrix from an existing `Model`. identifiers, and the (dense) stoichiometry matrix from an existing `SBML.Model`.
Returns a tuple with these values. Returns a tuple with these values.
The matrix is sparse by default (initially constructed by The matrix is sparse by default (initially constructed by
...@@ -10,7 +10,7 @@ The matrix is sparse by default (initially constructed by ...@@ -10,7 +10,7 @@ The matrix is sparse by default (initially constructed by
argument `zeros`; e.g. running with `zeros=zeros` will produce a dense matrix. argument `zeros`; e.g. running with `zeros=zeros` will produce a dense matrix.
""" """
function getS( function getS(
m::Model; m::SBML.Model;
zeros = spzeros, zeros = spzeros,
)::Tuple{Vector{String},Vector{String},AbstractMatrix{Float64}} )::Tuple{Vector{String},Vector{String},AbstractMatrix{Float64}}
rows = [k for k in keys(m.species)] rows = [k for k in keys(m.species)]
...@@ -25,30 +25,30 @@ function getS( ...@@ -25,30 +25,30 @@ function getS(
end end
""" """
function getLBs(m::Model)::Vector{Tuple{Float64,String}} function getLBs(m::SBML.Model)::Vector{Tuple{Float64,String}}
Extract a vector of lower bounds of reaction rates from the model. All bounds Extract a vector of lower bounds of reaction rates from the model. All bounds
are accompanied with the unit of the corresponding value (this behavior is are accompanied with the unit of the corresponding value (this behavior is
based on SBML specification). based on SBML specification).
""" """
function getLBs(m::Model)::Vector{Tuple{Float64,String}} function getLBs(m::SBML.Model)::Vector{Tuple{Float64,String}}
return broadcast(x -> x.lb, values(m.reactions)) return broadcast(x -> x.lb, values(m.reactions))
end end
""" """
function getUBs(m::Model)::Vector{Tuple{Float64,String}} function getUBs(m::SBML.Model)::Vector{Tuple{Float64,String}}
Likewise to `getLBs`, extract the upper bounds. Likewise to `getLBs`, extract the upper bounds.
""" """
function getUBs(m::Model)::Vector{Tuple{Float64,String}} function getUBs(m::SBML.Model)::Vector{Tuple{Float64,String}}
return broadcast(x -> x.ub, values(m.reactions)) return broadcast(x -> x.ub, values(m.reactions))
end end
""" """
function getOCs(m::Model)::Vector{Float64} function getOCs(m::SBML.Model)::Vector{Float64}
Extract the vector of objective coefficients of each reaction. Extract the vector of objective coefficients of each reaction.
""" """
function getOCs(m::Model)::Vector{Float64} function getOCs(m::SBML.Model)::Vector{Float64}
return broadcast(x -> x.oc, values(m.reactions)) return broadcast(x -> x.oc, values(m.reactions))
end end
using Test, SHA, SparseArrays using Test, SHA, SparseArrays
using SBML using SBML
using SBML: Model, Reaction, Species
import Pkg import Pkg
@testset "SBML test suite" begin @testset "SBML test suite" begin
......
Supports Markdown
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