Commit 9761031e authored by Miroslav Kratochvil's avatar Miroslav Kratochvil
Browse files

reduce copypasta

closes #119
parent cc059367
......@@ -358,42 +358,23 @@ function extractModel(mdl::VPtr)::SBML.Model
end
# extract stoichiometry
stoi = Dict{String,Float64}()
add_stoi(sr, factor) = begin
s = get_string(sr, :SpeciesReference_getSpecies)
stoi[s] =
get(stoi, s, 0) +
ccall(sbml(:SpeciesReference_getStoichiometry), Cdouble, (VPtr,), sr) *
factor
end
# extract stoichiometry
substoi = Dict{String,Float64}()
add_sstoi(sr, factor) = begin
s = get_string(sr, :SpeciesReference_getSpecies)
substoi[s] =
ccall(sbml(:SpeciesReference_getStoichiometry), Cdouble, (VPtr,), sr) *
factor
end
# extract stoichiometry
prodstoi = Dict{String,Float64}()
add_pstoi(sr, factor) = begin
reactants = Dict{String,Float64}()
products = Dict{String,Float64}()
add_stoi(sr, coll) = begin
s = get_string(sr, :SpeciesReference_getSpecies)
prodstoi[s] =
ccall(sbml(:SpeciesReference_getStoichiometry), Cdouble, (VPtr,), sr) *
factor
coll[s] =
ccall(sbml(:SpeciesReference_getStoichiometry), Cdouble, (VPtr,), sr)
end
# reactants and products
for j = 1:ccall(sbml(:Reaction_getNumReactants), Cuint, (VPtr,), re)
sr = ccall(sbml(:Reaction_getReactant), VPtr, (VPtr, Cuint), re, j - 1)
add_stoi(sr, -1)
add_sstoi(sr, 1)
add_stoi(sr, reactants)
end
for j = 1:ccall(sbml(:Reaction_getNumProducts), Cuint, (VPtr,), re)
sr = ccall(sbml(:Reaction_getProduct), VPtr, (VPtr, Cuint), re, j - 1)
add_stoi(sr, 1)
add_pstoi(sr, 1)
add_stoi(sr, products)
end
# gene product associations
......@@ -417,9 +398,8 @@ function extractModel(mdl::VPtr)::SBML.Model
reid = get_string(re, :Reaction_getId)
reactions[reid] = Reaction(
substoi,
prodstoi,
stoi,
reactants,
products,
lb,
ub,
haskey(objectives_fbc, reid) ? objectives_fbc[reid] : oc,
......
......@@ -99,17 +99,16 @@ end
$(TYPEDEF)
Reaction with stoichiometry that assigns reactants and products their relative
consumption/production rates (accessible in field `stoichiometry`), lower/upper
bounds (in tuples `lb` and `ub`, with unit names), and objective coefficient
(`oc`). Also may contains `notes` and `annotation`.
consumption/production rates, lower/upper bounds (in tuples `lb` and `ub`, with
unit names), and objective coefficient (`oc`). Also may contains `notes` and
`annotation`.
# Fields
$(TYPEDFIELDS)
"""
struct Reaction
substoich::Dict{String,Float64}
prodstoich::Dict{String,Float64}
stoichiometry::Dict{String,Float64}
reactants::Dict{String,Float64}
products::Dict{String,Float64}
lb::Tuple{Float64,String}
ub::Tuple{Float64,String}
oc::Float64
......@@ -118,8 +117,8 @@ struct Reaction
reversible::Bool
notes::Maybe{String}
annotation::Maybe{String}
Reaction(ss, ps, s, l, u, o, as, km, r, n = nothing, an = nothing) =
new(ss, ps, s, l, u, o, as, km, r, n, an)
Reaction(rs, ps, l, u, o, as, km, r, n = nothing, an = nothing) =
new(rs, ps, l, u, o, as, km, r, n, an)
end
"""
......
......@@ -18,8 +18,10 @@ function stoichiometry_matrix(
rowsd = Dict(k => i for (i, k) in enumerate(rows))
S = zeros(Float64, length(rows), length(cols))
for col = 1:length(cols)
stoi = m.reactions[cols[col]].stoichiometry
S[getindex.(Ref(rowsd), keys(stoi)), col] .= values(stoi)
s = m.reactions[cols[col]].reactants
S[getindex.(Ref(rowsd), keys(s)), col] .-= values(s)
s = m.reactions[cols[col]].products
S[getindex.(Ref(rowsd), keys(s)), col] .+= values(s)
end
return rows, cols, S
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