flux_balance_analysis.jl 2.48 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
See "Orth, J., Thiele, I. & Palsson, B. What is flux balance analysis?. Nat
46
Biotechnol 28, 245-248 (2010). https://doi.org/10.1038/nbt.1614" for more
St. Elmo's avatar
St. Elmo committed
47
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
Optionally, you may specify one or more modifications to be applied to the
53
54
model before the analysis, such as [`change_optimizer_attribute`](@ref),
[`change_objective`](@ref), and [`change_sense`](@ref).
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
55

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

58
59
# Example
```
60
61
62
63
64
65
66
67
model = load_model("e_coli_core.json")
solution = flux_balance_analysis(model, GLPK.optimizer)
value.(solution[:x])  # extract flux steady state from the optimizer

biomass_reaction_id = findfirst(model.reactions, "BIOMASS_Ecoli_core_w_GAM")

modified_solution = flux_balance_analysis(model, GLPK.optimizer;
    modifications=[change_objective(biomass_reaction_id)])
68
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
69

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

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
77
78
    opt_model = make_optimization_model(model, optimizer)

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
79
80
    for mod in modifications
        mod(model, opt_model)
81
    end
82

83
    optimize!(opt_model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
84
    return opt_model
St. Elmo's avatar
St. Elmo committed
85
end