-
Notifications
You must be signed in to change notification settings - Fork 211
/
test_mat.py
353 lines (307 loc) · 11.6 KB
/
test_mat.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""Test functionalities of I/O in MATLAB (.mat) format."""
from pathlib import Path
from pickle import load
from typing import TYPE_CHECKING, Callable
import pytest
from cobra.io import load_matlab_model, read_sbml_model, save_matlab_model
try:
import scipy
except ImportError:
scipy = None
if TYPE_CHECKING:
from cobra import Model
@pytest.fixture(scope="function")
def raven_model(data_directory: Path) -> "Model":
"""Fixture for RAVEN model."""
with open(data_directory / "raven.pickle", "rb") as infile:
return load(infile)
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
@pytest.mark.parametrize(
"fix_ref_model, fix_directory, filename",
[
("mini_model", "cobra_data_directory", "mini.mat"),
("raven_model", "data_directory", "raven.mat"),
],
)
def test_load_matlab_model(
compare_models: Callable,
fix_ref_model: str,
fix_directory: str,
filename: str,
request: pytest.FixtureRequest,
) -> None:
"""Test the reading of MAT model.
Will check Path and str for each model.
Parameters
----------
compare_models : Callable
A callable function to compare models.
fix_ref_model: str
Name of reference model fixture which will be requested.
fix_directory: str
Name of directory fixture which will be requested.
filename: str
Filename to use as a parameter
request: FixtureRequest
Will be used to request fixtures.
"""
current_model = load_matlab_model(
(request.getfixturevalue(fix_directory) / filename).resolve()
)
assert compare_models(request.getfixturevalue(fix_ref_model), current_model) is None
current_model = load_matlab_model(
str((request.getfixturevalue(fix_directory) / filename).resolve())
)
assert compare_models(request.getfixturevalue(fix_ref_model), current_model) is None
with request.getfixturevalue(fix_directory).joinpath(filename).open("rb") as file_h:
current_model = load_matlab_model(file_h)
assert (
compare_models(request.getfixturevalue(fix_ref_model), current_model)
is None
)
# @pytest.mark.xfail(reason="localPath not supported yet")
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
@pytest.mark.parametrize(
"fix_ref_model, filename",
[("mini_model", "mini.mat"), ("raven_model", "raven.mat")],
)
def test_save_matlab_model(tmp_path: Path, fix_ref_model, filename, request) -> None:
"""Test the writing of MAT model.
Parameters
----------
tmp_path : pathlib.Path
The path to the temporary test assets store.
fix_ref_model: str
Name of reference model fixture which will be requested.
filename: str
Filename to use as a parameter
request: FixtureRequest
Will be used to request fixtures.
"""
output_file = tmp_path / filename
save_matlab_model(
request.getfixturevalue(fix_ref_model), str(output_file.resolve())
)
assert output_file.exists()
output_file.unlink()
save_matlab_model(request.getfixturevalue(fix_ref_model), output_file.resolve())
assert output_file.exists()
output_file.unlink()
with output_file.open("wb") as file_h:
save_matlab_model(request.getfixturevalue(fix_ref_model), file_h)
assert output_file.exists()
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
def test_large_bounds(tmp_path: Path, model: "Model") -> None:
"""Verify that mat bounds don't get broken by the config defaults.
Parameters
----------
tmp_path : pathlib.Path
The path to the temporary test assets store.
model : cobra.Model
The "textbook" model.
"""
model.reactions[0].bounds = -1e6, 1e6
filepath = tmp_path / "model.mat"
save_matlab_model(model, filepath.resolve())
read = load_matlab_model(filepath.resolve())
assert read.reactions[0].bounds == (-1e6, 1e6)
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
@pytest.mark.parametrize(
"fix_directory, filename",
[("cobra_data_directory", "mini.mat"), ("data_directory", "raven.mat")],
)
def test_read_rewrite_matlab_model(
compare_models: Callable,
tmp_path: Path,
fix_directory: str,
filename: str,
request: pytest.FixtureRequest,
) -> None:
"""Verify that rewritten matlab model is identical to original.
Parameters
----------
compare_models : Callable
A callable to compare models.
tmp_path : pathlib.Path
The path to the temporary test assets store.
fix_directory: str
Name of directory fixture which will be requested.
filename: str
Filename to use as a parameter
request: FixtureRequest
Will be used to request fixtures.
"""
current_model = load_matlab_model(request.getfixturevalue(fix_directory) / filename)
output_file = tmp_path.joinpath(filename)
save_matlab_model(current_model, output_file)
mat_model_reload = load_matlab_model(output_file)
assert compare_models(current_model, mat_model_reload) is None
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
@pytest.mark.parametrize(
"dirname, xml_file",
[
("data_directory", "e_coli_core.xml"),
("cobra_data_directory", "salmonella.xml.gz"),
("cobra_data_directory", "mini_cobra.xml"),
("data_directory", "mini_fbc2.xml"),
],
)
# When using a better comparison function, can run test on
# "annotation.xml", "example_notes.xml", "fbc_ex1.xml", "fbc_ex2.xml", "validation.xml"
# "valid_annotation_output.xml" has reaction annotations in a metabolite, so they would
# be thrown out by matlab
def test_compare_xml_to_written_matlab_model(
compare_models: Callable,
tmp_path: Path,
dirname: str,
xml_file: str,
request: pytest.FixtureRequest,
) -> None:
"""Verify that xml rewritten as mat file is written and read correctly.
Parameters
----------
compare_models : Callable
A callable to compare models.
tmp_path : pathlib.Path
The path to the temporary test assets store.
xml_file : str
The name of the XML file to compare against.
"""
xml_model = read_sbml_model(request.getfixturevalue(dirname) / xml_file)
mat_output_file = tmp_path / xml_file.replace(".xml", ".mat")
save_matlab_model(xml_model, mat_output_file)
mat_model = load_matlab_model(mat_output_file)
assert compare_models(xml_model, mat_model) is None
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
def test_fail_on_problematic_compartments(data_directory: Path) -> None:
"""Test that mat import will fail if there are problems in compartments.
Parameters
----------
data_directory : pathlib.Path
The path to the test data directory.
"""
with pytest.raises(IOError):
# AntCore does not have defined compartments
load_matlab_model(data_directory / "AntCore.mat")
with pytest.raises(IOError):
# Ec_iAF1260_flux1 has underscore in compartment names which is not allowed
load_matlab_model(data_directory / "Ec_iAF1260_flux1.mat")
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
def test_mat_model_with_long_compartment_ids(
compare_models: Callable, data_directory: Path, tmp_path: Path
) -> None:
"""Test that long compartment IDs like "luSI" are correctly loaded.
Parameters
----------
compare_models : Callable
A callable to compare models.
data_directory : pathlib.Path
The path to the test data directory.
tmp_path : pathlib.Path
The path to the temporary test assets store.
"""
model_compartments = load_matlab_model(data_directory / "compartments.mat")
assert model_compartments.compartments == {
"csf": "csf",
"bcK": "bcK",
"a": "a",
"luSI": "luSI",
"luLI": "luLI",
"luP": "luP",
"aL": "aL",
"fe": "fe",
}
assert len(model_compartments.metabolites) == 8
assert len(model_compartments.reactions) == 15
for met in model_compartments.metabolites:
assert met.annotation == {
"bigg.metabolite": ["glc__D"],
"cas": ["50-99-7"],
"kegg.compound": ["C00031"],
"pubchem.substance": ["3333"],
}
output_file = tmp_path / "compartments.mat"
save_matlab_model(model_compartments, output_file.resolve())
model_compartments_reloaded = load_matlab_model(output_file.resolve())
assert compare_models(model_compartments, model_compartments_reloaded) is None
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
def test_mat_model_with_no_genes(
compare_models: Callable, data_directory: Path, tmp_path: Path
) -> None:
"""Test that a model with no genes is loaded and reloaded correctly.
Parameters
----------
compare_models : Callable
A callable to compare models.
data_directory : pathlib.Path
The path to the test data directory.
tmp_path : pathlib.Path
The path to the temporary test assets store.
"""
model_no_genes = load_matlab_model(
data_directory / "cardiac_mit_glcuptake_atpmax.mat"
)
assert not len(model_no_genes.genes)
output_file = tmp_path / "cardiac_mit_glcuptake_atpmax.mat"
save_matlab_model(model_no_genes, output_file.resolve())
model_no_genes_reloaded = load_matlab_model(output_file.resolve())
assert compare_models(model_no_genes, model_no_genes_reloaded) is None
@pytest.mark.skipif(scipy is None, reason="scipy unavailable")
def test_mat_model_wrong_caps(
compare_models: Callable, data_directory: Path, cobra_data_directory: Path
) -> None:
"""Check that wrong capitalization in matlab field names is processed correctly.
See https://gist.github.com/akaviaLab/3dcb0eed6563a9d3d1e07198337300ac to create it
again when needed.
Parameters
----------
compare_models : Callable
A callable to compare models.
data_directory : pathlib.Path
The path to the test data directory.
"""
mat_model = load_matlab_model((cobra_data_directory / "mini.mat").resolve())
mat_wrong_caps_model = load_matlab_model(
(data_directory / "mini_wrong_key_caps.mat").resolve()
)
assert compare_models(mat_model, mat_wrong_caps_model) is None
assert mat_wrong_caps_model.reactions.get_by_id("LDH_D").annotation == {
"rhea": ["16369", "16370", "16371", "16372"],
"metanetx.reaction": ["MNXR101037"],
"kegg.reaction": ["R00704"],
"bigg.reaction": ["LDH_D"],
"ec-code": ["1.1.1.28"],
"biocyc": ["META:DLACTDEHYDROGNAD-RXN"],
"sbo": ["SBO:0000375"],
}
for rxn in mat_model.reactions.list_attr("id"):
assert (
mat_wrong_caps_model.reactions.get_by_id(rxn).annotation
== mat_model.reactions.get_by_id(rxn).annotation
)
assert mat_wrong_caps_model.metabolites.get_by_id("pyr_c").annotation == {
"seed.compound": ["cpd00020"],
"unipathway.compound": ["UPC00022"],
"lipidmaps": ["LMFA01060077"],
"reactome": ["REACT_113557", "REACT_389680", "REACT_29398"],
"biocyc": ["PYRUVATE"],
"chebi": [
"CHEBI:15361",
"CHEBI:14987",
"CHEBI:8685",
"CHEBI:32816",
"CHEBI:45253",
"CHEBI:26466",
"CHEBI:26462",
],
"pubchem.substance": ["3324"],
"bigg.metabolite": ["pyr"],
"cas": ["127-17-3"],
"hmdb": ["HMDB00243"],
"kegg.compound": ["C00022"],
}
for met in mat_model.metabolites.list_attr("id"):
assert (
mat_wrong_caps_model.metabolites.get_by_id(met).annotation
== mat_model.metabolites.get_by_id(met).annotation
)