flux_variability_analysis.jl 4.81 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
22
23
24
25
26
where Z₀:= cᵀx₀ is the objective value of an optimal solution of the associated
FBA problem (see [`flux_balance_analysis`](@ref)).

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

`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
42
"""
43
function flux_variability_analysis(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
44
    model::MetabolicModel,
45
    reactions::Vector{Int},
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
46
47
48
49
50
51
    optimizer;
    modifications = [],
    workers = [myid()],
    bounds = z -> (z, z),
    ret = objective_value,
)
52
    if any(reactions .< 1) || any(reactions .> n_reactions(model))
53
        throw(DomainError(reactions, "Index exceeds number of reactions."))
54
55
    end

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

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

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

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

83
84
    return fluxes
end
85

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

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

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

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

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

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

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

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
137
138
"""
    _FVA_get_opt(model, rid)
139

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

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

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