napistu 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. napistu/__init__.py +12 -0
  2. napistu/__main__.py +867 -0
  3. napistu/consensus.py +1557 -0
  4. napistu/constants.py +500 -0
  5. napistu/gcs/__init__.py +10 -0
  6. napistu/gcs/constants.py +69 -0
  7. napistu/gcs/downloads.py +180 -0
  8. napistu/identifiers.py +805 -0
  9. napistu/indices.py +227 -0
  10. napistu/ingestion/__init__.py +10 -0
  11. napistu/ingestion/bigg.py +146 -0
  12. napistu/ingestion/constants.py +296 -0
  13. napistu/ingestion/cpr_edgelist.py +106 -0
  14. napistu/ingestion/identifiers_etl.py +148 -0
  15. napistu/ingestion/obo.py +268 -0
  16. napistu/ingestion/psi_mi.py +276 -0
  17. napistu/ingestion/reactome.py +218 -0
  18. napistu/ingestion/sbml.py +621 -0
  19. napistu/ingestion/string.py +356 -0
  20. napistu/ingestion/trrust.py +285 -0
  21. napistu/ingestion/yeast.py +147 -0
  22. napistu/mechanism_matching.py +597 -0
  23. napistu/modify/__init__.py +10 -0
  24. napistu/modify/constants.py +86 -0
  25. napistu/modify/curation.py +628 -0
  26. napistu/modify/gaps.py +635 -0
  27. napistu/modify/pathwayannot.py +1381 -0
  28. napistu/modify/uncompartmentalize.py +264 -0
  29. napistu/network/__init__.py +10 -0
  30. napistu/network/constants.py +117 -0
  31. napistu/network/neighborhoods.py +1594 -0
  32. napistu/network/net_create.py +1647 -0
  33. napistu/network/net_utils.py +652 -0
  34. napistu/network/paths.py +500 -0
  35. napistu/network/precompute.py +221 -0
  36. napistu/rpy2/__init__.py +127 -0
  37. napistu/rpy2/callr.py +168 -0
  38. napistu/rpy2/constants.py +101 -0
  39. napistu/rpy2/netcontextr.py +464 -0
  40. napistu/rpy2/rids.py +697 -0
  41. napistu/sbml_dfs_core.py +2216 -0
  42. napistu/sbml_dfs_utils.py +304 -0
  43. napistu/source.py +394 -0
  44. napistu/utils.py +943 -0
  45. napistu-0.1.0.dist-info/METADATA +56 -0
  46. napistu-0.1.0.dist-info/RECORD +77 -0
  47. napistu-0.1.0.dist-info/WHEEL +5 -0
  48. napistu-0.1.0.dist-info/entry_points.txt +2 -0
  49. napistu-0.1.0.dist-info/licenses/LICENSE +21 -0
  50. napistu-0.1.0.dist-info/top_level.txt +2 -0
  51. tests/__init__.py +0 -0
  52. tests/conftest.py +83 -0
  53. tests/test_consensus.py +255 -0
  54. tests/test_constants.py +20 -0
  55. tests/test_curation.py +134 -0
  56. tests/test_data/__init__.py +0 -0
  57. tests/test_edgelist.py +20 -0
  58. tests/test_gcs.py +23 -0
  59. tests/test_identifiers.py +151 -0
  60. tests/test_igraph.py +353 -0
  61. tests/test_indices.py +88 -0
  62. tests/test_mechanism_matching.py +126 -0
  63. tests/test_net_utils.py +66 -0
  64. tests/test_netcontextr.py +105 -0
  65. tests/test_obo.py +34 -0
  66. tests/test_pathwayannot.py +95 -0
  67. tests/test_precomputed_distances.py +222 -0
  68. tests/test_rpy2.py +61 -0
  69. tests/test_sbml.py +46 -0
  70. tests/test_sbml_dfs_create.py +307 -0
  71. tests/test_sbml_dfs_utils.py +22 -0
  72. tests/test_sbo.py +11 -0
  73. tests/test_set_coverage.py +50 -0
  74. tests/test_source.py +67 -0
  75. tests/test_uncompartmentalize.py +40 -0
  76. tests/test_utils.py +487 -0
  77. tests/utils.py +30 -0
tests/test_rpy2.py ADDED
@@ -0,0 +1,61 @@
1
+ from __future__ import annotations
2
+
3
+ import sys
4
+ from importlib import reload
5
+ from unittest.mock import Mock
6
+ from unittest.mock import patch
7
+
8
+ import pytest
9
+
10
+
11
+ # Patch rpy2_arrow.arrow to avoid ImportError
12
+ # if the R env is not properly set up during testing
13
+ sys.modules["rpy2_arrow.arrow"] = Mock()
14
+ import napistu.rpy2.callr # noqa: E402
15
+ import napistu.rpy2.rids # noqa: E402
16
+
17
+
18
+ def test_rpy2_has_rpy2_false():
19
+ with patch.dict("sys.modules", {"rpy2": None}):
20
+ reload(napistu.rpy2)
21
+ assert napistu.rpy2.has_rpy2 is False
22
+ # Test if other napistu.rpy2 modules can be
23
+ # loaded without rpy2 installed
24
+ reload(napistu.rpy2.callr)
25
+ reload(napistu.rpy2.rids)
26
+
27
+
28
+ # def test_rpy2_has_rpy2_true():
29
+ # with patch.dict("sys.modules", {"rpy2": "pytest"}):
30
+ # reload(napistu.rpy2)
31
+ # assert napistu.rpy2.has_rpy2 is True
32
+
33
+
34
+ @patch("napistu.rpy2.has_rpy2", False)
35
+ def test_warn_if_no_rpy2_false():
36
+ @napistu.rpy2.warn_if_no_rpy2
37
+ def test_func():
38
+ pass
39
+
40
+ with pytest.raises(ImportError):
41
+ test_func()
42
+
43
+
44
+ @patch("napistu.rpy2.has_rpy2", True)
45
+ def test_warn_if_no_rpy2_true():
46
+ @napistu.rpy2.warn_if_no_rpy2
47
+ def test_func():
48
+ pass
49
+
50
+ test_func()
51
+
52
+
53
+ ################################################
54
+ # __main__
55
+ ################################################
56
+
57
+ if __name__ == "__main__":
58
+ test_rpy2_has_rpy2_false()
59
+ # test_rpy2_has_rpy2_true()
60
+ test_warn_if_no_rpy2_false()
61
+ test_warn_if_no_rpy2_true()
tests/test_sbml.py ADDED
@@ -0,0 +1,46 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+
5
+ import pandas as pd
6
+ from napistu import sbml_dfs_core
7
+ from napistu.ingestion import sbml
8
+
9
+
10
+ def test_sbml_dfs(sbml_path):
11
+ sbml_model = sbml.SBML(sbml_path)
12
+ _ = sbml_model.model
13
+
14
+ dfs = sbml_dfs_core.SBML_dfs(sbml_model)
15
+ dfs.validate()
16
+
17
+ assert type(dfs.get_cspecies_features()) is pd.DataFrame
18
+ assert type(dfs.get_species_features()) is pd.DataFrame
19
+ assert type(dfs.get_identifiers("species")) is pd.DataFrame
20
+
21
+
22
+ def test_adding_sbml_annotations(sbml_model):
23
+ annotations = pd.DataFrame(
24
+ [
25
+ {
26
+ "id": "compartment_12045",
27
+ "type": "compartment",
28
+ "uri": "http://identifiers.org/chebi/CHEBI:00000",
29
+ },
30
+ {
31
+ "id": "species_9033251",
32
+ "type": "species",
33
+ "uri": "http://identifiers.org/bigg.metabolite/fakemet",
34
+ },
35
+ {
36
+ "id": "species_9033251",
37
+ "type": "species",
38
+ "uri": "http://identifiers.org/chebi/CHEBI:00000",
39
+ },
40
+ ]
41
+ )
42
+
43
+ outpath = "/tmp/tmp_write_test.sbml"
44
+
45
+ sbml.add_sbml_annotations(sbml_model, annotations, save_path=outpath)
46
+ assert os.path.isfile(outpath) is True
@@ -0,0 +1,307 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+
5
+ import pandas as pd
6
+ import pytest
7
+ from napistu import sbml_dfs_core
8
+ from napistu.ingestion import sbml
9
+ from napistu.modify import pathwayannot
10
+
11
+
12
+ def test_drop_cofactors(sbml_dfs):
13
+ starting_rscs = sbml_dfs.reaction_species.shape[0]
14
+ reduced_dfs = pathwayannot.drop_cofactors(sbml_dfs)
15
+
16
+ assert starting_rscs - reduced_dfs.reaction_species.shape[0] == 20
17
+
18
+
19
+ def test_sbml_dfs_from_dict_required(sbml_dfs):
20
+ val_dict = {k: getattr(sbml_dfs, k) for k in sbml_dfs._required_entities}
21
+ sbml_dfs2 = sbml_dfs_core.SBML_dfs(val_dict)
22
+ sbml_dfs2.validate()
23
+
24
+ for k in sbml_dfs._required_entities:
25
+ assert getattr(sbml_dfs2, k).equals(getattr(sbml_dfs, k))
26
+
27
+
28
+ def test_sbml_dfs_species_data(sbml_dfs):
29
+ data = pd.DataFrame({"bla": [1, 2, 3]}, index=sbml_dfs.species.iloc[:3].index)
30
+ sbml_dfs.add_species_data("test", data)
31
+ sbml_dfs.validate()
32
+
33
+
34
+ def test_sbml_dfs_species_data_existing(sbml_dfs):
35
+ data = pd.DataFrame({"bla": [1, 2, 3]}, index=sbml_dfs.species.iloc[:3].index)
36
+ sbml_dfs.add_species_data("test", data)
37
+ with pytest.raises(ValueError):
38
+ sbml_dfs.add_species_data("test", data)
39
+
40
+
41
+ def test_sbml_dfs_species_data_validation(sbml_dfs):
42
+ data = pd.DataFrame({"bla": [1, 2, 3]})
43
+ sbml_dfs.species_data["test"] = data
44
+ with pytest.raises(ValueError):
45
+ sbml_dfs.validate()
46
+
47
+
48
+ def test_sbml_dfs_species_data_missing_idx(sbml_dfs):
49
+ data = pd.DataFrame({"bla": [1, 2, 3]})
50
+ with pytest.raises(ValueError):
51
+ sbml_dfs.add_species_data("test", data)
52
+
53
+
54
+ def test_sbml_dfs_species_data_duplicated_idx(sbml_dfs):
55
+ an_s_id = sbml_dfs.species.iloc[0].index[0]
56
+ dup_idx = pd.Series([an_s_id, an_s_id], name="s_id")
57
+ data = pd.DataFrame({"bla": [1, 2]}, index=dup_idx)
58
+
59
+ with pytest.raises(ValueError):
60
+ sbml_dfs.add_species_data("test", data)
61
+
62
+
63
+ def test_sbml_dfs_species_data_wrong_idx(sbml_dfs):
64
+ data = pd.DataFrame(
65
+ {"bla": [1, 2, 3]}, index=pd.Series(["bla1", "bla2", "bla3"], name="s_id")
66
+ )
67
+ with pytest.raises(ValueError):
68
+ sbml_dfs.add_species_data("test", data)
69
+
70
+
71
+ def test_sbml_dfs_reactions_data(sbml_dfs):
72
+ reactions_data = pd.DataFrame(
73
+ {"bla": [1, 2, 3]}, index=sbml_dfs.reactions.iloc[:3].index
74
+ )
75
+ sbml_dfs.add_reactions_data("test", reactions_data)
76
+ sbml_dfs.validate()
77
+
78
+
79
+ def test_sbml_dfs_reactions_data_existing(sbml_dfs):
80
+ reactions_data = pd.DataFrame(
81
+ {"bla": [1, 2, 3]}, index=sbml_dfs.reactions.iloc[:3].index
82
+ )
83
+ sbml_dfs.add_reactions_data("test", reactions_data)
84
+ with pytest.raises(ValueError):
85
+ sbml_dfs.add_reactions_data("test", reactions_data)
86
+
87
+
88
+ def test_sbml_dfs_reactions_data_validate(sbml_dfs):
89
+ data = pd.DataFrame({"bla": [1, 2, 3]})
90
+ sbml_dfs.reactions_data["test"] = data
91
+ with pytest.raises(ValueError):
92
+ sbml_dfs.validate()
93
+
94
+
95
+ def test_sbml_dfs_reactions_data_missing_idx(sbml_dfs):
96
+ data = pd.DataFrame({"bla": [1, 2, 3]})
97
+ with pytest.raises(ValueError):
98
+ sbml_dfs.add_reactions_data("test", data)
99
+
100
+
101
+ def test_sbml_dfs_reactions_data_duplicated_idx(sbml_dfs):
102
+ an_r_id = sbml_dfs.reactions.iloc[0].index[0]
103
+ dup_idx = pd.Series([an_r_id, an_r_id], name="r_id")
104
+ data = pd.DataFrame({"bla": [1, 2]}, index=dup_idx)
105
+ with pytest.raises(ValueError):
106
+ sbml_dfs.add_reactions_data("test", data)
107
+
108
+
109
+ def test_sbml_dfs_reactions_data_wrong_idx(sbml_dfs):
110
+ data = pd.DataFrame(
111
+ {"bla": [1, 2, 3]}, index=pd.Series(["bla1", "bla2", "bla3"], name="r_id")
112
+ )
113
+ with pytest.raises(ValueError):
114
+ sbml_dfs.add_reactions_data("test", data)
115
+
116
+
117
+ def test_sbml_dfs_remove_species_check_species(sbml_dfs):
118
+ s_id = [sbml_dfs.species.index[0]]
119
+ sbml_dfs._remove_species(s_id)
120
+ assert s_id[0] not in sbml_dfs.species.index
121
+ sbml_dfs.validate()
122
+
123
+
124
+ def test_sbml_dfs_remove_species_check_cspecies(sbml_dfs):
125
+ s_id = [sbml_dfs.compartmentalized_species["s_id"].iloc[0]]
126
+ sbml_dfs._remove_species(s_id)
127
+ assert s_id[0] not in sbml_dfs.compartmentalized_species.index
128
+ sbml_dfs.validate()
129
+
130
+
131
+ @pytest.fixture
132
+ def sbml_dfs_w_data(sbml_dfs):
133
+ sbml_dfs.add_species_data(
134
+ "test_species",
135
+ pd.DataFrame({"test1": [1, 2]}, index=sbml_dfs.species.index[:2]),
136
+ )
137
+ sbml_dfs.add_reactions_data(
138
+ "test_reactions",
139
+ pd.DataFrame({"test2": [1, 2, 3]}, index=sbml_dfs.reactions.index[:3]),
140
+ )
141
+ return sbml_dfs
142
+
143
+
144
+ def test_sbml_dfs_remove_species_check_data(sbml_dfs_w_data):
145
+ data = list(sbml_dfs_w_data.species_data.values())[0]
146
+ s_id = [data.index[0]]
147
+ sbml_dfs_w_data._remove_species(s_id)
148
+ data_2 = list(sbml_dfs_w_data.species_data.values())[0]
149
+ assert s_id[0] not in data_2.index
150
+ sbml_dfs_w_data.validate()
151
+
152
+
153
+ def test_sbml_dfs_remove_cspecies_check_cspecies(sbml_dfs):
154
+ s_id = [sbml_dfs.compartmentalized_species.index[0]]
155
+ sbml_dfs._remove_compartmentalized_species(s_id)
156
+ assert s_id[0] not in sbml_dfs.compartmentalized_species.index
157
+ sbml_dfs.validate()
158
+
159
+
160
+ def test_sbml_dfs_remove_cspecies_check_reaction_species(sbml_dfs):
161
+ sc_id = [sbml_dfs.reaction_species["sc_id"].iloc[0]]
162
+ sbml_dfs._remove_compartmentalized_species(sc_id)
163
+ assert sc_id[0] not in sbml_dfs.reaction_species["sc_id"]
164
+ sbml_dfs.validate()
165
+
166
+
167
+ def test_sbml_dfs_remove_reactions_check_reactions(sbml_dfs):
168
+ r_id = [sbml_dfs.reactions.index[0]]
169
+ sbml_dfs.remove_reactions(r_id)
170
+ assert r_id[0] not in sbml_dfs.reactions.index
171
+ sbml_dfs.validate()
172
+
173
+
174
+ def test_sbml_dfs_remove_reactions_check_reaction_species(sbml_dfs):
175
+ r_id = [sbml_dfs.reaction_species["r_id"].iloc[0]]
176
+ sbml_dfs.remove_reactions(r_id)
177
+ assert r_id[0] not in sbml_dfs.reaction_species["r_id"]
178
+ sbml_dfs.validate()
179
+
180
+
181
+ def test_sbml_dfs_remove_reactions_check_data(sbml_dfs_w_data):
182
+ data = list(sbml_dfs_w_data.reactions_data.values())[0]
183
+ r_id = [data.index[0]]
184
+ sbml_dfs_w_data.remove_reactions(r_id)
185
+ data_2 = list(sbml_dfs_w_data.reactions_data.values())[0]
186
+ assert r_id[0] not in data_2.index
187
+ sbml_dfs_w_data.validate()
188
+
189
+
190
+ def test_sbml_dfs_remove_reactions_check_species(sbml_dfs):
191
+ # find all r_ids for a species and check if
192
+ # removing all these reactions also removes the species
193
+ s_id = sbml_dfs.species.index[0]
194
+ dat = (
195
+ sbml_dfs.compartmentalized_species.query("s_id == @s_id").merge(
196
+ sbml_dfs.reaction_species, on="sc_id"
197
+ )
198
+ )[["r_id", "sc_id"]]
199
+ r_ids = dat["r_id"]
200
+ sc_ids = dat["sc_id"]
201
+ sbml_dfs.remove_reactions(r_ids, remove_species=True)
202
+ for sc_id in sc_ids:
203
+ assert sc_id not in sbml_dfs.compartmentalized_species.index
204
+ assert s_id not in sbml_dfs.species.index
205
+
206
+
207
+ def test_formula(sbml_dfs):
208
+ # create a formula string
209
+
210
+ an_r_id = sbml_dfs.reactions.index[0]
211
+
212
+ reaction_species_df = sbml_dfs.reaction_species[
213
+ sbml_dfs.reaction_species["r_id"] == an_r_id
214
+ ].merge(sbml_dfs.compartmentalized_species, left_on="sc_id", right_index=True)
215
+
216
+ formula_str = sbml_dfs_core.construct_formula_string(
217
+ reaction_species_df, sbml_dfs.reactions, name_var="sc_name"
218
+ )
219
+
220
+ assert isinstance(formula_str, str)
221
+ assert (
222
+ formula_str
223
+ == "CO2 [extracellular region] -> CO2 [cytosol] ---- modifiers: AQP1 tetramer [plasma membrane]]"
224
+ )
225
+
226
+
227
+ def test_read_sbml_with_invalid_ids():
228
+ SBML_W_BAD_IDS = "R-HSA-166658.sbml"
229
+ test_path = os.path.abspath(os.path.join(__file__, os.pardir))
230
+ sbml_w_bad_ids_path = os.path.join(test_path, "test_data", SBML_W_BAD_IDS)
231
+ assert os.path.isfile(sbml_w_bad_ids_path)
232
+
233
+ # invalid identifiers still create a valid sbml_dfs
234
+ sbml_w_bad_ids = sbml.SBML(sbml_w_bad_ids_path)
235
+ assert isinstance(sbml_dfs_core.SBML_dfs(sbml_w_bad_ids), sbml_dfs_core.SBML_dfs)
236
+
237
+
238
+ def test_stubbed_compartment():
239
+ compartment = sbml_dfs_core._stub_compartments()
240
+
241
+ assert compartment["c_Identifiers"][0].ids[0] == {
242
+ "ontology": "go",
243
+ "identifier": "GO:0005575",
244
+ "url": "https://www.ebi.ac.uk/QuickGO/term/GO:0005575",
245
+ "bqb": "BQB_IS",
246
+ }
247
+
248
+
249
+ def test_get_table(sbml_dfs):
250
+ assert isinstance(sbml_dfs.get_table("species"), pd.DataFrame)
251
+ assert isinstance(sbml_dfs.get_table("species", {"id"}), pd.DataFrame)
252
+
253
+ # invalid table
254
+ with pytest.raises(ValueError):
255
+ sbml_dfs.get_table("foo", {"id"})
256
+
257
+ # bad type
258
+ with pytest.raises(AssertionError):
259
+ sbml_dfs.get_table("reaction_species", "id")
260
+
261
+ # reaction species don't have ids
262
+ with pytest.raises(ValueError):
263
+ sbml_dfs.get_table("reaction_species", {"id"})
264
+
265
+
266
+ def test_search_by_name(sbml_dfs_metabolism):
267
+ assert sbml_dfs_metabolism.search_by_name("atp", "species", False).shape[0] == 1
268
+ assert sbml_dfs_metabolism.search_by_name("pyr", "species").shape[0] == 3
269
+ assert sbml_dfs_metabolism.search_by_name("kinase", "reactions").shape[0] == 4
270
+
271
+
272
+ def test_search_by_id(sbml_dfs_metabolism):
273
+ identifiers_tbl = sbml_dfs_metabolism.get_identifiers("species")
274
+ ids, species = sbml_dfs_metabolism.search_by_ids(
275
+ ["P40926"], "species", identifiers_tbl
276
+ )
277
+ assert ids.shape[0] == 1
278
+ assert species.shape[0] == 1
279
+
280
+ ids, species = sbml_dfs_metabolism.search_by_ids(
281
+ ["57540", "30744"], "species", identifiers_tbl, {"chebi"}
282
+ )
283
+ assert ids.shape[0] == 2
284
+ assert species.shape[0] == 2
285
+
286
+ ids, species = sbml_dfs_metabolism.search_by_ids(
287
+ ["baz"], "species", identifiers_tbl
288
+ )
289
+ assert ids.shape[0] == 0
290
+ assert species.shape[0] == 0
291
+
292
+
293
+ def test_species_status(sbml_dfs):
294
+
295
+ species = sbml_dfs.species
296
+ select_species = species[species["s_name"] == "OxyHbA"]
297
+ assert select_species.shape[0] == 1
298
+
299
+ status = sbml_dfs_core.species_status(select_species.index[0], sbml_dfs)
300
+ assert (
301
+ status["r_formula_str"][0]
302
+ == "4.0 H+ + OxyHbA + 4.0 CO2 -> 4.0 O2 + Protonated Carbamino DeoxyHbA [cytosol]"
303
+ )
304
+
305
+
306
+ # if __name__ == "__main__":
307
+ # test_get_table()
@@ -0,0 +1,22 @@
1
+ from __future__ import annotations
2
+
3
+ from napistu import sbml_dfs_utils
4
+
5
+
6
+ def test_id_formatter():
7
+ input_vals = range(50, 100)
8
+
9
+ # create standard IDs
10
+ ids = sbml_dfs_utils.id_formatter(input_vals, "s_id", id_len=8)
11
+ # invert standard IDs
12
+ inv_ids = sbml_dfs_utils.id_formatter_inv(ids)
13
+
14
+ assert list(input_vals) == inv_ids
15
+
16
+
17
+ ################################################
18
+ # __main__
19
+ ################################################
20
+
21
+ if __name__ == "__main__":
22
+ test_id_formatter()
tests/test_sbo.py ADDED
@@ -0,0 +1,11 @@
1
+ from __future__ import annotations
2
+
3
+ from napistu.constants import MINI_SBO_FROM_NAME
4
+ from napistu.constants import MINI_SBO_TO_NAME
5
+
6
+
7
+ def test_sbo_to_from_name():
8
+ assert len(MINI_SBO_TO_NAME) == len(MINI_SBO_FROM_NAME)
9
+
10
+ for k, v in MINI_SBO_FROM_NAME.items():
11
+ assert MINI_SBO_TO_NAME[v] == k
@@ -0,0 +1,50 @@
1
+ from __future__ import annotations
2
+
3
+ from napistu import source
4
+ from napistu.network import net_utils
5
+
6
+
7
+ def test_get_minimal_source_edges(sbml_dfs_metabolism):
8
+ vertices = sbml_dfs_metabolism.reactions.reset_index().rename(
9
+ columns={"r_id": "node"}
10
+ )
11
+
12
+ minimal_source_edges = net_utils.get_minimal_sources_edges(
13
+ vertices, sbml_dfs_metabolism
14
+ )
15
+ # print(minimal_source_edges.shape)
16
+ assert minimal_source_edges.shape == (87, 3)
17
+
18
+
19
+ def test_greedy_set_coverge_of_sources(sbml_dfs_metabolism):
20
+ table_schema = sbml_dfs_metabolism.schema["reactions"]
21
+
22
+ source_df = source.unnest_sources(
23
+ sbml_dfs_metabolism.reactions, source_var="r_Source"
24
+ )
25
+ # print(source_df.shape)
26
+ assert source_df.shape == (111, 7)
27
+
28
+ set_coverage = source.greedy_set_coverge_of_sources(source_df, table_schema)
29
+ # print(set_coverage.shape)
30
+ assert set_coverage.shape == (87, 6)
31
+
32
+
33
+ ################################################
34
+ # __main__
35
+ ################################################
36
+
37
+ if __name__ == "__main__":
38
+ import os
39
+ from napistu import indices
40
+ from napistu import consensus
41
+
42
+ test_path = os.path.abspath(os.path.join(__file__, os.pardir))
43
+ test_data = os.path.join(test_path, "test_data")
44
+
45
+ pw_index = indices.PWIndex(os.path.join(test_data, "pw_index_metabolism.tsv"))
46
+ sbml_dfs_dict = consensus.construct_sbml_dfs_dict(pw_index)
47
+ sbml_dfs_metabolism = consensus.construct_consensus_model(sbml_dfs_dict, pw_index)
48
+
49
+ test_get_minimal_source_edges(sbml_dfs_metabolism)
50
+ test_greedy_set_coverge_of_sources(sbml_dfs_metabolism)
tests/test_source.py ADDED
@@ -0,0 +1,67 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+
5
+ import pandas as pd
6
+ from napistu import indices
7
+ from napistu import source
8
+
9
+ test_path = os.path.abspath(os.path.join(__file__, os.pardir))
10
+ test_data = os.path.join(test_path, "test_data")
11
+
12
+
13
+ def test_source():
14
+ source_example_df = pd.DataFrame(
15
+ [
16
+ {"model": "fun", "id": "baz", "pathway_id": "fun"},
17
+ {"model": "fun", "id": "bot", "pathway_id": "fun"},
18
+ {"model": "time", "id": "boof", "pathway_id": "time"},
19
+ {"model": "time", "id": "bor", "pathway_id": "time"},
20
+ ]
21
+ )
22
+
23
+ source_obj = source.Source(source_example_df)
24
+ source_init = source.Source(init=True)
25
+
26
+ assert source.merge_sources([source_init, source_init]) == source_init
27
+
28
+ pd._testing.assert_frame_equal(
29
+ source.merge_sources([source_obj, source_init]).source, source_example_df
30
+ )
31
+
32
+ assert source.merge_sources([source_obj, source_obj]).source.shape[0] == 8
33
+
34
+ alt_source_df = pd.DataFrame(
35
+ [
36
+ {"model": "fun", "identifier": "baz", "pathway_id": "fun"},
37
+ {"model": "fun", "identifier": "baz", "pathway_id": "fun"},
38
+ ]
39
+ )
40
+ alt_source_obj = source.Source(alt_source_df)
41
+
42
+ assert source.merge_sources([source_obj, alt_source_obj]).source.shape == (6, 4)
43
+
44
+
45
+ def test_source_w_pwindex():
46
+ # pathway_id not provided since this and other attributes will be found
47
+ # in pw_index.tsv
48
+ source_example_df = pd.DataFrame(
49
+ [
50
+ {"model": "R-HSA-1237044", "id": "baz"},
51
+ {"model": "R-HSA-1237044", "id": "bot"},
52
+ ]
53
+ )
54
+
55
+ pw_index = indices.PWIndex(os.path.join(test_data, "pw_index.tsv"))
56
+
57
+ source_obj = source.Source(source_example_df, pw_index=pw_index)
58
+ assert source_obj.source.shape == (2, 8)
59
+
60
+
61
+ ################################################
62
+ # __main__
63
+ ################################################
64
+
65
+ if __name__ == "__main__":
66
+ test_source()
67
+ test_source_w_pwindex()
@@ -0,0 +1,40 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+
5
+ from napistu import sbml_dfs_core
6
+ from napistu.ingestion import sbml
7
+ from napistu.modify import uncompartmentalize
8
+
9
+ test_path = os.path.abspath(os.path.join(__file__, os.pardir))
10
+ sbml_path = os.path.join(test_path, "test_data", "reactome_glucose_metabolism.sbml")
11
+
12
+ if not os.path.isfile(sbml_path):
13
+ raise ValueError(f"{sbml_path} not found")
14
+
15
+
16
+ def test_uncompartmentalize():
17
+ sbml_model = sbml.SBML(sbml_path)
18
+ sbml_dfs = sbml_dfs_core.SBML_dfs(sbml_model)
19
+ sbml_dfs.validate()
20
+
21
+ assert sbml_dfs.compartmentalized_species.shape[0] == 107
22
+ assert sbml_dfs.reactions.shape[0] == 50
23
+ assert sbml_dfs.reaction_species.shape[0] == 250
24
+
25
+ uncomp_sbml_dfs = uncompartmentalize.uncompartmentalize_sbml_dfs(sbml_dfs)
26
+ uncomp_sbml_dfs.validate()
27
+
28
+ assert uncomp_sbml_dfs.compartments.shape[0] == 1
29
+ # assert uncomp_sbml_dfs.species.shape[0] == sbml_dfs.species.shape[0]
30
+ assert uncomp_sbml_dfs.compartmentalized_species.shape[0] == 80
31
+ assert uncomp_sbml_dfs.reactions.shape[0] == 47
32
+ assert uncomp_sbml_dfs.reaction_species.shape[0] == 217
33
+
34
+
35
+ ################################################
36
+ # __main__
37
+ ################################################
38
+
39
+ if __name__ == "__main__":
40
+ test_uncompartmentalize()