flux_variability_analysis.jl 5.1 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
St. Elmo's avatar
St. Elmo committed
22
23
24
FBA problem (see [`flux_balance_analysis`](@ref)). See "Gudmundsson, S., Thiele,
I. Computationally efficient flux variability analysis. BMC Bioinformatics 11,
489 (2010). https://doi.org/10.1186/1471-2105-11-489" for more information.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
25
26
27
28

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
29
30
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
31
32
33
34
35
36
37
38
39
40
41
42
43

`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
individual reactions. More detailed information can be extracted e.g. by
setting it to `m -> (JuMP.objective_value(m), JuMP.value.(m[:x]))`.

Returns a matrix of extracted `ret` values for minima and maxima, of total size
`length(reactions)`×2. The optimizer result status is not checked by default,
instead `ret` function can access the `JuMP.termination_status` of the model
and react accordingly, depending on user decision.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
44
"""
45
function flux_variability_analysis(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
46
    model::MetabolicModel,
47
    reactions::Vector{Int},
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
48
49
50
51
52
53
    optimizer;
    modifications = [],
    workers = [myid()],
    bounds = z -> (z, z),
    ret = objective_value,
)
54
    if any(reactions .< 1) || any(reactions .> n_reactions(model))
55
        throw(DomainError(reactions, "Index exceeds number of reactions."))
56
57
    end

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
58
59
60
61
62
    Z = bounds(
        objective_value(
            flux_balance_analysis(model, optimizer; modifications = modifications),
        ),
    )
63

64
65
66
    # store a JuMP optimization model at all workers
    save_model = :(
        begin
67
68
69
70
71
            model = $model
            opt_model = $COBREXA.make_optimization_model(model, $optimizer)
            for mod in $modifications
                mod(model, opt_model)
            end
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
72
            $COBREXA._FVA_add_constraint(optmodel, $(objective(model)), optmodel[:x], $Z)
73
74
            optmodel
        end
75
    )
76
77
    map(fetch, save_at.(workers, :cobrexa_parfva_model, Ref(save_model)))
    save_model = nothing # this has some volume, free it again
78

79
    # schedule FVA parts parallely using pmap
80
    fluxes = dpmap(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
81
        rid -> :($COBREXA._FVA_optimize_reaction(cobrexa_parfva_model, $rid, $ret)),
82
        CachingPool(workers),
83
84
        [-reactions reactions],
    )
85

86
    # free the data on workers
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
87
    map(fetch, remove_from.(workers, :cobrexa_parfva_model))
88

89
90
    return fluxes
end
91

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

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
99
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
100
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
101
function flux_variability_analysis(model::MetabolicModel, optimizer; kwargs...)
102
    n = n_reactions(model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
103
    return flux_variability_analysis(model, collect(1:n), optimizer; kwargs...)
104
105
end

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
106
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
107
108
109
110
111
    flux_variability_analysis_dict(
        model::MetabolicModel,
        optimizer;
        kwargs...
    )
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
112

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
113
114
115
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.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
116
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
117
118
119
120
121
function flux_variability_analysis_dict(model::MetabolicModel, optimizer; kwargs...)
    vs = flux_variability_analysis(
        model,
        optimizer;
        kwargs...,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
122
        ret = m -> JuMP.value.(m[:x]),
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
123
124
    )
    rxns = reactions(model)
125

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
126
    return (
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
127
128
        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
129
    )
130
end
131

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
132
133
"""
    _FVA_add_constraint(model, c, x, Z)
St. Elmo's avatar
St. Elmo committed
134

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
135
136
137
138
139
140
141
Internal helper function for adding constraints to a model. Exists mainly
because for avoiding namespace problems on remote workers.
"""
function _FVA_add_constraint(model, c, x, Z)
    Z[1] > -Inf && @constraint(model, c' * x >= Z[1])
    Z[2] < Inf && @constraint(model, c' * x <= Z[2])
end
St. Elmo's avatar
St. Elmo committed
142

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
143
144
"""
    _FVA_get_opt(model, rid)
145

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
146
147
148
149
Internal helper for creating the optimized model on a remote worker, for
avoiding namespace problems.
"""
function _FVA_optimize_reaction(model, rid, ret)
St. Elmo's avatar
St. Elmo committed
150
    sense = rid > 0 ? MAX_SENSE : MIN_SENSE
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
151
    var = all_variables(model)[abs(rid)]
152

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
153
154
    @objective(model, sense, var)
    optimize!(model)
155

St. Elmo's avatar
St. Elmo committed
156
    if is_solved(model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
157
158
159
160
        return ret(model)
    else
        return nothing
    end
161
end