ODGI-FFI
A safe, ergonomic, and high-performance Rust wrapper for the odgi pangenome graph tool.
Overview
odgi-ffi provides idiomatic Rust bindings to the powerful odgi C++ library. It allows Rust developers to safely and efficiently load, query, and manipulate pangenome variation graphs. The library handles the complexity of the C++/Rust boundary, offering a clean API that abstracts away unsafe FFI calls.
This crate is ideal for bioinformaticians and developers who want to build high-performance pangenome analysis tools in Rust while leveraging the mature, feature-rich odgi ecosystem.
Features
- Safe Graph Handling: Load
.odgifiles into a memory-safeGraphstruct. - Comprehensive Graph Queries:
- Get node counts, sequences, and lengths.
- List all path names within the graph.
- Traverse the graph by finding node successors and predecessors.
- Identify which paths step on a specific node.
- Coordinate Projection: Project nucleotide positions on a path to their corresponding graph node and offset.
- File Format Conversion: Includes utilities to convert between GFA and ODGI formats by leveraging the bundled
odgiexecutable. - Thread Safety: The
Graphobject isSend + Sync, allowing it to be safely shared across threads for parallel processing.
Getting Started
Prerequisites
This crate compiles the odgi C++ library from source. To successfully build odgi-ffi, you must have a C++ build environment installed on your system. This includes:
- A C++17 compliant compiler (e.g.,
g++orclang++) CMake(version 3.10 or higher)make
On Debian/Ubuntu, you can install these with:
&&
Installation
Add odgi-ffi as a dependency in your Cargo.toml file:
[]
= "1.0.8"
Ask the AI
The combined_file.txt file contains all the source code contained in the /src folder. Just copy paste the entire contents of this file in your favorite LLM and ask questions like "Explain how this system works." or "How can I make an interactive pangenome visualization tool using this crate?" etc.
Usage
The primary entry point of this library is the Graph struct. The typical workflow involves loading an .odgi file and then using the Graph methods to inspect it.
If your data is in GFA format, you can first convert it using the provided gfa_to_odgi function.
Complete Example
Here is a complete example that demonstrates converting a GFA file, loading the resulting ODGI graph, and performing several queries.
use ;
use Write;
use NamedTempFile;
API Reference
The core of the library is the odgi_ffi::Graph struct. Below is a summary of its main methods. For detailed information, please refer to the official documentation on docs.rs.
| Method | Description |
|---|---|
Graph::load(path) |
Loads an ODGI graph from a file. |
node_count() |
Returns the total number of nodes in the graph. |
get_path_names() |
Returns a list of all path names. |
get_node_sequence(id) |
Gets the DNA sequence for a given node ID. |
get_node_len(id) |
Gets the length of the sequence for a given node ID. |
project(path, pos) |
Projects a linear coordinate on a path to graph coordinates. |
get_successors(id) |
Gets all successor edges for a given node. |
get_predecessors(id) |
Gets all predecessor edges for a given node. |
get_paths_on_node(id) |
Gets the names of all paths that step on a given node. |
Conversion Utilities
gfa_to_odgi(gfa_path, odgi_path): Converts a GFA file to an ODGI file.odgi_to_gfa(odgi_path, gfa_path): Converts an ODGI file back to a GFA file.
Building from Source
To build the project locally, clone the repository and use Cargo. Make sure you have the prerequisites installed.
# Clone the repository
# Build the project
# Run tests
The build script (build.rs) automatically handles the following:
- Copies the vendored
odgiC++ source code to a temporary build directory. - Invokes
cmaketo compileodgiand its dependencies. - Makes the
odgiexecutable path available as an environment variable (ODGI_EXE). - Configures Cargo to link against the compiled static libraries.
- Builds the C++ FFI wrapper code using
cxx.
Contributing
Contributions are welcome! If you find a bug, have a feature request, or want to contribute code, please open an issue or a pull request on our GitHub repository.
License
This project is licensed under the MIT License.