Skip to content
Snippets Groups Projects
Unverified Commit 3cd30a8e authored by St. Elmo's avatar St. Elmo
Browse files

changed set_bound to change_bound

parent 6436cab5
No related branches found
No related tags found
No related merge requests found
using Base: upperbound, lowerbound
"""
add_reactions!(model::StandardModel, rxns::Vector{Reaction})
......@@ -214,26 +215,162 @@ remove_gene!(model::StandardModel, gid::String; knockout_reactions::Bool = false
remove_genes!(model, [gid]; knockout_reactions = knockout_reactions)
"""
set_bound!(model::StandardModel, reaction_id::String; lb, ub)
change_bound!(
model::StandardModel,
reaction_id::String;
lower_bound = -_constants.default_reaction_bound,
upper_bound = _constants.default_reaction_bound,
)
Change the bounds of a reaction with `reaction_id` in `model` in-place. Note
that if the bound argument is not supplied then a default (see
`_constants.default_reaction_bound`) is used.
See also: [`change_bound`](@ref), [`change_bounds!`](@ref), [`change_bounds!`](@ref)
Change the bounds of a reaction in-place.
# Example
```
change_bound!(model, "PFL"; lower_bound=-10, ub=10)
change_bound!(model, "PFL"; lower_bound=-10.2) # upper_bound is set to _constants.default_reaction_bound
change_bound!(model, "PFL"; upper_bound=10)
```
"""
function set_bound!(model::StandardModel, reaction_id::String; lb, ub)
function change_bound!(
model::StandardModel,
reaction_id::String;
lower_bound = -_constants.default_reaction_bound,
upper_bound = _constants.default_reaction_bound,
)
reaction = model.reactions[reaction_id]
reaction.lb = lb
reaction.ub = ub
reaction.lb = float(lower_bound)
reaction.ub = float(upper_bound)
end
"""
set_bound(model::StandardModel, reaction_id::String; lb, ub)
change_bounds!(
model::StandardModel,
reaction_ids::Vector{String};
lower_bounds = repeat(
[-_constants.default_reaction_bound],
inner = length(reaction_ids),
),
upper_bounds = repeat(
[-_constants.default_reaction_bound],
inner = length(reaction_ids),
),
)
Change the bounds of all reactions with `reaction_ids` in `model` in-place. Note
that if the bound argument is not supplied then a default (see
`_constants.default_reaction_bound`) is used. The same default bound argument is
used for each reaction bound if not supplied.
See also: [`change_bound`](@ref), [`change_bound!`](@ref), [`change_bounds`](@ref)
# Example
```
change_bounds!(model, ["PFL", "FBA"]; lower_bounds=[-10, -20], ub=[10, 22])
change_bounds!(model, ["PFL", "FBA"]; lower_bounds=[-10.2, -14.3]) # all upper_bounds are set to _constants.default_reaction_bound
change_bounds!(model, ["PFL", "FBA"]; upper_bounds=[10.2, 23])
```
"""
function change_bounds!(
model::StandardModel,
reaction_ids::Vector{String};
lower_bounds = repeat(
[-_constants.default_reaction_bound],
inner = length(reaction_ids),
),
upper_bounds = repeat(
[-_constants.default_reaction_bound],
inner = length(reaction_ids),
),
)
for (rid, lb, ub) in zip(reaction_ids, lower_bounds, upper_bounds)
change_bound!(model, rid; lower_bound = lb, upper_bound = ub)
end
end
"""
change_bound(
model::StandardModel,
reaction_id::String;
lower_bound = -_constants.default_reaction_bound,
upper_bound = _constants.default_reaction_bound,
)
Return a shallow copy of the `model` where the reaction bounds of `reaction_id`
are changed. Note that if the bound argument is not supplied then a default (see
`_constants.default_reaction_bound`) is used.
Return a shallow copy of the `model` with reaction bounds changed.
See also: [`change_bounds`](@ref), [`change_bounds!`](@ref), [`change_bound!`](@ref)
# Example
```
new_model = change_bound(model, "PFL"; lower_bound=-10, ub=10)
new_model = change_bound(model, "PFL"; lower_bound=-10.2) # upper_bound is set to _constants.default_reaction_bound
new_model = change_bound(model, "PFL"; upper_bound=10)
```
"""
function set_bound(model::StandardModel, reaction_id::String; lb, ub)
function change_bound(
model::StandardModel,
reaction_id::String;
lower_bound = -_constants.default_reaction_bound,
upper_bound = _constants.default_reaction_bound,
)
m = copy(model)
m.reactions = copy(model.reactions)
r = m.reactions[reaction_id] = copy(model.reactions[reaction_id])
r.lb = lb
r.ub = ub
r.lb = float(lower_bound)
r.ub = float(upper_bound)
m
end
"""
change_bounds(
model::StandardModel,
reaction_ids::Vector{String};
lower_bounds = repeat(
[-_constants.default_reaction_bound],
inner = length(reaction_ids),
),
upper_bounds = repeat(
[-_constants.default_reaction_bound],
inner = length(reaction_ids),
),
)
Return a shallow copy of the `model` where the reaction bounds of all
`reaction_ids` are changed. Note that if the bound argument is not supplied then
a default (see `_constants.default_reaction_bound`) is used.
See also: [`change_bound`](@ref), [`change_bounds!`](@ref), [`change_bound!`](@ref)
# Example
```
new_model = change_bounds(model, ["PFL", "FBA"]; lower_bounds=[-10, -20], ub=[10, 22])
new_model = change_bounds(model, ["PFL", "FBA"]; lower_bounds=[-10, -20.2]) # all upper_bounds are set to _constants.default_reaction_bound
new_model = change_bounds(model, ["PFL", "FBA"]; ub=[10, 22])
```
"""
function change_bounds(
model::StandardModel,
reaction_ids::Vector{String};
lower_bounds = repeat(
[-_constants.default_reaction_bound],
inner = length(reaction_ids),
),
upper_bounds = repeat(
[-_constants.default_reaction_bound],
inner = length(reaction_ids),
),
)
m = copy(model)
m.reactions = copy(model.reactions)
for (rid, lb, ub) in zip(reaction_ids, lower_bounds, upper_bounds)
r = m.reactions[rid] = copy(model.reactions[rid])
r.lb = float(lb)
r.ub = float(ub)
end
return m
end
......@@ -32,6 +32,28 @@
model.metabolites = OrderedDict(m.id => m for m in mets)
model.genes = OrderedDict(g.id => g for g in genes)
# change bound tests - in place
change_bound!(model, "r2"; lower_bound=-10, upper_bound=10)
@test model.reactions["r2"].lb == -10
@test model.reactions["r2"].ub == 10
change_bounds!(model, ["r1", "r2"]; lower_bounds=[-10, -20], upper_bounds=[10.0, 20.0])
@test model.reactions["r1"].lb == -10
@test model.reactions["r1"].ub == 10
@test model.reactions["r2"].lb == -20
@test model.reactions["r2"].ub == 20
# change bound - new model
new_model = change_bound(model, "r2"; lower_bound=-10, upper_bound=10)
@test new_model.reactions["r2"].lb == -10
@test new_model.reactions["r2"].ub == 10
new_model = change_bounds(model, ["r1", "r2"]; lower_bounds=[-10, -20], upper_bounds=[10.0, 20.0])
@test new_model.reactions["r1"].lb == -10
@test new_model.reactions["r1"].ub == 10
@test new_model.reactions["r2"].lb == -20
@test new_model.reactions["r2"].ub == 20
### reactions
add_reactions!(model, [r3, r4])
@test length(model.reactions) == 4
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment