Skip to content

Commit 06917b1

Browse files
committed
Update for newer pybel
1 parent 63729e8 commit 06917b1

File tree

8 files changed

+118
-174
lines changed

8 files changed

+118
-174
lines changed

requirements.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
PACKAGES = setuptools.find_packages(where='src')
1212
META_PATH = os.path.join('src', 'bio2bel_hmdb', '__init__.py')
1313
INSTALL_REQUIRES = [
14-
'pybel>=0.11.1',
15-
'pybel_tools>=0.5.1',
16-
'bio2bel>=0.0.9',
14+
'pybel>=0.13.1',
15+
'bio2bel>=0.2.0',
1716
'pandas',
1817
'click',
1918
'sqlalchemy',
2019
'requests',
21-
'networkx==1.11',
2220
'tqdm',
2321
]
2422
EXTRAS_REQUIRE = {

src/bio2bel_hmdb/enrich.py

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@
4646
"""
4747

4848
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
5357
from .manager import Manager
5458

5559
log = logging.getLogger(__name__)
@@ -72,19 +76,15 @@ def _check_namespaces(data, bel_function, bel_namespace):
7276

7377

7478
# 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])
8888
else:
8989
continue
9090

@@ -109,20 +109,17 @@ def enrich_metabolites_proteins(graph, connection=None):
109109
})
110110

111111

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,
115115
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
119116
"""
117+
if manager is None:
118+
manager = Manager()
120119

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])
126123
else:
127124
continue
128125

@@ -149,24 +146,20 @@ def enrich_proteins_metabolites(graph, connection=None):
149146

150147

151148
# 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()
155154

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):
163156
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])
165158
else:
166159
continue
167160

168161
if metabolite_disease_interactions is None:
169-
log.warning("Unable to find node: %s", node)
162+
log.warning("Unable to find node: %s", data)
170163
continue
171164

172165
# add edges and collect all the references for this edge
@@ -188,7 +181,7 @@ def enrich_metabolites_diseases(graph, connection=None):
188181
# add disease node and construct edge
189182
disease_data = association.disease.serialize_to_bel()
190183
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={
192185
RELATION: ASSOCIATION,
193186
EVIDENCE: None,
194187
CITATION: {
@@ -202,24 +195,21 @@ def enrich_metabolites_diseases(graph, connection=None):
202195
})
203196

204197

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()
208204

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):
216206
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])
218208
else:
219209
continue
220210

221211
if not disease_metabolite_interactions:
222-
log.warning("Unable to find node: %s", node)
212+
log.warning("Unable to find node: %s", data)
223213
continue
224214

225215
# add edges and collect all the references for this edge
@@ -241,7 +231,7 @@ def enrich_diseases_metabolites(graph, connection=None):
241231
# add disease node and construct edge
242232
metabolite_data = association.metabolite.serialize_to_bel()
243233
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={
245235
RELATION: ASSOCIATION,
246236
EVIDENCE: None,
247237
CITATION: {

0 commit comments

Comments
 (0)