4
4
function filter_constspecs (specs, stoich:: AbstractVector{V} , smap) where {V <: Integer }
5
5
isempty (specs) && (return Vector {Int} (), Vector {V} ())
6
6
7
- # If any species are constant, go through these manually and add their indices and
8
- # stoichiometries to `ids` and `filtered_stoich`.
7
+ # if any species are constant, go through these manually and add their indices and
8
+ # stoichiometries to `ids` and `filtered_stoich`
9
9
if any (isconstant, specs)
10
10
ids = Vector {Int} ()
11
11
filtered_stoich = Vector {V} ()
@@ -46,15 +46,15 @@ function reactioncomplexmap(rn::ReactionSystem)
46
46
! isempty (nps. complextorxsmap) && return nps. complextorxsmap
47
47
complextorxsmap = nps. complextorxsmap
48
48
49
- # Retrieves system reactions and a map from species to their index in the species vector.
49
+ # retrieves system reactions and a map from species to their index in the species vector
50
50
rxs = reactions (rn)
51
51
smap = speciesmap (rn)
52
52
numreactions (rn) > 0 ||
53
53
error (" There must be at least one reaction to find reaction complexes." )
54
54
55
55
for (i, rx) in enumerate (rxs)
56
56
# Create the `ReactionComplex` corresponding to the reaction's substrates. Adds it
57
- # to the reaction complex dictionary (recording it as the substrates of the i'th reaction).
57
+ # to the reaction complex dictionary (recording it as the substrates of the i'th reaction).
58
58
subids, substoich = filter_constspecs (rx. substrates, rx. substoich, smap)
59
59
subrc = sort! (ReactionComplex (subids, substoich))
60
60
if haskey (complextorxsmap, subrc)
@@ -64,7 +64,7 @@ function reactioncomplexmap(rn::ReactionSystem)
64
64
end
65
65
66
66
# Create the `ReactionComplex` corresponding to the reaction's products. Adds it
67
- # to the reaction complex dictionary (recording it as the products of the i'th reaction).
67
+ # to the reaction complex dictionary (recording it as the products of the i'th reaction).
68
68
prodids, prodstoich = filter_constspecs (rx. products, rx. prodstoich, smap)
69
69
prodrc = sort! (ReactionComplex (prodids, prodstoich))
70
70
if haskey (complextorxsmap, prodrc)
@@ -103,7 +103,7 @@ function reactioncomplexes(rn::ReactionSystem; sparse = false)
103
103
error (" reactioncomplexes does not currently support subsystems." )
104
104
nps = get_networkproperties (rn)
105
105
106
- # If the complexes have not been cached, or the cached complexes uses a different sparsity.
106
+ # if the complexes have not been cached, or the cached complexes uses a different sparsity
107
107
if isempty (nps. complexes) || (sparse != issparse (nps. complexes))
108
108
# Computes the reaction complex dictionary. Use it to create a sparse/dense matrix.
109
109
complextorxsmap = reactioncomplexmap (rn)
@@ -116,11 +116,11 @@ function reactioncomplexes(rn::ReactionSystem; sparse = false)
116
116
nps. complexes, nps. incidencemat
117
117
end
118
118
119
- # Creates a *sparse* reaction complex matrix.
119
+ # creates a *sparse* reaction complex matrix
120
120
function reactioncomplexes (:: Type{SparseMatrixCSC{Int, Int}} , rn:: ReactionSystem ,
121
121
complextorxsmap)
122
- # Computes the I, J, and V vectors used for the sparse matrix (read about sparse matrix
123
- # representation for more information).
122
+ # computes the I, J, and V vectors used for the sparse matrix (read about sparse matrix
123
+ # representation for more information)
124
124
complexes = collect (keys (complextorxsmap))
125
125
Is = Int[]
126
126
Js = Int[]
@@ -136,7 +136,7 @@ function reactioncomplexes(::Type{SparseMatrixCSC{Int, Int}}, rn::ReactionSystem
136
136
complexes, B
137
137
end
138
138
139
- # Creates a *dense* reaction complex matrix.
139
+ # creates a *dense* reaction complex matrix
140
140
function reactioncomplexes (:: Type{Matrix{Int}} , rn:: ReactionSystem , complextorxsmap)
141
141
complexes = collect (keys (complextorxsmap))
142
142
B = zeros (Int, length (complexes), numreactions (rn))
@@ -174,8 +174,8 @@ function complexstoichmat(rn::ReactionSystem; sparse = false)
174
174
error (" complexstoichmat does not currently support subsystems." )
175
175
nps = get_networkproperties (rn)
176
176
177
- # If the complexes stoichiometry matrix has not been cached, or the cached one uses a
178
- # different sparsity, computes (and caches) it.
177
+ # if the complexes stoichiometry matrix has not been cached, or the cached one uses a
178
+ # different sparsity, computes (and caches) it
179
179
if isempty (nps. complexstoichmat) || (sparse != issparse (nps. complexstoichmat))
180
180
nps. complexstoichmat = if sparse
181
181
complexstoichmat (SparseMatrixCSC{Int, Int}, rn, keys (reactioncomplexmap (rn)))
@@ -186,10 +186,10 @@ function complexstoichmat(rn::ReactionSystem; sparse = false)
186
186
nps. complexstoichmat
187
187
end
188
188
189
- # Creates a *sparse* reaction complex stoichiometry matrix.
189
+ # creates a *sparse* reaction complex stoichiometry matrix
190
190
function complexstoichmat (:: Type{SparseMatrixCSC{Int, Int}} , rn:: ReactionSystem , rcs)
191
- # Computes the I, J, and V vectors used for the sparse matrix (read about sparse matrix
192
- # representation for more information).
191
+ # computes the I, J, and V vectors used for the sparse matrix (read about sparse matrix
192
+ # representation for more information)
193
193
Is = Int[]
194
194
Js = Int[]
195
195
Vs = Int[]
@@ -203,7 +203,7 @@ function complexstoichmat(::Type{SparseMatrixCSC{Int, Int}}, rn::ReactionSystem,
203
203
Z = sparse (Is, Js, Vs, numspecies (rn), length (rcs))
204
204
end
205
205
206
- # Creates a *dense* reaction complex stoichiometry matrix.
206
+ # creates a *dense* reaction complex stoichiometry matrix
207
207
function complexstoichmat (:: Type{Matrix{Int}} , rn:: ReactionSystem , rcs)
208
208
Z = zeros (Int, numspecies (rn), length (rcs))
209
209
for (i, rc) in enumerate (rcs)
@@ -236,8 +236,8 @@ function complexoutgoingmat(rn::ReactionSystem; sparse = false)
236
236
error (" complexoutgoingmat does not currently support subsystems." )
237
237
nps = get_networkproperties (rn)
238
238
239
- # If the outgoing complexes matrix has not been cached, or the cached one uses a
240
- # different sparsity, computes (and caches) it.
239
+ # if the outgoing complexes matrix has not been cached, or the cached one uses a
240
+ # different sparsity, computes (and caches) it
241
241
if isempty (nps. complexoutgoingmat) || (sparse != issparse (nps. complexoutgoingmat))
242
242
B = reactioncomplexes (rn, sparse = sparse)[2 ]
243
243
nps. complexoutgoingmat = if sparse
@@ -249,18 +249,18 @@ function complexoutgoingmat(rn::ReactionSystem; sparse = false)
249
249
nps. complexoutgoingmat
250
250
end
251
251
252
- # Creates a *sparse* outgoing reaction complex stoichiometry matrix.
252
+ # creates a *sparse* outgoing reaction complex stoichiometry matrix
253
253
function complexoutgoingmat (:: Type{SparseMatrixCSC{Int, Int}} , rn:: ReactionSystem , B)
254
- # Computes the I, J, and V vectors used for the sparse matrix (read about sparse matrix
255
- # representation for more information).
254
+ # computes the I, J, and V vectors used for the sparse matrix (read about sparse matrix
255
+ # representation for more information)
256
256
n = size (B, 2 )
257
257
rows = rowvals (B)
258
258
vals = nonzeros (B)
259
259
Is = Int[]
260
260
Js = Int[]
261
261
Vs = Int[]
262
262
263
- # Allocates space to the vectors (so that it is not done incrementally in the loop).
263
+ # allocates space to the vectors (so that it is not done incrementally in the loop)
264
264
sizehint! (Is, div (length (vals), 2 ))
265
265
sizehint! (Js, div (length (vals), 2 ))
266
266
sizehint! (Vs, div (length (vals), 2 ))
@@ -277,7 +277,7 @@ function complexoutgoingmat(::Type{SparseMatrixCSC{Int, Int}}, rn::ReactionSyste
277
277
sparse (Is, Js, Vs, size (B, 1 ), size (B, 2 ))
278
278
end
279
279
280
- # Creates a *dense* outgoing reaction complex stoichiometry matrix.
280
+ # creates a *dense* outgoing reaction complex stoichiometry matrix
281
281
function complexoutgoingmat (:: Type{Matrix{Int}} , rn:: ReactionSystem , B)
282
282
Δ = copy (B)
283
283
for (I, b) in pairs (Δ)
@@ -310,7 +310,7 @@ function incidencematgraph(rn::ReactionSystem)
310
310
nps. incidencegraph
311
311
end
312
312
313
- # Computes the incidence graph from an *dense* incidence matrix.
313
+ # computes the incidence graph from an *dense* incidence matrix
314
314
function incidencematgraph (incidencemat:: Matrix{Int} )
315
315
@assert all (∈ ([- 1 , 0 , 1 ]), incidencemat)
316
316
n = size (incidencemat, 1 ) # no. of nodes/complexes
@@ -331,7 +331,7 @@ function incidencematgraph(incidencemat::Matrix{Int})
331
331
return graph
332
332
end
333
333
334
- # Computes the incidence graph from an *sparse* incidence matrix.
334
+ # computes the incidence graph from an *sparse* incidence matrix
335
335
function incidencematgraph (incidencemat:: SparseMatrixCSC{Int, Int} )
336
336
@assert all (∈ ([- 1 , 0 , 1 ]), incidencemat)
337
337
m, n = size (incidencemat)
@@ -420,16 +420,18 @@ function terminallinkageclasses(rn::ReactionSystem)
420
420
nps. terminallinkageclasses
421
421
end
422
422
423
- # Helper function for terminallinkageclasses. Given a linkage class and a reaction network, say whether the linkage class is terminal,
424
- # i.e. all outgoing reactions from complexes in the linkage class produce a complex also in the linkage class
423
+ # Helper function for terminallinkageclasses. Given a linkage class and a reaction network, say
424
+ # whether the linkage class is terminal, i.e. all outgoing reactions from complexes in the linkage
425
+ # class produce a complex also in the linkage class
425
426
function isterminal (lc:: Vector , rn:: ReactionSystem )
426
427
imat = incidencemat (rn)
427
428
428
429
for r in 1 : size (imat, 2 )
429
430
# Find the index of the reactant complex for a given reaction
430
431
s = findfirst (== (- 1 ), @view imat[:, r])
431
432
432
- # If the reactant complex is in the linkage class, check whether the product complex is also in the linkage class. If any of them are not, return false.
433
+ # If the reactant complex is in the linkage class, check whether the product complex is
434
+ # also in the linkage class. If any of them are not, return false.
433
435
if s in Set (lc)
434
436
p = findfirst (== (1 ), @view imat[:, r])
435
437
p in Set (lc) ? continue : return false
@@ -697,7 +699,7 @@ conservation laws, each represented as a row in the output.
697
699
function conservationlaws (nsm:: T ; col_order = nothing ) where {T <: AbstractMatrix }
698
700
699
701
# compute the left nullspace over the integers
700
- # The `nullspace` function updates the `col_order`.
702
+ # the `nullspace` function updates the `col_order`
701
703
N = MT. nullspace (nsm' ; col_order)
702
704
703
705
# if all coefficients for a conservation law are negative, make positive
@@ -713,7 +715,7 @@ function conservationlaws(nsm::T; col_order = nothing) where {T <: AbstractMatri
713
715
T (N' )
714
716
end
715
717
716
- # Used in the subsequent function.
718
+ # used in the subsequent function
717
719
function cache_conservationlaw_eqs! (rn:: ReactionSystem , N:: AbstractMatrix , col_order)
718
720
# Retrieves nullity (the number of conservation laws). `r` is the rank of the netstoichmat.
719
721
nullity = size (N, 1 )
@@ -728,7 +730,7 @@ function cache_conservationlaw_eqs!(rn::ReactionSystem, N::AbstractMatrix, col_o
728
730
depidxs = col_order[(r + 1 ): end ]
729
731
depspecs = sps[depidxs]
730
732
731
- # Declares the conservation law parameters.
733
+ # declares the conservation law parameters
732
734
constants = MT. unwrap .(MT. scalarize (only (
733
735
@parameters $ (CONSERVED_CONSTANT_SYMBOL)[1 : nullity] [conserved = true ])))
734
736
@@ -738,20 +740,20 @@ function cache_conservationlaw_eqs!(rn::ReactionSystem, N::AbstractMatrix, col_o
738
740
conservedeqs = Equation[]
739
741
constantdefs = Equation[]
740
742
741
- # For each conserved quantity.
743
+ # for each conserved quantity
742
744
for (i, depidx) in enumerate (depidxs)
743
- # Finds the coefficient (in the conservation law) of the species that is eliminated
744
- # by this conservation law.
745
+ # finds the coefficient (in the conservation law) of the species that is eliminated
746
+ # by this conservation law
745
747
scaleby = (N[i, depidx] != 1 ) ? N[i, depidx] : one (eltype (N))
746
748
(scaleby != 0 ) || error (" Error, found a zero in the conservation law matrix where "
747
749
* " one was not expected." )
748
750
749
- # Creates , for this conservation law, the sum of all independent species (weighted by
750
- # the ratio between the coefficient of the species and the species which is elimianted.
751
+ # creates , for this conservation law, the sum of all independent species (weighted by
752
+ # the ratio between the coefficient of the species and the species which is elimianted
751
753
coefs = @view N[i, indepidxs]
752
754
terms = sum (coef / scaleby * sp for (coef, sp) in zip (coefs, indepspecs))
753
755
754
- # Computes the two equations corresponding to this conserved quantity.
756
+ # computes the two equations corresponding to this conserved quantity
755
757
eq = depspecs[i] ~ constants[i] - terms
756
758
push! (conservedeqs, eq)
757
759
eq = constants[i] ~ depspecs[i] + terms
@@ -784,7 +786,7 @@ function conservationlaws(rs::ReactionSystem)
784
786
nps = get_networkproperties (rs)
785
787
! isempty (nps. conservationmat) && (return nps. conservationmat)
786
788
787
- # If the conservation law matrix is not computed, do so and caches the result.
789
+ # if the conservation law matrix is not computed, do so and caches the result
788
790
nsm = netstoichmat (rs)
789
791
nps. conservationmat = conservationlaws (nsm; col_order = nps. col_order)
790
792
cache_conservationlaw_eqs! (rs, nps. conservationmat, nps. col_order)
0 commit comments