Commit 0f627b4d authored by Miroslav Kratochvil's avatar Miroslav Kratochvil
Browse files

add the conversion to normal Julia datatypes

parent 42bfd0a1
......@@ -30,3 +30,16 @@ julia> using SBML
julia> getLibSBMLDottedVersion()
"5.19.0"
```
## Usage
```julia
using SBML
m = readSBML("myModel.xml")
# m is now a Model with
m.reactions
m.species
m.compartments
...
```
......@@ -66,7 +66,7 @@ model_data read_sbml(const std::string& fn) {
for (unsigned j = 0; j < ud.getNumUnits(); ++j) {
Unit& u = *ud.getUnit(j);
m.units.emplace_back(
unit_part{ud.getName(), UnitKind_toString(u.getKind()),
unit_part{ud.getId(), UnitKind_toString(u.getKind()),
u.getExponent(), u.getScale(), u.getMultiplier()});
}
}
......
module SBML
include("readsbml.jl")
using CxxWrap
using SBML_jll
@wrapmodule(libsbml)
export getLibSBMLDottedVersion,
readSBML
function __init__()
@initcxx
end
@wrapmodule(libsbml)
include("readsbml.jl")
export getLibSBMLDottedVersion,
readSBML,
Model,
UnitPart,
Species,
Reaction,
getS,
getLBs,
getUBs,
getOCs
end # module
struct UnitPart
kind :: String
exponent :: Int
......@@ -11,7 +12,7 @@ struct Reaction
lb :: Tuple{Float64, String}
ub :: Tuple{Float64, String}
oc :: Float64
Reaction() = new()
Reaction(s,l,u,o) = new(s,l,u,o)
end
struct Species
......@@ -25,30 +26,60 @@ struct Model
compartments :: Vector{String}
species :: Dict{String,Species}
reactions :: Dict{String, Reaction}
Model() = new()
Model(u,c,s,r) = new(u,c,s,r)
end
function readSMBL(fn::String)::Model
function readSBML(fn::String)::Model
mi = readSBML_internal(fn)
if length(errors(mi))
if length(errors(mi))>0
@error "Loading failed!" errors(mi)
end
m = Model()
us = Dict{String, Vector{UnitPart}}()
for u in units(mi)
us[unit(u)]=Vector{UnitPart}()
end
for u in units(mi)
push!(us[unit(u)], UnitPart(kind(u), exponent(u), scale(u), multiplier(u)))
end
ss = Dict{String, Species}()
for s in species(mi)
ss[id(s)] = Species(name(s), compartment(s))
end
rs = Dict{String, Reaction}()
for r in reactions(mi)
sts = Dict{String, Float64}()
for s in species(r)
sts[id(s)] = stoichiometry(s)
end
rs[id(r)] = Reaction(sts, lb(r), ub(r), oc(r))
end
m.compartments = compartments(mi)
return m
return Model(us, compartments(mi), ss, rs)
end
function getS(m::Model)::Matrix{Float64}
#TODO this needs a sparse version and faster row ID lookup
function getS(m::Model)::Tuple{Vector{String}, Vector{String}, Matrix{Float64}}
rows = [k for k in keys(m.species)] #TODO this too
cols = [k for k in keys(m.reactions)]
S = zeros(Float64, length(rows), length(cols))
for ri in 1:length(cols)
stoi = m.reactions[cols[ri]].stoichiometry
S[indexin(keys(stoi), rows), ri] .= values(stoi)
end
return rows, cols, S
end
function getLBs(m::Model)::Vector{Tuple{Float64,String}}
return broadcast(x -> x.lb, values(m.reactions))
end
function getUBs(m::Model)::Vector{Tuple{Float64,String}}
return broadcast(x -> x.ub, values(m.reactions))
end
function getOCs(m::Model)::Vector{Float64}
return broadcast(x -> x.oc, values(m.reactions))
end
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