CoreModel.jl 5.82 KB
Newer Older
Sylvain Arreckx's avatar
Sylvain Arreckx committed
1
@testset "Change bounds" begin
2
    cp = test_LP()
3
    change_bounds!(cp, [3; 1], xl = [-10.0; -20], xu = [10.0; 20])
4
5
    @test cp.xl == [-20; 1; -10]
    @test cp.xu == [20; 1; 10]
6
    change_bounds!(
7
8
9
10
11
12
13
        cp,
        ["gibberish1"; "r3"; "r1"; "gibberish2"],
        xl = [0; -30.0; -40; 0],
        xu = [0; 30.0; 40; 0],
    )
    @test cp.xl == [-40; 1; -30]
    @test cp.xu == [40; 1; 30]
14
    change_bounds!(cp, ["r1"; "r3"], xl = [-50.0; -60])
15
    @test cp.xl == [-50; 1; -60]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
16
17
18
19
20
end

@testset "Verify consistency" begin
    cp = test_LP()
    @test size(cp.S) == (4, 3)
21
    (new_reactions, new_mets) = verify_consistency(
22
23
24
25
26
27
28
29
30
31
32
        cp,
        reshape(cp.S[:, end], :, 1),
        [1.0, 2.0, 3.0, 4.0],
        [2.0],
        [-1.0],
        [1.0],
        ["r4"],
        ["m1", "m2", "m3", "m6"],
        [1],
        [4],
    )
33
34
    @test new_reactions == [1]
    @test new_mets == [4]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
35

36
    (new_reactions, new_mets) = verify_consistency(
37
38
39
40
41
42
43
44
45
46
47
        cp,
        reshape(cp.S[:, end], :, 1),
        [1.0, 2.0, 3.0, 4.0],
        [2.0],
        [-1.0],
        [1.0],
        ["r1"],
        ["m1", "m2", "m3", "m6"],
        [],
        [4],
    )
48
49
    @test new_reactions == []
    @test new_mets == [4]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
50
51
52
53
54
end

@testset "Add reactions (checking existence and consistency)" begin
    cp = test_LP()
    @test size(cp.S) == (4, 3)
55
    (new_cp, new_reactions, new_mets) = add_reactions(
56
57
58
59
60
61
        cp,
        cp.S[:, end],
        [1.0, 2.0, 3.0, 4.0],
        2.0,
        -1.0,
        1.0,
62
        check_consistency = true,
63
    )
64
    @test n_reactions(cp) + 1 == n_reactions(new_cp)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
65

66
    (new_cp, new_reactions, new_mets) = add_reactions(
67
68
69
70
71
72
73
74
        cp,
        cp.S[:, end],
        [1.0, 2.0, 3.0, 4.0],
        2.0,
        -1.0,
        1.0,
        "r1",
        ["m1", "m2", "m3", "m6"],
75
        check_consistency = true,
76
    )
77
78
    @test n_reactions(cp) == n_reactions(new_cp)
    @test n_metabolites(cp) + 1 == n_metabolites(new_cp)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
79
80
81
82
83
end

@testset "Add reactions" begin
    cp = test_LP()
    @test size(cp.S) == (4, 3)
84
    cp = add_reactions(cp, 2.0 * ones(4), 3 .* ones(4), 2.0, -1.0, 1.0)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
85
    @test size(cp.S) == (8, 4)
86
    cp = add_reactions(cp, 2.0 * ones(4, 1), 3 .* ones(4), 2 .* ones(1), -ones(1), ones(1))
Sylvain Arreckx's avatar
Sylvain Arreckx committed
87
    @test size(cp.S) == (12, 5)
88
    cp = add_reactions(
89
90
91
92
93
94
95
        cp,
        2.0 * ones(4, 10),
        3 .* ones(4),
        2 .* ones(10),
        -ones(10),
        ones(10),
    )
Sylvain Arreckx's avatar
Sylvain Arreckx committed
96
97
98
99
    @test size(cp.S) == (16, 15)

    cp = test_sparseLP()
    @test size(cp.S) == (4000, 3000)
100
    cp = add_reactions(cp, 2.0 * sprand(4000, 0.5), 3 .* sprand(4000, 0.5), 2.0, -1.0, 1.0)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
101
    @test size(cp.S) == (8000, 3001)
102
    cp = add_reactions(
103
104
105
106
107
108
109
        cp,
        2.0 * sprand(4000, 1, 0.5),
        3 .* sprand(4000, 0.5),
        2 .* sprand(1, 0.5),
        -sprand(1, 0.5),
        sprand(1, 0.5),
    )
Sylvain Arreckx's avatar
Sylvain Arreckx committed
110
    @test size(cp.S) == (12000, 3002)
111
    cp = add_reactions(
112
113
114
115
116
117
118
        cp,
        2.0 * sprand(4000, 1000, 0.5),
        3 .* sprand(4000, 0.5),
        2 .* sprand(1000, 0.5),
        -sprand(1000, 0.5),
        sprand(1000, 0.5),
    )
Sylvain Arreckx's avatar
Sylvain Arreckx committed
119
120
121
122
    @test size(cp.S) == (16000, 4002)

    cp = test_sparseLP()
    @test size(cp.S) == (4000, 3000)
123
    cp = add_reactions(cp, 2.0 * ones(4000), 3 .* ones(4000), 2.0, -1.0, 1.0)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
124
    @test size(cp.S) == (8000, 3001)
125
    cp = add_reactions(
126
127
128
129
130
131
132
        cp,
        2.0 * ones(4000, 1),
        3 .* ones(4000),
        2 .* ones(1),
        -ones(1),
        ones(1),
    )
Sylvain Arreckx's avatar
Sylvain Arreckx committed
133
    @test size(cp.S) == (12000, 3002)
134
    cp = add_reactions(
135
136
137
138
139
140
141
        cp,
        2.0 * ones(4000, 1000),
        3 .* ones(4000),
        2 .* ones(1000),
        -ones(1000),
        ones(1000),
    )
Sylvain Arreckx's avatar
Sylvain Arreckx committed
142
143
144
145
    @test size(cp.S) == (16000, 4002)

    # proper subset of existing metabolites
    cp = test_LP()
146
147
    new_cp = add_reactions(cp, [-1.0], zeros(1), 1.0, 0.0, 1.0, "r4", ["m1"])
    @test n_reactions(cp) + 1 == n_reactions(new_cp)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
148

149
    @test_throws DimensionMismatch add_reactions(
150
151
152
153
154
155
156
        cp,
        2.0 * ones(4000, 1),
        3 .* ones(4000),
        2 .* ones(2),
        -ones(1),
        ones(1),
    )
Sylvain Arreckx's avatar
Sylvain Arreckx committed
157
158
159
160
161
end

@testset "Remove reactions" begin
    cp = test_LP()
    @test size(cp.S) == (4, 3)
162
    cp = remove_reactions(cp, 2)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
163
    @test size(cp.S) == (0, 2)
164
    cp = remove_reactions(cp, [2, 1])
Sylvain Arreckx's avatar
Sylvain Arreckx committed
165
166
167
168
    @test size(cp.S) == (0, 0)

    cp = test_LP()
    @test size(cp.S) == (4, 3)
169
    cp = remove_reactions(cp, "r0")
Sylvain Arreckx's avatar
Sylvain Arreckx committed
170
    @test size(cp.S) == (4, 3)
171
    cp = remove_reactions(cp, "r1")
Sylvain Arreckx's avatar
Sylvain Arreckx committed
172
    @test size(cp.S) == (0, 2)
173
    cp = remove_reactions(cp, ["r2"])
Sylvain Arreckx's avatar
Sylvain Arreckx committed
174
    @test size(cp.S) == (0, 1)
Taneli Pusa's avatar
Taneli Pusa committed
175
176
177

    lp = CoreModel(
        [1.0 1 1 0; 1 1 1 0; 1 1 1 0; 0 0 0 1],
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
178
179
180
181
        collect(1.0:4),
        collect(1.0:4),
        collect(1.0:4),
        collect(1.0:4),
Taneli Pusa's avatar
Taneli Pusa committed
182
183
184
185
186
187
188
189
190
191
192
193
        ["r1"; "r2"; "r3"; "r4"],
        ["m1"; "m2"; "m3"; "m4"],
    )

    modLp = remove_reactions(lp, [4; 1])
    @test stoichiometry(modLp) == stoichiometry(lp)[1:3, 2:3]
    @test balance(modLp) == balance(lp)[1:3]
    @test objective(modLp) == objective(lp)[2:3]
    @test bounds(modLp)[1] == bounds(lp)[1][2:3]
    @test bounds(modLp)[2] == bounds(lp)[2][2:3]
    @test reactions(modLp) == reactions(lp)[2:3]
    @test metabolites(modLp) == metabolites(lp)[1:3]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
194
end
St. Elmo's avatar
St. Elmo committed
195
196

@testset "Remove metabolites" begin
St. Elmo's avatar
St. Elmo committed
197
    model = load_model(CoreModel, model_paths["e_coli_core.json"])
St. Elmo's avatar
St. Elmo committed
198
199
200
201
202
203
204
205
206

    m1 = remove_metabolites(model, ["glc__D_e", "for_c"])
    m2 = remove_metabolites(model, "glc__D_e")
    m3 = remove_metabolites(model, indexin(["glc__D_e", "for_c"], metabolites(model)))
    m4 = remove_metabolites(model, first(indexin(["glc__D_e"], metabolites(model))))

    @test size(stoichiometry(m1)) == (70, 94)
    @test size(stoichiometry(m2)) == (71, 94)
    @test size(stoichiometry(m3)) == (70, 94)
cylon-x's avatar
cylon-x committed
207
    @test size(stoichiometry(m4)) == (71, 94)
St. Elmo's avatar
St. Elmo committed
208
209
210
211
212
    @test any(["glc__D_e", "for_c"] . Ref(metabolites(m1)))
    @test any(["glc__D_e"] . Ref(metabolites(m2)))
    @test any(["glc__D_e", "for_c"] . Ref(metabolites(m3)))
    @test any(["glc__D_e"] . Ref(metabolites(m4)))
end