Skip to content

Commit 4603532

Browse files
committed
up
1 parent d0400a2 commit 4603532

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

src/network_analysis.jl

+40-38
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
function filter_constspecs(specs, stoich::AbstractVector{V}, smap) where {V <: Integer}
55
isempty(specs) && (return Vector{Int}(), Vector{V}())
66

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`
99
if any(isconstant, specs)
1010
ids = Vector{Int}()
1111
filtered_stoich = Vector{V}()
@@ -46,15 +46,15 @@ function reactioncomplexmap(rn::ReactionSystem)
4646
!isempty(nps.complextorxsmap) && return nps.complextorxsmap
4747
complextorxsmap = nps.complextorxsmap
4848

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
5050
rxs = reactions(rn)
5151
smap = speciesmap(rn)
5252
numreactions(rn) > 0 ||
5353
error("There must be at least one reaction to find reaction complexes.")
5454

5555
for (i, rx) in enumerate(rxs)
5656
# 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).
5858
subids, substoich = filter_constspecs(rx.substrates, rx.substoich, smap)
5959
subrc = sort!(ReactionComplex(subids, substoich))
6060
if haskey(complextorxsmap, subrc)
@@ -64,7 +64,7 @@ function reactioncomplexmap(rn::ReactionSystem)
6464
end
6565

6666
# 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).
6868
prodids, prodstoich = filter_constspecs(rx.products, rx.prodstoich, smap)
6969
prodrc = sort!(ReactionComplex(prodids, prodstoich))
7070
if haskey(complextorxsmap, prodrc)
@@ -103,7 +103,7 @@ function reactioncomplexes(rn::ReactionSystem; sparse = false)
103103
error("reactioncomplexes does not currently support subsystems.")
104104
nps = get_networkproperties(rn)
105105

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
107107
if isempty(nps.complexes) || (sparse != issparse(nps.complexes))
108108
# Computes the reaction complex dictionary. Use it to create a sparse/dense matrix.
109109
complextorxsmap = reactioncomplexmap(rn)
@@ -116,11 +116,11 @@ function reactioncomplexes(rn::ReactionSystem; sparse = false)
116116
nps.complexes, nps.incidencemat
117117
end
118118

119-
# Creates a *sparse* reaction complex matrix.
119+
# creates a *sparse* reaction complex matrix
120120
function reactioncomplexes(::Type{SparseMatrixCSC{Int, Int}}, rn::ReactionSystem,
121121
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)
124124
complexes = collect(keys(complextorxsmap))
125125
Is = Int[]
126126
Js = Int[]
@@ -136,7 +136,7 @@ function reactioncomplexes(::Type{SparseMatrixCSC{Int, Int}}, rn::ReactionSystem
136136
complexes, B
137137
end
138138

139-
# Creates a *dense* reaction complex matrix.
139+
# creates a *dense* reaction complex matrix
140140
function reactioncomplexes(::Type{Matrix{Int}}, rn::ReactionSystem, complextorxsmap)
141141
complexes = collect(keys(complextorxsmap))
142142
B = zeros(Int, length(complexes), numreactions(rn))
@@ -174,8 +174,8 @@ function complexstoichmat(rn::ReactionSystem; sparse = false)
174174
error("complexstoichmat does not currently support subsystems.")
175175
nps = get_networkproperties(rn)
176176

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
179179
if isempty(nps.complexstoichmat) || (sparse != issparse(nps.complexstoichmat))
180180
nps.complexstoichmat = if sparse
181181
complexstoichmat(SparseMatrixCSC{Int, Int}, rn, keys(reactioncomplexmap(rn)))
@@ -186,10 +186,10 @@ function complexstoichmat(rn::ReactionSystem; sparse = false)
186186
nps.complexstoichmat
187187
end
188188

189-
# Creates a *sparse* reaction complex stoichiometry matrix.
189+
# creates a *sparse* reaction complex stoichiometry matrix
190190
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)
193193
Is = Int[]
194194
Js = Int[]
195195
Vs = Int[]
@@ -203,7 +203,7 @@ function complexstoichmat(::Type{SparseMatrixCSC{Int, Int}}, rn::ReactionSystem,
203203
Z = sparse(Is, Js, Vs, numspecies(rn), length(rcs))
204204
end
205205

206-
# Creates a *dense* reaction complex stoichiometry matrix.
206+
# creates a *dense* reaction complex stoichiometry matrix
207207
function complexstoichmat(::Type{Matrix{Int}}, rn::ReactionSystem, rcs)
208208
Z = zeros(Int, numspecies(rn), length(rcs))
209209
for (i, rc) in enumerate(rcs)
@@ -236,8 +236,8 @@ function complexoutgoingmat(rn::ReactionSystem; sparse = false)
236236
error("complexoutgoingmat does not currently support subsystems.")
237237
nps = get_networkproperties(rn)
238238

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
241241
if isempty(nps.complexoutgoingmat) || (sparse != issparse(nps.complexoutgoingmat))
242242
B = reactioncomplexes(rn, sparse = sparse)[2]
243243
nps.complexoutgoingmat = if sparse
@@ -249,18 +249,18 @@ function complexoutgoingmat(rn::ReactionSystem; sparse = false)
249249
nps.complexoutgoingmat
250250
end
251251

252-
# Creates a *sparse* outgoing reaction complex stoichiometry matrix.
252+
# creates a *sparse* outgoing reaction complex stoichiometry matrix
253253
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)
256256
n = size(B, 2)
257257
rows = rowvals(B)
258258
vals = nonzeros(B)
259259
Is = Int[]
260260
Js = Int[]
261261
Vs = Int[]
262262

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)
264264
sizehint!(Is, div(length(vals), 2))
265265
sizehint!(Js, div(length(vals), 2))
266266
sizehint!(Vs, div(length(vals), 2))
@@ -277,7 +277,7 @@ function complexoutgoingmat(::Type{SparseMatrixCSC{Int, Int}}, rn::ReactionSyste
277277
sparse(Is, Js, Vs, size(B, 1), size(B, 2))
278278
end
279279

280-
# Creates a *dense* outgoing reaction complex stoichiometry matrix.
280+
# creates a *dense* outgoing reaction complex stoichiometry matrix
281281
function complexoutgoingmat(::Type{Matrix{Int}}, rn::ReactionSystem, B)
282282
Δ = copy(B)
283283
for (I, b) in pairs(Δ)
@@ -310,7 +310,7 @@ function incidencematgraph(rn::ReactionSystem)
310310
nps.incidencegraph
311311
end
312312

313-
# Computes the incidence graph from an *dense* incidence matrix.
313+
# computes the incidence graph from an *dense* incidence matrix
314314
function incidencematgraph(incidencemat::Matrix{Int})
315315
@assert all(([-1, 0, 1]), incidencemat)
316316
n = size(incidencemat, 1) # no. of nodes/complexes
@@ -331,7 +331,7 @@ function incidencematgraph(incidencemat::Matrix{Int})
331331
return graph
332332
end
333333

334-
# Computes the incidence graph from an *sparse* incidence matrix.
334+
# computes the incidence graph from an *sparse* incidence matrix
335335
function incidencematgraph(incidencemat::SparseMatrixCSC{Int, Int})
336336
@assert all(([-1, 0, 1]), incidencemat)
337337
m, n = size(incidencemat)
@@ -420,16 +420,18 @@ function terminallinkageclasses(rn::ReactionSystem)
420420
nps.terminallinkageclasses
421421
end
422422

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
425426
function isterminal(lc::Vector, rn::ReactionSystem)
426427
imat = incidencemat(rn)
427428

428429
for r in 1:size(imat, 2)
429430
# Find the index of the reactant complex for a given reaction
430431
s = findfirst(==(-1), @view imat[:, r])
431432

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.
433435
if s in Set(lc)
434436
p = findfirst(==(1), @view imat[:, r])
435437
p in Set(lc) ? continue : return false
@@ -697,7 +699,7 @@ conservation laws, each represented as a row in the output.
697699
function conservationlaws(nsm::T; col_order = nothing) where {T <: AbstractMatrix}
698700

699701
# compute the left nullspace over the integers
700-
# The `nullspace` function updates the `col_order`.
702+
# the `nullspace` function updates the `col_order`
701703
N = MT.nullspace(nsm'; col_order)
702704

703705
# 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
713715
T(N')
714716
end
715717

716-
# Used in the subsequent function.
718+
# used in the subsequent function
717719
function cache_conservationlaw_eqs!(rn::ReactionSystem, N::AbstractMatrix, col_order)
718720
# Retrieves nullity (the number of conservation laws). `r` is the rank of the netstoichmat.
719721
nullity = size(N, 1)
@@ -728,7 +730,7 @@ function cache_conservationlaw_eqs!(rn::ReactionSystem, N::AbstractMatrix, col_o
728730
depidxs = col_order[(r + 1):end]
729731
depspecs = sps[depidxs]
730732

731-
# Declares the conservation law parameters.
733+
# declares the conservation law parameters
732734
constants = MT.unwrap.(MT.scalarize(only(
733735
@parameters $(CONSERVED_CONSTANT_SYMBOL)[1:nullity] [conserved = true])))
734736

@@ -738,20 +740,20 @@ function cache_conservationlaw_eqs!(rn::ReactionSystem, N::AbstractMatrix, col_o
738740
conservedeqs = Equation[]
739741
constantdefs = Equation[]
740742

741-
# For each conserved quantity.
743+
# for each conserved quantity
742744
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
745747
scaleby = (N[i, depidx] != 1) ? N[i, depidx] : one(eltype(N))
746748
(scaleby != 0) || error("Error, found a zero in the conservation law matrix where "
747749
* "one was not expected.")
748750

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
751753
coefs = @view N[i, indepidxs]
752754
terms = sum(coef / scaleby * sp for (coef, sp) in zip(coefs, indepspecs))
753755

754-
# Computes the two equations corresponding to this conserved quantity.
756+
# computes the two equations corresponding to this conserved quantity
755757
eq = depspecs[i] ~ constants[i] - terms
756758
push!(conservedeqs, eq)
757759
eq = constants[i] ~ depspecs[i] + terms
@@ -784,7 +786,7 @@ function conservationlaws(rs::ReactionSystem)
784786
nps = get_networkproperties(rs)
785787
!isempty(nps.conservationmat) && (return nps.conservationmat)
786788

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
788790
nsm = netstoichmat(rs)
789791
nps.conservationmat = conservationlaws(nsm; col_order = nps.col_order)
790792
cache_conservationlaw_eqs!(rs, nps.conservationmat, nps.col_order)

0 commit comments

Comments
 (0)