fba.jl 2.77 KB
Newer Older
1
"""
2
    flux_balance_analysis(model::M, optimizer) where {M<:MetabolicModel}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
3
4

Flux balance analysis solves the following problem for the input `model`:
Sylvain Arreckx's avatar
Sylvain Arreckx committed
5
6
7
8
9
```
max cᵀx
s.t. S x = b
     xₗ ≤ x ≤ xᵤ
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
10

11
Returns a solved model from [`optimize_model`](@ref).
Sylvain Arreckx's avatar
Sylvain Arreckx committed
12
"""
13
14
flux_balance_analysis(model::M, optimizer) where {M<:MetabolicModel} =
    optimize_model(model, optimizer; sense = MOI.MAX_SENSE)
15

16
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
17
    flux_balance_analysis_vec(args...)::Union{Vector{Float64},Nothing}
18
19
20
21
22

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).
"""
23
24
function flux_balance_analysis_vec(args...; kwargs...)::Union{Vector{Float64},Nothing}
    optmodel = flux_balance_analysis(args...; kwargs...)
25

St. Elmo's avatar
St. Elmo committed
26
27
    JuMP.termination_status(optmodel) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED] || return nothing
    value.(optmodel[:x])
28
29
30
end

"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
31
    flux_balance_analysis_dict(model::M, args...)::Union{Dict{String, Float64},Nothing} where {M <: MetabolicModel}
32
33
34
35
36
37

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).
"""
function flux_balance_analysis_dict(
    model::M,
38
39
    args...;
    kwargs...,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
40
)::Union{Dict{String,Float64},Nothing} where {M<:MetabolicModel}
41
    v = flux_balance_analysis_vec(model, args...; kwargs...)
42
43
44
45
    isnothing(v) && return nothing
    Dict(zip(reactions(model), v))
end

46
"""
St. Elmo's avatar
St. Elmo committed
47
    flux_balance_analysis(model::StandardModel, optimizer; modifications)
48

St. Elmo's avatar
St. Elmo committed
49
50
51
52
53
Run flux balance analysis (FBA) on the `model` optionally specifying `modifications` to the problem.
These modifications can be entered as an array of modifications, or a single modification.
Leave this keyword argument out if you do not want to modify the problem.
See [`modify_constraint`](@ref), [`modify_solver_attribute`](@ref),[`modify_objective`](@ref), and [`modify_sense`](@ref)
for possible modifications.
54
Note, the `optimizer` must be set to perform the analysis, any JuMP solver will work.
St. Elmo's avatar
St. Elmo committed
55
Returns a solved JuMP model.
56

57
58
59
# Example
```
optimizer = Gurobi.Optimizer
St. Elmo's avatar
St. Elmo committed
60
61
62
model = CobraTools.read_model("e_coli_core.json")
biomass = findfirst(model.reactions, "BIOMASS_Ecoli_core_w_GAM")
solved_model = fba(model, optimizer; modifications=[modify_objective(biomass)])
63
```
64
"""
St. Elmo's avatar
St. Elmo committed
65
66
function flux_balance_analysis(
    model::StandardModel,
67
    optimizer;
St. Elmo's avatar
St. Elmo committed
68
    modifications = [(model, opt_model) -> nothing],
69
)
St. Elmo's avatar
St. Elmo committed
70
    # get core optimization problem
St. Elmo's avatar
St. Elmo committed
71
    cbm = make_optimization_model(model, optimizer)
72
73

    # apply callbacks
St. Elmo's avatar
St. Elmo committed
74
75
76
    if typeof(modifications) <: Vector # many modifications
        for mod in modifications
            mod(model, cbm)
77
        end
St. Elmo's avatar
St. Elmo committed
78
79
    else # single modification
        modifications(model, cbm)
80
    end
81

St. Elmo's avatar
St. Elmo committed
82
    JuMP.optimize!(cbm)
83

St. Elmo's avatar
St. Elmo committed
84
85
    return cbm
end