46
46
"""
47
47
48
48
import logging
49
-
50
- from pybel .constants import *
51
- from pybel_tools import pipeline
52
-
49
+ from typing import Optional
50
+
51
+ from pybel import BELGraph
52
+ from pybel .constants import (
53
+ ABUNDANCE , ANNOTATIONS , ASSOCIATION , CITATION , CITATION_REFERENCE , CITATION_TYPE , CITATION_TYPE_PUBMED , EVIDENCE ,
54
+ FUNCTION , NAME , NAMESPACE , PATHOLOGY , PROTEIN , RELATION ,
55
+ )
56
+ from pybel .struct .pipeline .decorators import in_place_transformation
53
57
from .manager import Manager
54
58
55
59
log = logging .getLogger (__name__ )
@@ -72,19 +76,15 @@ def _check_namespaces(data, bel_function, bel_namespace):
72
76
73
77
74
78
# enrich proteins and metabolites
75
- @pipeline .in_place_mutator
76
- def enrich_metabolites_proteins (graph , connection = None ):
77
- """Enriches a given BEL graph, which includes metabolites with proteins, that are associated to the metabolites.
78
-
79
- :param pybel.BELGraph graph: A BEL graph
80
- :param str connection: connection for the manager used to connect to a database
81
- """
82
-
83
- m = Manager .ensure (connection )
84
-
85
- for node , data in graph .nodes (data = True ):
86
- if _check_namespaces (data , ABUNDANCE , 'HMDB' ):
87
- metabolite_protein_interactions = m .query_metabolite_associated_proteins (data [NAME ])
79
+ @in_place_transformation
80
+ def enrich_metabolites_proteins (graph : BELGraph , manager : Optional [Manager ] = None ):
81
+ """Enrich a given BEL graph, which includes metabolites with proteins, that are associated to the metabolites."""
82
+ if manager is None :
83
+ manager = Manager ()
84
+
85
+ for node in list (graph ):
86
+ if _check_namespaces (node , ABUNDANCE , 'HMDB' ):
87
+ metabolite_protein_interactions = manager .query_metabolite_associated_proteins (node [NAME ])
88
88
else :
89
89
continue
90
90
@@ -109,20 +109,17 @@ def enrich_metabolites_proteins(graph, connection=None):
109
109
})
110
110
111
111
112
- @pipeline . in_place_mutator
113
- def enrich_proteins_metabolites (graph , connection = None ):
114
- """Enriches a given BEL graph, which includes uniprot proteins with HMDB metabolites,
112
+ @in_place_transformation
113
+ def enrich_proteins_metabolites (graph : BELGraph , manager : Optional [ Manager ] = None ):
114
+ """Enrich a given BEL graph, which includes uniprot proteins with HMDB metabolites,
115
115
that are associated to the proteins.
116
-
117
- :param pybel.BELGraph graph: A BEL graph
118
- :param str connection: connection for the manager used to connect to a database
119
116
"""
117
+ if manager is None :
118
+ manager = Manager ()
120
119
121
- m = Manager .ensure (connection )
122
-
123
- for node , data in graph .nodes (data = True ):
124
- if _check_namespaces (data , PROTEIN , 'UP' ):
125
- protein_metabolite_interactions = m .query_protein_associated_metabolites (data [NAME ])
120
+ for node in list (graph ):
121
+ if _check_namespaces (node , PROTEIN , 'UP' ):
122
+ protein_metabolite_interactions = manager .query_protein_associated_metabolites (node [NAME ])
126
123
else :
127
124
continue
128
125
@@ -149,24 +146,20 @@ def enrich_proteins_metabolites(graph, connection=None):
149
146
150
147
151
148
# enrich diseases and metabolites
152
- @pipeline .in_place_mutator
153
- def enrich_metabolites_diseases (graph , connection = None ):
154
- """Enriches a given BEL graph, which includes metabolites with diseases, to which the metabolites are associated.
149
+ @in_place_transformation
150
+ def enrich_metabolites_diseases (graph : BELGraph , manager : Optional [Manager ] = None ):
151
+ """Enrich a given BEL graph, which includes metabolites with diseases, to which the metabolites are associated."""
152
+ if manager is None :
153
+ manager = Manager ()
155
154
156
- :param pybel.BELGraph graph: A BEL graph
157
- :param str connection: connection for the manager used to connect to a database
158
- """
159
-
160
- m = Manager .ensure (connection )
161
-
162
- for node , data in graph .nodes (data = True ):
155
+ for data in list (graph ):
163
156
if _check_namespaces (data , ABUNDANCE , 'HMDB' ):
164
- metabolite_disease_interactions = m .query_metabolite_associated_diseases (data [NAME ])
157
+ metabolite_disease_interactions = manager .query_metabolite_associated_diseases (data [NAME ])
165
158
else :
166
159
continue
167
160
168
161
if metabolite_disease_interactions is None :
169
- log .warning ("Unable to find node: %s" , node )
162
+ log .warning ("Unable to find node: %s" , data )
170
163
continue
171
164
172
165
# add edges and collect all the references for this edge
@@ -188,7 +181,7 @@ def enrich_metabolites_diseases(graph, connection=None):
188
181
# add disease node and construct edge
189
182
disease_data = association .disease .serialize_to_bel ()
190
183
disease_tuple = graph .add_node_from_data (disease_data )
191
- graph .add_edge (disease_tuple , node , attr_dict = {
184
+ graph .add_edge (disease_tuple , data , attr_dict = {
192
185
RELATION : ASSOCIATION ,
193
186
EVIDENCE : None ,
194
187
CITATION : {
@@ -202,24 +195,21 @@ def enrich_metabolites_diseases(graph, connection=None):
202
195
})
203
196
204
197
205
- @pipeline .in_place_mutator
206
- def enrich_diseases_metabolites (graph , connection = None ):
207
- """Enriches a given BEL graph, which includes HMDB diseases with HMDB metabolites, which are associated to the diseases.
198
+ @in_place_transformation
199
+ def enrich_diseases_metabolites (graph : BELGraph , manager : Optional [Manager ] = None ):
200
+ """Enrich a given BEL graph, which includes HMDB diseases with HMDB metabolites, which are associated to the
201
+ diseases."""
202
+ if manager is None :
203
+ manager = Manager ()
208
204
209
- :param pybel.BELGraph graph: A BEL graph
210
- :param str connection: connection for the manager used to connect to a database
211
- """
212
-
213
- m = Manager .ensure (connection )
214
-
215
- for node , data in graph .nodes (data = True ):
205
+ for data in list (graph ):
216
206
if _check_namespaces (data , PATHOLOGY , 'HMDB_D' ):
217
- disease_metabolite_interactions = m .query_disease_associated_metabolites (data [NAME ])
207
+ disease_metabolite_interactions = manager .query_disease_associated_metabolites (data [NAME ])
218
208
else :
219
209
continue
220
210
221
211
if not disease_metabolite_interactions :
222
- log .warning ("Unable to find node: %s" , node )
212
+ log .warning ("Unable to find node: %s" , data )
223
213
continue
224
214
225
215
# add edges and collect all the references for this edge
@@ -241,7 +231,7 @@ def enrich_diseases_metabolites(graph, connection=None):
241
231
# add disease node and construct edge
242
232
metabolite_data = association .metabolite .serialize_to_bel ()
243
233
metabolite_tuple = graph .add_node_from_data (metabolite_data )
244
- graph .add_edge (metabolite_tuple , node , attr_dict = {
234
+ graph .add_edge (metabolite_tuple , data , attr_dict = {
245
235
RELATION : ASSOCIATION ,
246
236
EVIDENCE : None ,
247
237
CITATION : {
0 commit comments