Unverified Commit f0d48d3a authored by St. Elmo's avatar St. Elmo
Browse files

Added in place `add_reaction` and tests

parent ff038a24
Pipeline #45329 passed with stages
in 14 minutes and 36 seconds
......@@ -7,6 +7,7 @@ version = "1.0.3"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
DistributedData = "f6a0035f-c5ac-4ad0-b410-ad102ced35df"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
......
"""
add_reactions!(model::CoreModel, rxns::Vector{Reaction})
Add `rxns` to `model` efficiently. The model must already contain the metabolites used by
`rxns` in the model.
"""
function add_reactions!(model::CoreModel, rxns::Vector{Reaction})
I = Int64[] # rows
J = Int64[] # cols
V = Float64[] # values
cs = zeros(length(rxns))
lbs = zeros(length(rxns))
ubs = zeros(length(rxns))
for (j, rxn) in enumerate(rxns)
req = rxn.metabolites
for (i, v) in zip(indexin(keys(req), metabolites(model)), values(req))
push!(J, j)
push!(I, i)
push!(V, v)
end
push!(model.rxns, rxn.id)
lbs[j] = rxn.lb
ubs[j] = rxn.ub
cs[j] = rxn.objective_coefficient
end
Sadd = sparse(I, J, V, n_metabolites(model), length(rxns))
model.S = [model.S Sadd]
model.c = dropzeros([model.c; cs])
model.xu = sparse(ubs)
model.xl = sparse(lbs)
return nothing
end
"""
add_reaction!(model::CoreModel, rxn::Reaction)
Add `rxn` to `model`. The model must already contain the metabolites used by
`rxn` in the model.
"""
add_reaction!(model::CoreModel, rxn::Reaction) = add_reactions!(model, [rxn])
"""
add_reactions(
m::CoreModel,
......@@ -10,7 +51,6 @@
) where {V1<:VecType,V2<:VecType}
Add reaction(s) to a `CoreModel` model `m`.
"""
function add_reactions(
m::CoreModel,
......@@ -44,7 +84,6 @@ end
mets::K;
check_consistency = false,
) where {V1<:VecType,V2<:VecType,K<:StringVecType}
"""
function add_reactions(
m::CoreModel,
......@@ -80,7 +119,6 @@ end
xu::V;
check_consistency = false,
) where {M<:MatType,V<:VecType}
"""
function add_reactions(
m::CoreModel,
......@@ -110,7 +148,6 @@ end
add_reactions(m1::CoreModel, m2::CoreModel; check_consistency = false)
Add all reactions from `m2` to `m1`.
"""
function add_reactions(m1::CoreModel, m2::CoreModel; check_consistency = false)
return add_reactions(
......@@ -138,7 +175,6 @@ end
mets::K;
check_consistency = false,
) where {M<:MatType,V<:VecType,K<:StringVecType}
"""
function add_reactions(
m::CoreModel,
......@@ -228,7 +264,6 @@ end
Check the consistency of given reactions with existing reactions in `m`.
TODO: work in progress, doesn't return consistency status.
"""
function verify_consistency(
m::CoreModel,
......@@ -271,7 +306,7 @@ function verify_consistency(
end
"""
remove_metabolites(model::CoreModel, metabolites)
remove_metabolites(model::CoreModel, mets)
Removes a set of `metabolites` from the `model` of type `CoreModel` and returns
a new `CoreModel` without those metabolites. Here, `metabolites` can be either a
......@@ -350,7 +385,6 @@ end
"""
remove_reactions(m::CoreModel, rxn::Int)
"""
function remove_reactions(m::CoreModel, rxn::Int)
return remove_reactions(m, [rxn])
......@@ -358,7 +392,6 @@ end
"""
remove_reactions(m::CoreModel, rxn::String)
"""
function remove_reactions(m::CoreModel, rxn::String)
return remove_reactions(m, [rxn])
......@@ -366,7 +399,6 @@ end
"""
remove_reactions(m::CoreModel, rxns::Vector{String})
"""
function remove_reactions(m::CoreModel, rxns::Vector{String})
rxn_indices = [findfirst(isequal(name), m.rxns) for name in intersect(rxns, m.rxns)]
......
......@@ -236,3 +236,23 @@ end
@test all((!in(metabolites(m3))).(["glc__D_e", "for_c"]))
@test !(["glc__D_e"] in metabolites(m4))
end
@testset "Core in place modifications" begin
toymodel = test_toyModel()
rxn1 = Reaction("nr1"; metabolites = Dict("m1[c]" => -1, "m3[c]" => 1))
rxn2 = Reaction("nr2"; metabolites = Dict("m1[c]" => -1, "m2[c]" => 1))
rxn3 = Reaction("nr3"; metabolites = Dict("m2[c]" => -1, "m3[c]" => 1))
rxn3.lb = 10
add_reaction!(toymodel, rxn1)
@test toymodel.S[1, 8] == -1
@test toymodel.S[2, 8] == 1
@test all(toymodel.S[3:end, 8] .== 0)
@test size(toymodel.S) == (6, 8)
@test toymodel.rxns[end] == "nr1"
add_reactions!(toymodel, [rxn2, rxn3])
@test size(toymodel.S) == (6, 10)
@test toymodel.xl[end] == 10
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