Skip to content

Commit e73a35c

Browse files
committed
Update chebi manager
1 parent bab61ca commit e73a35c

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/bio2bel_chebi/manager.py

+26-18
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
import time
88
from typing import Iterable, List, Mapping, Optional, Tuple
99

10-
from networkx import relabel_nodes
1110
import pandas as pd
11+
from networkx import relabel_nodes
12+
from pybel import BELGraph
13+
from pybel.constants import IDENTIFIER, NAME, NAMESPACE
14+
from pybel.dsl import BaseEntity
15+
from pybel.manager.models import Namespace, NamespaceEntry
1216
from tqdm import tqdm
1317

1418
from bio2bel import AbstractManager
1519
from bio2bel.manager.flask_manager import FlaskMixin
1620
from bio2bel.manager.namespace_manager import BELNamespaceManagerMixin
17-
from pybel import BELGraph
18-
from pybel.constants import IDENTIFIER, NAME, NAMESPACE
19-
from pybel.dsl import BaseEntity
20-
from pybel.manager.models import Namespace, NamespaceEntry
2121
from .constants import MODULE_NAME
2222
from .models import Accession, Base, Chemical, Relation, Synonym
2323
from .parser.accession import get_accession_df
@@ -36,7 +36,7 @@
3636

3737

3838
class Manager(AbstractManager, FlaskMixin, BELNamespaceManagerMixin):
39-
"""Bio2BEL ChEBI Manager."""
39+
"""Chemical multi-hierarchy."""
4040

4141
_base = Base
4242
module_name = MODULE_NAME
@@ -277,13 +277,14 @@ def _populate_relations(self, url: Optional[str] = None) -> None:
277277
log.info('committing Relations')
278278
self.session.commit()
279279

280-
def populate(self,
281-
inchis_url: Optional[str] = None,
282-
compounds_url: Optional[str] = None,
283-
relations_url: Optional[str] = None,
284-
names_url: Optional[str] = None,
285-
accessions_url: Optional[str] = None,
286-
) -> None:
280+
def populate(
281+
self,
282+
inchis_url: Optional[str] = None,
283+
compounds_url: Optional[str] = None,
284+
relations_url: Optional[str] = None,
285+
names_url: Optional[str] = None,
286+
accessions_url: Optional[str] = None,
287+
) -> None:
287288
"""Populate all tables."""
288289
t = time.time()
289290

@@ -295,16 +296,21 @@ def populate(self,
295296

296297
log.info('populated in %.2f seconds', time.time() - t)
297298

298-
def normalize_chemicals(self, graph: BELGraph) -> None:
299+
def normalize_chemicals(self, graph: BELGraph, use_tqdm: bool = False) -> None:
299300
mapping = {
300301
node: chemical.to_bel()
301-
for node, chemical in list(self.iter_chemicals(graph))
302+
for node, chemical in list(self.iter_chemicals(graph, use_tqdm=use_tqdm))
302303
}
303304
relabel_nodes(graph, mapping, copy=False)
304305

305-
def iter_chemicals(self, graph: BELGraph) -> Iterable[Tuple[BaseEntity, Chemical]]:
306-
"""Iterate over pairs of BEL nodes and HGNC genes."""
307-
for node in graph:
306+
def iter_chemicals(self, graph: BELGraph, use_tqdm: bool = False) -> Iterable[Tuple[BaseEntity, Chemical]]:
307+
"""Iterate over pairs of BEL nodes and ChEBI chemicals."""
308+
it = (
309+
tqdm(graph, desc='ChEBI chemicals')
310+
if use_tqdm else
311+
graph
312+
)
313+
for node in it:
308314
chemical = self.get_chemical_from_data(node)
309315
if chemical is not None:
310316
yield node, chemical
@@ -330,6 +336,8 @@ def get_chemical_from_data(self, node: BaseEntity) -> Optional[Chemical]:
330336
else: # elif name is not None:
331337
return self.get_chemical_by_chebi_name(name)
332338

339+
log.warning('Could not find ChEBI node: %r', node)
340+
333341
def enrich_chemical_hierarchy(self, graph: BELGraph) -> None:
334342
"""Enrich the parents for all ChEBI chemicals in the graph."""
335343
for _, data in graph.nodes(data=True):

0 commit comments

Comments
 (0)