Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
LCSB-BioCore
COBREXA.jl
Commits
a4119885
Unverified
Commit
a4119885
authored
Nov 29, 2021
by
St. Elmo
Committed by
GitHub
Nov 29, 2021
Browse files
Merge pull request #530 from LCSB-BioCore/mo-change-objective
added `change_objective!` function and tests
parents
ac9b0de6
53d876e9
Pipeline
#50157
failed with stages
in 5 minutes and 21 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/reconstruction/CoreModel.jl
View file @
a4119885
...
...
@@ -436,3 +436,39 @@ 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
)),
)
model
.
c
=
spzeros
(
length
(
model
.
c
))
model
.
c
[
rxn_idxs
]
.=
weights
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
])
src/reconstruction/CoreModelCoupled.jl
View file @
a4119885
...
...
@@ -424,3 +424,26 @@ end
@_remove_fn
metabolite
CoreModelCoupled
String
plural
begin
remove_metabolites
(
model
,
Int
.
(
indexin
(
metabolite_ids
,
metabolites
(
model
))))
end
"""
change_objective!(
model::CoreModelCoupled,
args...,
kwargs...,
)
Forwards arguments to [`change_objective!`](@ref).
"""
function
change_objective!
(
model
::
CoreModelCoupled
,
args
...
;
kwargs
...
)
change_objective!
(
model
.
lm
,
args
...
;
kwargs
...
)
end
change_objective!
(
model
::
CoreModelCoupled
,
rxn_xid
::
Int
)
=
change_objective!
(
model
.
lm
,
[
rxn_xid
])
function
change_objective!
(
model
::
CoreModelCoupled
,
args
...
;
kwargs
...
)
change_objective!
(
model
.
lm
,
args
...
;
kwargs
...
)
end
change_objective!
(
model
::
CoreModelCoupled
,
rxn_id
::
String
)
=
change_objective!
(
model
,
[
rxn_id
])
src/reconstruction/StandardModel.jl
View file @
a4119885
...
...
@@ -230,3 +230,31 @@ 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."
))
for
(
_
,
rxn
)
in
model
.
reactions
# reset to zero
rxn
.
objective_coefficient
=
0.0
end
for
(
k
,
rid
)
in
enumerate
(
rxn_ids
)
model
.
reactions
[
rid
]
.
objective_coefficient
=
weights
[
k
]
end
end
change_objective!
(
model
::
StandardModel
,
rxn_id
::
String
)
=
change_objective!
(
model
,
[
rxn_id
])
test/reconstruction/CoreModel.jl
View file @
a4119885
...
...
@@ -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
test/reconstruction/StandardModel.jl
View file @
a4119885
...
...
@@ -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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment