CoreModelCoupled.jl 9.3 KB
Newer Older
1
"""
2
3
4
5
6
7
8
9
10
11
12
13
    add_reactions(
        m::CoreModelCoupled,
        s::V1,
        b::V2,
        c::AbstractFloat,
        xl::AbstractFloat,
        xu::AbstractFloat;
        check_consistency = false,
    ) where {V1<:VecType,V2<:VecType}

Add reaction(s) to a `CoreModelCoupled` model `m`.

14
15
16
17
18
19
20
21
22
23
"""
function add_reactions(
    m::CoreModelCoupled,
    s::V1,
    b::V2,
    c::AbstractFloat,
    xl::AbstractFloat,
    xu::AbstractFloat;
    check_consistency = false,
) where {V1<:VecType,V2<:VecType}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
24
    new_lm = add_reactions(m.lm, s, b, c, xl, xu, check_consistency = check_consistency)
25
26
    return CoreModelCoupled(
        new_lm,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
27
        hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm) - n_reactions(m.lm))),
28
        m.cl,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
29
        m.cu,
30
31
32
    )
end

33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
    add_reactions(
        m::CoreModelCoupled,
        s::V1,
        b::V2,
        c::AbstractFloat,
        xl::AbstractFloat,
        xu::AbstractFloat,
        rxn::String,
        mets::K;
        check_consistency = false,
    ) where {V1<:VecType,V2<:VecType,K<:StringVecType}

"""
47
48
49
50
51
52
53
54
55
56
57
58
function add_reactions(
    m::CoreModelCoupled,
    s::V1,
    b::V2,
    c::AbstractFloat,
    xl::AbstractFloat,
    xu::AbstractFloat,
    rxn::String,
    mets::K;
    check_consistency = false,
) where {V1<:VecType,V2<:VecType,K<:StringVecType}
    new_lm = add_reactions(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
59
60
61
62
63
64
65
66
67
68
        m.lm,
        s,
        b,
        c,
        xl,
        xu,
        rxn,
        mets,
        check_consistency = check_consistency,
    )
69
70
    return CoreModelCoupled(
        new_lm,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
71
        hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm) - n_reactions(m.lm))),
72
        m.cl,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
73
        m.cu,
74
75
76
    )
end

77
78
79
80
81
82
83
84
85
86
87
88
"""
    add_reactions(
        m::CoreModelCoupled,
        Sp::M,
        b::V,
        c::V,
        xl::V,
        xu::V;
        check_consistency = false,
    ) where {M<:MatType,V<:VecType}

"""
89
90
91
92
93
94
95
96
97
function add_reactions(
    m::CoreModelCoupled,
    Sp::M,
    b::V,
    c::V,
    xl::V,
    xu::V;
    check_consistency = false,
) where {M<:MatType,V<:VecType}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
98
    new_lm = add_reactions(m.lm, Sp, b, c, xl, xu, check_consistency = check_consistency)
99
100
    return CoreModelCoupled(
        new_lm,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
101
        hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm) - n_reactions(m.lm))),
102
        m.cl,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
103
        m.cu,
104
105
106
    )
end

107
108
109
110
111
112
"""
    add_reactions(m1::CoreModelCoupled, m2::CoreModel; check_consistency = false)

Add all reactions from `m2` to `m1`.

"""
113
function add_reactions(m1::CoreModelCoupled, m2::CoreModel; check_consistency = false)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
114
    new_lm = add_reactions(m1.lm, m2, check_consistency = check_consistency)
115
116
    return CoreModelCoupled(
        new_lm,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
117
        hcat(m1.C, spzeros(size(m1.C, 1), n_reactions(new_lm) - n_reactions(m1.lm))),
118
        m1.cl,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
119
        m1.cu,
120
121
122
    )
end

123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
    add_reactions(
        m::CoreModelCoupled,
        Sp::M,
        b::V,
        c::V,
        xl::V,
        xu::V,
        rxns::K,
        mets::K;
        check_consistency = false,
    ) where {M<:MatType,V<:VecType,K<:StringVecType}

"""
137
138
139
140
141
142
143
144
145
146
147
148
function add_reactions(
    m::CoreModelCoupled,
    Sp::M,
    b::V,
    c::V,
    xl::V,
    xu::V,
    rxns::K,
    mets::K;
    check_consistency = false,
) where {M<:MatType,V<:VecType,K<:StringVecType}
    new_lm = add_reactions(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
149
150
151
152
153
154
155
156
157
158
        m.lm,
        Sp,
        b,
        c,
        xl,
        xu,
        rxns,
        mets,
        check_consistency = check_consistency,
    )
159
160
    return CoreModelCoupled(
        new_lm,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
161
        hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm) - n_reactions(m.lm))),
162
        m.cl,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
163
        m.cu,
164
165
166
    )
end

167
"""
168
169
170
171
172
    remove_reactions(m::CoreModelCoupled, rxns::Vector{Int})

Remove reaction(s) from a `CoreModelCoupled`.

Also removes any metabolites not involved in any reaction after the deletion.
173
174
175
176

"""
function remove_reactions(m::CoreModelCoupled, rxns::Vector{Int})
    return CoreModelCoupled(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
177
178
179
180
181
        remove_reactions(m.lm, rxns),
        m.C[:, filter(e -> e  rxns, 1:n_reactions(m))],
        m.cl,
        m.cu,
    )
182
183
end

184
185
186
187
"""
    remove_reactions(m::CoreModelCoupled, rxn::Integer)

"""
188
189
190
191
function remove_reactions(m::CoreModelCoupled, rxn::Integer)
    return remove_reactions(m, [rxn])
end

192
193
194
195
"""
    remove_reactions(m::CoreModelCoupled, rxn::String)

"""
196
197
198
199
function remove_reactions(m::CoreModelCoupled, rxn::String)
    return remove_reactions(m, [rxn])
end

200
201
202
203
"""
    remove_reactions(m::CoreModelCoupled, rxns::Vector{String})

"""
204
function remove_reactions(m::CoreModelCoupled, rxns::Vector{String})
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
205
206
    rxn_indices =
        [findfirst(isequal(name), m.lm.rxns) for name in intersect(rxns, m.lm.rxns)]
207
208
209
210
211
212
    if isempty(rxn_indices)
        return m
    else
        return remove_reactions(m, rxn_indices)
    end
end
213

Sylvain Arreckx's avatar
Sylvain Arreckx committed
214
"""
215
Add constraints of the following form to a CoreModelCoupled and return a modified one.
Sylvain Arreckx's avatar
Sylvain Arreckx committed
216

217
218
219
Add constraints to a [`CoreModelCoupled`](@ref) and return a modified one.

The arguments are same as for in-place [`add_coupling_constraints!`](@ref).
220
"""
221
function add_coupling_constraints(m::CoreModelCoupled, args...)
222
223
224
    new_lp = deepcopy(m)
    add_coupling_constraints!(new_lp, args...)
    return new_lp
Sylvain Arreckx's avatar
Sylvain Arreckx committed
225
226
end

227
"""
228
229
230
231
    add_coupling_constraints(m::CoreModel, args...)

Add coupling constraints to a plain [`CoreModel`](@ref) (returns a
[`CoreModelCoupled`](@ref)).
232
"""
233
add_coupling_constraints(m::CoreModel, args...) =
234
    add_coupling_constraints(convert(CoreModelCoupled, m), args...)
235

236
"""
237
238
239
240
241
242
243
244
    add_coupling_constraints!(
        m::CoreModelCoupled,
        c::VecType,
        cl::AbstractFloat,
        cu::AbstractFloat,
    )

Overload for adding a single coupling constraint.
245
"""
246
function add_coupling_constraints!(
247
    m::CoreModelCoupled,
248
    c::VecType,
249
250
    cl::AbstractFloat,
    cu::AbstractFloat,
251
)
252
    return add_coupling_constraints!(
253
254
255
256
257
        m,
        sparse(reshape(c, (1, length(c)))),
        sparse([cl]),
        sparse([cu]),
    )
Sylvain Arreckx's avatar
Sylvain Arreckx committed
258
259
260
end


261
262
263
264
265
266
267
268
269
270
271
272
273
"""
    add_coupling_constraints!(
        m::CoreModelCoupled,
        C::MatType,
        cl::V,
        cu::V,
    ) where {V<:VecType}

In-place add a single coupling constraint in form
```
    cₗ ≤ C x ≤ cᵤ
```
"""
274
function add_coupling_constraints!(
275
    m::CoreModelCoupled,
276
    C::MatType,
277
278
    cl::V,
    cu::V,
279
) where {V<:VecType}
Sylvain Arreckx's avatar
Sylvain Arreckx committed
280

281
282
    all([length(cu), length(cl)] .== size(C, 1)) ||
        throw(DimensionMismatch("mismatched numbers of constraints"))
283
    size(C, 2) == n_reactions(m) ||
284
        throw(DimensionMismatch("mismatched number of reactions"))
Sylvain Arreckx's avatar
Sylvain Arreckx committed
285

286
287
288
    m.C = vcat(m.C, sparse(C))
    m.cl = vcat(m.cl, sparse(cl))
    m.cu = vcat(m.cu, sparse(cu))
Sylvain Arreckx's avatar
Sylvain Arreckx committed
289
290
291
292
end


"""
293
    remove_coupling_constraints(m::CoreModelCoupled, args...)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
294

295
296
297
Remove coupling constraints from the linear model, and return the modified
model. Arguments are the same as for in-place version
[`remove_coupling_constraints!`](@ref).
298
"""
299
function remove_coupling_constraints(m::CoreModelCoupled, args...)
300
301
302
    new_model = deepcopy(m)
    remove_coupling_constraints!(new_model, args...)
    return new_model
Sylvain Arreckx's avatar
Sylvain Arreckx committed
303
304
305
end


306
"""
307
308
309
310
    remove_coupling_constraints!(m::CoreModelCoupled, constraint::Int)

Removes a single coupling constraints from a [`CoreModelCoupled`](@ref)
in-place.
311
"""
312
remove_coupling_constraints!(m::CoreModelCoupled, constraint::Int) =
313
    remove_coupling_constraints!(m, [constraint])
Sylvain Arreckx's avatar
Sylvain Arreckx committed
314
315


316
317
318
319
320
321
"""
    remove_coupling_constraints!(m::CoreModelCoupled, constraints::Vector{Int})

Removes a set of coupling constraints from a [`CoreModelCoupled`](@ref)
in-place.
"""
322
function remove_coupling_constraints!(m::CoreModelCoupled, constraints::Vector{Int})
323
324
325
326
    to_be_kept = filter(e -> e  constraints, 1:n_coupling_constraints(m))
    m.C = m.C[to_be_kept, :]
    m.cl = m.cl[to_be_kept]
    m.cu = m.cu[to_be_kept]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
327
328
329
330
end


"""
331
332
333
334
335
336
337
338
339
    change_coupling_bounds!(
        model::CoreModelCoupled,
        constraints::Vector{Int};
        cl::V = Float64[],
        cu::V = Float64[],
    ) where {V<:VecType}

Change the lower and/or upper bounds (`cl` and `cu`) for the given list of
coupling constraints.
Sylvain Arreckx's avatar
Sylvain Arreckx committed
340
"""
341
function change_coupling_bounds!(
342
    model::CoreModelCoupled,
343
    constraints::Vector{Int};
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
344
345
    cl::V = Float64[],
    cu::V = Float64[],
346
) where {V<:VecType}
347
348
    found = [index  1:n_coupling_constraints(model) for index in constraints]
    red_constraints = constraints[found]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
349

350
    length(red_constraints) == length(unique(red_constraints)) ||
351
        error("`constraints` appears to contain duplicates")
Sylvain Arreckx's avatar
Sylvain Arreckx committed
352
    if !isempty(cl)
353
354
        length(constraints) == length(cl) ||
            throw(DimensionMismatch("`constraints` size doesn't match with `cl`"))
355
        model.cl[red_constraints] = cl[found]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
356
357
358
    end

    if !isempty(cu)
359
360
        length(constraints) == length(cu) ||
            throw(DimensionMismatch("`constraints` size doesn't match with `cu`"))
361
        model.cu[red_constraints] = cu[found]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
362
363
    end
end
364

365
"""
366
367
368
369
370
371
372
373
374
    change_bounds!(
        model::CoreModelCoupled,
        rxns::Vector{Int};
        xl::V = Float64[],
        xu::V = Float64[],
    )

Change the lower and/or upper bounds ('xl' and 'xu') for given reactions.

375
376
377
378
379
380
381
"""
function change_bounds!(
    model::CoreModelCoupled,
    rxns::Vector{Int};
    xl::V = Float64[],
    xu::V = Float64[],
) where {V<:VecType}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
382
    change_bounds!(model.lm, rxns, xl = xl, xu = xu)
383
384
end

385
386
387
388
389
390
391
392
393
"""
    change_bounds!(
        model::CoreModelCoupled,
        rxns::Vector{String};
        xl::V = Float64[],
        xu::V = Float64[],
    ) where {V<:VecType}

"""
394
395
396
397
398
399
function change_bounds!(
    model::CoreModelCoupled,
    rxns::Vector{String};
    xl::V = Float64[],
    xu::V = Float64[],
) where {V<:VecType}
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
400
    change_bounds!(model.lm, rxns, xl = xl, xu = xu)
401
end