Skip to content

Commit 8cbff1e

Browse files
Merge pull request #22 from saezlab/feat/add-download-manager-port
Enhance downloader options and update documentation for OntoGraph
1 parent a45136b commit 8cbff1e

17 files changed

Lines changed: 619 additions & 80 deletions

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ uv pip install -e .
3030

3131
```python
3232
from ontograph.client import ClientCatalog
33+
from ontograph.downloader import DownloadManagerAdapter
34+
from ontograph.config.settings import DEFAULT_CACHE_DIR
3335

3436
# Instantiate a client for your catalog
3537
client_catalog = ClientCatalog(cache_dir="./data/out")
3638

39+
# Optional: choose a downloader adapter explicitly
40+
# downloader = DownloadManagerAdapter(cache_dir=DEFAULT_CACHE_DIR, backend="requests")
41+
# client_catalog = ClientCatalog(cache_dir="./data/out", downloader=downloader)
42+
3743
# Load the catalog, in case this one doesn't exist it will be downloaded automatically in the cache folder you specify.
3844
client_catalog.load_catalog()
3945
```
@@ -56,12 +62,18 @@ metadata_go = client_catalog.get_ontology_metadata(ontology_id="go", show_metada
5662
#### Create a client for your ontology
5763
```python
5864
from ontograph.client import ClientOntology
65+
from ontograph.downloader import DownloadManagerAdapter
66+
from ontograph.config.settings import DEFAULT_CACHE_DIR
5967

6068
# Instantiate a client for your ontology
6169
client_dummy_ontology = ClientOntology(cache_dir="./data/out")
6270

6371
# Load a dummy ontology, we prepare a simple one to try out this package.
64-
client_dummy_ontology.load(file_path_ontology="./tests/resources/dummy_ontology.obo")
72+
client_dummy_ontology.load(source="./tests/resources/dummy_ontology.obo")
73+
74+
# Optional: choose a downloader adapter explicitly
75+
# downloader = DownloadManagerAdapter(cache_dir=DEFAULT_CACHE_DIR, backend="requests")
76+
# client_dummy_ontology = ClientOntology(cache_dir="./data/out", downloader=downloader)
6577
```
6678
#### Queries for your ontology
6779

@@ -138,7 +150,17 @@ If you are interested in loading an ontology from the catalog, just use the `nam
138150

139151
```bash
140152
client_go = ClientOntology()
141-
client_go.load(name_id="go", format="obo")
153+
client_go.load(source="go")
154+
```
155+
156+
### Downloader configuration
157+
158+
By default, the project uses a configurable downloader backend. You can set a global default in `ontograph/config/settings.py`:
159+
160+
```python
161+
DEFAULT_DOWNLOADER = "pooch"
162+
# or
163+
DEFAULT_DOWNLOADER = "download_manager"
142164
```
143165

144166
## Contributing

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
Analyze ontology structure, calculate paths and trajectories, and visualize term hierarchies.
2525

2626
- **Caching & Download Management**
27-
Efficiently download and cache ontology files for offline use.
27+
Efficiently download and cache ontology files for offline use with configurable downloader backends.
2828

2929
---
3030

docs/learn/tutorials/quickstart.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ First, let's interact with the OBO Foundry catalog to discover available ontolog
1111

1212
```python
1313
from ontograph.client import ClientCatalog
14+
from ontograph.downloader import DownloadManagerAdapter
15+
from ontograph.config.settings import DEFAULT_CACHE_DIR
1416

1517
# Create a catalog client (specify a cache directory for downloads)
1618
client_catalog = ClientCatalog(cache_dir="./data/out")
1719

1820
# Load the catalog (downloads if not cached)
1921
client_catalog.load_catalog()
22+
23+
# Optional: choose a downloader adapter explicitly
24+
# downloader = DownloadManagerAdapter(cache_dir=DEFAULT_CACHE_DIR, backend="requests")
25+
# client_catalog = ClientCatalog(cache_dir="./data/out", downloader=downloader)
2026
```
2127

2228
### List Available Ontologies
@@ -48,12 +54,18 @@ Now, let's load an ontology and explore its structure.
4854

4955
```python
5056
from ontograph.client import ClientOntology
57+
from ontograph.downloader import DownloadManagerAdapter
58+
from ontograph.config.settings import DEFAULT_CACHE_DIR
5159

5260
# Create an ontology client
5361
client_ontology = ClientOntology(cache_dir="./data/out")
5462

5563
# Load a sample ontology (provided in the repo for testing)
56-
client_ontology.load(file_path_ontology="./tests/resources/dummy_ontology.obo")
64+
client_ontology.load(source="./tests/resources/dummy_ontology.obo")
65+
66+
# Optional: choose a downloader adapter explicitly
67+
# downloader = DownloadManagerAdapter(cache_dir=DEFAULT_CACHE_DIR, backend="requests")
68+
# client_ontology = ClientOntology(cache_dir="./data/out", downloader=downloader)
5769
```
5870

5971
---
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Basics: Downloading Ontologies
2+
3+
This minimal tutorial shows how to download ontologies using either Pooch or Download Manager, and how to load from the catalog or a URL.
4+
5+
---
6+
7+
## 1. Choose a Global Default Downloader
8+
9+
Set the default once in `ontograph/config/settings.py`:
10+
11+
```python
12+
DEFAULT_DOWNLOADER = "pooch"
13+
# or
14+
DEFAULT_DOWNLOADER = "download_manager"
15+
```
16+
17+
Now any client will use the configured backend unless you pass a downloader explicitly.
18+
19+
---
20+
21+
## 2. Use Pooch Explicitly
22+
23+
```python
24+
from ontograph.client import ClientCatalog, ClientOntology
25+
from ontograph.downloader import PoochDownloaderAdapter
26+
from ontograph.config.settings import DEFAULT_CACHE_DIR
27+
28+
downloader = PoochDownloaderAdapter(cache_dir=DEFAULT_CACHE_DIR)
29+
30+
catalog = ClientCatalog(cache_dir="./data/out", downloader=downloader)
31+
catalog.load_catalog()
32+
33+
client = ClientOntology(cache_dir="./data/out", downloader=downloader)
34+
client.load(source="go") # catalog download
35+
```
36+
37+
---
38+
39+
## 3. Use Download Manager Explicitly
40+
41+
```python
42+
from ontograph.client import ClientCatalog, ClientOntology
43+
from ontograph.downloader import DownloadManagerAdapter
44+
from ontograph.config.settings import DEFAULT_CACHE_DIR
45+
46+
downloader = DownloadManagerAdapter(
47+
cache_dir=DEFAULT_CACHE_DIR,
48+
backend="requests",
49+
)
50+
51+
catalog = ClientCatalog(cache_dir="./data/out", downloader=downloader)
52+
catalog.load_catalog()
53+
54+
client = ClientOntology(cache_dir="./data/out", downloader=downloader)
55+
client.load(source="go") # catalog download
56+
```
57+
58+
---
59+
60+
## 4. Download From a URL
61+
62+
```python
63+
from ontograph.client import ClientOntology
64+
from ontograph.downloader import PoochDownloaderAdapter
65+
from ontograph.config.settings import DEFAULT_CACHE_DIR
66+
67+
downloader = PoochDownloaderAdapter(cache_dir=DEFAULT_CACHE_DIR)
68+
69+
# URL to GO ontology
70+
source_go = "https://purl.obolibrary.org/obo/go.obo"
71+
72+
client = ClientOntology(cache_dir=DEFAULT_CACHE_DIR, downloader=downloader)
73+
client.load(source=source_go)
74+
```

docs/reference/source/ontograph/client-catalog.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ This class is ideal for users who want to explore the catalog of available ontol
1515
- Get download URLs and available formats for each ontology
1616
- Print the catalog schema tree for exploration
1717

18+
## Usage
19+
20+
```python
21+
from ontograph.client import ClientCatalog
22+
from ontograph.downloader import DownloadManagerAdapter
23+
from ontograph.config.settings import DEFAULT_CACHE_DIR
24+
25+
client_catalog = ClientCatalog(cache_dir="./data/out")
26+
27+
# Optional: use Download Manager for all catalog downloads
28+
# downloader = DownloadManagerAdapter(cache_dir=DEFAULT_CACHE_DIR, backend="requests")
29+
# client_catalog = ClientCatalog(cache_dir="./data/out", downloader=downloader)
30+
```
31+
1832
---
1933

2034
## API Reference

docs/reference/source/ontograph/client-ontology.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ This class is ideal for users who want to work directly with a specific ontology
1515
- Introspect ontology structure: calculate paths, trajectories, and visualize term hierarchies
1616
- Modular query adapters for navigation, relations, and introspection
1717

18+
## Usage
19+
20+
```python
21+
from ontograph.client import ClientOntology
22+
from ontograph.downloader import DownloadManagerAdapter
23+
from ontograph.config.settings import DEFAULT_CACHE_DIR
24+
25+
client_ontology = ClientOntology(cache_dir="./data/out")
26+
27+
# Optional: use Download Manager for all remote downloads
28+
# downloader = DownloadManagerAdapter(cache_dir=DEFAULT_CACHE_DIR, backend="requests")
29+
# client_ontology = ClientOntology(cache_dir="./data/out", downloader=downloader)
30+
```
31+
1832
---
1933

2034
## API Reference

docs/reference/source/ontograph/downloader.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
This module provides interfaces and adapters for downloading ontology files from URLs and catalogs.
44

5+
## Default Downloader
6+
7+
OntoGraph selects a default downloader backend via `DEFAULT_DOWNLOADER` in
8+
`ontograph/config/settings.py`. You can also override this per client by
9+
passing a downloader adapter.
10+
11+
```python
12+
from ontograph.downloader import get_default_downloader
13+
from ontograph.config.settings import DEFAULT_CACHE_DIR
14+
15+
downloader = get_default_downloader(cache_dir=DEFAULT_CACHE_DIR)
16+
```
17+
518
---
619

720
## API Reference

ontograph/client.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,22 @@ class ClientCatalog:
7373
{'id': 'ado', 'title': "Alzheimer's Disease Ontology"}
7474
"""
7575

76-
def __init__(self, cache_dir: str = DEFAULT_CACHE_DIR) -> None:
76+
def __init__(
77+
self,
78+
cache_dir: str = DEFAULT_CACHE_DIR,
79+
downloader: DownloaderPort | None = None,
80+
) -> None:
7781
"""Initialize the ClientCatalog.
7882
7983
Args:
8084
cache_dir (str, optional): Directory for caching catalog data. Defaults to DEFAULT_CACHE_DIR.
85+
downloader (DownloaderPort | None, optional): Downloader adapter for remote resources. Defaults to None.
8186
"""
82-
self.__catalog_adapter = CatalogOntologies(cache_dir=Path(cache_dir))
87+
self.__catalog_adapter = CatalogOntologies(
88+
cache_dir=Path(cache_dir),
89+
downloader=downloader,
90+
)
91+
self._downloader = downloader
8392

8493
def load_catalog(self, force_download: bool = False) -> None:
8594
"""Load the ontology catalog.
@@ -92,7 +101,8 @@ def load_catalog(self, force_download: bool = False) -> None:
92101
>>> catalog.load_catalog()
93102
"""
94103
return self.__catalog_adapter.load_catalog(
95-
force_download=force_download
104+
force_download=force_download,
105+
downloader=self._downloader,
96106
)
97107

98108
def catalog_as_dict(self) -> dict:
@@ -218,14 +228,20 @@ class ClientOntology:
218228
[Term('Z', name='root')]
219229
"""
220230

221-
def __init__(self, cache_dir: str = DEFAULT_CACHE_DIR) -> None:
231+
def __init__(
232+
self,
233+
cache_dir: str = DEFAULT_CACHE_DIR,
234+
downloader: DownloaderPort | None = None,
235+
) -> None:
222236
"""Initialize the ClientOntology.
223237
224238
Args:
225239
cache_dir (str, optional): Directory for caching ontology data. Defaults to DEFAULT_CACHE_DIR.
240+
downloader (DownloaderPort | None, optional): Downloader adapter for remote resources. Defaults to None.
226241
"""
227242
self._cache_dir = Path(cache_dir)
228243
self._ontology = None
244+
self._downloader = downloader
229245
self._lookup_tables = None
230246
self._navigator = None
231247
self._relations = None
@@ -346,20 +362,28 @@ def load(
346362
>>> client.load(source="./tests/resources/dummy_ontology.obo")
347363
"""
348364
logger.info(f'Loading ontology from source: {source} ...')
349-
loader = ProntoLoaderAdapter(cache_dir=self._cache_dir)
365+
logger.debug(
366+
'Using downloader: %s',
367+
type(downloader).__name__ if downloader else 'default',
368+
)
369+
loader = ProntoLoaderAdapter(
370+
cache_dir=self._cache_dir, downloader=self._downloader
371+
)
350372

351373
path = Path(source)
352374
ontology = None
353375

354376
# 1. Case 1: Local file exists
355377
if path.exists():
378+
logger.debug('Resolved source type: file')
356379
logger.info(
357380
f'Found local file at {path}, loading with ProntoLoaderAdapter...'
358381
)
359382
ontology = loader.load_from_file(file_path_ontology=path)
360383

361384
# 2. Case 2: Provided source is a URL
362385
elif re.match(r'^https?://', source):
386+
logger.debug('Resolved source type: url')
363387
logger.info(
364388
f'Detected URL source, downloading ontology from {source}'
365389
)
@@ -368,7 +392,11 @@ def load(
368392

369393
# 3. Case 3: Try OBO catalog (if file missing or simple ID)
370394
else:
371-
catalog_client = ClientCatalog(cache_dir=self._cache_dir)
395+
logger.debug('Resolved source type: catalog')
396+
catalog_client = ClientCatalog(
397+
cache_dir=self._cache_dir,
398+
downloader=self._downloader,
399+
)
372400
catalog_client.load_catalog()
373401
available = [
374402
o['id'] for o in catalog_client.list_available_ontologies()
@@ -380,7 +408,9 @@ def load(
380408
f"Ontology '{name_id}' found in catalog, downloading..."
381409
)
382410
ontology = loader.load_from_catalog(
383-
name_id=name_id, format='obo'
411+
name_id=name_id,
412+
format='obo',
413+
downloader=self._downloader,
384414
)
385415
else:
386416
msg = f"Ontology '{source}' not found as file, URL, or catalog entry."

ontograph/config/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'PACKAGE_VERSION',
1212
'SUPPORTED_FORMATS_ONTOGRAPH',
1313
'DEFAULT_FORMAT_ONTOLOGY',
14+
'DEFAULT_DOWNLOADER',
1415
]
1516

1617
# Package metadata from installed package
@@ -31,4 +32,7 @@
3132
SUPPORTED_FORMATS_ONTOGRAPH = ['obo', 'owl']
3233
DEFAULT_FORMAT_ONTOLOGY = 'obo'
3334

35+
# Default downloader backend for remote resources ('pooch' or 'download_manager')
36+
DEFAULT_DOWNLOADER = 'pooch'
37+
3438
# TODO: Ready for improvement

0 commit comments

Comments
 (0)