From a29517c5c4da3d2c59a59143733ada447f0048ec Mon Sep 17 00:00:00 2001 From: Torkel Date: Mon, 20 May 2024 15:24:44 -0400 Subject: [PATCH 1/4] init --- src/reaction.jl | 24 ++++++++++++++++++++++++ test/reactionsystem_core/reaction.jl | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/reaction.jl b/src/reaction.jl index f4f9cc0112..9118e2aa48 100644 --- a/src/reaction.jl +++ b/src/reaction.jl @@ -343,6 +343,30 @@ end MT.is_diff_equation(rx::Reaction) = false MT.is_alg_equation(rx::Reaction) = false +""" + get_variables(rx::Reaction) + +Returns all symbolic variables that are part of a reaction. This includes all variables +encountered in: + - Rates. + - Among substrates and products. + - Among stoichiometries. + - Among potential noise scaling metadata. +""" +function ModelingToolkit.get_variables(rx::Reaction) + sym_vars = get_variables(rx.rate) + sym_vars = unique!([sym_vars; rx.substrates; rx.products]) + for stoich in rx.substoich + sym_vars = unique!([sym_vars; get_variables(stoich)]) + end + for stoich in rx.prodstoich + sym_vars = unique!([sym_vars; get_variables(stoich)]) + end + if has_noise_scaling(rx) + sym_vars = unique!([sym_vars; get_variables(get_noise_scaling(rx))]) + end + return sym_vars +end ### Dependency-related Functions ### diff --git a/test/reactionsystem_core/reaction.jl b/test/reactionsystem_core/reaction.jl index 26b9d939ec..cab04c5277 100644 --- a/test/reactionsystem_core/reaction.jl +++ b/test/reactionsystem_core/reaction.jl @@ -3,6 +3,31 @@ # Fetch packages. using Catalyst, Test +# Sets the default `t` to use. +t = default_t() + +### Test Basic Accessors ### + +# Tests the `get_variables` function. +let + # Declare symbolic variables. + @parameters k1 k2 n1 n2 η1 η2 + @species X(t) Y(t) Z(t) + @variables A(t) + + # Create `Reaction`s. + rx1 = Reaction(k1, [], [X]) + rx2 = Reaction(k1 + k2, [X], [Y], [1], [n1]; metadata = [:noise_scaling => η1]) + rx3 = Reaction(k1 + k2 + A, [X], [X, Y, Z], [1], [n1 + n2, 2, 1]) + rx4 = Reaction(X + t, [], [Y]; metadata = [:noise_scaling => η1 + η2]) + + # Test `get_variables`. + @test issetequal(get_variables(rx1), [k1, X]) + @test issetequal(get_variables(rx2), [k1, k2, X, Y, n1, η1]) + @test issetequal(get_variables(rx3), [k1, k2, A, X, Y, Z, n1, n2]) + @test issetequal(get_variables(rx4), [X, t, Y, η1, η2]) +end + ### Tests Metadata ### # Tests creation. From 90524ba3c1667a179471cad24a30f2da0e696b5a Mon Sep 17 00:00:00 2001 From: Torkel Date: Tue, 21 May 2024 19:58:20 -0400 Subject: [PATCH 2/4] change to get_variables! --- src/reaction.jl | 34 +++++++++++++++++++--------- test/reactionsystem_core/reaction.jl | 20 +++++++++++----- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/reaction.jl b/src/reaction.jl index 9118e2aa48..6e4d9e105e 100644 --- a/src/reaction.jl +++ b/src/reaction.jl @@ -344,7 +344,7 @@ MT.is_diff_equation(rx::Reaction) = false MT.is_alg_equation(rx::Reaction) = false """ - get_variables(rx::Reaction) + get_symbolics(set, rx::Reaction) Returns all symbolic variables that are part of a reaction. This includes all variables encountered in: @@ -353,19 +353,31 @@ encountered in: - Among stoichiometries. - Among potential noise scaling metadata. """ -function ModelingToolkit.get_variables(rx::Reaction) - sym_vars = get_variables(rx.rate) - sym_vars = unique!([sym_vars; rx.substrates; rx.products]) - for stoich in rx.substoich - sym_vars = unique!([sym_vars; get_variables(stoich)]) - end - for stoich in rx.prodstoich - sym_vars = unique!([sym_vars; get_variables(stoich)]) +function get_symbolics(rx::Reaction) + return ModelingToolkit.get_variables!([], rx) +end + +""" + get_variables!(set, rx::Reaction) + +Adds all symbolic variables that are part of a reaction to set. This includes all variables +encountered in: + - Rates. + - Among substrates and products. + - Among stoichiometries. + - Among potential noise scaling metadata. +""" +function ModelingToolkit.get_variables!(set, rx::Reaction) + get_variables!(set, rx.rate) + append!(set, rx.substrates) + append!(set, rx.products) + for stoichs in (rx.substoich, rx.prodstoich), stoich in stoichs + get_variables!(set, stoich) end if has_noise_scaling(rx) - sym_vars = unique!([sym_vars; get_variables(get_noise_scaling(rx))]) + get_variables!(set, get_noise_scaling(rx)) end - return sym_vars + return unique!(set) end ### Dependency-related Functions ### diff --git a/test/reactionsystem_core/reaction.jl b/test/reactionsystem_core/reaction.jl index cab04c5277..5b5ebf1245 100644 --- a/test/reactionsystem_core/reaction.jl +++ b/test/reactionsystem_core/reaction.jl @@ -2,6 +2,8 @@ # Fetch packages. using Catalyst, Test +using Catalyst: get_symbolics +using ModelingToolkit: value, get_variables! # Sets the default `t` to use. t = default_t() @@ -11,7 +13,7 @@ t = default_t() # Tests the `get_variables` function. let # Declare symbolic variables. - @parameters k1 k2 n1 n2 η1 η2 + @parameters k1 k2 n1 n2 η1 η2 p @species X(t) Y(t) Z(t) @variables A(t) @@ -21,11 +23,17 @@ let rx3 = Reaction(k1 + k2 + A, [X], [X, Y, Z], [1], [n1 + n2, 2, 1]) rx4 = Reaction(X + t, [], [Y]; metadata = [:noise_scaling => η1 + η2]) - # Test `get_variables`. - @test issetequal(get_variables(rx1), [k1, X]) - @test issetequal(get_variables(rx2), [k1, k2, X, Y, n1, η1]) - @test issetequal(get_variables(rx3), [k1, k2, A, X, Y, Z, n1, n2]) - @test issetequal(get_variables(rx4), [X, t, Y, η1, η2]) + # Test `get_variables!`. + @test issetequal(get_variables!([value(p)], rx1), [k1, X, p]) + @test issetequal(get_variables!([value(p)], rx2), [k1, k2, X, Y, n1, η1, p]) + @test issetequal(get_variables!([value(p)], rx3), [k1, k2, A, X, Y, Z, n1, n2, p]) + @test issetequal(get_variables!([value(p)], rx4), [X, t, Y, η1, η2, p]) + + # Test `get_symbolics`. + @test issetequal(get_symbolics(rx1), [k1, X]) + @test issetequal(get_symbolics(rx2), [k1, k2, X, Y, n1, η1]) + @test issetequal(get_symbolics(rx3), [k1, k2, A, X, Y, Z, n1, n2]) + @test issetequal(get_symbolics(rx4), [X, t, Y, η1, η2]) end ### Tests Metadata ### From a55f782095cdbbdbf1552f2a2e8b53eed8a5be2e Mon Sep 17 00:00:00 2001 From: Torkel Date: Thu, 23 May 2024 13:13:11 -0400 Subject: [PATCH 3/4] up --- src/reaction.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/reaction.jl b/src/reaction.jl index 6e4d9e105e..e1bb68c3ff 100644 --- a/src/reaction.jl +++ b/src/reaction.jl @@ -368,11 +368,14 @@ encountered in: - Among potential noise scaling metadata. """ function ModelingToolkit.get_variables!(set, rx::Reaction) + if isdefined(Main, :Infiltrator) + Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__) + end get_variables!(set, rx.rate) append!(set, rx.substrates) append!(set, rx.products) for stoichs in (rx.substoich, rx.prodstoich), stoich in stoichs - get_variables!(set, stoich) + (stoich isa BasicSymbolic) && get_variables!(set, stoich) end if has_noise_scaling(rx) get_variables!(set, get_noise_scaling(rx)) From 8bca45bec2043703675d72a4beb2e6c0677bb804 Mon Sep 17 00:00:00 2001 From: Torkel Date: Thu, 23 May 2024 13:14:10 -0400 Subject: [PATCH 4/4] rebase fix --- src/reaction.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reaction.jl b/src/reaction.jl index e1bb68c3ff..fe00a23c99 100644 --- a/src/reaction.jl +++ b/src/reaction.jl @@ -372,15 +372,15 @@ function ModelingToolkit.get_variables!(set, rx::Reaction) Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__) end get_variables!(set, rx.rate) - append!(set, rx.substrates) - append!(set, rx.products) + foreach(sub -> push!(set, sub), rx.substrates) + foreach(prod -> push!(set, prod), rx.products) for stoichs in (rx.substoich, rx.prodstoich), stoich in stoichs (stoich isa BasicSymbolic) && get_variables!(set, stoich) end if has_noise_scaling(rx) get_variables!(set, get_noise_scaling(rx)) end - return unique!(set) + return (set isa AbstractVector) ? unique!(set) : set end ### Dependency-related Functions ###