Crate odgi_ffi

Crate odgi_ffi 

Source
Expand description

A safe Rust interface to the odgi C++ library.

The odgi-ffi crate provides high-level, idiomatic Rust bindings for querying ODGI graphs. It handles the complexity of the C++ FFI boundary, providing a safe and easy-to-use API for Rust developers.

The primary entry point is the Graph struct, which represents a loaded ODGI graph in memory. This crate also provides utility functions for converting between GFA and ODGI file formats.

§Modules

  • [graph]: Contains the main Graph struct for querying graph data.
  • [conversion]: Provides functions like [gfa_to_odgi] for format conversion.

§Features

  • Load ODGI graphs from disk into a safe Rust wrapper.
  • Query graph properties, such as node count, path names, and node sequences.
  • Perform topological queries, such as finding node successors and predecessors.
  • Project path coordinates to their corresponding nodes and offsets.
  • Convert between GFA and ODGI formats using the bundled odgi executable.

§Example

Here’s a complete example of loading a graph and performing some basic queries.

use odgi_ffi::{Graph, gfa_to_odgi};
use tempfile::NamedTempFile;
use std::io::Write;

// Create a temporary GFA file for the example.
let mut gfa_file = NamedTempFile::new().unwrap();
writeln!(gfa_file, "H\tVN:Z:1.0").unwrap();
writeln!(gfa_file, "S\t1\tGATTACA").unwrap();
writeln!(gfa_file, "S\t2\tT").unwrap();
writeln!(gfa_file, "L\t1\t+\t2\t+\t0M").unwrap();
writeln!(gfa_file, "P\tx\t1+,2+\t*").unwrap();
let gfa_path = gfa_file.path();

// Create a path for the ODGI output file.
let odgi_file = NamedTempFile::new().unwrap();
let odgi_path = odgi_file.path();

// 1. Convert the GFA file to an ODGI file.
// This function is only available when not using the `docs-only` feature.
gfa_to_odgi(gfa_path.to_str().unwrap(), odgi_path.to_str().unwrap())
     .expect("Failed to convert GFA to ODGI");

// 2. Load the ODGI graph into memory.
let graph = Graph::load(odgi_path.to_str().unwrap())
     .expect("Failed to load ODGI graph");

// 3. Query the graph.
assert_eq!(graph.node_count(), 2);

let path_names = graph.get_path_names();
assert_eq!(path_names, vec!["x"]);

let seq = graph.get_node_sequence(1);
assert_eq!(seq, "GATTACA");

// Get path length using the new method.
let length = graph.get_path_length("x").unwrap();
assert_eq!(length, 8);

// Projecting position 7 on path "x" should land at the start of node 2.
let position = graph.project("x", 7).unwrap();
assert_eq!(position.node_id, 2);
assert_eq!(position.offset, 0);

Structs§

Edge
Error
A custom error type for operations within the odgi-ffi crate.
Graph
A safe, idiomatic Rust wrapper around a C++ odgi::graph_t object.
PathPosition