Commit eebfb568 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil
Browse files

unify the code in *_analysis_dict and *_analysis_vec functions a bit

closes #474
parent 8fcd8e19
......@@ -5,28 +5,28 @@ A variant of FBA that returns a vector of fluxes in the same order as reactions
of the model, if the solution is found.
Arguments are passed to [`flux_balance_analysis`](@ref).
This function is kept for backwards compatibility, use [`flux_vector`](@ref)
instead.
"""
function flux_balance_analysis_vec(args...; kwargs...)::Union{Vector{Float64},Nothing}
optmodel = flux_balance_analysis(args...; kwargs...)
is_solved(optmodel) || return nothing
value.(optmodel[:x])
end
flux_balance_analysis_vec(args...; kwargs...)::Maybe{Vector{Float64}} =
flux_vector(flux_balance_analysis(args...; kwargs...))
"""
flux_balance_analysis_dict(model::MetabolicModel, args...)::Union{Dict{String, Float64},Nothing}
A variant of FBA that returns a dictionary assigning fluxes to reactions, if
the solution is found. Arguments are passed to [`flux_balance_analysis`](@ref).
This function is kept for backwards compatibility, use [`flux_dict`](@ref)
instead.
"""
function flux_balance_analysis_dict(
flux_balance_analysis_dict(
model::MetabolicModel,
args...;
kwargs...,
)::Union{Dict{String,Float64},Nothing}
v = flux_balance_analysis_vec(model, args...; kwargs...)
isnothing(v) && return nothing
Dict(zip(reactions(model), v))
end
)::Maybe{Dict{String,Float64}} =
flux_dict(model, flux_balance_analysis(model, args...; kwargs...))
"""
flux_balance_analysis(
......
......@@ -140,18 +140,11 @@ mins, maxs = flux_variability_analysis_dict(
```
"""
function flux_variability_analysis_dict(model::MetabolicModel, optimizer; kwargs...)
vs = flux_variability_analysis(
model,
optimizer;
kwargs...,
ret = m -> JuMP.value.(m[:x]),
)
fluxes = flux_variability_analysis(model, optimizer; kwargs..., ret = flux_vector)
rxns = reactions(model)
dicts = zip.(Ref(rxns), fluxes)
return (
Dict(zip(rxns, [Dict(zip(rxns, fluxes)) for fluxes in vs[:, 1]])),
Dict(zip(rxns, [Dict(zip(rxns, fluxes)) for fluxes in vs[:, 2]])),
)
return (Dict(rxns .=> Dict.(dicts[:, 1])), Dict(rxns .=> Dict.(dicts[:, 2])))
end
"""
......@@ -166,9 +159,5 @@ function _max_variability_flux(opt_model, rid, ret)
@objective(opt_model, sense, var)
optimize!(opt_model)
if is_solved(opt_model)
return ret(opt_model)
else
return nothing
end
is_solved(opt_model) ? ret(opt_model) : nothing
end
......@@ -87,30 +87,22 @@ minimize_metabolic_adjustment(flux_ref_dict::Dict{String,Float64}) =
Perform minimization of metabolic adjustment (MOMA) and return a vector of fluxes in the
same order as the reactions in `model`. Arguments are forwarded to
[`minimize_metabolic_adjustment`](@ref) internally.
"""
function minimize_metabolic_adjustment_analysis_vec(args...; kwargs...)
opt_model = minimize_metabolic_adjustment_analysis(args...; kwargs...)
isnothing(opt_model) && return nothing
return value.(opt_model[:x])
end
This function is kept for backwards compatibility, use [`flux_vector`](@ref)
instead.
"""
minimize_metabolic_adjustment_analysis_vec(args...; kwargs...) =
flux_vector(minimize_metabolic_adjustment_analysis(args...; kwargs...))
"""
minimize_metabolic_adjustment_analysis_dict(args...; kwargs...)
minimize_metabolic_adjustment_analysis_dict(model::MetabolicModel, args...; kwargs...)
Perform minimization of metabolic adjustment (MOMA) and return a dictionary mapping the
reaction IDs to fluxes. Arguments are forwarded to [`minimize_metabolic_adjustment`](@ref)
internally.
"""
function minimize_metabolic_adjustment_analysis_dict(
model::MetabolicModel,
args...;
kwargs...,
)
opt_fluxes = minimize_metabolic_adjustment_analysis_vec(model, args...; kwargs...)
isnothing(opt_fluxes) && return nothing
return Dict(zip(reactions(model), opt_fluxes))
end
This function is kept for backwards compatibility, use [`flux_vector`](@ref)
instead.
"""
minimize_metabolic_adjustment_analysis_dict(model::MetabolicModel, args...; kwargs...) =
flux_dict(model, minimize_metabolic_adjustment_analysis(model, args...; kwargs...))
......@@ -100,14 +100,12 @@ Perform parsimonious flux balance analysis on `model` using `optimizer`.
Returns a vector of fluxes in the same order as the reactions in `model`.
Arguments are forwarded to [`parsimonious_flux_balance_analysis`](@ref)
internally.
"""
function parsimonious_flux_balance_analysis_vec(args...; kwargs...)
opt_model = parsimonious_flux_balance_analysis(args...; kwargs...)
isnothing(opt_model) && return nothing
return value.(opt_model[:x])
end
This function is kept for backwards compatibility, use [`flux_vector`](@ref)
instead.
"""
parsimonious_flux_balance_analysis_vec(args...; kwargs...) =
flux_vector(parsimonious_flux_balance_analysis(args...; kwargs...))
"""
parsimonious_flux_balance_analysis_dict(model::MetabolicModel, args...; kwargs...)
......@@ -115,11 +113,9 @@ end
Perform parsimonious flux balance analysis on `model` using `optimizer`.
Returns a dictionary mapping the reaction IDs to fluxes. Arguments are
forwarded to [`parsimonious_flux_balance_analysis`](@ref) internally.
"""
function parsimonious_flux_balance_analysis_dict(model::MetabolicModel, args...; kwargs...)
opt_fluxes = parsimonious_flux_balance_analysis_vec(model, args...; kwargs...)
isnothing(opt_fluxes) && return nothing
return Dict(zip(reactions(model), opt_fluxes))
end
This function is kept for backwards compatibility, use [`flux_dict`](@ref)
instead.
"""
parsimonious_flux_balance_analysis_dict(model::MetabolicModel, args...; kwargs...) =
flux_dict(model, parsimonious_flux_balance_analysis(model, args...; kwargs...))
......@@ -32,29 +32,23 @@ function make_optimization_model(model::MetabolicModel, optimizer; sense = MOI.M
end
"""
is_solved(optmodel)
is_solved(opt_model)
Return `true` if `optmodel` solved successfully (solution is optimal or locally
Return `true` if `opt_model` solved successfully (solution is optimal or locally
optimal). Return `false` if any other termination status is reached.
Termination status is defined in the documentation of `JuMP`.
"""
function is_solved(optmodel)
termination_status(optmodel) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED] ? true : false
end
is_solved(opt_model) = termination_status(opt_model) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED]
"""
optimize_objective(optmodel)::Union{Float64,Nothing}
optimize_objective(opt_model)::Maybe{Float64}
Shortcut for running JuMP `optimize!` on a model and returning the objective
value, if solved.
"""
function optimize_objective(optmodel)::Maybe{Float64}
optimize!(optmodel)
if is_solved(optmodel)
objective_value(optmodel)
else
nothing
end
function optimize_objective(opt_model)::Maybe{Float64}
optimize!(opt_model)
solved_objective_value(opt_model)
end
"""
......@@ -88,3 +82,43 @@ function set_optmodel_bound!(
isnothing(lb) || set_normalized_rhs(opt_model[:lbs][vidx], -lb)
isnothing(ub) || set_normalized_rhs(opt_model[:ubs][vidx], ub)
end
"""
solved_objective_value(opt_model)::Maybe{Float64}
Returns the current objective value of a model, if solved.
# Example
```
solved_objective_value(flux_balance_analysis(model, ...))
```
"""
solved_objective_value(opt_model)::Maybe{Float64} =
is_solved(opt_model) ? objective_value(opt_model) : nothing
"""
flux_vector(opt_model)::Maybe{Vector{Float64}}
Returns a vector of fluxes of the model, if solved.
# Example
```
flux_vector(flux_balance_analysis(model, ...))
```
"""
flux_vector(opt_model)::Maybe{Vector{Float64}} =
is_solved(opt_model) ? value.(opt_model[:x]) : nothing
"""
flux_dict(model::MetabolicModel, opt_model)::Maybe{Dict{String, Float64}, Nothing}
Returns the fluxes of the model as a reaction-keyed dictionary, if solved.
# Example
```
flux_dict(model, flux_balance_analysis(model, ...))
```
"""
flux_dict(model::MetabolicModel, opt_model)::Maybe{Dict{String,Float64}} =
is_solved(opt_model) ? Dict(reactions(model) .=> value.(opt_model[:x])) : nothing
......@@ -47,7 +47,7 @@ larger than `large_flux_bound` are only stored if `keep_unbounded` is `true`.
# Example
```
julia> sol = flux_balance_analysis_dict(model, Tulip.Optimizer)
julia> sol = flux_dict(flux_balance_analysis(model, Tulip.Optimizer))
julia> fr = flux_summary(sol)
Biomass:
BIOMASS_Ecoli_core_w_GAM: 0.8739
......
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