utils.jl 4.16 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

"""
    initial_amounts(m::SBML.Model; convert_concentrations = false)

Return a vector of initial amounts for each species, or `nothing` if the
information is not available. If `convert_concentrations` is true and there is
information about initial concentration available together with compartment
size, the result is computed from the species' initial concentration.

In the current version, units of the measurements are completely ignored.
"""
function initial_amounts(
    m::SBML.Model;
    convert_concentrations = false,
)::Vector{Union{Float64,Nothing}}
    function get_ia(x::Species)
        if !isnothing(x.initial_amount)
            x.initial_amount[1]
        elseif !isnothing(x.initial_concentration) &&
               haskey(m.compartments, x.compartment) &&
               !isnothing(m.compartments[x.compartment].size)
            x.initial_concentration[1] * m.compartments[x.compartment].size
        else
            nothing
        end
    end

    if convert_concentrations
        get_ia.(values(m.species))
    else
        return broadcast(
            x -> isnothing(x.initial_amount) ? nothing : x.initial_amount[1],
            values(m.species),
        )
    end
end

"""
    initial_concentrations(m::SBML.Model; convert_amounts = false)

Return a vector of initial concentrations for each species, or `nothing` if
the information is not available. If `convert_amounts` is true and there is
information about initial amount available together with compartment size, the
result is computed from the species' initial amount.

In the current version, units of the measurements are completely ignored.
"""
function initial_concentrations(
    m::SBML.Model;
    convert_amounts = false,
)::Vector{Union{Float64,Nothing}}
    function get_ic(x::Species)
        if !isnothing(x.initial_concentration)
            x.initial_concentration[1]
        elseif !isnothing(x.initial_amount) &&
               haskey(m.compartments, x.compartment) &&
               !isnothing(m.compartments[x.compartment].size)
            x.initial_amount[1] / m.compartments[x.compartment].size
        else
            nothing
        end
    end

    if convert_amounts
        get_ic.(values(m.species))
    else
        broadcast(
            x -> isnothing(x.initial_concentration) ? nothing : x.initial_concentration[1],
            values(m.species),
        )
    end
end