Skip to content

Commit 64ffd18

Browse files
chores: fix ruff check messages
1 parent 29ff663 commit 64ffd18

9 files changed

Lines changed: 127 additions & 42 deletions

File tree

notebooks/graphblas_usecase_1.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,7 @@
19181918
"source": [
19191919
"from typing import Set, List\n",
19201920
"\n",
1921+
"\n",
19211922
"class QueryEngine:\n",
19221923
" def __init__(self, graph):\n",
19231924
" \"\"\"Initialize the QueryEngine with a Graph instance.\n",

notebooks/query_graphblas.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@
10191019
"import re\n",
10201020
"from typing import Set, List\n",
10211021
"\n",
1022+
"\n",
10221023
"class QueryEngine:\n",
10231024
" def __init__(self, graph):\n",
10241025
" self.graph = graph # Graph must contain lookup_tables\n",
@@ -1350,6 +1351,7 @@
13501351
"source": [
13511352
"from typing import Set, List\n",
13521353
"\n",
1354+
"\n",
13531355
"class QueryEngine:\n",
13541356
" def __init__(self, graph):\n",
13551357
" \"\"\"Initialize the QueryEngine with a Graph instance.\n",

ontograph/client.py

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@
3535
)
3636
from ontograph.downloader import DownloaderPort
3737
from ontograph.config.settings import DEFAULT_CACHE_DIR
38-
from ontograph.queries.navigator import NavigatorPronto, NavigatorGraphblas
39-
from ontograph.queries.relations import RelationsPronto, RelationsGraphblas
38+
from ontograph.queries.navigator import (
39+
NavigatorPronto,
40+
NavigatorGraphblas,
41+
)
42+
from ontograph.queries.relations import (
43+
RelationsPronto,
44+
RelationsGraphblas,
45+
)
4046
from ontograph.utils.pronto_utils import extract_terms
4147
from ontograph.queries.introspection import (
4248
IntrospectionPronto,
@@ -225,7 +231,9 @@ def __init__(self, cache_dir: str = DEFAULT_CACHE_DIR) -> None:
225231
self._relations = None
226232
self._introspection = None
227233

228-
def __create_graphblas_ontology(self, ontology, include_obsolete=False):
234+
def __create_graphblas_ontology(
235+
self, ontology: Ontology, include_obsolete: bool = False
236+
) -> tuple[LookUpTables, Graph]:
229237
terms = extract_terms(
230238
ontology=ontology, include_obsolete=include_obsolete
231239
)
@@ -263,11 +271,30 @@ def __create_graphblas_ontology(self, ontology, include_obsolete=False):
263271
return lookup_tables, graph
264272

265273
def _detect_source_type(self, source: str) -> str:
266-
"""Determine whether 'source' is a file, an OBO Foundry ID, or a URL.
267-
Priority order:
268-
1. Local file path
269-
2. OBO Foundry identifier
270-
3. Other URL
274+
"""Detect the type of ontology source: file path, OBO Foundry ID, or URL.
275+
276+
This method checks the input string and determines its type in the following priority order:
277+
1. Local file path
278+
2. OBO Foundry identifier (alphanumeric, underscore, or hyphen; no slashes or dots)
279+
3. HTTP/HTTPS URL
280+
281+
Args:
282+
source (str): The ontology source string to check. Can be a file path, OBO Foundry ID, or URL.
283+
284+
Returns:
285+
str: The detected source type. One of 'file', 'obo', or 'url'.
286+
287+
Raises:
288+
ValueError: If the source type cannot be determined.
289+
290+
Example:
291+
>>> client = ClientOntology()
292+
>>> client._detect_source_type("./ontology.obo")
293+
'file'
294+
>>> client._detect_source_type("go")
295+
'obo'
296+
>>> client._detect_source_type("https://example.com/ontology.obo")
297+
'url'
271298
"""
272299
path = Path(source)
273300

@@ -364,9 +391,35 @@ def load(
364391
self,
365392
source: str,
366393
downloader: DownloaderPort = None,
367-
include_obsolete=False,
368-
backend='pronto',
369-
):
394+
include_obsolete: bool = False,
395+
backend: str = 'pronto',
396+
) -> None:
397+
"""Load an ontology from a file path, URL, or OBO Foundry catalog.
398+
399+
This method detects the source type and loads the ontology using the appropriate strategy:
400+
- Local file path
401+
- URL
402+
- OBO Foundry catalog identifier
403+
404+
It also initializes query adapters for navigation, relations, and introspection based on the selected backend.
405+
406+
Args:
407+
source (str): Path to the ontology file, URL, or OBO Foundry identifier.
408+
downloader (DownloaderPort, optional): Downloader adapter for remote files. Defaults to None.
409+
include_obsolete (bool, optional): If True, include obsolete terms when building GraphBLAS structures. Defaults to False.
410+
backend (str, optional): Backend for queries ('pronto' or 'graphblas'). Defaults to 'pronto'.
411+
412+
Raises:
413+
FileNotFoundError: If the ontology source cannot be found as a file, URL, or catalog entry.
414+
ValueError: If an unknown backend is specified.
415+
416+
Returns:
417+
None
418+
419+
Example:
420+
>>> client = ClientOntology()
421+
>>> client.load(source="./tests/resources/dummy_ontology.obo")
422+
"""
370423
logger.info(f'Loading ontology from source: {source} ...')
371424
loader = ProntoLoaderAdapter(cache_dir=self._cache_dir)
372425

@@ -431,8 +484,16 @@ def load(
431484

432485
logger.info('Ontology loading complete.')
433486

434-
def _initialize_queries(self, backend) -> None:
435-
"""Initialize query adapters for navigation, relations, and introspection."""
487+
def _initialize_queries(self, backend: str) -> None:
488+
"""Initializes query adapters for navigation, relations, and introspection based on the specified backend.
489+
490+
Args:
491+
backend (str): The backend to use for query adapters. Supported values are 'pronto' and 'graphblas'.
492+
493+
Raises:
494+
KeyError: If the specified backend is not supported.
495+
496+
"""
436497

437498
if backend == 'pronto':
438499
self._navigator = NavigatorPronto(ontology=self._get_ontology)

ontograph/loader.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ def _extract_ontology_id(self, ontology: pronto.Ontology) -> str | None:
156156
)
157157
return None
158158

159-
def find_file_encoding(self, file):
159+
def find_file_encoding(self, file: str | Path) -> str | None:
160+
"""Detect the encoding of a file.
161+
162+
Args:
163+
file (str | Path): Path to the file whose encoding should be detected.
164+
165+
Returns:
166+
str | None: The detected encoding, or None if it cannot be determined.
167+
"""
160168
result = from_path(file).best()
161169
return result.encoding
162170

ontograph/queries/introspection.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import logging
33
from collections import deque
44

5-
from ontograph.queries.navigator import NavigatorPronto
6-
from ontograph.queries.relations import RelationsPronto
5+
from ontograph.models import LookUpTables
6+
from ontograph.queries.navigator import NavigatorOntology
7+
from ontograph.queries.relations import RelationsPronto, RelationsOntology
78

89
__all__ = [
9-
'OntologyIntrospection',
10+
'IntrospectionOntology',
1011
]
1112

1213
logger = logging.getLogger(__name__)
@@ -16,10 +17,12 @@
1617
# ------------------------------------------------------------
1718
# ---- OntologyIntrospection Port (abstract class) ----
1819
# ------------------------------------------------------------
19-
class OntologyIntrospection(ABC):
20+
class IntrospectionOntology(ABC):
2021
"""Abstract class for ontology introspection utilities."""
2122

22-
def __init__(self, navigator, relations) -> None:
23+
def __init__(
24+
self, navigator: NavigatorOntology, relations: RelationsOntology
25+
) -> None:
2326
self._navigator = navigator
2427
self._relations = relations
2528

@@ -60,14 +63,14 @@ def print_term_trajectories_tree(trajectories: list[dict]) -> None:
6063
# -------------------------------------------------------------
6164
# ---- IntrospectionPronto adapter (concrete class) ----
6265
# -------------------------------------------------------------
63-
class IntrospectionPronto(OntologyIntrospection):
66+
class IntrospectionPronto(IntrospectionOntology):
6467
"""Provides introspection utilities for ontology graphs.
6568
6669
Includes methods for calculating distances, paths, and ancestor trajectories.
6770
"""
6871

6972
def __init__(
70-
self, navigator: NavigatorPronto, relations: RelationsPronto
73+
self, navigator: NavigatorOntology, relations: RelationsPronto
7174
) -> None:
7275
"""Initialize the introspection utility.
7376
@@ -242,8 +245,8 @@ def print_term_trajectories_tree(trajectories: list[dict]) -> None:
242245
print(f'{node["id"]}: {node["name"]}')
243246
return
244247
# Otherwise, use tree printing
245-
root = OntologyIntrospection._build_tree_from_trajectories(trajectories)
246-
OntologyIntrospection._print_ascii_tree(root)
248+
root = IntrospectionOntology._build_tree_from_trajectories(trajectories)
249+
IntrospectionOntology._print_ascii_tree(root)
247250

248251
@staticmethod
249252
def _build_tree_from_trajectories(trajectories: list[dict]) -> object:
@@ -306,30 +309,31 @@ def print_ascii_tree(
306309
print_ascii_tree(child, '', is_last_child)
307310

308311

309-
class IntrospectionGraphblas(OntologyIntrospection):
312+
class IntrospectionGraphblas(IntrospectionOntology):
310313
"""Provides introspection utilities for ontology graphs using GraphBLAS.
311314
312315
Includes methods for calculating distances, paths, and ancestor trajectories.
313316
"""
314317

315318
def __init__(
316319
self,
317-
navigator: NavigatorPronto,
318-
relations: RelationsPronto,
319-
lookup_tables,
320+
navigator: NavigatorOntology,
321+
relations: RelationsOntology,
322+
lookup_tables: LookUpTables,
320323
) -> None:
321324
"""Initialize the introspection utility.
322325
323326
Args:
324-
navigator (OntologyNavigator): The ontology navigator.
325-
relations (OntologyRelations): The ontology relations.
327+
navigator (NavigatorPronto): The ontology navigator.
328+
relations (RelationsPronto): The ontology relations.
329+
lookup_tables (LookUpTables): Lookup tables for term/index/description mapping.
326330
"""
327331
self.__navigator = navigator
328332
self.__relations = relations
329333
self.lookup_tables = lookup_tables
330334
self.matrices_container = navigator.matrices_container
331335

332-
def get_distance_from_root(self, term_id):
336+
def get_distance_from_root(self, term_id: str) -> int:
333337
"""Calculate the distance from the given term to the root node(s) of the ontology.
334338
335339
Parameters
@@ -361,7 +365,7 @@ def get_distance_from_root(self, term_id):
361365

362366
return max_distance
363367

364-
def get_path_between(self, node_a, node_b):
368+
def get_path_between(self, node_a: str, node_b: str) -> list:
365369
"""Find the shortest path between two nodes in the ontology.
366370
367371
Parameters
@@ -403,7 +407,7 @@ def get_path_between(self, node_a, node_b):
403407
# BFS to find shortest path
404408

405409
queue = deque([[start_idx]])
406-
visited = set([start_idx])
410+
visited = {start_idx}
407411

408412
while queue:
409413
path = queue.popleft()

ontograph/queries/navigator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ontograph.models import Ontology, LookUpTables
1010

1111
__all__ = [
12-
'NavigatorPronto',
12+
'NavigatorOntology',
1313
]
1414

1515
logger = logging.getLogger(__name__)
@@ -19,7 +19,7 @@
1919
# --------------------------------------------------------
2020
# ---- OntologyNavigator Port (abstract class) ----
2121
# --------------------------------------------------------
22-
class NavigatorPronto(ABC):
22+
class NavigatorOntology(ABC):
2323
"""Abstract navigator for traversing and querying ontology term relationships."""
2424

2525
def __init__(self, ontology: 'Ontology') -> None:
@@ -90,7 +90,7 @@ def get_root(self) -> list:
9090
# ---------------------------------------------------------
9191
# ---- NavigatorPronto adapter (concrete class) ----
9292
# ---------------------------------------------------------
93-
class NavigatorPronto(NavigatorPronto):
93+
class NavigatorPronto(NavigatorOntology):
9494
"""Navigator for traversing and querying ontology term relationships.
9595
9696
This class provides methods to retrieve parent, ancestor, descendant, sibling, and root terms
@@ -431,7 +431,7 @@ def get_root(self) -> list:
431431
# ------------------------------------------------------------
432432
# ---- NavigatorGraphblas adapter (concrete class) ----
433433
# ------------------------------------------------------------
434-
class NavigatorGraphblas(NavigatorPronto):
434+
class NavigatorGraphblas(NavigatorOntology):
435435
def __init__(self, ontology: Ontology, lookup_tables: LookUpTables) -> None:
436436
self.__ontology = ontology
437437

ontograph/queries/relations.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import logging
33
from collections import deque
44

5-
from ontograph.queries.navigator import NavigatorPronto as _OntologyNavigator
5+
from ontograph.queries.navigator import NavigatorOntology as _OntologyNavigator
66

77
__all__ = [
8-
'RelationsPronto',
8+
'RelationsOntology',
99
]
1010

1111
logger = logging.getLogger(__name__)
@@ -15,7 +15,7 @@
1515
# --------------------------------------------------------
1616
# ---- OntologyRelations Port (abstract class) ----
1717
# --------------------------------------------------------
18-
class RelationsPronto(ABC):
18+
class RelationsOntology(ABC):
1919
"""Abstract class for querying relationships between ontology terms."""
2020

2121
def __init__(self, navigator: _OntologyNavigator) -> None:
@@ -50,7 +50,7 @@ def get_lowest_common_ancestors(self, node_ids: list[str]) -> set:
5050
# ---------------------------------------------------------
5151
# ---- RelationsPronto adapter (abstract class) ----
5252
# ---------------------------------------------------------
53-
class RelationsPronto(RelationsPronto):
53+
class RelationsPronto(RelationsOntology):
5454
"""Provides methods for querying relationships between ontology terms.
5555
5656
Includes ancestor, descendant, sibling checks, and common ancestor computations.
@@ -256,7 +256,7 @@ def get_lowest_common_ancestors(self, node_ids: list[str]) -> set:
256256
# ------------------------------------------------------------
257257
# ---- RelationsGraphblas adapter (abstract class) ----
258258
# ------------------------------------------------------------
259-
class RelationsGraphblas(RelationsPronto):
259+
class RelationsGraphblas(RelationsOntology):
260260
def __init__(
261261
self, navigator: _OntologyNavigator, lookup_tables: object
262262
) -> None:

ontograph/utils/pronto_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import logging
2+
13
from pronto import Ontology
24

5+
logger = logging.getLogger(__name__)
6+
logger.addHandler(logging.NullHandler())
7+
38

49
def extract_terms(ontology: Ontology, include_obsolete: bool = False) -> list:
510
"""Single-pass extraction of pronto.Term objects, sorted by term.id."""

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ exclude = [
159159
"docs/_build",
160160
"tests/",
161161
"test_*.py",
162-
"*/test_*.py"
162+
"*/test_*.py",
163+
"notebooks/",
164+
"examples/",
165+
"*.ipynb",
166+
"**/*.ipynb"
163167
]
164168
ignore = [
165169
# flake8-bugbear (B)

0 commit comments

Comments
 (0)