Skip to content

Commit 6bca0f2

Browse files
committed
working community
1 parent 36e7e24 commit 6bca0f2

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

docs/src/examples/08-community-models.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,51 @@ model = load_model("e_coli_core.json")
2323
# numbers, molar masses of enzymes, and capacity bounds.
2424

2525
import AbstractFBCModels as A
26+
27+
m1 = fbc_model_constraints(model)
28+
m2 = fbc_model_constraints(model)
29+
30+
lbs, ubs = A.bounds(model)
31+
env_ex_rxns = Dict(rid => (lbs[i], ubs[i]) for (i, rid) in enumerate(A.reactions(model)) if startswith(rid, "EX_"))
32+
33+
34+
m = build_community_environment(env_ex_rxns)
35+
m += :bug1^m1
36+
m += :bug2^m2
37+
38+
member_abundances = [(:bug1, 0.2), (:bug2, 0.8)]
39+
m *=
40+
:environmental_exchange_balances^link_environmental_exchanges(
41+
m,
42+
[(:bug1, 0.2), (:bug2, 0.8)],
43+
)
44+
45+
m *=
46+
:equal_growth_rate_constraint^equal_growth_rate_constraints(
47+
[(:bug1, m.bug1.fluxes.:BIOMASS_Ecoli_core_w_GAM.value), (:bug2, m.bug2.fluxes.:BIOMASS_Ecoli_core_w_GAM.value)]
48+
)
49+
50+
m.bug1.fluxes.EX_glc__D_e.bound = (-1000.0, 1000.0)
51+
m.bug2.fluxes.EX_glc__D_e.bound = (-1000.0, 1000.0)
52+
m.bug1.fluxes.CYTBD.bound = (-10.0, 10.0) # respiration limited
53+
54+
m *= :objective^C.Constraint(m.bug1.fluxes.:BIOMASS_Ecoli_core_w_GAM.value)
55+
56+
using Gurobi
57+
58+
sol = optimized_constraints(m; objective = m.objective.value, optimizer=Gurobi.Optimizer)
59+
60+
Dict(k => v for (k, v) in sol.bug1.fluxes if startswith(string(k), "EX_"))
61+
Dict(k => v for (k, v) in sol.bug2.fluxes if startswith(string(k), "EX_"))
62+
63+
# exchange cytosolic metabolites
64+
mets = [:EX_akg_e, :EX_succ_e, :EX_pyr_e, :EX_acald_e, :EX_fum_e, :EX_mal__L_e]
65+
for met in mets
66+
m.bug1.fluxes[met].bound = (-1000.0, 1000.0)
67+
m.bug2.fluxes[met].bound = (-1000.0, 1000.0)
68+
end
69+
70+
sol = optimized_constraints(m; objective = m.objective.value, optimizer=Tulip.Optimizer, modifications=[set_optimizer_attribute("IPM_IterationsLimit", 100000)])
71+
72+
Dict(k => v for (k, v) in sol.bug1.fluxes if startswith(string(k), "EX_"))
73+
Dict(k => v for (k, v) in sol.bug2.fluxes if startswith(string(k), "EX_"))

src/COBREXA.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ include("builders/objectives.jl")
3737
include("builders/enzymes.jl")
3838
include("builders/thermodynamic.jl")
3939
include("builders/loopless.jl")
40+
include("builders/communities.jl")
4041

4142
include("analysis/modifications.jl")
4243
include("analysis/flux_balance.jl")

src/builders/communities.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
$(TYPEDSIGNATURES)
3+
4+
Helper function to create environmental exchange rections.
5+
"""
6+
function environment_exchange_variables(env_ex_rxns = Dict{String,Tuple{Float64,Float64}}())
7+
rids = collect(keys(env_ex_rxns))
8+
lbs_ubs = collect(values(env_ex_rxns))
9+
C.variables(; keys = Symbol.(rids), bounds = lbs_ubs)
10+
end
11+
12+
export environment_exchange_variables
13+
14+
function build_community_environment(env_ex_rxns = Dict{String,Tuple{Float64,Float64}}())
15+
C.ConstraintTree(
16+
:environmental_exchange_reactions => environment_exchange_variables(env_ex_rxns),
17+
)
18+
end
19+
20+
export build_community_environment
21+
22+
function link_environmental_exchanges(
23+
m::C.ConstraintTree,
24+
member_abundances::Vector{Tuple{Symbol,Float64}};
25+
on = m.:environmental_exchange_reactions,
26+
member_fluxes_id = :fluxes,
27+
)
28+
C.ConstraintTree(
29+
rid => C.Constraint(
30+
value = -rxn.value + sum(
31+
abundance * m[member][member_fluxes_id][rid].value for
32+
(member, abundance) in member_abundances if
33+
haskey(m[member][member_fluxes_id], rid);
34+
init = zero(C.LinearValue),
35+
),
36+
bound = 0.0,
37+
) for (rid, rxn) in on
38+
)
39+
end
40+
41+
export link_environmental_exchanges
42+
43+
function equal_growth_rate_constraints(member_biomasses::Vector{Tuple{Symbol,C.LinearValue}})
44+
C.ConstraintTree(
45+
Symbol(bid1, :_, bid2) => C.Constraint(value = bval1 - bval2, bound = 0.0) for
46+
((bid1, bval1), (bid2, bval2)) in
47+
zip(member_biomasses[1:end-1], member_biomasses[2:end])
48+
)
49+
end
50+
51+
export equal_growth_rate_constraints

0 commit comments

Comments
 (0)