Skip to content

ontometer_corpus.steps.fetch_ontologies

Ontology bulk fetcher (orchestrator).

About

Thin orchestrator that drives the per-source fetcher modules in ontometer_corpus.fetchers and writes results to a consolidated manifest.jsonl in the corpus directory.

Raw files go into the content-addressed object store under <store>/objects/. Every download attempt, successful or not, produces one FetchRecord (see ontometer_corpus.fetchers._base) that is appended to manifest.jsonl (one JSON object per line) with status ok or fail. Failures are also written to fetch_failures.log in the corpus directory. Source_ids already captured with status ok are skipped on a re-run; failed ones are retried.

--sources selects the non-OntoPortal fetchers (LOV, OBO, OKG, Ontohub); --ontoportal selects OntoPortal Alliance instance(s). Independent instances can be run concurrently as separate processes, since each has its own host and rate limit.

API keys are read from the environment only, one variable per OntoPortal instance (BIOPORTAL, AGROPORTAL, …); an instance without its key is skipped.

Usage

ontometer-corpus fetch --store /path/to/store --sources all ontometer-corpus fetch --store /path/to/store --ontoportal BioPortal ONTOMETER_STORE=/path/to/store ontometer-corpus fetch --sources lov,okg

Attributes

ONTOPORTAL_INSTANCES = [OntoPortalInstance('BioPortal', 'bp', 'https://data.bioontology.org', os.environ.get('BIOPORTAL', '')), OntoPortalInstance('AgroPortal', 'agroportal', 'https://data.agroportal.eu', os.environ.get('AGROPORTAL', '')), OntoPortalInstance('BiodivPortal', 'biodivportal', 'https://data.biodivportal.gfbio.org', os.environ.get('BIODIVPORTAL', '')), OntoPortalInstance('MatPortal', 'matportal', 'https://rest.matportal.org', os.environ.get('MATPORTAL', '')), OntoPortalInstance('OntoPortal-Astro', 'ontoportalastro', 'https://data.ontoportal-astro.eu', os.environ.get('ONTOPORTAL_ASTRO', '')), OntoPortalInstance('TechnoPortal', 'technoportal', 'https://data.technoportal.hevs.ch', os.environ.get('TECHNOPORTAL', ''))] module-attribute

SOURCE_NAMES = ('lov', 'obo', 'okg', 'ontohub') module-attribute

Classes

Functions:

load_captured_source_ids(manifest_path)

Return the set of source_ids already captured (status 'ok') in a previous run, so they can be skipped. Failed attempts are NOT included — they are retried on the next run.

Source code in ontometer_corpus/steps/fetch_ontologies.py
def load_captured_source_ids(manifest_path: Path) -> set[str]:
    """
    Return the set of source_ids already captured (status 'ok') in a previous
    run, so they can be skipped. Failed attempts are NOT included — they are
    retried on the next run.
    """
    done: set[str] = set()
    if not manifest_path.exists():
        return done
    for rec in iter_jsonl(manifest_path, strict=False):
        if rec.get("status", "ok") == "ok" and rec.get("source_id"):
            done.add(rec["source_id"])
    return done

build_source_fetchers(fail_log, objects_dir, manifest)

Bind every non-OntoPortal fetcher to this run's resolved paths.

Returns a mapping from source name to a callable taking (session, resume) and returning list[FetchRecord], so main() can dispatch every selected source the same way.

Source code in ontometer_corpus/steps/fetch_ontologies.py
def build_source_fetchers(
    fail_log: Path, objects_dir: Path, manifest: Path
) -> dict[str, Callable[[requests.Session, set[str]], list[FetchRecord]]]:
    """Bind every non-OntoPortal fetcher to this run's resolved paths.

    Returns a mapping from source name to a callable taking
    ``(session, resume)`` and returning ``list[FetchRecord]``, so ``main()``
    can dispatch every selected source the same way.
    """
    return {
        "lov": lambda s, r: fetch_lov(s, fail_log, objects_dir, manifest, r),
        "obo": lambda s, r: fetch_obo(s, fail_log, objects_dir, manifest, r),
        "okg": lambda s, r: fetch_okg(s, fail_log, objects_dir, manifest, r),
        "ontohub": lambda s, r: fetch_ontohub(s, fail_log, objects_dir, manifest, r),
    }

main(sources, ontoportal, paths)

Fetch ontologies from the registries into the object store and the manifest.

Each OntoPortal instance is an independent host with its own rate limit and API key, so instances can run as parallel processes. All of them append to the same manifest.jsonl, which is safe: each appends only its own records.

 ontometer-corpus fetch --sources lov,okg --ontoportal BioPortal ontometer-corpus fetch --sources all ontometer-corpus fetch --ontoportal AgroPortal &

Source code in ontometer_corpus/steps/fetch_ontologies.py
@click.command()
@click.option(
    "--sources",
    metavar="SOURCE[,SOURCE...]",
    multiple=True,
    help=(
        f"Non-OntoPortal sources to fetch: {', '.join(sorted(SOURCE_NAMES))}, or 'all'. "
        "Comma-separated and/or repeatable. If omitted, none of them runs."
    ),
)
@click.option(
    "--ontoportal",
    metavar="INSTANCE",
    default=None,
    help=(
        "OntoPortal Alliance instance to fetch, matched case-insensitively against name or "
        f"slug: {', '.join(sorted(i.name for i in ONTOPORTAL_INSTANCES))}, or 'all' to run "
        "every configured instance in this process."
    ),
)
@corpus_paths
def main(sources: tuple[str, ...], ontoportal: str | None, paths: CorpusPaths) -> None:
    """Fetch ontologies from the registries into the object store and the manifest.

    Each OntoPortal instance is an independent host with its own rate limit and
    API key, so instances can run as parallel processes. All of them append to
    the same manifest.jsonl, which is safe: each appends only its own records.

    \b
    ontometer-corpus fetch --sources lov,okg --ontoportal BioPortal
    ontometer-corpus fetch --sources all
    ontometer-corpus fetch --ontoportal AgroPortal &
    """
    paths.ensure_corpus_dir()

    objects_dir = paths.objects_dir
    objects_dir.mkdir(parents=True, exist_ok=True)
    manifest = paths.manifest_path
    fail_log = paths.fail_log

    print("OntoMeter — ontology bulk fetcher")
    print(paths.describe())
    print(f"  object store : {objects_dir}")
    print(f"  failure log  : {fail_log}")

    session = make_session()
    total_records: list[FetchRecord] = []
    fetchers = build_source_fetchers(fail_log, objects_dir, manifest)

    # Skip source_ids already captured (status 'ok') on a previous run.
    resume = load_captured_source_ids(manifest)
    if resume:
        print(f"  resume       : {len(resume)} source_ids already captured — skipping")

    for name in _resolve_sources(sources):
        try:
            total_records.extend(fetchers[name](session, resume))
        except Exception as exc:
            print(f"  [err] {name} fetch aborted: {exc}")

    # OntoPortal Alliance instances each have their own host and rate limit, so
    # they can be run in parallel as separate processes. Each instance is
    # isolated: one erroring out does not stop the others.
    for instance in _resolve_ontoportal_instances(ontoportal):
        if not instance.api_key:
            print(f"  [skip] {instance.name}: no API key configured")
            continue
        try:
            total_records.extend(
                fetch_ontoportal(session, fail_log, objects_dir, manifest, instance, resume)
            )
        except Exception as exc:
            print(f"  [err] {instance.name} fetch aborted: {exc}")

    n_ok = sum(1 for r in total_records if r.status == "ok")
    n_fail = sum(1 for r in total_records if r.status == "fail")

    print(f"\n{'=' * 60}")
    print(f"Total attempts written to manifest: {len(total_records)}")
    print(f"  captured (ok): {n_ok}")
    print(f"  failed:        {n_fail}")
    print(f"  manifest : {manifest}")
    print(f"  failures : {fail_log}")
    print("\nNext: seed, parse and deduplicate —")
    print("  ontometer-corpus process")