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

Merge pull request #84 from LCSB-BioCore/mk-initial-conversions

fun accessors for initial amounts and concentrations
parents 3cecd7c3 68a2fadd
...@@ -25,30 +25,78 @@ function getS( ...@@ -25,30 +25,78 @@ function getS(
end end
""" """
function getLBs(m::SBML.Model)::Vector{Tuple{Float64,String}} 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::SBML.Model)::Vector{Tuple{Float64,String}} getLBs(m::SBML.Model)::Vector{Tuple{Float64,String}} =
return broadcast(x -> x.lb, values(m.reactions)) broadcast(x -> x.lb, values(m.reactions))
end
""" """
function getUBs(m::SBML.Model)::Vector{Tuple{Float64,String}} getUBs(m::SBML.Model)::Vector{Tuple{Float64,String}}
Likewise to `getLBs`, extract the upper bounds. Likewise to [`getLBs`](@ref), extract the upper bounds.
""" """
function getUBs(m::SBML.Model)::Vector{Tuple{Float64,String}} getUBs(m::SBML.Model)::Vector{Tuple{Float64,String}} =
return broadcast(x -> x.ub, values(m.reactions)) broadcast(x -> x.ub, values(m.reactions))
end
""" """
function getOCs(m::SBML.Model)::Vector{Float64} 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::SBML.Model)::Vector{Float64} getOCs(m::SBML.Model)::Vector{Float64} = broadcast(x -> x.oc, values(m.reactions))
return broadcast(x -> x.oc, values(m.reactions))
end """
initial_amounts(m::SBML.Model; convert_concentrations = false)
Return initial amounts for each species as a generator of pairs
`species_name => initial_amount`; the amount is set to `nothing` if not
available. If `convert_concentrations` is true and there is information about
initial concentration available together with compartment size, the result is
computed from the species' initial concentration.
In the current version, units of the measurements are completely ignored.
# Example
```
# get the initial amounts as dictionary
Dict(initial_amounts(model, convert_concentrations = true))
# remove the empty entries
Dict(k => v for (k,v) in initial_amounts(model) if !isnothing(v))
```
"""
initial_amounts(m::SBML.Model; convert_concentrations = false) = (
k => if !isnothing(s.initial_amount)
s.initial_amount[1]
elseif convert_concentrations &&
!isnothing(s.initial_concentration) &&
haskey(m.compartments, s.compartment) &&
!isnothing(m.compartments[s.compartment].size)
s.initial_concentration[1] * m.compartments[s.compartment].size
else
nothing
end for (k, s) in m.species
)
"""
initial_concentrations(m::SBML.Model; convert_amounts = false)
Return initial concentrations of the species in the model. Refer to work-alike
[`initial_amounts`](@ref) for details.
"""
initial_concentrations(m::SBML.Model; convert_amounts = false) = (
k => if !isnothing(s.initial_concentration)
s.initial_concentration[1]
elseif convert_amounts &&
!isnothing(s.initial_amount) &&
haskey(m.compartments, s.compartment) &&
!isnothing(m.compartments[s.compartment].size)
s.initial_amount[1] / m.compartments[s.compartment].size
else
nothing
end for (k, s) in m.species
)
...@@ -18,7 +18,9 @@ end ...@@ -18,7 +18,9 @@ end
@test typeof(mdl) == Model @test typeof(mdl) == Model
@test_logs (:error, "SBML reported error: File unreadable.") @test_throws AssertionError readSBML(sbmlfile * ".does.not.really.exist") @test_logs (:error, "SBML reported error: File unreadable.") @test_throws AssertionError readSBML(
sbmlfile * ".does.not.really.exist",
)
@test length(mdl.compartments) == 2 @test length(mdl.compartments) == 2
......
...@@ -68,3 +68,13 @@ end ...@@ -68,3 +68,13 @@ end
m = readSBML(joinpath(@__DIR__, "data", "sbml00852.xml")) m = readSBML(joinpath(@__DIR__, "data", "sbml00852.xml"))
@test all(contains_time.(r.kinetic_math for (_, r) in m.reactions)) @test all(contains_time.(r.kinetic_math for (_, r) in m.reactions))
end end
@testset "Initial amounts and concentrations" begin
m = readSBML(joinpath(@__DIR__, "data", "sbml00852.xml"))
@test all(isnothing(ic) for (k, ic) in SBML.initial_concentrations(m))
@test length(SBML.initial_amounts(m)) == 4
@test sum(ia for (sp, ia) in SBML.initial_amounts(m)) == 0.001
@test sum(ic for (sp, ic) in SBML.initial_concentrations(m, convert_amounts = true)) ==
0.001
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