utils.jl 1.78 KB
Newer Older
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
1
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
2
    function getS(m::Model; zeros=spzeros)::Tuple{Vector{String},Vector{String},AbstractMatrix{Float64}}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
3
4
5
6

Extract the vector of species (aka metabolite) identifiers, vector of reaction
identifiers, and the (dense) stoichiometry matrix from an existing `Model`.
Returns a tuple with these values.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
7
8
9
10
11
12
13
14
15
16

The matrix is sparse by default (initially constructed by
`SparseArrays.spzeros`). You can fill in a custom empty matrix constructed to
argument `zeros`; e.g. running with `zeros=zeros` will produce a dense matrix.
"""
function getS(
    m::Model;
    zeros = spzeros,
)::Tuple{Vector{String},Vector{String},AbstractMatrix{Float64}}
    rows = [k for k in keys(m.species)]
17
    cols = [k for k in keys(m.reactions)]
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
18
    rowsd = Dict(k => i for (i, k) in enumerate(rows))
19
    S = zeros(Float64, length(rows), length(cols))
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
20
21
22
    for col = 1:length(cols)
        stoi = m.reactions[cols[col]].stoichiometry
        S[getindex.(Ref(rowsd), keys(stoi)), col] .= values(stoi)
23
24
25
26
    end
    return rows, cols, S
end

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
27
28
29
30
31
32
33
"""
    function getLBs(m::Model)::Vector{Tuple{Float64,String}}

Extract a vector of lower bounds of reaction rates from the model. All bounds
are accompanied with the unit of the corresponding value (this behavior is
based on SBML specification).
"""
34
35
36
37
function getLBs(m::Model)::Vector{Tuple{Float64,String}}
    return broadcast(x -> x.lb, values(m.reactions))
end

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
38
39
40
41
42
"""
    function getUBs(m::Model)::Vector{Tuple{Float64,String}}

Likewise to `getLBs`, extract the upper bounds.
"""
43
44
45
46
function getUBs(m::Model)::Vector{Tuple{Float64,String}}
    return broadcast(x -> x.ub, values(m.reactions))
end

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
47
48
49
50
51
"""
    function getOCs(m::Model)::Vector{Float64}

Extract the vector of objective coefficients of each reaction.
"""
52
53
54
function getOCs(m::Model)::Vector{Float64}
    return broadcast(x -> x.oc, values(m.reactions))
end