Skip to content

Commit 5bb1d10

Browse files
committed
up
1 parent f9d01a3 commit 5bb1d10

File tree

3 files changed

+96
-34
lines changed

3 files changed

+96
-34
lines changed

ext/CatalystGraphMakieExtension/rn_graph_plot.jl

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ struct SRGraphWrap{T} <: AbstractGraph{T}
1212
rateedges::Vector{SimpleEdge}
1313
end
1414

15+
# Create the SimpleDiGraph corresponding to the species and reactions
16+
function SRGraphWrap(rn::ReactionSystem)
17+
srg = speciesreactiongraph(rn)
18+
rateedges = Vector{SimpleEdge}()
19+
sm = speciesmap(rn); specs = species(rn)
20+
21+
deps = Set()
22+
for (i, rx) in enumerate(reactions(rn))
23+
empty!(deps)
24+
get_variables!(deps, rx.rate, specs)
25+
if !isempty(deps)
26+
for spec in deps
27+
specidx = sm[spec]
28+
push!(rateedges, SimpleEdge(specidx, i + length(specs)))
29+
end
30+
end
31+
end
32+
SRGraphWrap(srg, rateedges)
33+
end
34+
1535
Base.eltype(g::SRGraphWrap) = eltype(g.g)
1636
Graphs.edgetype(g::SRGraphWrap) = edgetype(g.g)
1737
Graphs.has_edge(g::SRGraphWrap, s, d) = has_edge(g.g, s, d)
@@ -25,8 +45,7 @@ Graphs.is_directed(g::SRGraphWrap) = is_directed(g.g)
2545

2646
function Graphs.edges(g::SRGraphWrap)
2747
edgelist = vcat(collect(Graphs.edges(g.g)), g.rateedges)
28-
edgeorder = sortperm(edgelist)
29-
edgelist = edgelist[edgeorder]
48+
edgelist = sort!(edgelist)
3049
end
3150

3251
function gen_distances(g::SRGraphWrap; inc = 0.2)
@@ -114,26 +133,6 @@ function plot_speciesreaction_graph(rn::ReactionSystem; interactive = false)
114133
f
115134
end
116135

117-
# Create the SimpleDiGraph corresponding to the species and reactions
118-
function SRGraphWrap(rn::ReactionSystem)
119-
srg = speciesreactiongraph(rn)
120-
rateedges = Vector{SimpleEdge}()
121-
sm = speciesmap(rn); specs = species(rn)
122-
123-
deps = Set()
124-
for (i, rx) in enumerate(reactions(rn))
125-
empty!(deps)
126-
get_variables!(deps, rx.rate, specs)
127-
if !isempty(deps)
128-
for spec in deps
129-
specidx = sm[spec]
130-
push!(rateedges, SimpleEdge(specidx, i + length(specs)))
131-
end
132-
end
133-
end
134-
SRGraphWrap(srg, rateedges)
135-
end
136-
137136
"""
138137
ComplexGraph(rn::ReactionSystem; interactive=false)
139138

src/network_analysis.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -316,37 +316,37 @@ end
316316

317317

318318
"""
319-
speciesreactiongraph(rn::ReactionSystem)
319+
species_reaction_graph(rn::ReactionSystem)
320320
321321
Construct a directed simple graph where there are two types of nodes: species and reactions.
322-
An edge from a species to reaction indicates that it is a reactant, and an edge from a reaction
323-
to a species indicates that it is a product. By default, the species vertices are listed
324-
first, so the first n indices correspond to species nodes.
322+
An edge from a species *s* to reaction *r* indicates that *s* is a reactant for *r*, and an edge from a reaction
323+
r to a species s indicates that *s* is a product of *r*. By default, the species vertices are listed
324+
first, so the first *n* indices correspond to species nodes.
325325
326326
For example,
327327
```julia
328328
sir = @reaction_network SIR begin
329329
β, S + I --> 2I
330330
ν, I --> R
331331
end
332-
speciesreactiongraph(sir)
332+
species_reaction_graph(sir)
333333
"""
334334
function species_reaction_graph(rn::ReactionSystem)
335335
specs = species(rn)
336336
rxs = reactions(rn)
337337
sm = speciesmap(rn)
338-
s = length(specs); r = length(rxs); nv = s + r
338+
s = length(specs)
339339

340-
adjmat = zeros(Int64, nv, nv)
340+
edgelist = Graphs.Edge[]
341341
for (i, rx) in enumerate(rxs)
342-
for (spec, stoich) in zip(rx.substrates, rx.substoich)
343-
adjmat[sm[spec], s+i] = stoich
342+
for spec in rx.substrates
343+
push!(edgelist, Graphs.Edge(sm[spec], s+i))
344344
end
345-
for (spec, stoich) in zip(rx.products, rx.prodstoich)
346-
adjmat[s+i, sm[spec]] = stoich
345+
for spec in rx.products
346+
push!(edgelist, Graphs.Edge(s+i, sm[spec]))
347347
end
348348
end
349-
srg = SimpleDiGraph(adjmat)
349+
srg = Graphs.SimpleDiGraphFromIterator(edgelist)
350350
end
351351

352352
### Linkage, Deficiency, Reversibility ###

test/extensions/graphmakie.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Catalyst, GraphMakie, GLMakie
2+
include("../test_networks.jl")
3+
# Test that speciesreactiongraph is generated correctly
4+
let
5+
brusselator = @reaction_network begin
6+
A, ∅ --> X
7+
1, 2X + Y --> 3X
8+
B, X --> Y
9+
1, X -->
10+
end
11+
12+
srg = Catalyst.species_reaction_graph(brusselator)
13+
s = length(species(brusselator))
14+
edgel = Edge.([(s+1, 1),
15+
(1, s+2),
16+
(2, s+2),
17+
(s+2, 1),
18+
(s+3, 2),
19+
(1, s+3),
20+
(1, s+4)])
21+
@test all((collect(Graphs.edges(srg))), edgel)
22+
23+
MAPK = @reaction_network MAPK begin
24+
(k₁, k₂),KKK + E1 <--> KKKE1
25+
k₃, KKKE1 --> KKK_ + E1
26+
(k₄, k₅), KKK_ + E2 <--> KKKE2
27+
k₆, KKKE2 --> KKK + E2
28+
(k₇, k₈), KK + KKK_ <--> KK_KKK_
29+
k₉, KK_KKK_ --> KKP + KKK_
30+
(k₁₀, k₁₁), KKP + KKK_ <--> KKPKKK_
31+
k₁₂, KKPKKK_ --> KKPP + KKK_
32+
(k₁₃, k₁₄), KKP + KKPase <--> KKPKKPase
33+
k₁₅, KKPPKKPase --> KKP + KKPase
34+
k₁₆,KKPKKPase --> KK + KKPase
35+
(k₁₇, k₁₈), KKPP + KKPase <--> KKPPKKPase
36+
(k₁₉, k₂₀), KKPP + K <--> KKPPK
37+
k₂₁, KKPPK --> KKPP + KP
38+
(k₂₂, k₂₃), KKPP + KP <--> KPKKPP
39+
k₂₄, KPKKPP --> KPP + KKPP
40+
(k₂₅, k₂₆), KP + KPase <--> KPKPase
41+
k₂₇, KKPPKPase --> KP + KPase
42+
k₂₈, KPKPase --> K + KPase
43+
(k₂₉, k₃₀), KPP + KPase <--> KKPPKPase
44+
end
45+
srg = Catalyst.species_reaction_graph(MAPK)
46+
@test nv(srg) == length(species(MAPK)) + length(reactions(MAPK))
47+
@test ne(srg) == 90
48+
end
49+
50+
# Test that rate edges are inferred correctly
51+
let
52+
rn = @reaction_network begin
53+
k, A --> B
54+
k * C, A --> C
55+
k * B, B --> C
56+
end
57+
srg = SRGraphWrap(rn)
58+
s = length(species(rn))
59+
@test Edge(3, s+2) srg.rateedges
60+
@test Edge(2, s+3) srg.rateedges
61+
# Since B is both a dep and a reactant
62+
@test count(==(Edge(2, s+3)), edges(srg)) == 2
63+
end

0 commit comments

Comments
 (0)