Skip to content

Commit

Permalink
Merge ce7f9f7 into 59294a5
Browse files Browse the repository at this point in the history
  • Loading branch information
TorkelE committed Jun 8, 2024
2 parents 59294a5 + ce7f9f7 commit 7692915
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/registered_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ expand_registered_functions(expr)
Takes an expression, and expands registered function expressions. E.g. `mm(X,v,K)` is replaced with v*X/(X+K). Currently supported functions: `mm`, `mmr`, `hill`, `hillr`, and `hill`.
"""
function expand_registered_functions(expr)
function expand_registered_functions!(expr)
iscall(expr) || return expr
args = arguments(expr)
if operation(expr) == Catalyst.mm
Expand All @@ -132,18 +132,18 @@ function expand_registered_functions(expr)
((args[1])^args[5] + (args[2])^args[5] + (args[4])^args[5])
end
for i in 1:length(args)
args[i] = expand_registered_functions(args[i])
args[i] = expand_registered_functions!(args[i])
end
return expr
end
# If applied to a Reaction, return a reaction with its rate modified.
function expand_registered_functions(rx::Reaction)
Reaction(expand_registered_functions(rx.rate), rx.substrates, rx.products,
Reaction(expand_registered_functions!(deepcopy(rx.rate)), rx.substrates, rx.products,
rx.substoich, rx.prodstoich, rx.netstoich, rx.only_use_rate, rx.metadata)
end
# If applied to a Equation, returns it with it applied to lhs and rhs
function expand_registered_functions(eq::Equation)
return expand_registered_functions(eq.lhs) ~ expand_registered_functions(eq.rhs)
return expand_registered_functions!(deepcopy(eq.lhs)) ~ expand_registered_functions!(deepcopy(eq.rhs))
end
# If applied to a ReactionSystem, applied function to all Reactions and other Equations, and return updated system.
function expand_registered_functions(rs::ReactionSystem)
Expand Down
20 changes: 20 additions & 0 deletions test/reactionsystem_core/custom_crn_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,24 @@ let
@test isequal(Catalyst.expand_registered_functions(eq3), 0 ~ V * (X^N) / (X^N + K^N))
@test isequal(Catalyst.expand_registered_functions(eq4), 0 ~ V * (K^N) / (X^N + K^N))
@test isequal(Catalyst.expand_registered_functions(eq5), 0 ~ V * (X^N) / (X^N + Y^N + K^N))
end

# Ensures that original system is not modified.
let
# Create model with a registered function.
@species X(t)
@variables V(t)
@parameters v K
eqs = [
Reaction(mm(X,v,K), [], [X]),
mm(V,v,K) ~ V + 1
]
@named rs = ReactionSystem(eqs, t)

# Check that `expand_registered_functions` does not mutate original model.
rs_expanded_funcs = Catalyst.expand_registered_functions(rs)
@test isequal(only(Catalyst.get_rxs(rs)).rate, Catalyst.mm(X,v,K))
@test isequal(only(Catalyst.get_rxs(rs_expanded_funcs)).rate, v*X/(X + K))
@test isequal(last(Catalyst.get_eqs(rs)).lhs, Catalyst.mm(V,v,K))
@test isequal(last(Catalyst.get_eqs(rs_expanded_funcs)).lhs, v*V/(V + K))
end

0 comments on commit 7692915

Please sign in to comment.