2 releases

new 0.2.2-post1 Apr 16, 2025
0.2.2-post0 Mar 17, 2025

#200 in Biology

Download history 85/week @ 2025-03-14 20/week @ 2025-03-21 102/week @ 2025-04-11

126 downloads per month

Custom license

245KB
5K SLoC

Continuous Integration Documentation Crate

Rust bindings for Phenopacket Schema

See the documentation on docs.rs for usage examples and more info.


lib.rs:

Rust bindings for Phenopacket Schema.

The crate contains Rust structs and enums generated from Phenopacket Schema protobuf descriptors. Phenopacket Schema versions v1 and v2 are supported.

Examples

Create a Phenopacket Schema element programmatically

Any struct or enum of the schema can be created by invoking its initializer. For instance, an schema::v2::core::OntologyClass:

use phenopackets::schema::v2::core::OntologyClass;

let seizure = OntologyClass {
  id: "HP:0001250".into(),
  label: "Seizure".into(),
};

assert_eq!(&seizure.id, "HP:0001250");
assert_eq!(&seizure.label, "Seizure");

JSON

Phenopacket Schema elements can be read from or written into JSON format. The functionality is gated by the serde feature which derives Serde's Serialize and Deserialize traits for all Phenopacket Schema components and enables interoperability with Serde data formats, such as JSON.

The following must be added into your Cargo.toml file to read a phenopacket from a JSON file. The serde feature must be enabled for phenopackets and serde_json must be added:

phenopackets = { version = "*", features = ["serde"]}
serde_json = "1.0.140"

Then, a phenopacket can be read from a JSON file:

use phenopackets::schema::v2::Phenopacket;

// An example phenopacket
let path = "data/v2/phenopacket.json";
let bytes = std::fs::read(path).expect("Reading should succeed");

let pp: Phenopacket = serde_json::from_reader(&bytes[..]).expect("Expecting no decoding issues");

assert_eq!(pp.id, "comprehensive-phenopacket-id");

Protobuf wire format

The schema elements can be encoded into or decoded from bytes:

use phenopackets::schema::v2::core::OntologyClass;
use prost::Message;

let seizure = OntologyClass {
  id: "HP:0001250".into(),
  label: "Seizure".into(),
};

// Encode the component into bytes ...
let bytes: Vec<u8> = seizure.encode_to_vec();

// ... and decode them back.
let decoded = OntologyClass::decode(&bytes[..]).unwrap();

assert_eq!(seizure, decoded);

Feature flags

Phenopackets uses feature flags to enable adding optional functionality.

No features are turned on by default.

Optional feature flags

The following features customize Phenopackets' behavior:

  • serde: Enables interoperability with serde to support encoding into or decoding from JSON format.

Dependencies

~0.4–2.4MB
~36K SLoC