flux_balance_analysis.jl 2.35 KB
Newer Older
1
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
2
    flux_balance_analysis_vec(args...)::Union{Vector{Float64},Nothing}
3
4
5

A variant of FBA that returns a vector of fluxes in the same order as reactions
of the model, if the solution is found.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
6

7
8
Arguments are passed to [`flux_balance_analysis`](@ref).
"""
9
10
function flux_balance_analysis_vec(args...; kwargs...)::Union{Vector{Float64},Nothing}
    optmodel = flux_balance_analysis(args...; kwargs...)
St. Elmo's avatar
St. Elmo committed
11
    is_solved(optmodel) || return nothing
St. Elmo's avatar
St. Elmo committed
12
    value.(optmodel[:x])
13
14
15
end

"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
16
    flux_balance_analysis_dict(model::MetabolicModel, args...)::Union{Dict{String, Float64},Nothing}
17
18
19
20
21

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(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
22
    model::MetabolicModel,
23
24
    args...;
    kwargs...,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
25
)::Union{Dict{String,Float64},Nothing}
26
    v = flux_balance_analysis_vec(model, args...; kwargs...)
27
28
29
30
    isnothing(v) && return nothing
    Dict(zip(reactions(model), v))
end

31
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
32
33
34
    flux_balance_analysis(
        model::M,
        optimizer;
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
35
        modifications = [],
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
36
37
38
    ) where {M<:MetabolicModel}

Run flux balance analysis (FBA) on the `model` optionally specifying
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
39
`modifications` to the problem.  Basically, FBA solves this optimization problem:
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
40
41
42
43
44
```
max cᵀx
s.t. S x = b
     xₗ ≤ x ≤ xᵤ
```
St. Elmo's avatar
St. Elmo committed
45
46
47
See "Orth, J., Thiele, I. & Palsson, B. What is flux balance analysis?. Nat
Biotechnol 28, 245–248 (2010). https://doi.org/10.1038/nbt.1614" for more
information.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
48

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
49
50
The `optimizer` must be set to a `JuMP`-compatible optimizer, such as
`GLPK.Optimizer` or `Tulip.Optimizer`
51

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
52
53
Optionally, you may specify one or more modifications to be applied to the
model before the analysis, such as
54
[`change_optimizer_attribute`](@ref),[`change_objective`](@ref), and
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
55
[`change_sense`](@ref).
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
56

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
57
Returns an optimized `JuMP` model.
58

59
60
# Example
```
61
model = load_model(StandardModel, "e_coli_core.json")
St. Elmo's avatar
St. Elmo committed
62
biomass = findfirst(model.reactions, "BIOMASS_Ecoli_core_w_GAM")
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
63
solution = flux_balance_analysis(model, GLPK.optimizer; modifications=[change_objective(biomass)])
64
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
65

66
"""
St. Elmo's avatar
St. Elmo committed
67
function flux_balance_analysis(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
68
    model::M,
69
    optimizer;
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
70
    modifications = [],
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
71
) where {M<:MetabolicModel}
72

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
73
74
    opt_model = make_optimization_model(model, optimizer)

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
75
76
    for mod in modifications
        mod(model, opt_model)
77
    end
78

St. Elmo's avatar
St. Elmo committed
79
    COBREXA.JuMP.optimize!(opt_model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
80
    return opt_model
St. Elmo's avatar
St. Elmo committed
81
end