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

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

St. Elmo's avatar
St. Elmo committed
11
12
    COBREXA.JuMP.termination_status(optmodel) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED] ||
        return nothing
St. Elmo's avatar
St. Elmo committed
13
    value.(optmodel[:x])
14
15
16
end

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

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

32
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    flux_balance_analysis(
        model::M,
        optimizer;
        modifications = [(model, opt_model) -> nothing],
    ) where {M<:MetabolicModel}

Run flux balance analysis (FBA) on the `model` optionally specifying
`modifications` to the problem.

Effectively solves the optimization problem:
```
max cᵀx
s.t. S x = b
     xₗ ≤ x ≤ xᵤ
```

49
50
51
Optionally, you may specify one or more "modifications" to be applied to the models.
[`change_solver_attribute`](@ref),[`change_objective`](@ref), or
[`change_sense`](@ref) for examples of modifications.
52

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
53
54
55
The `optimizer` must be set to perform the analysis, any JuMP solver will work.

Returns a solved JuMP model from [`optimize_model`](@ref).
56

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

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

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

    # support for multiple modification, fallback to single one
    if typeof(modifications) <: AbstractVector
St. Elmo's avatar
St. Elmo committed
76
        for mod in modifications
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
77
            mod(model, opt_model)
78
        end
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
79
80
    else
        modifications(model, opt_model)
81
    end
82

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