fba.jl 3.91 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).
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
23
function flux_balance_analysis_vec(args...)::Union{Vector{Float64},Nothing}
St. Elmo's avatar
St. Elmo committed
24
    optmodel = flux_balance_analysis(args...)
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
38

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

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

St. Elmo's avatar
St. Elmo committed
48
49
50
51
52
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.
53
Note, the `optimizer` must be set to perform the analysis, any JuMP solver will work.
St. Elmo's avatar
St. Elmo committed
54
Returns a solved JuMP model.
55

56
57
58
# Example
```
optimizer = Gurobi.Optimizer
St. Elmo's avatar
St. Elmo committed
59
60
61
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)])
62
```
63
"""
St. Elmo's avatar
St. Elmo committed
64
65
function flux_balance_analysis(
    model::StandardModel,
66
    optimizer;
St. Elmo's avatar
St. Elmo committed
67
    modifications = [(model, opt_model) -> nothing],
68
)
St. Elmo's avatar
St. Elmo committed
69
    # get core optimization problem
St. Elmo's avatar
St. Elmo committed
70
    cbm = make_optimization_model(model, optimizer)
71
72

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

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

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

St. Elmo's avatar
St. Elmo committed
86
87
"""
    flux_balance_analysis_vec(model::StandardModel, optimizer; modifications)
88

St. Elmo's avatar
St. Elmo committed
89
90
91
92
93
94
95
96
97
98
Perform flux balance analysis on `model` using `optimizer`. 
Returns a vector fluxes in the order supplied in `model`.
Calls [`flux_balance_analysis`](@ref) internally.
"""
function flux_balance_analysis_vec(
    model::StandardModel,
    optimizer;
    modifications = [(model, opt_model) -> nothing],
)
    cbm = flux_balance_analysis(model, optimizer; modifications = modifications)
99

St. Elmo's avatar
St. Elmo committed
100
101
102
    JuMP.termination_status(cbm) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED] || return nothing

    return value.(cbm[:x])
St. Elmo's avatar
St. Elmo committed
103
104
end

St. Elmo's avatar
St. Elmo committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
    flux_balance_analysis_dict(model::StandardModel, optimizer; modifications)

Perform flux balance analysis on `model` using `optimizer`. 
Returns a dictionary mapping reaction `id`s to fluxes. 
Calls [`flux_balance_analysis`](@ref) internally.
"""
function flux_balance_analysis_dict(
    model::StandardModel,
    optimizer;
    modifications = [(model, opt_model) -> nothing],
)
    cbm = flux_balance_analysis(model, optimizer; modifications = modifications)

    JuMP.termination_status(cbm) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED] || return nothing
St. Elmo's avatar
St. Elmo committed
120
    
St. Elmo's avatar
St. Elmo committed
121
    return Dict(zip(reactions(model), value.(cbm[:x])))
122
end