CoreModelCoupled.jl 3.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

"""
    struct CoreModelCoupled <: MetabolicModel

The linear model with additional coupling constraints in the form
```
    cₗ ≤ C x ≤ cᵤ
```
"""
mutable struct CoreModelCoupled <: MetabolicModel
    lm::CoreModel
    C::SparseMat
    cl::SparseVec
    cu::SparseVec

    function CoreModelCoupled(
        lm::MetabolicModel,
        C::M,
        cl::V,
        cu::V,
    ) where {V<:VecType,M<:MatType}

        length(cu) == length(cl) ||
            throw(DimensionMismatch("`cl` and `cu` need to have the same size"))
        size(C) == (length(cu), n_reactions(lm)) ||
            throw(DimensionMismatch("wrong dimensions of `C`"))

        new(convert(CoreModel, lm), sparse(C), sparse(cl), sparse(cu))
    end
end

32
33
34
35
36
37
"""
    reactions(a::CoreModelCoupled)

Extract reactions from [`CoreModelCoupled`](@ref) (uses the internal
[`CoreModel`](@ref)).
"""
38
reactions(a::CoreModelCoupled) = reactions(a.lm)
39
40
41
42
43
44
45

"""
    metabolites(a::CoreModelCoupled)

Extract metabolites from [`CoreModelCoupled`](@ref) (uses the internal
[`CoreModel`](@ref)).
"""
46
metabolites(a::CoreModelCoupled) = metabolites(a.lm)
47
48
49
50
51
52
53

"""
    stoichiometry(a::CoreModelCoupled)

Extract stoichiometry from [`CoreModelCoupled`](@ref) (uses the internal
[`CoreModel`](@ref)).
"""
54
stoichiometry(a::CoreModelCoupled) = stoichiometry(a.lm)
55
56
57
58
59
60
61

"""
    bounds(a::CoreModelCoupled)

Extract bounds from [`CoreModelCoupled`](@ref) (uses the internal
[`CoreModel`](@ref)).
"""
62
bounds(a::CoreModelCoupled) = bounds(a.lm)
63
64
65
66
67
68
69

"""
    balance(a::CoreModelCoupled)

Extract balance from [`CoreModelCoupled`](@ref) (uses the internal
[`CoreModel`](@ref)).
"""
70
balance(a::CoreModelCoupled) = balance(a.lm)
71
72
73
74
75
76
77

"""
    objective(a::CoreModelCoupled)

Extract objective from [`CoreModelCoupled`](@ref) (uses the internal
[`CoreModel`](@ref)).
"""
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
objective(a::CoreModelCoupled) = objective(a.lm)

"""
    coupling(a::CoreModelCoupled)::SparseMat

Coupling constraint matrix for a `CoreModelCoupled`.
"""
function coupling(a::CoreModelCoupled)::SparseMat
    a.C
end

"""
    n_coupling_constraints(a::CoreModelCoupled)::Int

The number of coupling constraints in a `CoreModelCoupled`.
"""
function n_coupling_constraints(a::CoreModelCoupled)::Int
    return size(a.C, 1)
end

"""
    coupling_bounds(a::CoreModelCoupled)::Tuple{SparseVec,SparseVec}

Coupling bounds for a `CoreModelCoupled`.
"""
function coupling_bounds(a::CoreModelCoupled)::Tuple{SparseVec,SparseVec}
    (a.cl, a.cu)
end

107
"""
St. Elmo's avatar
St. Elmo committed
108
    reaction_stoichiometry(model::CoreModelCoupled, rxn_id::String)::Dict{String, Float64}
109
110
111
112

Return the reaction equation of reaction with id `rxn_id` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
113
function reaction_stoichiometry(m::CoreModelCoupled, rxn_id::String)::Dict{String,Float64}
St. Elmo's avatar
St. Elmo committed
114
    reaction_stoichiometry(m.lm, rxn_id)
115
116
end

St. Elmo's avatar
St. Elmo committed
117
"""
St. Elmo's avatar
St. Elmo committed
118
    reaction_stoichiometry(model::CoreModelCoupled, rxn_ind::Int)::Dict{String, Float64}
St. Elmo's avatar
St. Elmo committed
119
120
121
122

Return the reaction equation of reaction with id `rxn_ind` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
123
function reaction_stoichiometry(m::CoreModelCoupled, rxn_ind::Int)::Dict{String,Float64}
St. Elmo's avatar
St. Elmo committed
124
    reaction_stoichiometry(m.lm, rxn_ind)
St. Elmo's avatar
St. Elmo committed
125
126
end

127
"""
128
    Base.convert(::Type{CoreModelCoupled}, mm::MetabolicModel)
129
130
131

Make a `CoreModelCoupled` out of any compatible model type.
"""
132
133
134
function Base.convert(::Type{CoreModelCoupled}, mm::MetabolicModel)
    if typeof(mm) == CoreModelCoupled
        return mm
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
135
136
    end

137
138
    (cl, cu) = coupling_bounds(mm)
    CoreModelCoupled(convert(CoreModel, mm), coupling(mm), cl, cu)
139
end