flux_variability_analysis.jl 5.04 KB
Newer Older
Sylvain Arreckx's avatar
Sylvain Arreckx committed
1
"""
2
    flux_variability_analysis(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
3
        model::MetabolicModel,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
4
        reactions::Vector{Int},
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
5
6
7
8
9
10
11
12
13
        optimizer;
        modifications = [],
        workers = [myid()],
        bounds = z -> (z,z),
        ret = objective_value,
    )::Matrix{Float64}

Flux variability analysis solves a pair of optimization problems in `model` for
each flux listed in `reactions`:
Sylvain Arreckx's avatar
Sylvain Arreckx committed
14
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
15
 min,max xᵢ
Sylvain Arreckx's avatar
Sylvain Arreckx committed
16
s.t. S x = b
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
17
18
19
    xₗ ≤ x ≤ xᵤ
     cᵀx ≥ bounds(Z₀)[1]
     cᵀx ≤ bounds(Z₀)[2]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
20
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
21
where Z₀:= cᵀx₀ is the objective value of an optimal solution of the associated
22
23
FBA problem (see [`flux_balance_analysis`](@ref) for a related analysis, also
for explanation of the `modifications` argument).
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
24
25
26
27

The `bounds` is a user-supplied function that specifies the objective bounds
for the variability optimizations, by default it restricts the flux objective
value to the precise optimum reached in FBA. It can return `-Inf` and `Inf` in
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
28
29
first and second pair to remove the limit. Use [`gamma_bounds`](@ref) and
[`objective_bounds`](@ref) for simple bounds.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
30
31
32
33
34
35

`optimizer` must be set to a `JuMP`-compatible optimizer. The computation of
the individual optimization problems is transparently distributed to `workers`
(see `Distributed.workers()`).

`ret` is a function used to extract results from optimized JuMP models of the
36
37
38
39
individual reactions. By default, it calls and returns the value of
`JuMP.objective_value`. More information can be extracted e.g. by setting it to
a function that returns a more elaborate data structure; such as `m ->
(JuMP.objective_value(m), JuMP.value.(m[:x]))`.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
40
41

Returns a matrix of extracted `ret` values for minima and maxima, of total size
42
43
44
(`length(reactions)`,2). The optimizer result status is checked with
[`is_solved`](@ref); `nothing` is returned if the optimization failed for any
reason.
45
46
47
48
49
50

# Example
```
model = load_model("e_coli_core.json")
flux_variability_analysis(model, [1, 2, 3, 42], GLPK.optimizer)
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
51
"""
52
function flux_variability_analysis(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
53
    model::MetabolicModel,
54
    reactions::Vector{Int},
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
55
56
57
58
59
60
    optimizer;
    modifications = [],
    workers = [myid()],
    bounds = z -> (z, z),
    ret = objective_value,
)
61
    if any(reactions .< 1) || any(reactions .> n_reactions(model))
62
        throw(DomainError(reactions, "Index exceeds number of reactions."))
63
64
    end

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
65
66
67
68
69
    Z = bounds(
        objective_value(
            flux_balance_analysis(model, optimizer; modifications = modifications),
        ),
    )
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    return screen_optmodel_modifications(
        model,
        optimizer;
        common_modifications = vcat(
            modifications,
            [
                (model, opt_model) -> begin
                    Z[1] > -Inf && @constraint(
                        opt_model,
                        objective(model)' * opt_model[:x] >= Z[1]
                    )
                    Z[2] < Inf && @constraint(
                        opt_model,
                        objective(model)' * opt_model[:x] <= Z[2]
                    )
                end,
            ],
        ),
        args = [-reactions reactions],
        analysis = (_, opt_model, ridx) -> _max_variability_flux(opt_model, ridx, ret),
91
    )
92
end
93

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
94
"""
95
    flux_variability_analysis(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
96
        model::MetabolicModel,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
97
        optimizer;
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
98
99
        kwargs...
    )
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
100

101
102
A simpler version of [`flux_variability_analysis`](@ref) that maximizes and
minimizes all reactions in the model. Arguments are forwarded.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
103
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
104
function flux_variability_analysis(model::MetabolicModel, optimizer; kwargs...)
105
    n = n_reactions(model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
106
    return flux_variability_analysis(model, collect(1:n), optimizer; kwargs...)
107
108
end

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
109
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
110
111
112
113
114
    flux_variability_analysis_dict(
        model::MetabolicModel,
        optimizer;
        kwargs...
    )
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
115

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
116
117
118
A variant of [`flux_variability_analysis`](@ref) that returns the individual
maximized and minimized fluxes of all reactions as two dictionaries (of
dictionaries). All keyword arguments except `ret` are passed through.
St. Elmo's avatar
St. Elmo committed
119
120
121
122
123
124
125
126
127

# Example
```
mins, maxs = flux_variability_analysis_dict(
    model,
    Tulip.Optimizer;
    bounds = objective_bounds(0.99),
    modifications = [
        change_optimizer_attribute("IPM_IterationsLimit", 500),
128
129
        change_constraint("EX_glc__D_e"; lb = -10, ub = -10),
        change_constraint("EX_o2_e"; lb = 0, ub = 0),
St. Elmo's avatar
St. Elmo committed
130
131
132
    ],
)
```
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
133
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
134
135
136
137
138
function flux_variability_analysis_dict(model::MetabolicModel, optimizer; kwargs...)
    vs = flux_variability_analysis(
        model,
        optimizer;
        kwargs...,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
139
        ret = m -> JuMP.value.(m[:x]),
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
140
141
    )
    rxns = reactions(model)
142

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
143
    return (
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
144
145
        Dict(zip(rxns, [Dict(zip(rxns, fluxes)) for fluxes in vs[:, 1]])),
        Dict(zip(rxns, [Dict(zip(rxns, fluxes)) for fluxes in vs[:, 2]])),
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
146
    )
147
end
148

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
149
"""
150
    _max_variability_flux(opt_model, rid, ret)
151

152
Internal helper for maximizing reactions in optimization model.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
153
"""
154
function _max_variability_flux(opt_model, rid, ret)
St. Elmo's avatar
St. Elmo committed
155
    sense = rid > 0 ? MAX_SENSE : MIN_SENSE
156
    var = all_variables(opt_model)[abs(rid)]
157

158
159
    @objective(opt_model, sense, var)
    optimize!(opt_model)
160

161
162
    if is_solved(opt_model)
        return ret(opt_model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
163
164
165
    else
        return nothing
    end
166
end