Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve MMDF #5

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JSONFBCModels = "475c1105-d6ed-49c1-9b32-c11adca6d3e8"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Tulip = "6dd1b50a-3aae-11e9-10b5-ef983d2400fa"
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"

[compat]
Documenter = "1"
Expand Down
51 changes: 39 additions & 12 deletions docs/src/examples/06-mmdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import Downloads: download
# Additionally to COBREXA, and the model format package, we will need a solver
# -- let's use GLPK here:

using COBREXA
stelmo marked this conversation as resolved.
Show resolved Hide resolved
import JSONFBCModels
import GLPK

Expand All @@ -64,7 +63,6 @@ reaction_standard_gibbs_free_energies = Dict{String,Float64}(
"TPI" => 5.621932460512994,
)


# (The units of the energies are kJ/mol.)

# ## Running basic max min driving force analysis
Expand All @@ -85,16 +83,16 @@ reaction_standard_gibbs_free_energies = Dict{String,Float64}(
# other reactions.

reference_flux = Dict(
"ENO" => 1.0,
"ENO" => 2.0,
"FBA" => 1.0,
"GAPD" => 1.0,
"GAPD" => 2.0,
"GLCpts" => 1.0,
"LDH_D" => -1.0,
"LDH_D" => -2.0,
"PFK" => 1.0,
"PGI" => 1.0,
"PGK" => -1.0,
"PGM" => -1.0,
"PYK" => 1.0,
"PGK" => -2.0,
"PGM" => -2.0,
"PYK" => 2.0,
"TPI" => 1.0,
)

Expand All @@ -108,11 +106,12 @@ reference_flux = Dict(

mmdf_solution = max_min_driving_force_analysis(
model,
reaction_standard_gibbs_free_energies;
reference_flux,
reaction_standard_gibbs_free_energies,
reference_flux;
constant_concentrations = Dict("lac__D_c" => 1e-1, "nad_c" => 2.6e-3),
concentration_ratios = Dict(
"atp" => ("atp_c", "adp_c", 10.0),
"nadh" => ("nadh_c", "nad_c", 0.13),
"nadh" => ("nadh_c", "nad_c", 0.1),
),
proton_metabolites = ["h_c", "h_e"],
water_metabolites = ["h2o_c", "h2o_e"],
Expand All @@ -124,4 +123,32 @@ mmdf_solution = max_min_driving_force_analysis(
)

# TODO verify correctness
@test isapprox(mmdf_solution.min_driving_force, 2.79911, atol = TEST_TOLERANCE) #src
@test isapprox(mmdf_solution.min_driving_force, -2.4739129, atol = TEST_TOLERANCE) #src

# ## Plot the results
# We can see that the ΔG bottleneck is 2.5 kJ/mol, and that there is a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from where is this visible?

# precipitous increase in driving force near the end of glycolysis. The overall
# ΔG for the optimized pathway, under the restrictions in the model, is -158
# kJ/mol, which compares favourably with the estimated ΔG under standard
# biological conditions: -133 kJ/mol.

using CairoMakie

glycolysis_reaction_order =
["GLCpts", "PGI", "PFK", "FBA", "TPI", "GAPD", "PGK", "PGM", "ENO", "PYK", "LDH_D"]

glycolysis_thermo = cumsum(
reference_flux[rid] * mmdf_solution.reaction_gibbs_free_energies[Symbol(rid)] for
rid in glycolysis_reaction_order
)

lines(
1:length(glycolysis_reaction_order),
glycolysis_thermo,
axis = (
xlabel = "Reactions",
ylabel = "Cumulative ΔG [kJ/mol]",
xticks = (1:length(glycolysis_reaction_order), glycolysis_reaction_order),
),
)

1 change: 1 addition & 0 deletions src/COBREXA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ include("builders/loopless.jl")
include("builders/objectives.jl")
include("builders/scale.jl")
include("builders/unsigned.jl")
include("builders/mmdf.jl")

# simplified front-ends for the above
include("frontend/balance.jl")
Expand Down
43 changes: 0 additions & 43 deletions src/builders/fbc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,46 +109,3 @@ function flux_balance_constraints(
end

export flux_balance_constraints

"""
$(TYPEDSIGNATURES)

Build log-concentration-stoichiometry constraints for the `model`, as used e.g.
by [`max_min_driving_force_analysis`](@ref).

The output constraint tree contains a log-concentration variable for each
metabolite in subtree `log_concentrations`. Individual reactions' total
reactant log concentrations (i.e., all log concentrations of actual reactants
minus all log concentrations of products) have their own variables in
`reactant_log_concentrations`.

Function `concentration_bound` may return a bound for the log-concentration of
a given metabolite (compatible with `ConstraintTrees.Bound`), or `nothing`.
"""
function log_concentration_constraints(
model::A.AbstractFBCModel;
concentration_bound = _ -> nothing,
)
rxns = Symbol.(A.reactions(model))
mets = Symbol.(A.metabolites(model))
stoi = A.stoichiometry(model)

constraints =
:log_concentrations^C.variables(keys = mets, bounds = concentration_bound.(mets))

cs = C.ConstraintTree()

for (midx, ridx, coeff) in zip(SparseArrays.findnz(stoi)...)
rid = rxns[ridx]
value = constraints.log_concentrations[mets[midx]].value * coeff
if haskey(cs, rid)
cs[rid].value += value
else
cs[rid] = C.Constraint(; value)
end
end

return constraints * :reactant_log_concentrations^cs
end

export log_concentration_constraints
70 changes: 70 additions & 0 deletions src/builders/mmdf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

# Copyright (c) 2021-2024, University of Luxembourg
# Copyright (c) 2021-2024, Heinrich-Heine University Duesseldorf
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
$(TYPEDSIGNATURES)

Allocate log-concentration-stoichiometry constraints for the `reaction_subset`
and `metabolite_subset` in `model`, as used by e.g.
[`max_min_driving_force_analysis`](@ref).

The output constraint tree contains a log-concentration variable for each
metabolite in subtree `log_concentrations`. The reaction quotient in log
variables for each reaction is stored in `log_concentration_stoichiometry`.

Function `concentration_bound` may return a bound for the log-concentration of a
given metabolite (compatible with `ConstraintTrees.Bound`), or `nothing`.
"""
function log_concentration_constraints(
model::A.AbstractFBCModel;
reaction_subset = A.reactions(model),
metabolite_subset = A.metabolites(model),
concentration_bound = _ -> nothing,
)
stoi = A.stoichiometry(model)

constraints =
:log_concentrations^C.variables(
keys = Symbol.(metabolite_subset),
bounds = concentration_bound.(metabolite_subset),
)

# map old idxs of metabolites to new order of smaller system
midx_lookup = Dict(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kill indexes, we worked hard to finally eradicate the stupid indexing from everywhere.

this can be done by just using the original code and adding a conditional continue to the right spot

indexin(metabolite_subset, A.metabolites(model)) .=>
1:length(metabolite_subset),
)

cs = C.ConstraintTree()
for (ridx_new, ridx_old) in enumerate(indexin(reaction_subset, A.reactions(model)))
midxs_old, stoich_coeffs = SparseArrays.findnz(stoi[:, ridx_old])
rid = Symbol(reaction_subset[ridx_new])
for (midx_old, stoich_coeff) in zip(midxs_old, stoich_coeffs)
midx_new = midx_lookup[midx_old]
mid = Symbol(metabolite_subset[midx_new])
value = constraints.log_concentrations[mid].value * stoich_coeff
if haskey(cs, rid)
cs[rid].value += value
else
cs[rid] = C.Constraint(; value)
end
end
end

return constraints * :log_concentration_stoichiometries^cs
end

export log_concentration_constraints
Loading
Loading