Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ jobs:
- name: run JSON metadata example
run: |
cargo run --example json_metadata --features derive
- name: run bincode metadata example
run: |
cargo run --example bincode_metadata --features derive
- name: setup Python and run tests
run: |
uv venv -p ${{ matrix.python }}
source .venv/bin/activate
uv pip install -r python/requirements_locked_3_13.txt
uv pip install python/tskit_glue
python -m pytest python

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ name = "tree_traversals"
[[example]]
name = "json_metadata"
required-features = ["derive"]

[[example]]
name = "bincode_metadata"
required-features = ["derive"]
51 changes: 51 additions & 0 deletions examples/bincode_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#[derive(serde::Serialize, serde::Deserialize, tskit::metadata::MutationMetadata)]
#[serializer("bincode")]
struct MutationMetadata {
effect_size: f64,
dominance: f64,
}

#[derive(serde::Serialize, serde::Deserialize, tskit::metadata::IndividualMetadata)]
#[serializer("bincode")]
struct IndividualMetadata {
name: String,
phenotypes: Vec<i32>,
}

fn main() {
let ts = make_treeseq().unwrap();
ts.dump("with_bincode_metadata.trees", 0).unwrap();
}

fn make_tables() -> anyhow::Result<tskit::TableCollection> {
let mut tables = tskit::TableCollection::new(100.0)?;
let pop0 = tables.add_population()?;
let ind0 = tables.add_individual_with_metadata(
0,
None,
None,
&IndividualMetadata {
name: "Jerome".to_string(),
phenotypes: vec![0, 1, 2, 0],
},
)?;
let node0 = tables.add_node(tskit::NodeFlags::new_sample(), 0.0, pop0, ind0)?;
let site0 = tables.add_site(50.0, Some("A".as_bytes()))?;
let _ = tables.add_mutation_with_metadata(
site0,
node0,
tskit::MutationId::NULL,
1.0,
Some("G".as_bytes()),
&MutationMetadata {
effect_size: -1e-3,
dominance: 0.1,
},
)?;
tables.build_index()?;
Ok(tables)
}

fn make_treeseq() -> anyhow::Result<tskit::TreeSequence> {
Ok(make_tables()?.tree_sequence(0)?)
}
1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tskit>=0.6.3
pytest
maturin
2 changes: 2 additions & 0 deletions python/requirements_locked_3_13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jsonschema==4.23.0
# via tskit
jsonschema-specifications==2025.4.1
# via jsonschema
maturin==1.8.6
# via -r requirements.txt
numpy==2.2.5
# via tskit
packaging==25.0
Expand Down
28 changes: 28 additions & 0 deletions python/test_bincode_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import tskit
import tskit_glue
import numpy as np


def setup_ts_without_schema():
ts = tskit.TreeSequence.load("with_bincode_metadata.trees")
return ts


def test_individual_metadata():
# NOTE: the assertions here rely on knowing
# what examples/json_metadata.rs put into the
# metadata!
ts = setup_ts_without_schema()
md = tskit_glue.decode_bincode_individual_metadata(ts.individual(0).metadata)
assert md.name() == "Jerome"
assert md.phenotypes() == [0, 1, 2, 0]


def test_mutation_metadata():
# NOTE: the assertions here rely on knowing
# what examples/json_metadata.rs put into the
# metadata!
ts = setup_ts_without_schema()
md = tskit_glue.decode_bincode_mutation_metadata(ts.mutation(0).metadata)
assert np.isclose(md.effect_size(), -1e-3)
assert np.isclose(md.dominance(), 0.1)
202 changes: 202 additions & 0 deletions python/tskit_glue/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions python/tskit_glue/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "tskit_glue"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "tskit_glue"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.24.0"
serde = {version = "1.0.203", features = ["derive"]}
bincode = {version = "1.3.1"}
15 changes: 15 additions & 0 deletions python/tskit_glue/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[build-system]
requires = ["maturin>=1.8,<2.0"]
build-backend = "maturin"

[project]
name = "tskit_glue"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dynamic = ["version"]
[tool.maturin]
features = ["pyo3/extension-module"]
Loading
Loading