flux_variability_analysis.jl 4.99 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
            optmodel = $COBREXA.make_optimization_model($model, $optimizer)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
68
            $COBREXA._FVA_add_constraint(optmodel, $(objective(model)), optmodel[:x], $Z)
69
70
            optmodel
        end
71
    )
72
73
    map(fetch, save_at.(workers, :cobrexa_parfva_model, Ref(save_model)))
    save_model = nothing # this has some volume, free it again
74

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

82
    # free the data on workers
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
83
    map(fetch, remove_from.(workers, :cobrexa_parfva_model))
84

85
86
    return fluxes
end
87

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
88
"""
89
    flux_variability_analysis(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
90
        model::MetabolicModel,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
91
        optimizer;
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
92
93
        kwargs...
    )
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
94

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

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
102
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
103
104
105
106
107
    flux_variability_analysis_dict(
        model::MetabolicModel,
        optimizer;
        kwargs...
    )
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
108

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

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
122
    return (
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
123
124
        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
125
    )
126
end
127

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
128
129
"""
    _FVA_add_constraint(model, c, x, Z)
St. Elmo's avatar
St. Elmo committed
130

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
131
132
133
134
135
136
137
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
138

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
139
140
"""
    _FVA_get_opt(model, rid)
141

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
142
143
144
145
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
146
    sense = rid > 0 ? MAX_SENSE : MIN_SENSE
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
147
    var = all_variables(model)[abs(rid)]
148

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
149
150
    @objective(model, sense, var)
    optimize!(model)
151

St. Elmo's avatar
St. Elmo committed
152
    if is_solved(model)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
153
154
155
156
        return ret(model)
    else
        return nothing
    end
157
end