Skip to content

Commit ed1a0fd

Browse files
authored
Merge pull request #63 from isaacsas/network-datastructs-for-SSAs
pass SSAs species to dependent reaction maps if needed
2 parents d2534f5 + 2ff6bcd commit ed1a0fd

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
julia 1.0
2-
DiffEqJump 4.2.0
2+
DiffEqJump 5.6.0
33
DiffEqBase 3.11.0
44
Compat 0.17.0
55
DataStructures 0.8.1

src/massaction_jump_utils.jl

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# collection of utility functions for getting mass action jumps out of
1+
# collection of utility functions for getting mass action jumps out of
22
# DiffEqBiological reaction network structures
33

44
# return a map from species symbol to species index
55
function species_to_indices(network)
66
specs = network.syms
7-
Dict( specs[i] => i for i in eachindex(specs) )
7+
Dict( specs[i] => i for i in eachindex(specs) )
88
end
99

1010
# return a map from reaction param symbol to rate index
@@ -13,7 +13,7 @@ function rate_to_indices(network)
1313
Dict( rates[i] => i for i in eachindex(rates) )
1414
end
1515

16-
# get substrate stoichiometry for a reaction
16+
# get substrate stoichiometry for a reaction
1717
function get_substrate_stoich(rs, specmap)
1818
reactant_stoich = Vector{Pair{Int,Int}}()
1919
for substrate in rs.substrates
@@ -79,7 +79,7 @@ function network_to_jumpset(rn, specmap, ratemap, params)
7979
majumpvec = Vector{typeof(empty_majump)}()
8080
cjumpvec = Vector{ConstantRateJump}()
8181

82-
for (i,rs) in enumerate(rn.reactions)
82+
for (i,rs) in enumerate(rn.reactions)
8383
if rs.is_pure_mass_action
8484
push!(majumpvec, make_majump(rs, specmap, ratemap, params))
8585
else
@@ -104,7 +104,7 @@ function rxidxs_to_jidxs_map(rn, num_majumps)
104104
cjidx = num_majumps + 1
105105
numrxs = length(rn.reactions)
106106
rxi_to_ji = zeros(Int, numrxs)
107-
for i = 1:numrxs
107+
for i = 1:numrxs
108108
if rn.reactions[i].is_pure_mass_action
109109
rxi_to_ji[i] = majidx
110110
majidx += 1
@@ -116,15 +116,29 @@ function rxidxs_to_jidxs_map(rn, num_majumps)
116116
rxi_to_ji
117117
end
118118

119+
# map from jump to vector of species it changes
120+
function jump_to_dep_specs_map(rn, specmap, rxidxs_jidxs)
121+
numrxs = length(rn.reactions)
122+
numspec = length(specmap)
123+
124+
# map from a jump to vector of species that depend on it
125+
jtos_vec = Vector{Vector{valtype(specmap)}}(undef, numrxs)
126+
for rx in 1:numrxs
127+
jidx = rxidxs_jidxs[rx]
128+
jtos_vec[jidx] = sort!( [ns.first for ns in get_net_stoich(rn.reactions[rx], specmap)] )
129+
end
130+
131+
jtos_vec
132+
end
133+
119134
# map from species to Set of jumps depending on that species
120135
function spec_to_dep_jumps_map(rn, specmap, rxidxs_to_jidxs)
121-
122136
numrxs = length(rn.reactions)
123137
numspec = length(specmap)
124138

125139
# map from a species to jumps that depend on it
126140
spec_to_dep_jumps = [Set{Int}() for n = 1:numspec]
127-
for rx in 1:numrxs
141+
for rx in 1:numrxs
128142
for specsym in rn.reactions[rx].dependants
129143
push!(spec_to_dep_jumps[specmap[specsym]], rxidxs_to_jidxs[rx])
130144
end
@@ -134,26 +148,17 @@ function spec_to_dep_jumps_map(rn, specmap, rxidxs_to_jidxs)
134148
end
135149

136150
# given a reaction network and species map, construct a jump dependency graph
137-
function depgraph_from_network(rn, specmap, jset)
138-
139-
numrxs = length(rn.reactions)
140-
numspec = length(specmap)
141-
num_majumps = get_num_majumps(jset.massaction_jump)
142-
143-
# map reaction indices to jump indices
144-
rxidxs_to_jidxs = rxidxs_to_jidxs_map(rn, num_majumps)
145-
146-
# map from species to jumps that depend on it
147-
spec_to_dep_jumps = spec_to_dep_jumps_map(rn, specmap, rxidxs_to_jidxs)
151+
function depgraph_from_network(rn, specmap, jset, rxidxs_to_jidxs, spec_to_dep_jumps)
148152

149153
# create map from a jump to jumps depending on it
154+
numrxs = length(rn.reactions)
150155
dep_sets = [SortedSet{Int}() for n = 1:numrxs]
151-
for rx in 1:numrxs
156+
for rx in 1:numrxs
152157
jidx = rxidxs_to_jidxs[rx]
153158

154159
# get the net reaction stoichiometry
155160
net_stoich = get_net_stoich(rn.reactions[rx], specmap)
156-
161+
157162
# rx changes spec, hence rxs depending on spec depend on rx
158163
for (spec,stoch) in net_stoich
159164
for dependent_jump in spec_to_dep_jumps[spec]
@@ -169,4 +174,4 @@ function depgraph_from_network(rn, specmap, jset)
169174
end
170175

171176
dep_graph
172-
end
177+
end

src/problem.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,30 @@ function DiffEqJump.JumpProblem(prob,aggregator,rn::DiffEqBase.AbstractReactionN
1717
# get a JumpSet of the possible jumps
1818
jset = network_to_jumpset(rn, spec_to_idx, param_to_idx, prob.p)
1919

20-
# # construct dependency graph if needed by aggregator
20+
# construct map from species index to indices of reactions that depend on it
21+
if needs_vartojumps_map(aggregator) || needs_depgraph(aggregator)
22+
rxidxs_to_jidxs = rxidxs_to_jidxs_map(rn, get_num_majumps(jset))
23+
spec_to_jumps_set = spec_to_dep_jumps_map(rn, spec_to_idx, rxidxs_to_jidxs)
24+
spec_to_jumps_vec = [sort!(collect(specset)) for specset in spec_to_jumps_set]
25+
jump_to_specs_vec = jump_to_dep_specs_map(rn, spec_to_idx, rxidxs_to_jidxs)
26+
else
27+
rxidxs_to_jidxs = nothing
28+
spec_to_jumps_set = nothing
29+
spec_to_jumps_vec = nothing
30+
jump_to_specs_vec = nothing
31+
end
32+
33+
# construct reaction dependency graph
2134
if needs_depgraph(aggregator)
22-
dep_graph = depgraph_from_network(rn, spec_to_idx, jset)
35+
dep_graph = depgraph_from_network(rn, spec_to_idx, jset, rxidxs_to_jidxs, spec_to_jumps_set)
2336
else
2437
dep_graph = nothing
2538
end
2639

27-
JumpProblem(prob, aggregator, jset; dep_graph=dep_graph, kwargs...)
40+
JumpProblem(prob, aggregator, jset; dep_graph=dep_graph,
41+
vartojumps_map=spec_to_jumps_vec,
42+
jumptovars_map=jump_to_specs_vec,
43+
kwargs...)
2844
end
2945

3046
### SteadyStateProblem ###

0 commit comments

Comments
 (0)