@@ -297,7 +297,7 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
297297
298298 # Get macro options.
299299 if length (unique (arg. args[1 ] for arg in option_lines)) < length (option_lines)
300- error (" Some options where given multiple times." )
300+ error (" Some options were given multiple times." )
301301 end
302302 options = Dict (map (arg -> Symbol (String (arg. args[1 ])[2 : end ]) => arg,
303303 option_lines))
@@ -315,12 +315,12 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
315315 parameters_declared = extract_syms (options, :parameters )
316316 variables_declared = extract_syms (options, :variables )
317317
318- # Reads more options.
318+ # Reads equations.
319319 vars_extracted, add_default_diff, equations = read_equations_options (
320320 options, variables_declared)
321321 variables = vcat (variables_declared, vars_extracted)
322322
323- # handle independent variables
323+ # Handle independent variables
324324 if haskey (options, :ivs )
325325 ivs = Tuple (extract_syms (options, :ivs ))
326326 ivexpr = copy (options[:ivs ])
@@ -339,14 +339,16 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
339339 combinatoric_ratelaws = true
340340 end
341341
342- # Reads more options .
342+ # Reads observables .
343343 observed_vars, observed_eqs, obs_syms = read_observed_options (
344344 options, [species_declared; variables], all_ivs)
345345
346+ # Collect species and parameters, including ones inferred from the reactions.
346347 declared_syms = Set (Iterators. flatten ((parameters_declared, species_declared,
347348 variables)))
348- species_extracted, parameters_extracted = extract_species_and_parameters! (reactions,
349- declared_syms)
349+ species_extracted, parameters_extracted = extract_species_and_parameters! (
350+ reactions, declared_syms)
351+
350352 species = vcat (species_declared, species_extracted)
351353 parameters = vcat (parameters_declared, parameters_extracted)
352354
@@ -376,9 +378,11 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
376378 push! (rxexprs. args, get_rxexprs (reaction))
377379 end
378380 for equation in equations
381+ equation = escape_equation_RHS! (equation)
379382 push! (rxexprs. args, equation)
380383 end
381384
385+ # Output code corresponding to the reaction system.
382386 quote
383387 $ ivexpr
384388 $ ps
@@ -572,7 +576,7 @@ function get_rxexprs(rxstruct)
572576 subs_stoich_init = deepcopy (subs_init)
573577 prod_init = isempty (rxstruct. products) ? nothing : :([])
574578 prod_stoich_init = deepcopy (prod_init)
575- reaction_func = :(Reaction ($ (recursive_expand_functions ! (rxstruct. rate)), $ subs_init,
579+ reaction_func = :(Reaction ($ (recursive_escape_functions ! (rxstruct. rate)), $ subs_init,
576580 $ prod_init, $ subs_stoich_init, $ prod_stoich_init,
577581 metadata = $ (rxstruct. metadata)))
578582 for sub in rxstruct. substrates
@@ -904,17 +908,24 @@ end
904908
905909# ## Generic Expression Manipulation ###
906910
907- # Recursively traverses an expression and replaces special function call like "hill(...)" with the actual corresponding expression.
908- function recursive_expand_functions ! (expr:: ExprValues )
911+ # Recursively traverses an expression and escapes all the user-defined functions. Special function calls like "hill(...)" are not expanded.
912+ function recursive_escape_functions ! (expr:: ExprValues )
909913 (typeof (expr) != Expr) && (return expr)
910- foreach (i -> expr. args[i] = recursive_expand_functions ! (expr. args[i]),
914+ foreach (i -> expr. args[i] = recursive_escape_functions ! (expr. args[i]),
911915 1 : length (expr. args))
912916 if expr. head == :call
913917 ! isdefined (Catalyst, expr. args[1 ]) && (expr. args[1 ] = esc (expr. args[1 ]))
914918 end
915919 expr
916920end
917921
922+ # Recursively escape functions in the right-hand-side of an equation written using user-defined functions. Special function calls like "hill(...)" are not expanded.
923+ function escape_equation_RHS! (eqexpr:: Expr )
924+ rhs = recursive_escape_functions! (eqexpr. args[3 ])
925+ eqexpr. args[3 ] = rhs
926+ eqexpr
927+ end
928+
918929# Returns the length of a expression tuple, or 1 if it is not an expression tuple (probably a Symbol/Numerical).
919930function tup_leng (ex:: ExprValues )
920931 (typeof (ex) == Expr && ex. head == :tuple ) && (return length (ex. args))
0 commit comments