napistu 0.2.5.dev7__py3-none-any.whl → 0.3.1__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 (107) hide show
  1. napistu/__main__.py +126 -96
  2. napistu/constants.py +35 -41
  3. napistu/context/__init__.py +10 -0
  4. napistu/context/discretize.py +462 -0
  5. napistu/context/filtering.py +387 -0
  6. napistu/gcs/__init__.py +1 -1
  7. napistu/identifiers.py +74 -15
  8. napistu/indices.py +68 -0
  9. napistu/ingestion/__init__.py +1 -1
  10. napistu/ingestion/bigg.py +47 -62
  11. napistu/ingestion/constants.py +18 -133
  12. napistu/ingestion/gtex.py +113 -0
  13. napistu/ingestion/hpa.py +147 -0
  14. napistu/ingestion/sbml.py +0 -97
  15. napistu/ingestion/string.py +2 -2
  16. napistu/matching/__init__.py +10 -0
  17. napistu/matching/constants.py +18 -0
  18. napistu/matching/interactions.py +518 -0
  19. napistu/matching/mount.py +529 -0
  20. napistu/matching/species.py +510 -0
  21. napistu/mcp/__init__.py +7 -4
  22. napistu/mcp/__main__.py +128 -72
  23. napistu/mcp/client.py +16 -25
  24. napistu/mcp/codebase.py +201 -145
  25. napistu/mcp/component_base.py +170 -0
  26. napistu/mcp/config.py +223 -0
  27. napistu/mcp/constants.py +45 -2
  28. napistu/mcp/documentation.py +253 -136
  29. napistu/mcp/documentation_utils.py +13 -48
  30. napistu/mcp/execution.py +372 -305
  31. napistu/mcp/health.py +47 -65
  32. napistu/mcp/profiles.py +10 -6
  33. napistu/mcp/server.py +161 -80
  34. napistu/mcp/tutorials.py +139 -87
  35. napistu/modify/__init__.py +1 -1
  36. napistu/modify/gaps.py +1 -1
  37. napistu/network/__init__.py +1 -1
  38. napistu/network/constants.py +101 -34
  39. napistu/network/data_handling.py +388 -0
  40. napistu/network/ig_utils.py +351 -0
  41. napistu/network/napistu_graph_core.py +354 -0
  42. napistu/network/neighborhoods.py +40 -40
  43. napistu/network/net_create.py +373 -309
  44. napistu/network/net_propagation.py +47 -19
  45. napistu/network/{net_utils.py → ng_utils.py} +124 -272
  46. napistu/network/paths.py +67 -51
  47. napistu/network/precompute.py +11 -11
  48. napistu/ontologies/__init__.py +10 -0
  49. napistu/ontologies/constants.py +129 -0
  50. napistu/ontologies/dogma.py +243 -0
  51. napistu/ontologies/genodexito.py +649 -0
  52. napistu/ontologies/mygene.py +369 -0
  53. napistu/ontologies/renaming.py +198 -0
  54. napistu/rpy2/__init__.py +229 -86
  55. napistu/rpy2/callr.py +47 -77
  56. napistu/rpy2/constants.py +24 -23
  57. napistu/rpy2/rids.py +61 -648
  58. napistu/sbml_dfs_core.py +587 -222
  59. napistu/scverse/__init__.py +15 -0
  60. napistu/scverse/constants.py +28 -0
  61. napistu/scverse/loading.py +727 -0
  62. napistu/utils.py +118 -10
  63. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dist-info}/METADATA +8 -3
  64. napistu-0.3.1.dist-info/RECORD +133 -0
  65. tests/conftest.py +22 -0
  66. tests/test_context_discretize.py +56 -0
  67. tests/test_context_filtering.py +267 -0
  68. tests/test_identifiers.py +100 -0
  69. tests/test_indices.py +65 -0
  70. tests/{test_edgelist.py → test_ingestion_napistu_edgelist.py} +2 -2
  71. tests/test_matching_interactions.py +108 -0
  72. tests/test_matching_mount.py +305 -0
  73. tests/test_matching_species.py +394 -0
  74. tests/test_mcp_config.py +193 -0
  75. tests/test_mcp_documentation_utils.py +12 -3
  76. tests/test_mcp_server.py +156 -19
  77. tests/test_network_data_handling.py +397 -0
  78. tests/test_network_ig_utils.py +23 -0
  79. tests/test_network_neighborhoods.py +19 -0
  80. tests/test_network_net_create.py +459 -0
  81. tests/test_network_ng_utils.py +30 -0
  82. tests/test_network_paths.py +56 -0
  83. tests/{test_precomputed_distances.py → test_network_precompute.py} +8 -6
  84. tests/test_ontologies_genodexito.py +58 -0
  85. tests/test_ontologies_mygene.py +39 -0
  86. tests/test_ontologies_renaming.py +110 -0
  87. tests/test_rpy2_callr.py +79 -0
  88. tests/test_rpy2_init.py +151 -0
  89. tests/test_sbml.py +0 -31
  90. tests/test_sbml_dfs_core.py +134 -10
  91. tests/test_scverse_loading.py +778 -0
  92. tests/test_set_coverage.py +2 -2
  93. tests/test_utils.py +121 -1
  94. napistu/mechanism_matching.py +0 -1353
  95. napistu/rpy2/netcontextr.py +0 -467
  96. napistu-0.2.5.dev7.dist-info/RECORD +0 -98
  97. tests/test_igraph.py +0 -367
  98. tests/test_mechanism_matching.py +0 -784
  99. tests/test_net_utils.py +0 -149
  100. tests/test_netcontextr.py +0 -105
  101. tests/test_rpy2.py +0 -61
  102. /napistu/ingestion/{cpr_edgelist.py → napistu_edgelist.py} +0 -0
  103. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dist-info}/WHEEL +0 -0
  104. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dist-info}/entry_points.txt +0 -0
  105. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dist-info}/licenses/LICENSE +0 -0
  106. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dist-info}/top_level.txt +0 -0
  107. /tests/{test_obo.py → test_ingestion_obo.py} +0 -0
tests/test_igraph.py DELETED
@@ -1,367 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import os
4
- import pytest
5
-
6
- import pandas as pd
7
- from napistu import sbml_dfs_core
8
- from napistu.constants import DEFAULT_WT_TRANS
9
- from napistu.constants import MINI_SBO_FROM_NAME
10
- from napistu.ingestion import sbml
11
- from napistu.network import neighborhoods
12
- from napistu.network import net_create
13
- from napistu.network import net_utils
14
- from napistu.network import paths
15
-
16
- test_path = os.path.abspath(os.path.join(__file__, os.pardir))
17
- test_data = os.path.join(test_path, "test_data")
18
-
19
- sbml_path = os.path.join(test_data, "R-HSA-1237044.sbml")
20
- sbml_model = sbml.SBML(sbml_path).model
21
- sbml_dfs = sbml_dfs_core.SBML_dfs(sbml_model)
22
-
23
- # create a dict containing reaction species for a few example reactions
24
- reaction_species_examples_dict = dict()
25
-
26
- # stub with a random reaction
27
- r_id = sbml_dfs.reactions.index[0]
28
-
29
- reaction_species_examples_dict["valid_interactor"] = pd.DataFrame(
30
- {
31
- "r_id": [r_id, r_id],
32
- "sbo_term": [
33
- MINI_SBO_FROM_NAME["interactor"],
34
- MINI_SBO_FROM_NAME["interactor"],
35
- ],
36
- "sc_id": ["sc1", "sc2"],
37
- "stoichiometry": [0, 0],
38
- }
39
- ).set_index(["r_id", "sbo_term"])
40
-
41
- reaction_species_examples_dict["invalid_interactor"] = pd.DataFrame(
42
- {
43
- "r_id": [r_id, r_id],
44
- "sbo_term": [
45
- MINI_SBO_FROM_NAME["interactor"],
46
- MINI_SBO_FROM_NAME["product"],
47
- ],
48
- "sc_id": ["sc1", "sc2"],
49
- "stoichiometry": [0, 0],
50
- }
51
- ).set_index(["r_id", "sbo_term"])
52
-
53
-
54
- # simple reaction with just substrates and products
55
- reaction_species_examples_dict["sub_and_prod"] = pd.DataFrame(
56
- {
57
- "r_id": [r_id, r_id],
58
- "sbo_term": [MINI_SBO_FROM_NAME["reactant"], MINI_SBO_FROM_NAME["product"]],
59
- "sc_id": ["sub", "prod"],
60
- "stoichiometry": [-1, 1],
61
- }
62
- ).set_index(["r_id", "sbo_term"])
63
-
64
- reaction_species_examples_dict["stimulator"] = pd.DataFrame(
65
- {
66
- "r_id": [r_id, r_id, r_id],
67
- "sbo_term": [
68
- MINI_SBO_FROM_NAME["reactant"],
69
- MINI_SBO_FROM_NAME["product"],
70
- MINI_SBO_FROM_NAME["stimulator"],
71
- ],
72
- "sc_id": ["sub", "prod", "stim"],
73
- "stoichiometry": [-1, 1, 0],
74
- }
75
- ).set_index(["r_id", "sbo_term"])
76
-
77
- reaction_species_examples_dict["all_entities"] = pd.DataFrame(
78
- {
79
- "r_id": [r_id, r_id, r_id, r_id],
80
- "sbo_term": [
81
- MINI_SBO_FROM_NAME["reactant"],
82
- MINI_SBO_FROM_NAME["product"],
83
- MINI_SBO_FROM_NAME["stimulator"],
84
- MINI_SBO_FROM_NAME["catalyst"],
85
- ],
86
- "sc_id": ["sub", "prod", "stim", "cat"],
87
- "stoichiometry": [-1, 1, 0, 0],
88
- }
89
- ).set_index(["r_id", "sbo_term"])
90
-
91
- reaction_species_examples_dict["no_substrate"] = pd.DataFrame(
92
- {
93
- "r_id": [r_id, r_id, r_id, r_id, r_id],
94
- "sbo_term": [
95
- MINI_SBO_FROM_NAME["product"],
96
- MINI_SBO_FROM_NAME["stimulator"],
97
- MINI_SBO_FROM_NAME["stimulator"],
98
- MINI_SBO_FROM_NAME["inhibitor"],
99
- MINI_SBO_FROM_NAME["catalyst"],
100
- ],
101
- "sc_id": ["prod", "stim1", "stim2", "inh", "cat"],
102
- "stoichiometry": [1, 0, 0, 0, 0],
103
- }
104
- ).set_index(["r_id", "sbo_term"])
105
-
106
-
107
- def test_create_cpr_graph():
108
- _ = net_create.create_cpr_graph(sbml_dfs, graph_type="bipartite")
109
- _ = net_create.create_cpr_graph(sbml_dfs, graph_type="regulatory")
110
- _ = net_create.create_cpr_graph(sbml_dfs, graph_type="surrogate")
111
-
112
-
113
- def test_create_cpr_graph_none_attrs():
114
- # Should not raise when reaction_graph_attrs is None
115
- _ = net_create.create_cpr_graph(
116
- sbml_dfs, reaction_graph_attrs=None, graph_type="bipartite"
117
- )
118
-
119
-
120
- def test_igraph_construction():
121
- _ = net_create.process_cpr_graph(sbml_dfs)
122
-
123
-
124
- def test_process_cpr_graph_none_attrs():
125
- # Should not raise when reaction_graph_attrs is None
126
- _ = net_create.process_cpr_graph(sbml_dfs, reaction_graph_attrs=None)
127
-
128
-
129
- @pytest.mark.skip_on_windows
130
- def test_igraph_loading():
131
- # test read/write of an igraph network
132
- directeds = [True, False]
133
- graph_types = ["bipartite", "regulatory"]
134
-
135
- net_utils.export_networks(
136
- sbml_dfs,
137
- model_prefix="tmp",
138
- outdir="/tmp",
139
- directeds=directeds,
140
- graph_types=graph_types,
141
- )
142
-
143
- for graph_type in graph_types:
144
- for directed in directeds:
145
- import_pkl_path = net_utils._create_network_save_string(
146
- model_prefix="tmp",
147
- outdir="/tmp",
148
- directed=directed,
149
- graph_type=graph_type,
150
- )
151
- network_graph = net_utils.read_network_pkl(
152
- model_prefix="tmp",
153
- network_dir="/tmp",
154
- directed=directed,
155
- graph_type=graph_type,
156
- )
157
-
158
- assert network_graph.is_directed() == directed
159
- # cleanup
160
- os.unlink(import_pkl_path)
161
-
162
-
163
- def test_shortest_paths():
164
- species = sbml_dfs.species
165
- source_species = species[species["s_name"] == "NADH"]
166
- dest_species = species[species["s_name"] == "NAD+"]
167
- target_species_paths = net_utils.compartmentalize_species_pairs(
168
- sbml_dfs, source_species.index.tolist(), dest_species.index.tolist()
169
- )
170
-
171
- # directed graph
172
- cpr_graph = net_create.process_cpr_graph(
173
- sbml_dfs, directed=True, weighting_strategy="topology"
174
- )
175
- (
176
- all_shortest_reaction_paths_df,
177
- all_shortest_reaction_path_edges_df,
178
- edge_sources,
179
- paths_graph,
180
- ) = paths.find_all_shortest_reaction_paths(
181
- cpr_graph, sbml_dfs, target_species_paths, weight_var="weights"
182
- )
183
-
184
- # undirected graph
185
- cpr_graph = net_create.process_cpr_graph(
186
- sbml_dfs, directed=False, weighting_strategy="topology"
187
- )
188
- (
189
- all_shortest_reaction_paths_df,
190
- all_shortest_reaction_path_edges_df,
191
- edge_sources,
192
- paths_graph,
193
- ) = paths.find_all_shortest_reaction_paths(
194
- cpr_graph, sbml_dfs, target_species_paths, weight_var="weights"
195
- )
196
-
197
- assert all_shortest_reaction_paths_df.shape[0] == 3
198
-
199
-
200
- def test_neighborhood():
201
- species = sbml_dfs.species
202
- source_species = species[species["s_name"] == "NADH"].index.tolist()
203
-
204
- query_sc_species = net_utils.compartmentalize_species(sbml_dfs, source_species)
205
- compartmentalized_species = query_sc_species["sc_id"].tolist()
206
-
207
- cpr_graph = net_create.process_cpr_graph(
208
- sbml_dfs, directed=True, weighting_strategy="topology"
209
- )
210
-
211
- neighborhood = neighborhoods.find_neighborhoods(
212
- sbml_dfs,
213
- cpr_graph,
214
- compartmentalized_species=compartmentalized_species,
215
- order=3,
216
- )
217
-
218
- assert neighborhood["species_73473"]["vertices"].shape[0] == 6
219
-
220
-
221
- def test_format_interactors():
222
- # interactions are formatted
223
-
224
- graph_hierarchy_df = net_create._create_graph_hierarchy_df("regulatory")
225
-
226
- assert (
227
- net_create._format_tiered_reaction_species(
228
- r_id,
229
- reaction_species_examples_dict["valid_interactor"],
230
- sbml_dfs,
231
- graph_hierarchy_df,
232
- ).shape[0]
233
- == 1
234
- )
235
-
236
- print("Re-enable test once Issue #102 is solved")
237
-
238
- # catch error from invalid interactor specification
239
- # with pytest.raises(ValueError) as excinfo:
240
- # net_create._format_tiered_reaction_species(
241
- # r_id, reaction_species_examples_dict["invalid_interactor"], sbml_dfs
242
- # )
243
- # assert str(excinfo.value).startswith("Invalid combinations of SBO_terms")
244
-
245
- # simple reaction with just substrates and products
246
- assert (
247
- net_create._format_tiered_reaction_species(
248
- r_id,
249
- reaction_species_examples_dict["sub_and_prod"],
250
- sbml_dfs,
251
- graph_hierarchy_df,
252
- ).shape[0]
253
- == 2
254
- )
255
-
256
- # add a stimulator (activator)
257
- rxn_edges = net_create._format_tiered_reaction_species(
258
- r_id, reaction_species_examples_dict["stimulator"], sbml_dfs, graph_hierarchy_df
259
- )
260
-
261
- assert rxn_edges.shape[0] == 3
262
- assert rxn_edges.iloc[0][["from", "to"]].tolist() == ["stim", "sub"]
263
-
264
- # add catalyst + stimulator
265
- rxn_edges = net_create._format_tiered_reaction_species(
266
- r_id,
267
- reaction_species_examples_dict["all_entities"],
268
- sbml_dfs,
269
- graph_hierarchy_df,
270
- )
271
-
272
- assert rxn_edges.shape[0] == 4
273
- assert rxn_edges.iloc[0][["from", "to"]].tolist() == ["stim", "cat"]
274
- assert rxn_edges.iloc[1][["from", "to"]].tolist() == ["cat", "sub"]
275
-
276
- # no substrate
277
- rxn_edges = net_create._format_tiered_reaction_species(
278
- r_id,
279
- reaction_species_examples_dict["no_substrate"],
280
- sbml_dfs,
281
- graph_hierarchy_df,
282
- )
283
-
284
- assert rxn_edges.shape[0] == 5
285
- # stimulator -> reactant
286
- assert rxn_edges.iloc[0][["from", "to"]].tolist() == ["stim1", "cat"]
287
- assert rxn_edges.iloc[1][["from", "to"]].tolist() == ["stim2", "cat"]
288
- assert rxn_edges.iloc[2][["from", "to"]].tolist() == ["inh", "cat"]
289
-
290
- # use the surrogate model tiered layout also
291
-
292
- graph_hierarchy_df = net_create._create_graph_hierarchy_df("surrogate")
293
-
294
- rxn_edges = net_create._format_tiered_reaction_species(
295
- r_id,
296
- reaction_species_examples_dict["all_entities"],
297
- sbml_dfs,
298
- graph_hierarchy_df,
299
- )
300
-
301
- assert rxn_edges.shape[0] == 4
302
- assert rxn_edges.iloc[0][["from", "to"]].tolist() == ["stim", "sub"]
303
- assert rxn_edges.iloc[1][["from", "to"]].tolist() == ["sub", "cat"]
304
-
305
-
306
- def test_reverse_network_edges():
307
- graph_hierarchy_df = net_create._create_graph_hierarchy_df("regulatory")
308
-
309
- rxn_edges = net_create._format_tiered_reaction_species(
310
- r_id,
311
- reaction_species_examples_dict["all_entities"],
312
- sbml_dfs,
313
- graph_hierarchy_df,
314
- )
315
- augmented_network_edges = rxn_edges.assign(r_isreversible=True)
316
- augmented_network_edges["sc_parents"] = range(0, augmented_network_edges.shape[0])
317
- augmented_network_edges["sc_children"] = range(
318
- augmented_network_edges.shape[0], 0, -1
319
- )
320
-
321
- assert net_create._reverse_network_edges(augmented_network_edges).shape[0] == 2
322
-
323
-
324
- def test_net_polarity():
325
- polarity_series = pd.Series(
326
- ["ambiguous", "ambiguous"], index=[0, 1], name="link_polarity"
327
- )
328
- assert all(
329
- [x == "ambiguous" for x in paths._calculate_net_polarity(polarity_series)]
330
- )
331
-
332
- polarity_series = pd.Series(
333
- ["activation", "inhibition", "inhibition", "ambiguous"],
334
- index=range(0, 4),
335
- name="link_polarity",
336
- )
337
- assert paths._calculate_net_polarity(polarity_series) == [
338
- "activation",
339
- "inhibition",
340
- "activation",
341
- "ambiguous activation",
342
- ]
343
- assert paths._terminal_net_polarity(polarity_series) == "ambiguous activation"
344
-
345
-
346
- def test_entity_validation():
347
- entity_attrs = {"table": "reactions", "variable": "foo"}
348
-
349
- assert net_create._EntityAttrValidator(**entity_attrs).model_dump() == {
350
- **entity_attrs,
351
- **{"trans": DEFAULT_WT_TRANS},
352
- }
353
-
354
-
355
- ################################################
356
- # __main__
357
- ################################################
358
-
359
- if __name__ == "__main__":
360
- test_create_cpr_graph()
361
- test_igraph_loading()
362
- test_igraph_construction()
363
- test_shortest_paths()
364
- test_neighborhood()
365
- test_format_interactors()
366
- test_reverse_network_edges()
367
- test_entity_validation()