Commit bfb68846 authored by St. Elmo's avatar St. Elmo
Browse files

serial fva using cobramodel implemented

parent 1ff07dd5
......@@ -38,7 +38,7 @@ function fba(
sense = MOI.MAX_SENSE
)
# get core optimization problem
cbm, v, mb, lbcons, ubcons = makeOptimizationModel(model, optimizer)
cbm, v, mb, lbcons, ubcons = makeOptimizationModel(model, optimizer, sense=sense)
# modify core optimization problem according to user specifications
if !isempty(solver_attributes) # set other attributes
......@@ -80,7 +80,7 @@ function fba(
end
end
@objective(cbm, Max, sum(opt_weights[i] * v[i] for i in objective_indices))
@objective(cbm, sense, sum(opt_weights[i] * v[i] for i in objective_indices))
else # use default objective
# automatically assigned by makeOptimizationModel
end
......
......@@ -96,16 +96,16 @@ fva_max, fva_min = fva(model, biomass, optimizer; solver_attributes=atts)
"""
function fva(
model::CobraModel,
objective_rxns::Union{Reaction,Array{Reaction,1}},
optimizer;
objective_func::Union{Reaction,Array{Reaction,1}} = Reaction[],
optimum_bound = 0.9999,
weights = Float64[],
solver_attributes = Dict{Any,Any}(),
constraints = Dict{String,Tuple{Float64,Float64}}(),
sense = MOI.MAX_SENSE
)
cbm, _, _, ubcons, lbcons = build_cbm(model) # get the base constraint based model
cbm, v, mb, lbcons, ubcons = makeOptimizationModel(model, optimizer, sense=sense)
set_optimizer(cbm, optimizer) # choose optimizer
if !isempty(solver_attributes) # set other attributes
for (k, v) in solver_attributes
set_optimizer_attribute(cbm, k, v)
......@@ -115,14 +115,16 @@ function fva(
# set additional constraints
for (rxnid, con) in constraints
ind = model.reactions[findfirst(model.reactions, rxnid)]
set_bound(ind, ubcons, lbcons; ub = con[1], lb = con[2])
set_bound(ind, lbcons, ubcons; 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_rxns) == Reaction
objective_indices = [model[objective_rxns]]
if typeof(objective_func) == Reaction
objective_indices = [model[objective_func]]
else
objective_indices = [model[rxn] for rxn in objective_rxns]
objective_indices = [model[rxn] for rxn in objective_func]
end
if isempty(weights)
......@@ -131,19 +133,20 @@ function fva(
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]
# model.reactions[i].objective_coefficient = weights[wcounter]
opt_weights[i] = weights[wcounter]
wcounter += 1
else
model.reactions[i].objective_coefficient = 0.0
# 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
v = all_variables(cbm)
@objective(cbm, Max, sum(opt_weights[i] * v[i] for i in objective_indices))
optimize!(cbm)
status = (
......@@ -159,6 +162,7 @@ function fva(
return fva_max, fva_min
end
# Now do FVA
λ = objective_value(cbm) # objective value
@constraint(
cbm,
......
......@@ -33,14 +33,14 @@ function pfba(
if typeof(optimizer) <: AbstractArray # choose optimizer
cbm, v, mb, lbcons, ubcons = makeOptimizationModel(model, optimizer[1])
cbm, v, mb, lbcons, ubcons = makeOptimizationModel(model, optimizer[1], sense=sense)
if !isempty(solver_attributes["opt1"]) # set other attributes
for (k, v) in solver_attributes["opt1"]
set_optimizer_attribute(cbm, k, v)
end
end
else # singe optimizer
cbm, v, mb, lbcons, ubcons = makeOptimizationModel(model, optimizer)
cbm, v, mb, lbcons, ubcons = makeOptimizationModel(model, optimizer, sense=sense)
if !isempty(solver_attributes) # set other attributes
for (k, v) in solver_attributes
set_optimizer_attribute(cbm, k, v)
......@@ -79,7 +79,7 @@ function pfba(
end
end
@objective(cbm, Max, sum(opt_weights[i] * v[i] for i in objective_indices))
@objective(cbm, sense, sum(opt_weights[i] * v[i] for i in objective_indices))
else
# objective_indices = findnz(objective(model))
# opt_weights = ones(length(objective_indices)) # assume equal weighting, assume sense is max
......
# @testset "Flux variability analysis" begin
# cp = test_simpleLP()
# optimizer = GLPK.Optimizer
# fluxes = fluxVariabilityAnalysis(cp, optimizer)
@testset "Flux variability analysis" begin
cp = test_simpleLP()
optimizer = GLPK.Optimizer
fluxes = fluxVariabilityAnalysis(cp, optimizer)
# @test size(fluxes) == (2, 2)
# @test fluxes ≈ [
# 1.0 1.0
# 2.0 2.0
# ]
@test size(fluxes) == (2, 2)
@test fluxes [
1.0 1.0
2.0 2.0
]
# fluxes = fluxVariabilityAnalysis(cp, [2], optimizer)
fluxes = fluxVariabilityAnalysis(cp, [2], optimizer)
# @test size(fluxes) == (1, 2)
# @test fluxes == Array{Float64,2}([2 2])
@test size(fluxes) == (1, 2)
@test fluxes == Array{Float64,2}([2 2])
# # a special testcase for slightly sub-optimal FVA (gamma<1)
# cp = LinearModel(
# [-1.0 -1.0 -1.0],
# [0.0],
# [1.0, 0.0, 0.0],
# [0.0, 0.0, -1.0],
# 1.0 * ones(3),
# ["r$x" for x = 1:3],
# ["m1"],
# )
# fluxes = fluxVariabilityAnalysis(cp, optimizer)
# @test fluxes ≈ [
# 1.0 1.0
# 0.0 0.0
# -1.0 -1.0
# ]
# fluxes = fluxVariabilityAnalysis(cp, optimizer; gamma = 0.5)
# @test fluxes ≈ [
# 0.5 1.0
# 0.0 0.5
# -1.0 -0.5
# ]
# fluxes = fluxVariabilityAnalysis(cp, optimizer; gamma = 0.0)
# @test fluxes ≈ [
# 0.0 1.0
# 0.0 1.0
# -1.0 0.0
# ]
# a special testcase for slightly sub-optimal FVA (gamma<1)
cp = LinearModel(
[-1.0 -1.0 -1.0],
[0.0],
[1.0, 0.0, 0.0],
[0.0, 0.0, -1.0],
1.0 * ones(3),
["r$x" for x = 1:3],
["m1"],
)
fluxes = fluxVariabilityAnalysis(cp, optimizer)
@test fluxes [
1.0 1.0
0.0 0.0
-1.0 -1.0
]
fluxes = fluxVariabilityAnalysis(cp, optimizer; gamma = 0.5)
@test fluxes [
0.5 1.0
0.0 0.5
-1.0 -0.5
]
fluxes = fluxVariabilityAnalysis(cp, optimizer; gamma = 0.0)
@test fluxes [
0.0 1.0
0.0 1.0
-1.0 0.0
]
# @test isempty(fluxVariabilityAnalysis(cp, Vector{Int}(), GLPK.Optimizer))
# @test_throws DomainError fluxVariabilityAnalysis(cp, [-1], GLPK.Optimizer)
# @test_throws DomainError fluxVariabilityAnalysis(cp, [99999999], GLPK.Optimizer)
# end
@test isempty(fluxVariabilityAnalysis(cp, Vector{Int}(), GLPK.Optimizer))
@test_throws DomainError fluxVariabilityAnalysis(cp, [-1], GLPK.Optimizer)
@test_throws DomainError fluxVariabilityAnalysis(cp, [99999999], GLPK.Optimizer)
end
# @testset "Parallel FVA" begin
# cp = test_simpleLP()
# pids = addprocs(2, topology = :master_worker)
# @everywhere using COBREXA, GLPK
# fluxes = fluxVariabilityAnalysis(cp, [1, 2], GLPK.Optimizer, pids)
# @test fluxes ≈ [
# 1.0 1.0
# 2.0 2.0
# ]
# rmprocs(pids)
# end
@testset "Parallel FVA" begin
cp = test_simpleLP()
pids = addprocs(2, topology = :master_worker)
@everywhere using COBREXA, GLPK
fluxes = fluxVariabilityAnalysis(cp, [1, 2], GLPK.Optimizer, pids)
@test fluxes [
1.0 1.0
2.0 2.0
]
rmprocs(pids)
end
@testset "Flux variability analysis with CobraModel" begin
model = read_model(download("http://bigg.ucsd.edu/static/models/e_coli_core.json", joinpath("data", "e_coli_core.json")))
@test length(model.reactions) == 95 # read in correctly
# FVA
optimizer = Tulip.Optimizer
atts = Dict("IPM_IterationsLimit" => 400)
cons = Dict("EX_glc__D_e" => (-10.0, -10.0))
fva_max, fva_min = fva(model, biomass, optimizer, solver_attributes = atts)
fva_max2, fva_min2 =
fva(model, [biomass, pfl], optimizer, weights = [0.5, 0.5], constraints = cons)
@testset "FVA" begin
@test isapprox(fva_max["PDH"]["PDH"], 9.338922420065819, atol = 1e-6)
@test isapprox(fva_min["PDH"]["PDH"], 9.270274952732315, atol = 1e-6)
@test !isempty(fva_max2)
@test !isempty(fva_min2)
end
end
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