Ontology-Registry

March 26, 2026 ยท View on GitHub

Crates.io Docs.rs License RobisonGroup

A robust, thread-safe Rust library for managing the lifecycle of biological ontologies.

ontology-registry automates the process of resolving, downloading, and caching ontology files (JSON, OBO, OWL). It acts as a centralized local registry, ensuring that your applications always have access to the data they need without redundant network requests or race conditions.


โœจ Features

  • ๐Ÿš€ Smart Caching: Automatically downloads ontologies from the OBO Foundry and persists them locally. Subsequent requests load instantly from the disk.
  • ๐Ÿ”„ Version Resolution: resolving Version::Latest automatically queries BioRegistry.io to find the most recent semantic version.
  • ๐Ÿ›ก๏ธ Thread-Safe & Atomic: Built for concurrency. It uses Mutex locks and atomic file writes (downloading to .tmp first) to ensure you never read a corrupted or partially downloaded file.
  • ๐Ÿ”Œ Modular Architecture: The logic is split into MetadataProviding, OntologyProviding, and Registration traits, allowing you to swap out backends if needed.
  • ๐Ÿ“‚ Multiple Formats: First-class support for .json, .obo, and .owl formats.

๐Ÿ“ฆ Installation

Add this to your Cargo.toml:

cargo add ontology-registry

Quick Start

use ontology_registry::blocking::bio_registry_metadata_provider::BioRegistryMetadataProvider;
use ontology_registry::blocking::file_system_ontology_registry::FileSystemOntologyRegistry;
use ontology_registry::blocking::obolib_ontology_provider::OboLibraryProvider;
use ontology_registry::enums::{FileType, Version, SupportedOntology};
use ontology_registry::traits::OntologyRegistration;
use std::path::PathBuf;
use std::io::Read;
use ontology_registry::RegistryKey;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1. Setup the registry with standard providers
    // In a real app, use a persistent path like "~/.cache/ontologies"
    let cache_dir = PathBuf::from("./local_ontology_cache");
    let registry = FileSystemOntologyRegistry::new(
        cache_dir,
        BioRegistryMetadataProvider::default(),
        OboLibraryProvider::default(),
    );

    // 2. Register (Download & Cache)
    // This resolves 'Latest' to a specific date (e.g., "2024-01-01")
    let reg_key = RegistryKey::new(
        SupportedOntology::MONDO, // This can also just be a string "mondo"
        Version::Latest,
        FileType::Obo
    );

    let mut reader = registry.register(reg_key)?;

    // 3. Read the content
    let mut content = String::new();
    reader.read_to_string(&mut content)?;

    println!("Successfully loaded Mondo Ontology ({} bytes)", content.len());
    Ok(())
}

Pinning a Specific Version

If you need reproducibility, you can request a specific version string.

use ontology_registry::enums::{FileType, Version};
use ontology_registry::blocking::file_system_ontology_registry::FileSystemOntologyRegistry;
use ontology_registry::traits::OntologyRegistration;
use ontology_registry::RegistryKey;
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let registry = FileSystemOntologyRegistry::default();

    let version = Version::Declared("2023-01-01".to_string());
    let reg_key = RegistryKey::new(
        "go",
        version,
        FileType::Owl
    );
    let reader = registry.register(reg_key)?;
}

๐Ÿ“‚ Supported Formats

Enum VariantExtensionDescription
FileType::Json.jsonOBO Graph JSON format
FileType::Obo.oboStandard OBO flat file
FileType::Owl.owlWeb Ontology Language format