Skip to content

Commit 20f8994

Browse files
committed
init
1 parent abf9b93 commit 20f8994

File tree

6 files changed

+297
-9
lines changed

6 files changed

+297
-9
lines changed

src/network_analysis.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,8 @@ end
721721
iscomplexbalanced(rs::ReactionSystem, parametermap)
722722
723723
Constructively compute whether a network will have complex-balanced equilibrium
724-
solutions, following the method in van der Schaft et al., [2015](https://link.springer.com/article/10.1007/s10910-015-0498-2#Sec3). Accepts a dictionary, vector, or tuple of variable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
724+
solutions, following the method in van der Schaft et al., [2015](https://link.springer.com/article/10.1007/s10910-015-0498-2#Sec3).
725+
Accepts a dictionary, vector, or tuple of variable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
725726
"""
726727

727728
function iscomplexbalanced(rs::ReactionSystem, parametermap::Dict)
@@ -779,12 +780,12 @@ function iscomplexbalanced(rs::ReactionSystem, parametermap::Dict)
779780
end
780781
end
781782

782-
function iscomplexbalanced(rs::ReactionSystem, parametermap::Vector{Pair{Symbol, Float64}})
783+
function iscomplexbalanced(rs::ReactionSystem, parametermap::Vector{<:Pair})
783784
pdict = Dict(parametermap)
784785
iscomplexbalanced(rs, pdict)
785786
end
786787

787-
function iscomplexbalanced(rs::ReactionSystem, parametermap::Tuple{Pair{Symbol, Float64}})
788+
function iscomplexbalanced(rs::ReactionSystem, parametermap::Tuple)
788789
pdict = Dict(parametermap)
789790
iscomplexbalanced(rs, pdict)
790791
end
@@ -796,7 +797,9 @@ end
796797
"""
797798
ratematrix(rs::ReactionSystem, parametermap)
798799
799-
Given a reaction system with n complexes, outputs an n-by-n matrix where R_{ij} is the rate constant of the reaction between complex i and complex j. Accepts a dictionary, vector, or tuple of variable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
800+
Given a reaction system with n complexes, outputs an n-by-n matrix where R_{ij} is the rate
801+
constant of the reaction between complex i and complex j. Accepts a dictionary, vector, or tuple
802+
of variable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
800803
"""
801804

802805
function ratematrix(rs::ReactionSystem, rates::Vector{Float64})
@@ -826,12 +829,12 @@ function ratematrix(rs::ReactionSystem, parametermap::Dict)
826829
ratematrix(rs, rates)
827830
end
828831

829-
function ratematrix(rs::ReactionSystem, parametermap::Vector{Pair{Symbol, Float64}})
832+
function ratematrix(rs::ReactionSystem, parametermap::Vector{<:Pair})
830833
pdict = Dict(parametermap)
831834
ratematrix(rs, pdict)
832835
end
833836

834-
function ratematrix(rs::ReactionSystem, parametermap::Tuple{Pair{Symbol, Float64}})
837+
function ratematrix(rs::ReactionSystem, parametermap::Tuple)
835838
pdict = Dict(parametermap)
836839
ratematrix(rs, pdict)
837840
end

test/miscellaneous_tests/stability_computation.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,13 @@ let
9696
u = [1.0]
9797
@test_throws Exception steady_state_stability(u, rn, p; tol = 1e2)
9898
end
99+
100+
# Test that stability computation for non-autonomous (t-dependent) systems throws error.
101+
let
102+
rn = @reaction_network begin
103+
(p + 1/(1+t),d), 0 <--> X
104+
end
105+
p = [:p => 1.0, :d => 1.0]
106+
u = [1.0]
107+
@test_throws Exception steady_state_stability(u, rn, p)
108+
end

test/network_analysis/network_properties.jl

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
### Prepares Tests ###
22

33
# Fetch packages.
4-
using Catalyst, LinearAlgebra, Test, StableRNGs
4+
using Catalyst, LinearAlgebra, Test, SparseArrays
55

6+
# Sets stable rng number.
7+
using StableRNGs
68
rng = StableRNG(514)
79

810
### Basic Tests ###
911

12+
# Tests basic `ReactionComplex` properties.
13+
let
14+
rcs1 = Catalyst.ReactionComplex([1, 2], [1, 3])
15+
rcs2 = Catalyst.ReactionComplex([1, 2], [1, 3])
16+
rcs3 = Catalyst.ReactionComplex([3], [2])
17+
@test rcs1 == rcs2
18+
@test rcs2 != rcs3
19+
@test length(rcs1) == 2
20+
@test length(rcs3) == 1
21+
end
22+
1023
# Tests network analysis functions on MAPK network (by comparing to manually computed outputs).
1124
let
1225
MAPK = @reaction_network MAPK begin
@@ -203,6 +216,7 @@ let
203216
rates = Dict(zip(parameters(rn), k))
204217
@test Catalyst.iscomplexbalanced(rn, rates) == false
205218
end
219+
206220
let
207221
rn = @reaction_network begin
208222
k1, A --> B
@@ -215,6 +229,7 @@ let
215229
rates = Dict(zip(parameters(rn), k))
216230
@test Catalyst.iscomplexbalanced(rn, rates) == false
217231
end
232+
218233
let
219234
rn = @reaction_network begin
220235
k1, A --> B
@@ -229,6 +244,7 @@ let
229244
rates = Dict(zip(parameters(rn), k))
230245
@test Catalyst.iscomplexbalanced(rn, rates) == false
231246
end
247+
232248
let
233249
rn = @reaction_network begin
234250
(k2, k1), A <--> 2B
@@ -244,6 +260,7 @@ let
244260
rates = Dict(zip(parameters(rn), k))
245261
@test Catalyst.iscomplexbalanced(rn, rates) == true
246262
end
263+
247264
let
248265
rn = @reaction_network begin
249266
(k2, k1), A + E <--> AE
@@ -257,6 +274,7 @@ let
257274
rates = Dict(zip(parameters(rn), k))
258275
@test Catalyst.iscomplexbalanced(rn, rates) == false
259276
end
277+
260278
let
261279
rn = @reaction_network begin
262280
(k2, k1), A + E <--> AE
@@ -270,6 +288,7 @@ let
270288
rates = Dict(zip(parameters(rn), k))
271289
@test Catalyst.iscomplexbalanced(rn, rates) == true
272290
end
291+
273292
let
274293
rn = @reaction_network begin (k2, k1), A + B <--> 2A end
275294
rev = true
@@ -280,6 +299,7 @@ let
280299
rates = Dict(zip(parameters(rn), k))
281300
@test Catalyst.iscomplexbalanced(rn, rates) == true
282301
end
302+
283303
let
284304
rn = @reaction_network begin
285305
k1, A + B --> 3A
@@ -295,6 +315,7 @@ let
295315
rates = Dict(zip(parameters(rn), k))
296316
@test Catalyst.iscomplexbalanced(rn, rates) == true
297317
end
318+
298319
let
299320
rn = @reaction_network begin
300321
(k2, k1), A + E <--> AE
@@ -326,3 +347,113 @@ let
326347
@test Catalyst.iscomplexbalanced(rn, rates) == true
327348
end
328349

350+
### Other Network Properties Tests ###
351+
352+
# Tests outgoing complexes matrices (1).
353+
# Checks using dense and sparse representation.
354+
let
355+
# Declares network.
356+
rs = @reaction_network begin
357+
k1, X1 + X2 --> X3 + X4
358+
(k2,k2), X3 + X4 <--> X1
359+
k3, X1 --> X2
360+
k4, X1 + X2 --> X2
361+
end
362+
363+
# Compares to manually computed matrix.
364+
cmplx_out_mat = [
365+
-1 0 0 0 -1;
366+
0 -1 0 0 0;
367+
0 0 -1 -1 0;
368+
0 0 0 0 0;
369+
]
370+
complexoutgoingmat(rs) == cmplx_out_mat
371+
complexoutgoingmat(rs; sparse = true) == sparse(cmplx_out_mat)
372+
end
373+
374+
# Tests outgoing complexes matrices (2).
375+
# Checks using dense and sparse representation.
376+
let
377+
# Declares network.
378+
rs = @reaction_network begin
379+
k1, X1 --> X2
380+
k2, X2 --> X3
381+
k3, X3 --> X4
382+
k4, X3 --> X5
383+
k5, X2 --> X1
384+
k6, X1 --> X2
385+
end
386+
387+
# Compares to manually computed matrix.
388+
cmplx_out_mat = [
389+
-1 0 0 0 0 -1;
390+
0 -1 0 0 -1 0;
391+
0 0 -1 -1 0 0;
392+
0 0 0 0 0 0;
393+
0 0 0 0 0 0;
394+
]
395+
complexoutgoingmat(rs)
396+
complexoutgoingmat(rs; sparse = true) == sparse(cmplx_out_mat)
397+
end
398+
399+
# Tests that `iscomplexbalanced` works for different rate inputs.
400+
# Tests that non-valid rate input yields and error
401+
let
402+
# Declares network.
403+
rn = @reaction_network begin
404+
k1, 3A + 2B --> 3C
405+
k2, B + 4D --> 2E
406+
k3, 2E --> 3C
407+
(k4, k5), B + 4D <--> 3A + 2B
408+
k6, F --> B + 4D
409+
k7, 3C --> F
410+
end
411+
412+
# Declares rate alternatives.
413+
k = rand(rng, numparams(rn))
414+
rates_vec = Pair.(parameters(rn), k)
415+
rates_tup = Tuple(rates_vec)
416+
rates_dict = Dict(rates_vec)
417+
rates_invalid = k
418+
419+
# Tests that inputs are handled correctly.
420+
@test Catalyst.iscomplexbalanced(rn, rates_vec) == Catalyst.iscomplexbalanced(rn, rates_tup)
421+
@test Catalyst.iscomplexbalanced(rn, rates_tup) == Catalyst.iscomplexbalanced(rn, rates_dict)
422+
@test_throws Exception Catalyst.iscomplexbalanced(rn, k)
423+
end
424+
425+
# Tests rate matrix computation for various input types.
426+
let
427+
# Declares network and its known rate matrix.
428+
rn = @reaction_network begin
429+
(k2, k1), A1 <--> A2 + A3
430+
k3, A2 + A3 --> A4
431+
k4, A4 --> A5
432+
(k6, k5), A5 <--> 2A6
433+
k7, 2A6 --> A4
434+
k8, A4 + A5 --> A7
435+
end
436+
rate_mat = [
437+
0.0 1.0 0.0 0.0 0.0 0.0 0.0;
438+
2.0 0.0 3.0 0.0 0.0 0.0 0.0;
439+
0.0 0.0 0.0 4.0 0.0 0.0 0.0;
440+
0.0 0.0 0.0 0.0 5.0 0.0 0.0;
441+
0.0 0.0 7.0 6.0 0.0 0.0 0.0;
442+
0.0 0.0 0.0 0.0 0.0 0.0 8.0;
443+
0.0 0.0 0.0 0.0 0.0 0.0 0.0;
444+
]
445+
446+
# Declares rate alternatives.
447+
rate_vals = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
448+
rates_vec = Pair.(parameters(rn), rate_vals)
449+
rates_tup = Tuple(rates_vec)
450+
rates_dict = Dict(rates_vec)
451+
rates_invalid = reshape(rate_vals, 1, 8)
452+
453+
# Tests that all input types generates the correct rate matrix.
454+
Catalyst.ratematrix(rn, rate_vals) == rate_mat
455+
Catalyst.ratematrix(rn, rates_vec) == rate_mat
456+
Catalyst.ratematrix(rn, rates_tup) == rate_mat
457+
Catalyst.ratematrix(rn, rates_dict) == rate_mat
458+
@test_throws Exception Catalyst.iscomplexbalanced(rn, rates_invalid)
459+
end

test/reactionsystem_core/reaction.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ end
124124
# Tests creation.
125125
# Tests basic accessor functions.
126126
# Tests that repeated metadata entries are not permitted.
127+
# Tests that attempting to access non-existent metadata throws an error.
127128
let
128129
@parameters k
129130
@species X(t) X2(t)
@@ -135,6 +136,7 @@ let
135136
@test Catalyst.hasmetadata(r, :noise_scaling)
136137
@test !Catalyst.hasmetadata(r, :nonexisting_metadata)
137138
@test Catalyst.getmetadata(r, :noise_scaling) == 0.0
139+
@test_throws Exception Catalyst.getmetadata(r, :misc)
138140

139141
metadata_repeated = [:noise_scaling => 0.0, :noise_scaling => 1.0, :metadata_entry => "unused"]
140142
@test_throws Exception Reaction(k, [X], [X2], [2], [1]; metadata=metadata_repeated)

0 commit comments

Comments
 (0)