Skip to content

Commit 3e37fc4

Browse files
committed
updates
1 parent 8f48831 commit 3e37fc4

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

src/expression_utils.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ function is_escaped_expr(expr)
9393
return (expr isa Expr) && (expr.head == :escape) && (length(expr.args) == 1)
9494
end
9595

96+
97+
### Generic Expression Handling ###
98+
99+
# Convert an expression with equal signs (e.g. :(a=1.0, b=2.0)) to one with pairs (e.g. :(a=>1.0, b=>2.0))
100+
function expr_equal_vector_to_pairs(expr_vec)
101+
pair_vector = :([])
102+
foreach(arg -> push!(pair_vector.args, arg.args[1] => arg.args[2]), expr_vec.args)
103+
return pair_vector
104+
end
105+
96106
### Old Stuff ###
97107

98108
#This will be called whenever a function stored in funcdict is called.

src/reaction_network.jl

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,10 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
364364
default_reaction_metadata = :([])
365365
compound_expr, compound_species = read_compound_options(options)
366366
check_default_noise_scaling!(default_reaction_metadata, options)
367+
default_reaction_metadata = expr_equal_vector_to_pairs(default_reaction_metadata)
367368

368369
# Parses reactions, species, and parameters.
369-
reactions = get_reactions(reaction_lines; default_reaction_metadata)
370+
reactions = get_reactions(reaction_lines)
370371
species_declared = [extract_syms(options, :species); compound_species]
371372
parameters_declared = extract_syms(options, :parameters)
372373
variables = extract_syms(options, :variables)
@@ -424,9 +425,12 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
424425
$observed_vars
425426
$comps
426427

427-
Catalyst.make_ReactionSystem_internal($rxexprs, $tiv, setdiff(union($spssym, $varssym, $compssym), $obs_syms),
428-
$pssym; name = $name, spatial_ivs = $sivs,
429-
observed = $observed_eqs)
428+
Catalyst.remake_ReactionSystem_internal(
429+
Catalyst.make_ReactionSystem_internal(
430+
$rxexprs, $tiv, setdiff(union($spssym, $varssym, $compssym), $obs_syms),
431+
$pssym; name = $name, spatial_ivs = $sivs, observed = $observed_eqs);
432+
default_reaction_metadata = $default_reaction_metadata
433+
)
430434
end
431435
end
432436

@@ -593,8 +597,7 @@ function get_reaction(line)
593597
end
594598

595599
# Generates a vector containing a number of reaction structures, each containing the information about one reaction.
596-
function get_reactions(exprs::Vector{Expr}, reactions = Vector{ReactionStruct}(undef, 0);
597-
default_reaction_metadata = :([]))
600+
function get_reactions(exprs::Vector{Expr}, reactions = Vector{ReactionStruct}(undef, 0))
598601
for line in exprs
599602
# Reads core reaction information.
600603
arrow, rate, reaction, metadata = read_reaction_line(line)
@@ -604,16 +607,12 @@ function get_reactions(exprs::Vector{Expr}, reactions = Vector{ReactionStruct}(u
604607
if typeof(rate) != Expr || rate.head != :tuple
605608
error("Error: Must provide a tuple of reaction rates when declaring a bi-directional reaction.")
606609
end
607-
push_reactions!(reactions, reaction.args[2], reaction.args[3], rate.args[1], metadata.args[1], arrow;
608-
default_reaction_metadata)
609-
push_reactions!(reactions, reaction.args[3], reaction.args[2], rate.args[2], metadata.args[2], arrow;
610-
default_reaction_metadata)
610+
push_reactions!(reactions, reaction.args[2], reaction.args[3], rate.args[1], metadata.args[1], arrow)
611+
push_reactions!(reactions, reaction.args[3], reaction.args[2], rate.args[2], metadata.args[2], arrow)
611612
elseif in(arrow, fwd_arrows)
612-
push_reactions!(reactions, reaction.args[2], reaction.args[3], rate, metadata, arrow;
613-
default_reaction_metadata)
613+
push_reactions!(reactions, reaction.args[2], reaction.args[3], rate, metadata, arrow)
614614
elseif in(arrow, bwd_arrows)
615-
push_reactions!(reactions, reaction.args[3], reaction.args[2], rate, metadata, arrow;
616-
default_reaction_metadata)
615+
push_reactions!(reactions, reaction.args[3], reaction.args[2], rate, metadata, arrow)
617616
else
618617
throw("Malformed reaction, invalid arrow type used in: $(MacroTools.striplines(line))")
619618
end
@@ -646,7 +645,7 @@ end
646645
# Used to create multiple reactions from, for instance, `k, (X,Y) --> 0`.
647646
# Handles metadata, e.g. `1.0, Z --> 0, [noise_scaling=η]`.
648647
function push_reactions!(reactions::Vector{ReactionStruct}, sub_line::ExprValues, prod_line::ExprValues,
649-
rate::ExprValues, metadata::ExprValues, arrow::Symbol; default_reaction_metadata::Expr)
648+
rate::ExprValues, metadata::ExprValues, arrow::Symbol)
650649
# The rates, substrates, products, and metadata may be in a tupple form (e.g. `k, (X,Y) --> 0`).
651650
# This finds the length of these tuples (or 1 if not in tuple forms). Errors if lengs inconsistent.
652651
lengs = (tup_leng(sub_line), tup_leng(prod_line), tup_leng(rate), tup_leng(metadata))
@@ -658,7 +657,6 @@ function push_reactions!(reactions::Vector{ReactionStruct}, sub_line::ExprValues
658657
for i in 1:maximum(lengs)
659658
# If the `only_use_rate` metadata was not provided, this has to be infered from the arrow used.
660659
metadata_i = get_tup_arg(metadata, i)
661-
merge_metadata!(metadata_i, default_reaction_metadata)
662660
if all([arg.args[1] != :only_use_rate for arg in metadata_i.args])
663661
push!(metadata_i.args, :(only_use_rate = $(in(arrow, pure_rate_arrows))))
664662
end
@@ -673,16 +671,6 @@ function push_reactions!(reactions::Vector{ReactionStruct}, sub_line::ExprValues
673671
end
674672
end
675673

676-
# Merge the metadata encoded by a reaction with the default metadata (for all reactions) given by the system.
677-
# If a metadata value is present in both vectors, uses the non-default value.
678-
function merge_metadata!(metadata, default_reaction_metadata)
679-
for entry in default_reaction_metadata.args
680-
if !in(entry.args[1], arg.args[1] for arg in metadata.args)
681-
push!(metadata.args, entry)
682-
end
683-
end
684-
end
685-
686674
function processmult(op, mult, stoich)
687675
if (mult isa Number) && (stoich isa Number)
688676
op(mult, stoich)

src/reactionsystem.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,39 @@ function ReactionSystem(iv; kwargs...)
756756
ReactionSystem(Reaction[], iv, [], []; kwargs...)
757757
end
758758

759+
760+
"""
761+
remake_ReactionSystem_internal(rs::ReactionSystem;
762+
default_reaction_metadata::Vector{Pair{Symbol, T}} = Vector{Pair{Symbol, Any}}()) where {T}
763+
764+
Takes a reaction systems and reameks it, returning a modified reaction system. Modifications depend
765+
on which arguments are provided.
766+
767+
Arguments:
768+
- `rs::ReactionSystem`: The `ReactionSystem` which you wish to make a remade version of.
769+
- `default_reaction_metadata::Vector{Pair{Symbol, T}}`: A vector with default `Reaction` metadata values.
770+
Each metadata in each `Reaction` of the updated `ReactionSystem` will have teh value desiganted in
771+
`default_reaction_metadata` (however, `Reaction`s that already have that metadata designated will not
772+
have their value updated).
773+
"""
774+
#function remake_ReactionSystem_internal(rs::ReactionSystem; default_reaction_metadata::Vector{Pair{Symbol, T}} = Vector{Pair{Symbol, Any}}()) where {T}
775+
function remake_ReactionSystem_internal(rs; default_reaction_metadata = default_reaction_metadata)
776+
# Updates the metadata for all reactions (reactions are ignored).
777+
updated_reactions = [set_default_metadata(rx, default_reaction_metadata) for rx in reactions(rs)]
778+
rs = @set rs.rxs = updated_reactions
779+
return rs
780+
end
781+
782+
# For a `Reaction`, adds missing default metadata values. Equations are passed back unmodified.
783+
function set_default_metadata(rx, default_metadata)
784+
missing_metadata = filter(md -> !in(md[1], entry[1] for entry in rx.metadata), default_metadata)
785+
updated_metadata = vcat(rx.metadata, missing_metadata)
786+
updated_metadata = convert(Vector{Pair{Symbol, Any}}, updated_metadata)
787+
return @set rx.metadata = updated_metadata
788+
end
789+
790+
791+
759792
"""
760793
isspatial(rn::ReactionSystem)
761794

0 commit comments

Comments
 (0)