Skip to content

Commit

Permalink
add id field to table row types. Closes #76 (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen authored Apr 17, 2021
1 parent 346c804 commit ae20376
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 46 deletions.
5 changes: 4 additions & 1 deletion src/edge_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{tsk_id_t, tsk_size_t, TskitError};

/// Row of an [`EdgeTable`]
pub struct EdgeTableRow {
pub id: tsk_id_t,
pub left: f64,
pub right: f64,
pub parent: tsk_id_t,
Expand All @@ -13,7 +14,8 @@ pub struct EdgeTableRow {

impl PartialEq for EdgeTableRow {
fn eq(&self, other: &Self) -> bool {
self.parent == other.parent
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::f64_partial_cmp_equal(&self.left, &other.left)
&& crate::util::f64_partial_cmp_equal(&self.right, &other.right)
Expand All @@ -28,6 +30,7 @@ fn make_edge_table_row(
) -> Option<EdgeTableRow> {
if pos < table.num_rows() as tsk_id_t {
let rv = EdgeTableRow {
id: pos,
left: table.left(pos).unwrap(),
right: table.right(pos).unwrap(),
parent: table.parent(pos).unwrap(),
Expand Down
7 changes: 5 additions & 2 deletions src/individual_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ use crate::{tsk_flags_t, tsk_id_t, tsk_size_t, TskitError};

/// Row of a [`IndividualTable`]
pub struct IndividualTableRow {
pub flags: u32,
pub id: tsk_id_t,
pub flags: tsk_flags_t,
pub location: Option<Vec<f64>>,
pub parents: Option<Vec<tsk_id_t>>,
pub metadata: Option<Vec<u8>>,
}

impl PartialEq for IndividualTableRow {
fn eq(&self, other: &Self) -> bool {
self.flags == other.flags
self.id == other.id
&& self.flags == other.flags
&& self.parents == other.parents
&& self.metadata == other.metadata
&& match &self.location {
Expand Down Expand Up @@ -52,6 +54,7 @@ fn make_individual_table_row(
) -> Option<IndividualTableRow> {
if pos < table.num_rows() as tsk_id_t {
let rv = IndividualTableRow {
id: pos,
flags: table.flags(pos).unwrap(),
location: table.location(pos).unwrap(),
parents: table.parents(pos).unwrap(),
Expand Down
5 changes: 4 additions & 1 deletion src/migration_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{tsk_id_t, TskitError};

/// Row of a [`MigrationTable`]
pub struct MigrationTableRow {
pub id: tsk_id_t,
pub left: f64,
pub right: f64,
pub node: tsk_id_t,
Expand All @@ -15,7 +16,8 @@ pub struct MigrationTableRow {

impl PartialEq for MigrationTableRow {
fn eq(&self, other: &Self) -> bool {
self.node == other.node
self.id == other.id
&& self.node == other.node
&& self.source == other.source
&& self.dest == other.dest
&& crate::util::f64_partial_cmp_equal(&self.left, &other.left)
Expand All @@ -32,6 +34,7 @@ fn make_migration_table_row(
) -> Option<MigrationTableRow> {
if pos < table.num_rows() as tsk_id_t {
Some(MigrationTableRow {
id: pos,
left: table.left(pos).unwrap(),
right: table.right(pos).unwrap(),
node: table.node(pos).unwrap(),
Expand Down
5 changes: 4 additions & 1 deletion src/mutation_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{tsk_id_t, tsk_size_t, TskitError};

/// Row of a [`MutationTable`]
pub struct MutationTableRow {
pub id: tsk_id_t,
pub site: tsk_id_t,
pub node: tsk_id_t,
pub parent: tsk_id_t,
Expand All @@ -14,7 +15,8 @@ pub struct MutationTableRow {

impl PartialEq for MutationTableRow {
fn eq(&self, other: &Self) -> bool {
self.site == other.site
self.id == other.id
&& self.site == other.site
&& self.node == other.node
&& self.parent == other.parent
&& crate::util::f64_partial_cmp_equal(&self.time, &other.time)
Expand All @@ -30,6 +32,7 @@ fn make_mutation_table_row(
) -> Option<MutationTableRow> {
if pos < table.num_rows() as tsk_id_t {
let rv = MutationTableRow {
id: pos,
site: table.site(pos).unwrap(),
node: table.node(pos).unwrap(),
parent: table.parent(pos).unwrap(),
Expand Down
5 changes: 4 additions & 1 deletion src/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{tsk_flags_t, tsk_id_t, TskitError};

/// Row of a [`NodeTable`]
pub struct NodeTableRow {
pub id: tsk_id_t,
pub time: f64,
pub flags: tsk_flags_t,
pub population: tsk_id_t,
Expand All @@ -13,7 +14,8 @@ pub struct NodeTableRow {

impl PartialEq for NodeTableRow {
fn eq(&self, other: &Self) -> bool {
self.flags == other.flags
self.id == other.id
&& self.flags == other.flags
&& self.population == other.population
&& self.individual == other.individual
&& crate::util::f64_partial_cmp_equal(&self.time, &other.time)
Expand All @@ -28,6 +30,7 @@ fn make_node_table_row(
) -> Option<NodeTableRow> {
if pos < table.num_rows() as tsk_id_t {
Some(NodeTableRow {
id: pos,
time: table.time(pos).unwrap(),
flags: table.flags(pos).unwrap(),
population: table.population(pos).unwrap(),
Expand Down
4 changes: 3 additions & 1 deletion src/population_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use crate::{tsk_id_t, tsk_size_t};
/// Row of a [`PopulationTable`]
#[derive(Eq)]
pub struct PopulationTableRow {
pub id: tsk_id_t,
pub metadata: Option<Vec<u8>>,
}

impl PartialEq for PopulationTableRow {
fn eq(&self, other: &Self) -> bool {
self.metadata == other.metadata
self.id == other.id && self.metadata == other.metadata
}
}

Expand All @@ -22,6 +23,7 @@ fn make_population_table_row(
) -> Option<PopulationTableRow> {
if pos < table.num_rows() as tsk_id_t {
let rv = PopulationTableRow {
id: pos,
metadata: match decode_metadata {
true => match metadata_to_vector!(table, pos).unwrap() {
Some(x) => Some(x),
Expand Down
5 changes: 4 additions & 1 deletion src/site_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use crate::{tsk_id_t, tsk_size_t};

/// Row of a [`SiteTable`]
pub struct SiteTableRow {
pub id: tsk_id_t,
pub position: f64,
pub ancestral_state: Option<Vec<u8>>,
pub metadata: Option<Vec<u8>>,
}

impl PartialEq for SiteTableRow {
fn eq(&self, other: &Self) -> bool {
crate::util::f64_partial_cmp_equal(&self.position, &other.position)
self.id == other.id
&& crate::util::f64_partial_cmp_equal(&self.position, &other.position)
&& self.ancestral_state == other.ancestral_state
&& self.metadata == other.metadata
}
Expand All @@ -25,6 +27,7 @@ fn make_site_table_row(
) -> Option<SiteTableRow> {
if pos < table.num_rows() as tsk_id_t {
let rv = SiteTableRow {
id: pos,
position: table.position(pos).unwrap(),
ancestral_state: table.ancestral_state(pos).unwrap(),
metadata: match decode_metadata {
Expand Down
60 changes: 22 additions & 38 deletions src/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,20 +631,11 @@ mod test {
assert!(row.metadata.is_none());
}

for (i, row) in tables.nodes_iter(true).enumerate() {
assert!(close_enough(
tables.nodes().time(i as tsk_id_t).unwrap(),
row.time
));
assert_eq!(tables.nodes().flags(i as tsk_id_t).unwrap(), row.flags);
assert_eq!(
tables.nodes().population(i as tsk_id_t).unwrap(),
row.population
);
assert_eq!(
tables.nodes().individual(i as tsk_id_t).unwrap(),
row.individual
);
for row in tables.nodes_iter(true) {
assert!(close_enough(tables.nodes().time(row.id).unwrap(), row.time));
assert_eq!(tables.nodes().flags(row.id).unwrap(), row.flags);
assert_eq!(tables.nodes().population(row.id).unwrap(), row.population);
assert_eq!(tables.nodes().individual(row.id).unwrap(), row.individual);
assert!(row.metadata.is_none());
}
}
Expand All @@ -665,17 +656,14 @@ mod test {
assert_eq!(tables.edges().child(i as tsk_id_t).unwrap(), row.child);
assert!(row.metadata.is_none());
}
for (i, row) in tables.edges_iter(true).enumerate() {
for row in tables.edges_iter(true) {
assert!(close_enough(tables.edges().left(row.id).unwrap(), row.left));
assert!(close_enough(
tables.edges().left(i as tsk_id_t).unwrap(),
row.left
));
assert!(close_enough(
tables.edges().right(i as tsk_id_t).unwrap(),
tables.edges().right(row.id).unwrap(),
row.right
));
assert_eq!(tables.edges().parent(i as tsk_id_t).unwrap(), row.parent);
assert_eq!(tables.edges().child(i as tsk_id_t).unwrap(), row.child);
assert_eq!(tables.edges().parent(row.id).unwrap(), row.parent);
assert_eq!(tables.edges().child(row.id).unwrap(), row.child);
assert!(row.metadata.is_none());
}
}
Expand Down Expand Up @@ -728,15 +716,12 @@ mod test {
}
assert_eq!(no_anc_state, 1);
no_anc_state = 0;
for (i, row) in tables.sites_iter(true).enumerate() {
assert!(close_enough(
sites.position(i as tsk_id_t).unwrap(),
row.position
));
for row in tables.sites_iter(true) {
assert!(close_enough(sites.position(row.id).unwrap(), row.position));
if row.ancestral_state.is_some() {
if i == 0 {
if row.id == 0 {
assert_eq!(row.ancestral_state.unwrap(), b"Eggnog");
} else if i == 2 {
} else if row.id == 2 {
assert_eq!(row.ancestral_state.unwrap(), longer_metadata.as_bytes());
}
} else {
Expand Down Expand Up @@ -803,16 +788,13 @@ mod test {
assert_eq!(nmuts, 3);

nmuts = 0;
for (i, row) in tables.mutations_iter(true).enumerate() {
assert_eq!(row.site, tables.mutations().site(i as tsk_id_t).unwrap());
assert_eq!(row.node, tables.mutations().node(i as tsk_id_t).unwrap());
assert_eq!(
row.parent,
tables.mutations().parent(i as tsk_id_t).unwrap()
);
for row in tables.mutations_iter(true) {
assert_eq!(row.site, tables.mutations().site(row.id).unwrap());
assert_eq!(row.node, tables.mutations().node(row.id).unwrap());
assert_eq!(row.parent, tables.mutations().parent(row.id).unwrap());
assert!(close_enough(
row.time,
tables.mutations().time(i as tsk_id_t).unwrap()
tables.mutations().time(row.id).unwrap()
));
assert!(row.metadata.is_none());
nmuts += 1;
Expand Down Expand Up @@ -981,6 +963,7 @@ mod test {
fn test_edge_table_row_equality() {
let tables = make_small_table_collection();
for (i, row) in tables.edges_iter(true).enumerate() {
assert!(row.id == i as tsk_id_t);
assert!(row == tables.edges().row(i as tsk_id_t, true).unwrap());
assert!(!(row != tables.edges().row(i as tsk_id_t, true).unwrap()));
if i > 0 {
Expand All @@ -993,11 +976,12 @@ mod test {
fn test_node_table_row_equality() {
let tables = make_small_table_collection();
for (i, row) in tables.nodes_iter(true).enumerate() {
assert!(row.id == i as tsk_id_t);
assert!(row == tables.nodes().row(i as tsk_id_t, true).unwrap());
assert!(!(row != tables.nodes().row(i as tsk_id_t, true).unwrap()));
}
assert!(tables.nodes().row(0, true) != tables.nodes().row(1, true));
assert!(tables.nodes().row(1, true) == tables.nodes().row(2, true));
assert!(tables.nodes().row(1, true) != tables.nodes().row(2, true));
}

#[test]
Expand Down

0 comments on commit ae20376

Please sign in to comment.