Skip to content

Commit

Permalink
implement reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
stelmo committed Nov 27, 2021
1 parent 043f2aa commit d585420
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 145 deletions.
103 changes: 39 additions & 64 deletions src/analysis/max_min_driving_force.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function max_min_driving_force(

# add the relative bounds
for ((mid1, mid2), val) in concentration_ratios
idxs = indexin([mid1, mid2], metabolites(model))
idxs = indexin([mid1, mid2], metabolites(model)) # TODO: this is not performant
any(isnothing.(idxs)) &&
throw(DomainError((mid1, mid2), "metabolite pair not found in model."))
@constraint(opt_model, logcs[idxs[1]] == log(val) + logcs[idxs[2]])
Expand Down Expand Up @@ -188,7 +188,8 @@ end
reaction_standard_gibbs_free_energies::Dict{String,Float64},
optimizer;
workers =[myid()],
relax_driving_force = 0.9,
optimal_objective_value = nothing,
bounds = z -> (z, Inf),
flux_solution::Dict{String,Float64} = Dict{String,Float64}(),
proton_ids::Vector{String} = ["h_c", "h_e"],
water_ids::Vector{String} = ["h2o_c", "h2o_e"],
Expand All @@ -206,16 +207,20 @@ end
ignore_reaction_ids = [],
)
Perform a variant of flux variability analysis on a max min driving force type problem. All
the arguments are forwarded to [`max_min_driving_force`](@ref). Calls [`screen`](@ref)
internally and possibly distributes computation across `workers`. First performs regular max
min driving force analysis to find the max min driving force of the model. Then iteratively
maximizes and minimizes the driving force across each reaction, and then the concentrations
while staying close to the original max min driving force. Specifically, the max min driving
force is constrained to be beween the optimum, as found using
[`max_min_driving_force`](@ref), and `relax_driving_force` times the optimum. If
`relax_driving_force = 1`, then the max min driving force of the problem is fixed to that
value instead of constrained.
Perform a variant of flux variability analysis on a max min driving force type problem.
Arguments are forwarded to [`max_min_driving_force`](@ref). Calls [`screen`](@ref)
internally and possibly distributes computation across `workers`. If
`optimal_objective_value = nothing`, the function first performs regular max min driving
force analysis to find the max min driving force of the model and sets this to
`optimal_objective_value`. Then iteratively maximizes and minimizes the driving force across
each reaction, and then the concentrations while staying close to the original max min
driving force as specified in `bounds`.
The `bounds` is a user-supplied function that specifies the max min driving force bounds for
the variability optimizations, by default it restricts the flux objective value to the
precise optimum reached in the normal max min driving force analysis. It can return `-Inf`
and `Inf` in first and second pair to remove the limit. Use [`gamma_bounds`](@ref) and
[`objective_bounds`](@ref) for simple bounds.
Returns a matrix of solutions to [`max_min_driving_force`](@ref) additionally constrained as
described above, where the rows are in the order of the reactions and then the metabolites
Expand All @@ -229,50 +234,31 @@ function max_min_driving_force_variability(
reaction_standard_gibbs_free_energies::Dict{String,Float64},
optimizer;
workers = [myid()],
relax_driving_force = 0.9,
flux_solution::Dict{String,Float64} = Dict{String,Float64}(),
proton_ids::Vector{String} = ["h_c", "h_e"],
water_ids::Vector{String} = ["h2o_c", "h2o_e"],
constant_concentrations::Dict{String,Float64} = Dict{String,Float64}(),
concentration_ratios::Dict{Tuple{String,String},Float64} = Dict{
Tuple{String,String},
Float64,
}(),
concentration_lb = 1e-9,
concentration_ub = 100e-3,
T = _constants.T,
R = _constants.R,
small_flux_tol = 1e-6,
modifications = [],
ignore_reaction_ids = [],
optimal_objective_value = nothing,
bounds = z -> (z, Inf),
kwargs...
)
initsol = max_min_driving_force(
model,
reaction_standard_gibbs_free_energies,
optimizer;
flux_solution,
proton_ids,
water_ids,
constant_concentrations,
concentration_ratios,
concentration_lb,
concentration_ub,
T,
R,
small_flux_tol,
modifications,
ignore_reaction_ids,
)
if isnothing(optimal_objective_value)
initsol = max_min_driving_force(
model,
reaction_standard_gibbs_free_energies,
optimizer;
kwargs...
)
mmdf = initsol.mmdf
else
mmdf = optimal_objective_value
end

mmdf = initsol.mmdf
lb, ub = bounds(mmdf)

dgr_variants = [
[[_mmdf_add_df_bound(mmdf, relax_driving_force), _mmdf_dgr_objective(ridx, sense)]] for ridx = 1:n_reactions(model), sense in [MOI.MAX_SENSE, MOI.MIN_SENSE]
[[_mmdf_add_df_bound(lb, ub), _mmdf_dgr_objective(ridx, sense)]] for ridx = 1:n_reactions(model), sense in [MOI.MAX_SENSE, MOI.MIN_SENSE]
]
concen_variants = [
[
[
_mmdf_add_df_bound(mmdf, relax_driving_force),
_mmdf_add_df_bound(lb, ub),
_mmdf_concen_objective(midx, sense),
],
] for midx = 1:n_metabolites(model), sense in [MOI.MAX_SENSE, MOI.MIN_SENSE]
Expand All @@ -285,18 +271,7 @@ function max_min_driving_force_variability(
m,
reaction_standard_gibbs_free_energies,
optimizer;
flux_solution,
proton_ids,
water_ids,
constant_concentrations,
concentration_ratios,
concentration_lb,
concentration_ub,
T,
R,
small_flux_tol,
modifications = [modifications; args],
ignore_reaction_ids,
kwargs...
),
workers,
)
Expand All @@ -323,12 +298,12 @@ end
"""
Helper function to add a new constraint on the driving force.
"""
function _mmdf_add_df_bound(dg_opt, gamma)
function _mmdf_add_df_bound(lb, ub)
(model, opt_model) -> begin
if gamma == 1
fix(opt_model[:mmdf], dg_opt; force = true)
if lb == ub
fix(opt_model[:mmdf], lb; force = true)
else
@constraint(opt_model, gamma * dg_opt <= opt_model[:mmdf])
@constraint(opt_model, lb <= opt_model[:mmdf] <= ub)
end
end
end
3 changes: 1 addition & 2 deletions test/analysis/max_min_driving_force.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
modifications = [add_loopless_constraints()],
)

reaction_standard_gibbs_free_energies = get_thermo_data()

sol = max_min_driving_force(
model,
reaction_standard_gibbs_free_energies,
Expand All @@ -34,6 +32,7 @@
model,
reaction_standard_gibbs_free_energies,
Tulip.Optimizer;
bounds = gamma_bounds(0.9),
flux_solution = flux_solution,
proton_ids = ["h_c", "h_e"],
water_ids = ["h2o_c", "h2o_e"],
Expand Down
154 changes: 75 additions & 79 deletions test/data_static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,82 +73,78 @@ test_toyModel() = CoreModel(
["m1[c]", "m3[c]", "m2[c]", "m1[e]", "m3[e]", "biomass[c]"],
)

function get_thermo_data()
# these data come from eQuilibrator. Use the Julia API to get them.
reaction_standard_gibbs_free_energies = Dict(
"ACALD" => -21.268198981756314,
"PTAr" => 8.651025243027988,
"ALCD2x" => 17.47260052762408,
"PDH" => -34.246780702043225,
"PYK" => -24.48733600711958,
"CO2t" => 0.0,
"MALt2_2" => -6.839209921974724,
"CS" => -39.330940148207475,
"PGM" => -4.470553692565886,
"TKT1" => -1.4976299544215408,
"ACONTa" => 8.46962176350985,
"GLNS" => -15.771242654033706,
"ICL" => 9.53738025507404,
"FBA" => 23.376920310319235,
"SUCCt3" => -43.97082007726285,
"FORt2" => -3.4211767267069124,
"G6PDH2r" => -7.3971867270165035,
"AKGDH" => -28.235442362320782,
"TKT2" => -10.318436817276165,
"FRD7" => 73.61589524884772,
"SUCOAS" => -1.1586968559555615,
"FBP" => -11.606887851480572,
"ICDHyr" => 5.398885342794983,
"AKGt2r" => 10.085299238039797,
"GLUSy" => -47.21690395283849,
"TPI" => 5.621932460512994,
"FORt" => 13.509690780237293,
"ACONTb" => -1.622946931741609,
"GLNabc" => -30.194559792842753,
"RPE" => -3.3881719029747615,
"ACKr" => 14.027197131450492,
"THD2" => -33.84686533309243,
"PFL" => -19.814421615735,
"RPI" => 4.477649590945703,
"D_LACt2" => -3.4223903975852004,
"TALA" => -0.949571985515206,
"PPCK" => 10.659841402564751,
"ACt2r" => -3.4196348363397995,
"NH4t" => -13.606633927039097,
"PGL" => -25.94931748161696,
"NADTRHD" => -0.014869680795754903,
"PGK" => 19.57192102020454,
"LDH_D" => 20.04059765689044,
"ME1" => 12.084968268076864,
"PIt2r" => 10.415108493818785,
"ATPS4r" => -37.570267233299816,
"PYRt2" => -3.422891289768689,
"GLCpts" => -45.42430981510088,
"GLUDy" => 32.834943812395665,
"CYTBD" => -59.700410815493775,
"FUMt2_2" => -6.845105500839001,
"FRUpts2" => -42.67529760694199,
"GAPD" => 0.5307809794271634,
"H2Ot" => -1.5987211554602254e-14,
"PPC" => -40.81304419704113,
"NADH16" => -80.37770501380615,
"PFK" => -18.546314942995934,
"MDH" => 25.912462872631522,
"PGI" => 2.6307087407442395,
"O2t" => 0.0,
"ME2" => 12.099837948872533,
"GND" => 10.312275879236381,
"SUCCt2_2" => -6.82178244356977,
"GLUN" => -14.381960140443113,
"ETOHt2r" => -16.930867506944217,
"ADK1" => 0.3893321583896068,
"ACALDt" => -3.197442310920451e-14,
"SUCDi" => -73.61589524884772,
"ENO" => -3.8108376097261782,
"MALS" => -39.22150045995042,
"GLUt2r" => -3.499043558772904,
"PPS" => -6.0551989457468665,
"FUM" => -3.424133018702122,
)
return reaction_standard_gibbs_free_energies
end
const reaction_standard_gibbs_free_energies = Dict(
"ACALD" => -21.268198981756314,
"PTAr" => 8.651025243027988,
"ALCD2x" => 17.47260052762408,
"PDH" => -34.246780702043225,
"PYK" => -24.48733600711958,
"CO2t" => 0.0,
"MALt2_2" => -6.839209921974724,
"CS" => -39.330940148207475,
"PGM" => -4.470553692565886,
"TKT1" => -1.4976299544215408,
"ACONTa" => 8.46962176350985,
"GLNS" => -15.771242654033706,
"ICL" => 9.53738025507404,
"FBA" => 23.376920310319235,
"SUCCt3" => -43.97082007726285,
"FORt2" => -3.4211767267069124,
"G6PDH2r" => -7.3971867270165035,
"AKGDH" => -28.235442362320782,
"TKT2" => -10.318436817276165,
"FRD7" => 73.61589524884772,
"SUCOAS" => -1.1586968559555615,
"FBP" => -11.606887851480572,
"ICDHyr" => 5.398885342794983,
"AKGt2r" => 10.085299238039797,
"GLUSy" => -47.21690395283849,
"TPI" => 5.621932460512994,
"FORt" => 13.509690780237293,
"ACONTb" => -1.622946931741609,
"GLNabc" => -30.194559792842753,
"RPE" => -3.3881719029747615,
"ACKr" => 14.027197131450492,
"THD2" => -33.84686533309243,
"PFL" => -19.814421615735,
"RPI" => 4.477649590945703,
"D_LACt2" => -3.4223903975852004,
"TALA" => -0.949571985515206,
"PPCK" => 10.659841402564751,
"ACt2r" => -3.4196348363397995,
"NH4t" => -13.606633927039097,
"PGL" => -25.94931748161696,
"NADTRHD" => -0.014869680795754903,
"PGK" => 19.57192102020454,
"LDH_D" => 20.04059765689044,
"ME1" => 12.084968268076864,
"PIt2r" => 10.415108493818785,
"ATPS4r" => -37.570267233299816,
"PYRt2" => -3.422891289768689,
"GLCpts" => -45.42430981510088,
"GLUDy" => 32.834943812395665,
"CYTBD" => -59.700410815493775,
"FUMt2_2" => -6.845105500839001,
"FRUpts2" => -42.67529760694199,
"GAPD" => 0.5307809794271634,
"H2Ot" => -1.5987211554602254e-14,
"PPC" => -40.81304419704113,
"NADH16" => -80.37770501380615,
"PFK" => -18.546314942995934,
"MDH" => 25.912462872631522,
"PGI" => 2.6307087407442395,
"O2t" => 0.0,
"ME2" => 12.099837948872533,
"GND" => 10.312275879236381,
"SUCCt2_2" => -6.82178244356977,
"GLUN" => -14.381960140443113,
"ETOHt2r" => -16.930867506944217,
"ADK1" => 0.3893321583896068,
"ACALDt" => -3.197442310920451e-14,
"SUCDi" => -73.61589524884772,
"ENO" => -3.8108376097261782,
"MALS" => -39.22150045995042,
"GLUt2r" => -3.499043558772904,
"PPS" => -6.0551989457468665,
"FUM" => -3.424133018702122,
)

0 comments on commit d585420

Please sign in to comment.