utils.jl 5.63 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
"""
28
    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).
"""
34
35
getLBs(m::SBML.Model)::Vector{Tuple{Float64,String}} =
    broadcast(x -> x.lb, values(m.reactions))
36

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
37
"""
38
    getUBs(m::SBML.Model)::Vector{Tuple{Float64,String}}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
39

40
Likewise to [`getLBs`](@ref), extract the upper bounds.
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
41
"""
42
43
getUBs(m::SBML.Model)::Vector{Tuple{Float64,String}} =
    broadcast(x -> x.ub, values(m.reactions))
44

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
45
"""
46
    getOCs(m::SBML.Model)::Vector{Float64}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
47
48
49

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

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

55
56
57
58
59
Return initial amounts for each species as a generator of pairs
`species_name => initial_amount`; the amount is set to `nothing` if 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.
60
61
62

In the current version, units of the measurements are completely ignored.

63
64
65
66
67
68
69
70
71
# Example
```
# get the initial amounts as dictionary
Dict(initial_amounts(model, convert_concentrations = true))

# remove the empty entries
Dict(k => v for (k,v) in initial_amounts(model) if !isnothing(v))
```
"""
72
initial_amounts(m::SBML.Model; convert_concentrations = false) = (
73
74
75
76
77
78
79
    k => if !isnothing(s.initial_amount)
        s.initial_amount[1]
    elseif convert_concentrations &&
           !isnothing(s.initial_concentration) &&
           haskey(m.compartments, s.compartment) &&
           !isnothing(m.compartments[s.compartment].size)
        s.initial_concentration[1] * m.compartments[s.compartment].size
80
    else
81
82
83
        nothing
    end for (k, s) in m.species
)
84
85
86
87

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

88
89
Return initial concentrations of the species in the model. Refer to work-alike
[`initial_amounts`](@ref) for details.
90
"""
91
initial_concentrations(m::SBML.Model; convert_amounts = false) = (
92
93
94
95
96
97
98
    k => if !isnothing(s.initial_concentration)
        s.initial_concentration[1]
    elseif convert_amounts &&
           !isnothing(s.initial_amount) &&
           haskey(m.compartments, s.compartment) &&
           !isnothing(m.compartments[s.compartment].size)
        s.initial_amount[1] / m.compartments[s.compartment].size
99
    else
100
101
102
        nothing
    end for (k, s) in m.species
)
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132


"""
    extensive_kinetic_math(m::SBML.Model, formula::SBML.Math)

Convert a SBML math `formula` to "extensive" kinetic laws, where the references
to species that are marked as not having only substance units are converted
from amounts to concentrations.

Handling of units in the conversion process is ignored in this version.
"""
function extensive_kinetic_math(m::SBML.Model, formula::SBML.Math)
    conv(x::SBML.MathIdent) = begin
        haskey(m.species, x.id) || return x
        sp = m.species[x.id]
        sp.only_substance_units && return x
        sz = m.compartments[sp.compartment].size
        isnothing(sz) && throw(
            DomainError(
                formula,
                "Non-substance-only-unit reference to species `$(x.id)' in an unsized compartment `$(sp.compartment)'.",
            ),
        )
        SBML.MathApply("/", [x, SBML.MathVal(sz)])
    end
    conv(x::SBML.MathApply) = SBML.MathApply(x.fn, conv.(x.args))
    conv(x::SBML.Math) = x

    conv(formula)
end
133
134
135
136
137
138
139
140
141
142

"""
    get_error_messages(doc::Ptr{Cvoid}, error::Exception)

Show the error messages reported by SBML in the `doc` document and throw the
`error` if they are more than 1.
"""
function get_error_messages(doc::VPtr, error::Exception)
    n_errs = ccall(sbml(:SBMLDocument_getNumErrors), Cuint, (VPtr,), doc)
    for i = 1:n_errs
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
143
        err = ccall(sbml(:SBMLDocument_getError), VPtr, (VPtr, Cuint), doc, i - 1)
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        msg = string(strip(get_string(err, :XMLError_getMessage)))
        @error "SBML reported error: $(msg)"
    end
    if n_errs > 0
        throw(error)
    end
    nothing
end

"""
    check_errors(success::Integer, doc::Ptr{Cvoid}, error::Exception)

If success is a 0-valued `Integer` (a logical `false`), then call
[`get_error_messages`](@ref) to show the error messages reported by SBML in the
`doc` document and throw the `error` if they are more than 1.  `success` is
typically the value returned by an SBML C function operating on `doc` which
returns a boolean flag to signal a successful operation.
"""
check_errors(success::Integer, doc::VPtr, error::Exception) =
    Bool(success) || get_error_messages(doc, error)