7
7
import time
8
8
from typing import Iterable , List , Mapping , Optional , Tuple
9
9
10
- from networkx import relabel_nodes
11
10
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
12
16
from tqdm import tqdm
13
17
14
18
from bio2bel import AbstractManager
15
19
from bio2bel .manager .flask_manager import FlaskMixin
16
20
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
21
21
from .constants import MODULE_NAME
22
22
from .models import Accession , Base , Chemical , Relation , Synonym
23
23
from .parser .accession import get_accession_df
36
36
37
37
38
38
class Manager (AbstractManager , FlaskMixin , BELNamespaceManagerMixin ):
39
- """Bio2BEL ChEBI Manager ."""
39
+ """Chemical multi-hierarchy ."""
40
40
41
41
_base = Base
42
42
module_name = MODULE_NAME
@@ -277,13 +277,14 @@ def _populate_relations(self, url: Optional[str] = None) -> None:
277
277
log .info ('committing Relations' )
278
278
self .session .commit ()
279
279
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 :
287
288
"""Populate all tables."""
288
289
t = time .time ()
289
290
@@ -295,16 +296,21 @@ def populate(self,
295
296
296
297
log .info ('populated in %.2f seconds' , time .time () - t )
297
298
298
- def normalize_chemicals (self , graph : BELGraph ) -> None :
299
+ def normalize_chemicals (self , graph : BELGraph , use_tqdm : bool = False ) -> None :
299
300
mapping = {
300
301
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 ))
302
303
}
303
304
relabel_nodes (graph , mapping , copy = False )
304
305
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 :
308
314
chemical = self .get_chemical_from_data (node )
309
315
if chemical is not None :
310
316
yield node , chemical
@@ -330,6 +336,8 @@ def get_chemical_from_data(self, node: BaseEntity) -> Optional[Chemical]:
330
336
else : # elif name is not None:
331
337
return self .get_chemical_by_chebi_name (name )
332
338
339
+ log .warning ('Could not find ChEBI node: %r' , node )
340
+
333
341
def enrich_chemical_hierarchy (self , graph : BELGraph ) -> None :
334
342
"""Enrich the parents for all ChEBI chemicals in the graph."""
335
343
for _ , data in graph .nodes (data = True ):
0 commit comments