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

added change_objective! function and tests

parent 9671d5aa
Pipeline #50137 passed with stages
in 6 minutes and 44 seconds
......@@ -436,3 +436,41 @@ end
@_remove_fn metabolite CoreModel String plural begin
remove_metabolites(model, Int.(indexin(metabolite_ids, metabolites(model))))
end
"""
change_objective!(
model::CoreModel,
rxn_idxs::Vector{Int};
weights = ones(length(rxn_idxs)),
)
Change the objective for `model` to reaction(s) with indices `rxn_idxs`, optionally
specifying their `weights`. By default, assume equal weights. If no objective exists in
model, sets objective. Note, also accepts `String` or `Vector{String}` of reaction ids.
"""
function change_objective!(
model::CoreModel,
rxn_idxs::Vector{Int};
weights = ones(length(rxn_idxs)),
)
nz_idxs, _ = findnz(objective(model))
model.c[nz_idxs] .= 0.0 # reset
dropzeros!(model.c) # cleanup
model.c[rxn_idxs] .= weights # set
nothing
end
change_objective!(model::CoreModel, rxn_idx::Int) = change_objective!(model, [rxn_idx])
function change_objective!(
model::CoreModel,
rxn_ids::Vector{String};
weights = ones(length(rxn_ids)),
)
idxs = indexin(rxn_ids, reactions(model))
any(isnothing(idx) for idx in idxs) &&
throw(DomainError(rxn_ids, "Some reaction ids were not found in model."))
change_objective!(model, Int.(idxs); weights)
end
change_objective!(model::CoreModel, rxn_id::String) = change_objective!(model, [rxn_id])
......@@ -424,3 +424,36 @@ end
@_remove_fn metabolite CoreModelCoupled String plural begin
remove_metabolites(model, Int.(indexin(metabolite_ids, metabolites(model))))
end
"""
change_objective!(
model::CoreModelCoupled,
rxn_idxs::Vector{Int};
weights = ones(length(rxns)),
)
Change the objective for `model` to reaction(s) with indices `rxn_ids`, optionally
specifying their `weights`. By default, assume equal weights. If no objective exists in
model, sets objective.
"""
function change_objective!(
model::CoreModelCoupled,
rxn_idxs::Vector{Int};
weights = ones(length(rxns)),
)
change_objective!(model.lm, rxn_idxs; weights)
end
change_objective!(model::CoreModelCoupled, rxn_xid::Int) =
change_objective!(model.lm, [rxn_xid])
function change_objective!(
model::CoreModelCoupled,
rxn_ids::Vector{String};
weights = ones(length(rxns)),
)
change_objective!(model.lm, rxn_ids; weights)
end
change_objective!(model::CoreModelCoupled, rxn_id::String) =
change_objective!(model, [rxn_id])
......@@ -230,3 +230,33 @@ end
remove_metabolites!(n, metabolite_ids)
return n
end
"""
change_objective!(
model::StandardModel,
rxn_ids::Vector{String};
weights = ones(length(rxn_ids)),
)
Change the objective for `model` to reaction(s) with `rxn_ids`, optionally specifying their `weights`. By default,
assume equal weights. If no objective exists in model, sets objective.
"""
function change_objective!(
model::StandardModel,
rxn_ids::Vector{String};
weights = ones(length(rxn_ids)),
)
all(!haskey(model.reactions, rid) for rid in rxn_ids) &&
throw(DomainError(rxn_ids, "Some reaction ids were not found in model."))
k = 1 # counter
for rid in reactions(model)
if rid in rxn_ids
model.reactions[rid].objective_coefficient = weights[k]
k += 1
else # set others to zero
model.reactions[rid].objective_coefficient = 0.0
end
end
end
change_objective!(model::StandardModel, rxn_id::String) = change_objective!(model, [rxn_id])
......@@ -248,4 +248,8 @@ end
add_reactions!(toymodel, [rxn2, rxn3])
@test size(toymodel.S) == (6, 10)
@test toymodel.xl[end] == 10
change_objective!(toymodel, "nr1")
@test objective(toymodel)[8] == 1.0
@test objective(toymodel)[7] == 0.0
end
......@@ -109,4 +109,8 @@
remove_gene!(model, "g1")
@test length(model.genes) == 4
### objective
change_objective!(model, "r2")
@test model.reactions["r2"].objective_coefficient == 1.0
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