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

more updates

parent b8818d22
...@@ -22,10 +22,9 @@ Arguments are passed to [`flux_balance_analysis`](@ref). ...@@ -22,10 +22,9 @@ Arguments are passed to [`flux_balance_analysis`](@ref).
""" """
function flux_balance_analysis_vec(args...)::Union{Vector{Float64},Nothing} function flux_balance_analysis_vec(args...)::Union{Vector{Float64},Nothing}
optmodel = flux_balance_analysis(args...) optmodel = flux_balance_analysis(args...)
vars = optmodel[:x]
termination_status(optmodel) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED] || return nothing termination_status(optmodel) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED] || return nothing
value.(vars) value.(optmodel[:x])
end end
""" """
...@@ -69,7 +68,7 @@ function flux_balance_analysis( ...@@ -69,7 +68,7 @@ function flux_balance_analysis(
) )
# get core optimization problem # get core optimization problem
cbm = make_optimization_model(model, optimizer) cbm = make_optimization_model(model, optimizer)
# apply callbacks - user can also just put in a function # apply callbacks - user can also just put in a function
if typeof(modifications) <: Vector if typeof(modifications) <: Vector
for mod in modifications for mod in modifications
...@@ -80,16 +79,22 @@ function flux_balance_analysis( ...@@ -80,16 +79,22 @@ function flux_balance_analysis(
end end
optimize!(cbm) optimize!(cbm)
return cbm
end
status = ( function flux_balance_analysis_vec()
termination_status(cbm) == MOI.OPTIMAL ||
termination_status(cbm) == MOI.LOCALLY_SOLVED
)
if status end
return map_fluxes(v, model)
else status = (
@warn "Optimization issues occurred." termination_status(cbm) == MOI.OPTIMAL ||
return Dict{String,Float64}() termination_status(cbm) == MOI.LOCALLY_SOLVED
end )
if status
return map_fluxes(cbm[:v], model)
else
@warn "Optimization issues occurred."
return Dict{String,Float64}()
end end
...@@ -134,55 +134,20 @@ fva_max, fva_min = fva(model, biomass, optimizer; solver_attributes=atts) ...@@ -134,55 +134,20 @@ fva_max, fva_min = fva(model, biomass, optimizer; solver_attributes=atts)
function fva( function fva(
model::CobraModel, model::CobraModel,
optimizer; optimizer;
objective_func::Union{Reaction,Array{Reaction,1}} = Reaction[],
optimum_bound = 0.9999, optimum_bound = 0.9999,
weights = Float64[], modifications = [(model, opt_model) -> nothing]
solver_attributes = Dict{Any,Any}(),
constraints = Dict{String,Tuple{Float64,Float64}}(),
sense = MOI.MAX_SENSE,
) )
cbm = make_optimization_model(model, optimizer, sense = sense) # get core optimization problem
cbm = make_optimization_model(model, optimizer)
v = cbm[:x] v = cbm[:x]
if !isempty(solver_attributes) # set other attributes # apply callbacks - user can also just put in a function
for (k, v) in solver_attributes if typeof(modifications) <: Vector
set_optimizer_attribute(cbm, k, v) for mod in modifications
end mod(model, cbm)
end end
else
# set additional constraints modifications(model, cbm)
for (rxnid, con) in constraints
ind = model.reactions[findfirst(model.reactions, rxnid)]
set_bound(ind, cbm; lb = con[1], ub = con[2])
end
# if an objective function is supplied, modify the default objective
if typeof(objective_func) == Reaction || !isempty(objective_func)
# ensure that an array of objective indices are fed in
if typeof(objective_func) == Reaction
objective_indices = [model[objective_func]]
else
objective_indices = [model[rxn] for rxn in objective_func]
end
if isempty(weights)
weights = ones(length(objective_indices))
end
opt_weights = zeros(length(model.reactions))
# update the objective function tracker
# don't update model objective function - silly thing to do
wcounter = 1
for i in eachindex(model.reactions)
if i in objective_indices
# model.reactions[i].objective_coefficient = weights[wcounter]
opt_weights[i] = weights[wcounter]
wcounter += 1
# else
# model.reactions[i].objective_coefficient = 0.0
end
end
@objective(cbm, sense, sum(opt_weights[i] * v[i] for i in objective_indices))
end end
optimize!(cbm) optimize!(cbm)
......
Supports Markdown
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