From b08afd9f653c9347d05a98183f481690b6705cd0 Mon Sep 17 00:00:00 2001 From: "St. Elmo Wilken" Date: Sat, 11 Feb 2023 13:49:56 +0100 Subject: [PATCH] remove switching argument for reaction constructors --- .../04b_standardmodel_construction.jl | 20 +++--- src/types/Reaction.jl | 70 ++++++++++++------- test/reconstruction/ObjectModel.jl | 10 +-- test/reconstruction/constrained_allocation.jl | 12 ++-- test/reconstruction/enzyme_constrained.jl | 12 ++-- .../gapfill_minimum_reactions.jl | 41 ++++++----- test/types/BalancedGrowthCommunityModel.jl | 20 +++--- test/types/ObjectModel.jl | 6 +- test/types/Reaction.jl | 12 ++-- 9 files changed, 110 insertions(+), 93 deletions(-) diff --git a/docs/src/examples/04b_standardmodel_construction.jl b/docs/src/examples/04b_standardmodel_construction.jl index 73d9860d3..de4642178 100644 --- a/docs/src/examples/04b_standardmodel_construction.jl +++ b/docs/src/examples/04b_standardmodel_construction.jl @@ -34,17 +34,17 @@ add_metabolites!(model, metabolite_list) # ### Add reactions to the model -r_m1 = Reaction("EX_m1", Dict("m1" => -1.0), :bidirectional) # exchange reaction: m1 <-> (is the same as m1 ↔ nothing) -r1 = Reaction("r1", Dict("m1" => -1.0, "m2" => 1.0), :forward) +r_m1 = ReactionBidirectional("EX_m1", Dict("m1" => -1.0)) # exchange reaction: m1 <-> (is the same as m1 ↔ nothing) +r1 = ReactionForward("r1", Dict("m1" => -1.0, "m2" => 1.0)) r1.gene_associations = [Isozyme(["g1", "g2"]), Isozyme(["g3"])] # add some gene reaction rules -r2 = Reaction("r2", Dict("m2" => -1.0, "m1" => 1.0), :backward) -r3 = Reaction("r3", Dict("m2" => -1.0, "m3" => 1.0), :bidirectional) -r4 = Reaction("r3", Dict("m2" => -1.0, "m4" => 1.0), :forward) -r_m3 = Reaction("r3", Dict("m3" => -1.0), :bidirectional) -r_m4 = Reaction("r3", Dict("m4" => -1.0), :forward) -r5 = Reaction("r5", Dict("m4" => -1.0, "m2" => 1.0), :forward) - -add_reactions!(model, [r1, r2, r3, r_m1, r4, r_m3, r_m4, r5]) # function approach +r2 = ReactionBackward("r2", Dict("m2" => -1.0, "m1" => 1.0)) +r3 = ReactionBidirectional("r3", Dict("m2" => -1.0, "m3" => 1.0)) +r4 = ReactionForward("r3", Dict("m2" => -1.0, "m4" => 1.0)) +r_m3 = ReactionBidirectional("r3", Dict("m3" => -1.0)) +r_m4 = ReactionForward("r3", Dict("m4" => -1.0)) +r5 = ReactionForward("r5", Dict("m4" => -1.0, "m2" => 1.0)) + +add_reactions!(model, [r1, r2, r3, r_m1, r4, r_m3, r_m4, r5]) m1 = metabolite_list[1] m2 = metabolite_list[2] diff --git a/src/types/Reaction.jl b/src/types/Reaction.jl index 96107855f..0230b8f15 100644 --- a/src/types/Reaction.jl +++ b/src/types/Reaction.jl @@ -31,35 +31,53 @@ Reaction(id; kwargs...) = Reaction(; id, kwargs...) """ $(TYPEDSIGNATURES) -Convenience constructor for `Reaction`. The reaction equation is specified using -`metabolites`, which is a dictionary mapping metabolite ids to stoichiometric -coefficients. The direcion of the reaction is set through `dir` which can take -`:bidirectional`, `:forward`, and `:backward` as values. Finally, the +Convenience constructor for `Reaction` that generates a reaction constrained to +carry flux only in the forward direction relative to the `metabolites`, which is +a dictionary mapping metabolite ids to stoichiometric coefficients. The +`default_bound` is the value taken to mean infinity in the context of constraint +based models, often this is set to a very high flux value like 1000. + +See also: [`Reaction`](@ref), [`ReactionBackward`](@ref), [`ReactionBidirectional`](@ref) +""" +ReactionForward(id::String, metabolites; default_bound = constants.default_reaction_bound) = + Reaction(id; metabolites = metabolites, lower_bound = 0.0, upper_bound = default_bound) + +""" +$(TYPEDSIGNATURES) + +Convenience constructor for `Reaction` that generates a reaction constrained to +carry flux only in the backward direction relative to the `metabolites`, which is +a dictionary mapping metabolite ids to stoichiometric coefficients. The `default_bound` is the value taken to mean infinity in the context of constraint based models, often this is set to a very high flux value like 1000. + +See also: [`Reaction`](@ref), [`ReactionForward`](@ref), [`ReactionBidirectional`](@ref) """ -function Reaction( +ReactionBackward( id::String, - metabolites, - dir = :bidirectional; + metabolites; default_bound = constants.default_reaction_bound, +) = Reaction(id; metabolites = metabolites, lower_bound = -default_bound, upper_bound = 0.0) + +""" +$(TYPEDSIGNATURES) + +Convenience constructor for `Reaction` that generates a reaction constrained to +carry flux in both the forward and backward direction relative to the +`metabolites`, which is a dictionary mapping metabolite ids to stoichiometric +coefficients. The `default_bound` is the value taken to mean infinity in the +context of constraint based models, often this is set to a very high flux value +like 1000. + +See also: [`Reaction`](@ref), [`ReactionForward`](@ref), [`ReactionBackward`](@ref) +""" +ReactionBidirectional( + id::String, + metabolites; + default_bound = constants.default_reaction_bound, +) = Reaction( + id; + metabolites = metabolites, + lower_bound = -default_bound, + upper_bound = default_bound, ) - if dir == :forward - lower_bound = 0.0 - upper_bound = default_bound - elseif dir == :backward - lower_bound = -default_bound - upper_bound = 0.0 - elseif dir == :bidirectional - lower_bound = -default_bound - upper_bound = default_bound - else - throw(DomainError(dir, "unsupported direction")) - end - Reaction( - id; - metabolites = metabolites, - lower_bound = lower_bound, - upper_bound = upper_bound, - ) -end diff --git a/test/reconstruction/ObjectModel.jl b/test/reconstruction/ObjectModel.jl index edd9b94e4..87aa4bc2a 100644 --- a/test/reconstruction/ObjectModel.jl +++ b/test/reconstruction/ObjectModel.jl @@ -17,12 +17,12 @@ g6 = Gene("g6") g7 = Gene("g7") - r1 = Reaction("r1", Dict(m1.id => -1.0, m2.id => 1.0), :forward) - r2 = Reaction("r2", Dict(m2.id => -2.0, m3.id => 1.0), :bidirectional) + r1 = ReactionForward("r1", Dict(m1.id => -1.0, m2.id => 1.0)) + r2 = ReactionBidirectional("r2", Dict(m2.id => -2.0, m3.id => 1.0)) r2.gene_associations = [Isozyme(x) for x in [["g2"], ["g1", "g3"]]] - r3 = Reaction("r3", Dict(m1.id => -1.0, m4.id => 2.0), :backward) - r4 = Reaction("r4", Dict(m1.id => -5.0, m4.id => 2.0), :backward) - r5 = Reaction("r5", Dict(m1.id => -11.0, m4.id => 2.0, m3.id => 2.0), :backward) + r3 = ReactionBackward("r3", Dict(m1.id => -1.0, m4.id => 2.0)) + r4 = ReactionBackward("r4", Dict(m1.id => -5.0, m4.id => 2.0)) + r5 = ReactionBackward("r5", Dict(m1.id => -11.0, m4.id => 2.0, m3.id => 2.0)) rxns = [r1, r2] diff --git a/test/reconstruction/constrained_allocation.jl b/test/reconstruction/constrained_allocation.jl index 6293824df..c33ab87fb 100644 --- a/test/reconstruction/constrained_allocation.jl +++ b/test/reconstruction/constrained_allocation.jl @@ -9,12 +9,12 @@ add_reactions!( m, [ - Reaction("r1", Dict("m1" => 1), :forward), - Reaction("r2", Dict("m2" => 1), :forward), - Reaction("r3", Dict("m1" => -1, "m2" => -1, "m3" => 1), :forward), - Reaction("r4", Dict("m3" => -1, "m4" => 1), :forward), - Reaction("r5", Dict("m2" => -1, "m4" => 1), :bidirectional), - Reaction("r6", Dict("m4" => -1), :bidirectional), # different! + ReactionForward("r1", Dict("m1" => 1)), + ReactionForward("r2", Dict("m2" => 1)), + ReactionForward("r3", Dict("m1" => -1, "m2" => -1, "m3" => 1)), + ReactionForward("r4", Dict("m3" => -1, "m4" => 1)), + ReactionBidirectional("r5", Dict("m2" => -1, "m4" => 1)), + ReactionBidirectional("r6", Dict("m4" => -1)), # different! ], ) diff --git a/test/reconstruction/enzyme_constrained.jl b/test/reconstruction/enzyme_constrained.jl index e8cd20a92..53490a3e8 100644 --- a/test/reconstruction/enzyme_constrained.jl +++ b/test/reconstruction/enzyme_constrained.jl @@ -102,12 +102,12 @@ end add_reactions!( m, [ - Reaction("r1", Dict("m1" => 1), :forward), - Reaction("r2", Dict("m2" => 1), :forward), - Reaction("r3", Dict("m1" => -1, "m2" => -1, "m3" => 1), :forward), - Reaction("r4", Dict("m3" => -1, "m4" => 1), :forward), - Reaction("r5", Dict("m2" => -1, "m4" => 1), :bidirectional), - Reaction("r6", Dict("m4" => -1), :forward), + ReactionForward("r1", Dict("m1" => 1)), + ReactionForward("r2", Dict("m2" => 1)), + ReactionForward("r3", Dict("m1" => -1, "m2" => -1, "m3" => 1)), + ReactionForward("r4", Dict("m3" => -1, "m4" => 1)), + ReactionBidirectional("r5", Dict("m2" => -1, "m4" => 1)), + ReactionForward("r6", Dict("m4" => -1)), ], ) diff --git a/test/reconstruction/gapfill_minimum_reactions.jl b/test/reconstruction/gapfill_minimum_reactions.jl index 921df6bbd..65c6b5848 100644 --- a/test/reconstruction/gapfill_minimum_reactions.jl +++ b/test/reconstruction/gapfill_minimum_reactions.jl @@ -13,21 +13,20 @@ add_reactions!( model, [ - Reaction("r1", Dict("m1" => 1), :forward; default_bound = 1), - Reaction("r2", Dict("m1" => -1, "m2" => 1), :bidirectional; default_bound = 10), - Reaction("r3", Dict("m1" => -1, "m3" => 1), :forward), - Reaction("r4", Dict("m2" => -1, "m4" => 1), :bidirectional), - # Reaction("r5", Dict("m3" => -1, "m4" => 1), :forward), - Reaction("r6", Dict("m4" => -1), :forward), - # Reaction("r7", Dict("m2" => -1, "m7" => 1, "m6" => 1), :forward), - Reaction("r8", Dict("m7" => -1, "m8" => 1), :forward), - Reaction("r9", Dict("m8" => -1), :forward), - # Reaction("r10", Dict("m6" => -1), :forward), - Reaction("r11", Dict("m2" => -1, "m3" => -1, "m7" => -1), :forward), - Reaction( + ReactionForward("r1", Dict("m1" => 1); default_bound = 1), + ReactionBidirectional("r2", Dict("m1" => -1, "m2" => 1); default_bound = 10), + ReactionForward("r3", Dict("m1" => -1, "m3" => 1)), + ReactionBidirectional("r4", Dict("m2" => -1, "m4" => 1)), + # ReactionForward("r5", Dict("m3" => -1, "m4" => 1)), + ReactionForward("r6", Dict("m4" => -1)), + # ReactionForward("r7", Dict("m2" => -1, "m7" => 1, "m6" => 1)), + ReactionForward("r8", Dict("m7" => -1, "m8" => 1)), + ReactionForward("r9", Dict("m8" => -1)), + # ReactionForward("r10", Dict("m6" => -1)), + ReactionForward("r11", Dict("m2" => -1, "m3" => -1, "m7" => -1)), + ReactionBidirectional( "r12", - Dict("m3" => -1, "m5" => 1), - :bidirectional; + Dict("m3" => -1, "m5" => 1); default_bound = 10, ), ], @@ -37,13 +36,13 @@ add_metabolites!(model, [m1, m2, m3, m4, m5, m7, m8]) - r5 = Reaction("r5", Dict("m3" => -1, "m4" => 1), :forward) - r7 = Reaction("r7", Dict("m2" => -1, "m7" => 1, "m6" => 1), :forward) - r10 = Reaction("r10", Dict("m6" => -1), :forward) - rA = Reaction("rA", Dict("m1" => -1, "m2" => 1, "m3" => 1), :forward) - rB = Reaction("rB", Dict("m2" => -1, "m9" => 1), :forward) - rC = Reaction("rC", Dict("m9" => -1, "m10" => 1), :bidirectional) - rD = Reaction("rC", Dict("m10" => -1), :backward) + r5 = ReactionForward("r5", Dict("m3" => -1, "m4" => 1)) + r7 = ReactionForward("r7", Dict("m2" => -1, "m7" => 1, "m6" => 1)) + r10 = ReactionForward("r10", Dict("m6" => -1)) + rA = ReactionForward("rA", Dict("m1" => -1, "m2" => 1, "m3" => 1)) + rB = ReactionForward("rB", Dict("m2" => -1, "m9" => 1)) + rC = ReactionBidirectional("rC", Dict("m9" => -1, "m10" => 1)) + rD = ReactionBackward("rC", Dict("m10" => -1)) universal_reactions = [r5, r7, r10, rA, rB, rC, rD] diff --git a/test/types/BalancedGrowthCommunityModel.jl b/test/types/BalancedGrowthCommunityModel.jl index df2c32060..0e9daf46b 100644 --- a/test/types/BalancedGrowthCommunityModel.jl +++ b/test/types/BalancedGrowthCommunityModel.jl @@ -14,11 +14,11 @@ add_reactions!( m1, [ - Reaction("EX_A", Dict("Ae" => -1), :bidirectional), - Reaction("r1", Dict("Ae" => -1, "A" => 1), :bidirectional), - Reaction("r2", Dict("A" => -1, "B" => 1, "X1" => 1), :bidirectional), - Reaction("r3", Dict("B" => -1, "Be" => 1), :forward), - Reaction("EX_B", Dict("Be" => -1), :forward), + ReactionBidirectional("EX_A", Dict("Ae" => -1)), + ReactionBidirectional("r1", Dict("Ae" => -1, "A" => 1)), + ReactionBidirectional("r2", Dict("A" => -1, "B" => 1, "X1" => 1)), + ReactionForward("r3", Dict("B" => -1, "Be" => 1)), + ReactionForward("EX_B", Dict("Be" => -1)), ], ) @@ -37,11 +37,11 @@ add_reactions!( m2, [ - Reaction("r3", Dict("C" => -1, "Ce" => 1), :forward), - Reaction("EX_C", Dict("Ce" => -1), :forward), - Reaction("EX_A", Dict("Ae" => -1), :bidirectional), - Reaction("r1", Dict("Ae" => -1, "A" => 1), :bidirectional), - Reaction("r2", Dict("A" => -1, "C" => 1, "X2" => 1), :bidirectional), + ReactionForward("r3", Dict("C" => -1, "Ce" => 1)), + ReactionForward("EX_C", Dict("Ce" => -1)), + ReactionBidirectional("EX_A", Dict("Ae" => -1)), + ReactionBidirectional("r1", Dict("Ae" => -1, "A" => 1)), + ReactionBidirectional("r2", Dict("A" => -1, "C" => 1, "X2" => 1)), ], ) diff --git a/test/types/ObjectModel.jl b/test/types/ObjectModel.jl index 3c061a694..9b4d04604 100644 --- a/test/types/ObjectModel.jl +++ b/test/types/ObjectModel.jl @@ -29,9 +29,9 @@ r1.notes = Dict("notes" => ["blah", "blah"]) r1.annotations = Dict("sboterm" => ["sbo"], "biocyc" => ["ads", "asds"]) - r2 = Reaction("r2", Dict(m1.id => -2.0, m4.id => 1.0), :backward) - r3 = Reaction("r3", Dict(m3.id => -1.0, m4.id => 1.0), :forward) - r4 = Reaction("r4", Dict(m3.id => -1.0, m4.id => 1.0), :bidirectional) + r2 = ReactionBackward("r2", Dict(m1.id => -2.0, m4.id => 1.0)) + r3 = ReactionForward("r3", Dict(m3.id => -1.0, m4.id => 1.0)) + r4 = ReactionBidirectional("r4", Dict(m3.id => -1.0, m4.id => 1.0)) r4.annotations = Dict("sboterm" => ["sbo"], "biocyc" => ["ads", "asds"]) mets = [m1, m2, m3, m4] diff --git a/test/types/Reaction.jl b/test/types/Reaction.jl index 14dd14769..aa76527be 100644 --- a/test/types/Reaction.jl +++ b/test/types/Reaction.jl @@ -37,13 +37,13 @@ ), ) - r2 = Reaction("r2", Dict(m1.id => -2.0, m4.id => 1.0), :backward) + r2 = ReactionBackward("r2", Dict(m1.id => -2.0, m4.id => 1.0)) @test r2.lower_bound == -1000.0 && r2.upper_bound == 0.0 - r3 = Reaction("r3", Dict(m3.id => -1.0, m4.id => 1.0), :forward) + r3 = ReactionForward("r3", Dict(m3.id => -1.0, m4.id => 1.0)) @test r3.lower_bound == 0.0 && r3.upper_bound == 1000.0 - r4 = Reaction("r4", Dict(m3.id => -1.0, m4.id => 1.0), :bidirectional) + r4 = ReactionBidirectional("r4", Dict(m3.id => -1.0, m4.id => 1.0)) r4.annotations = Dict("sboterm" => ["sbo"], "biocyc" => ["ads", "asds"]) @test r4.lower_bound == -1000.0 && r4.upper_bound == 1000.0 @@ -53,15 +53,15 @@ id = check_duplicate_reaction(r4, rd) @test id == "r3" - r5 = Reaction("r5", Dict(m3.id => -11.0, m4.id => 1.0), :bidirectional) + r5 = ReactionBidirectional("r5", Dict(m3.id => -11.0, m4.id => 1.0)) id = check_duplicate_reaction(r5, rd) @test id == "r3" - r5 = Reaction("r5", Dict(m3.id => -11.0, m4.id => 1.0), :bidirectional) + r5 = ReactionBidirectional("r5", Dict(m3.id => -11.0, m4.id => 1.0)) id = check_duplicate_reaction(r5, rd; only_metabolites = false) @test isnothing(id) - r5 = Reaction("r5", Dict(m3.id => -1.0, m4.id => 1.0), :bidirectional) + r5 = ReactionBidirectional("r5", Dict(m3.id => -1.0, m4.id => 1.0)) id = check_duplicate_reaction(r5, rd; only_metabolites = false) @test id == "r3" end