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

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

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(
anand jain's avatar
anand jain committed
13
    m::SBML.Model;
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
14
15
16
    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
"""
anand jain's avatar
anand jain committed
28
    function getLBs(m::SBML.Model)::Vector{Tuple{Float64,String}}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
29
30
31
32
33

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).
"""
anand jain's avatar
anand jain committed
34
function getLBs(m::SBML.Model)::Vector{Tuple{Float64,String}}
35
36
37
    return broadcast(x -> x.lb, values(m.reactions))
end

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

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

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

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