flux_balance_analysis.jl 2.19 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
45
```
max cᵀx
s.t. S x = b
     xₗ ≤ x ≤ xᵤ
```

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

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

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
54
Returns an optimized `JuMP` model.
55

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

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

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
70
71
    opt_model = make_optimization_model(model, optimizer)

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
72
73
    for mod in modifications
        mod(model, opt_model)
74
    end
75

St. Elmo's avatar
St. Elmo committed
76
    COBREXA.JuMP.optimize!(opt_model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
77
    return opt_model
St. Elmo's avatar
St. Elmo committed
78
end