@@ -13,17 +13,24 @@ function rate_to_indices(network)
1313 Dict ( rates[i] => i for i in eachindex (rates) )
1414end
1515
16- # given a ReactionStruct and a species map construct a MassActionJump
17- function make_majump (rs, specmap, ratemap, params )
16+ # get substrate stoichiometry for a reaction
17+ function get_substrate_stoich (rs, specmap)
1818 reactant_stoich = Vector {Pair{Int,Int}} ()
19- nsdict = Dict {Int,Int} ()
2019 for substrate in rs. substrates
2120 specpair = specmap[substrate. reactant] => substrate. stoichiometry
2221 push! (reactant_stoich, specpair)
22+ end
23+ sort! (reactant_stoich)
24+ reactant_stoich
25+ end
26+
27+ # get the net stoichiometry for a reaction
28+ function get_net_stoich (rs, specmap)
29+ nsdict = Dict {Int,Int} ()
30+ for substrate in rs. substrates
2331 specpair = specmap[substrate. reactant] => - substrate. stoichiometry
2432 push! (nsdict, specpair)
2533 end
26- sort! (reactant_stoich)
2734
2835 for product in rs. products
2936 prodidx = specmap[product. reactant]
@@ -36,9 +43,18 @@ function make_majump(rs, specmap, ratemap, params)
3643
3744 net_stoich = Vector {Pair{Int,Int}} ()
3845 for stoich_map in sort (collect (nsdict))
39- push! (net_stoich, stoich_map)
46+ if stoich_map[2 ] != zero (Int)
47+ push! (net_stoich, stoich_map)
48+ end
4049 end
4150
51+ net_stoich
52+ end
53+
54+ # given a ReactionStruct and a species map construct a MassActionJump
55+ function make_majump (rs, specmap, ratemap, params)
56+ reactant_stoich = get_substrate_stoich (rs, specmap)
57+ net_stoich = get_net_stoich (rs, specmap)
4258 if isempty (net_stoich)
4359 error (" Empty net stoichiometry vectors for mass action reactions are not allowed." )
4460 end
@@ -76,4 +92,81 @@ function network_to_jumpset(rn, specmap, ratemap, params)
7692 else
7793 return JumpSet ((), cjumpvec, nothing , majumpvec)
7894 end
95+ end
96+
97+
98+ # ######################## dependency graph utilities #########################
99+
100+ # map from a reaction index to the corresponding jump index
101+ # assumes all mass action jumps are ordered before constant rate jumps
102+ function rxidxs_to_jidxs_map (rn, num_majumps)
103+ majidx = 1
104+ cjidx = num_majumps + 1
105+ numrxs = length (rn. reactions)
106+ rxi_to_ji = zeros (Int, numrxs)
107+ for i = 1 : numrxs
108+ if rn. reactions[i]. is_pure_mass_action
109+ rxi_to_ji[i] = majidx
110+ majidx += 1
111+ else
112+ rxi_to_ji[i] = cjidx
113+ cjidx += 1
114+ end
115+ end
116+ rxi_to_ji
117+ end
118+
119+ # map from species to Set of jumps depending on that species
120+ function spec_to_dep_jumps_map (rn, specmap, rxidxs_to_jidxs)
121+
122+ numrxs = length (rn. reactions)
123+ numspec = length (specmap)
124+
125+ # map from a species to jumps that depend on it
126+ spec_to_dep_jumps = [Set {Int} () for n = 1 : numspec]
127+ for rx in 1 : numrxs
128+ for specsym in rn. reactions[rx]. dependants
129+ push! (spec_to_dep_jumps[specmap[specsym]], rxidxs_to_jidxs[rx])
130+ end
131+ end
132+
133+ spec_to_dep_jumps
134+ end
135+
136+ # 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)
148+
149+ # create map from a jump to jumps depending on it
150+ dep_sets = [SortedSet {Int} () for n = 1 : numrxs]
151+ for rx in 1 : numrxs
152+ jidx = rxidxs_to_jidxs[rx]
153+
154+ # get the net reaction stoichiometry
155+ net_stoich = get_net_stoich (rn. reactions[rx], specmap)
156+
157+ # rx changes spec, hence rxs depending on spec depend on rx
158+ for (spec,stoch) in net_stoich
159+ for dependent_jump in spec_to_dep_jumps[spec]
160+ push! (dep_sets[jidx], dependent_jump)
161+ end
162+ end
163+ end
164+
165+ # convert to Vectors of Vectors
166+ dep_graph = Vector {Vector{Int}} (numrxs)
167+ for jidx in 1 : numrxs
168+ dep_graph[jidx] = [dep for dep in dep_sets[jidx]]
169+ end
170+
171+ dep_graph
79172end
0 commit comments