flux_balance_analysis.jl 4.18 KB
Newer Older
St. Elmo's avatar
St. Elmo committed
1
"""
St. Elmo's avatar
St. Elmo committed
2
3
4
5
6
7
    @flux_balance_analysis_vec

# Example
```
vec = @flux_balance_analysis_vec model Tulip.Optimizer
```
St. Elmo's avatar
St. Elmo committed
8
9
10
11
12
13
14
15
"""
macro flux_balance_analysis_vec(model, optimizer)
    model = esc(model)
    optimizer = esc(optimizer)
    return :(flux_balance_analysis_vec($model, $optimizer))
end

"""
St. Elmo's avatar
St. Elmo committed
16
17
18
19
20
21
22
23
24
    @flux_balance_analysis_vec

# Example
```
@flux_balance_analysis_vec model Tulip.Optimizer begin
    modify_objective(biomass)
    modify_constraint(glucose, -8.0, -8.0)
end
```
St. Elmo's avatar
St. Elmo committed
25
26
27
28
"""
macro flux_balance_analysis_vec(model, optimizer, kws)
    model = esc(model)
    optimizer = esc(optimizer)
St. Elmo's avatar
St. Elmo committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    fs = [m for m in MacroTools.striplines(kws).args]
    mods = Expr(:vect)
    for f in fs
        push!(mods.args, esc(f))
    end
    kwarg = Expr(:kw, :modifications, mods)
    return :(flux_balance_analysis_vec($model, $optimizer; $kwarg))
end

"""
    @flux_balance_analysis_dict

# Example
```
dict = @flux_balance_analysis_dict model Tulip.Optimizer
```
"""
macro flux_balance_analysis_dict(model, optimizer)
    model = esc(model)
    optimizer = esc(optimizer)
    return :(flux_balance_analysis_dict($model, $optimizer))
end

"""
    @flux_balance_analysis_dict

# Example
```
@flux_balance_analysis_dict model Tulip.Optimizer begin
    modify_objective(biomass)
    modify_constraint(glucose, -8.0, -8.0)
end
```
"""
macro flux_balance_analysis_dict(model, optimizer, kws)
    model = esc(model)
    optimizer = esc(optimizer)
    fs = [m for m in MacroTools.striplines(kws).args]
    mods = Expr(:vect)
    for f in fs
        push!(mods.args, esc(f))
St. Elmo's avatar
St. Elmo committed
70
    end
St. Elmo's avatar
St. Elmo committed
71
72
    kwarg = Expr(:kw, :modifications, mods)
    return :(flux_balance_analysis_dict($model, $optimizer; $kwarg))
St. Elmo's avatar
St. Elmo committed
73
end
74

75
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
76
    flux_balance_analysis_vec(args...)::Union{Vector{Float64},Nothing}
77
78
79
80
81

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

St. Elmo's avatar
St. Elmo committed
85
86
    COBREXA.JuMP.termination_status(optmodel) in [MOI.OPTIMAL, MOI.LOCALLY_SOLVED] ||
        return nothing
St. Elmo's avatar
St. Elmo committed
87
    value.(optmodel[:x])
88
89
90
end

"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
91
    flux_balance_analysis_dict(model::M, args...)::Union{Dict{String, Float64},Nothing} where {M <: MetabolicModel}
92
93
94
95
96
97

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,
98
99
    args...;
    kwargs...,
Marvin van Aalst's avatar
Marvin van Aalst committed
100
)::Union{Dict{String,Float64},Nothing} where {M <: MetabolicModel}
101
    v = flux_balance_analysis_vec(model, args...; kwargs...)
102
103
104
105
    isnothing(v) && return nothing
    Dict(zip(reactions(model), v))
end

106
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    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ᵤ
```

Optionally, you may specify one or more `modifications` to be applied to the models.
See [`modify_constraint`](@ref),
[`modify_solver_attribute`](@ref),[`modify_objective`](@ref), or
[`modify_sense`](@ref) for examples of modifications.
127

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
128
129
130
The `optimizer` must be set to perform the analysis, any JuMP solver will work.

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

132
133
# Example
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
134
optimizer = GLPK.Optimizer
St. Elmo's avatar
St. Elmo committed
135
136
137
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)])
138
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
139

140
"""
St. Elmo's avatar
St. Elmo committed
141
function flux_balance_analysis(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
142
    model::M,
143
    optimizer;
Marvin van Aalst's avatar
Marvin van Aalst committed
144
145
    modifications=[(model, opt_model) -> nothing],
) where {M <: MetabolicModel}
146

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
147
148
149
150
    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
151
        for mod in modifications
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
152
            mod(model, opt_model)
153
        end
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
154
155
    else
        modifications(model, opt_model)
156
    end
157

St. Elmo's avatar
St. Elmo committed
158
    COBREXA.JuMP.optimize!(opt_model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
159
    return opt_model
St. Elmo's avatar
St. Elmo committed
160
end