From a8db4b0586d77d907f8ae1f67208b28c1f28eeee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=ABlija=20Pe=C4=8Derska?= Date: Thu, 16 Nov 2023 20:20:11 +0100 Subject: [PATCH] Test get_idx_by_id and fix panic when not found Added a test for the det_idx_by_id function in the tree. Changed function signature to return Result with the idx if the id exists and an Error otherwise. --- phylo/src/pip_model/pip_model_tests.rs | 44 +++++++++++++------------- phylo/src/tree/mod.rs | 8 ++--- phylo/src/tree/tree_tests.rs | 24 ++++++++++++++ 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/phylo/src/pip_model/pip_model_tests.rs b/phylo/src/pip_model/pip_model_tests.rs index f170a00..679bea6 100644 --- a/phylo/src/pip_model/pip_model_tests.rs +++ b/phylo/src/pip_model/pip_model_tests.rs @@ -395,42 +395,42 @@ fn pip_hky_likelihood_example_leaf_values() { }; let iota = 0.133; let beta = 0.787; - cost.set_leaf_values(cost.info.tree.get_idx_by_id("A").into()); + cost.set_leaf_values(cost.info.tree.get_idx_by_id("A").unwrap().into()); assert_values( &cost, - cost.info.tree.get_idx_by_id("A").into(), + cost.info.tree.get_idx_by_id("A").unwrap().into(), iota, beta, &[[0.0, 1.0, 0.0, 0.0], [0.0; 4], [0.0; 4]].concat(), &[0.0, 0.33, 0.0, 0.0], &[0.0, 0.33 * iota * beta, 0.0, 0.0], ); - cost.set_leaf_values(cost.info.tree.get_idx_by_id("B").into()); + cost.set_leaf_values(cost.info.tree.get_idx_by_id("B").unwrap().into()); assert_values( &cost, - cost.info.tree.get_idx_by_id("B").into(), + cost.info.tree.get_idx_by_id("B").unwrap().into(), iota, beta, &[[1.0, 1.0, 0.0, 0.0], [0.0; 4], [0.0; 4]].concat(), &[0.26, 0.33, 0.0, 0.0], &[0.26 * iota * beta, 0.33 * iota * beta, 0.0, 0.0], ); - cost.set_leaf_values(cost.info.tree.get_idx_by_id("C").into()); + cost.set_leaf_values(cost.info.tree.get_idx_by_id("C").unwrap().into()); let iota = 0.067; let beta = 0.885; assert_values( &cost, - cost.info.tree.get_idx_by_id("C").into(), + cost.info.tree.get_idx_by_id("C").unwrap().into(), iota, beta, &[[0.0, 1.0, 0.0, 1.0], [0.0; 4], [0.0; 4]].concat(), &[0.0, 0.33, 0.0, 0.19], &[0.0, 0.33 * iota * beta, 0.0, 0.19 * iota * beta], ); - cost.set_leaf_values(cost.info.tree.get_idx_by_id("D").into()); + cost.set_leaf_values(cost.info.tree.get_idx_by_id("D").unwrap().into()); assert_values( &cost, - cost.info.tree.get_idx_by_id("D").into(), + cost.info.tree.get_idx_by_id("D").unwrap().into(), iota, beta, &[[0.0, 1.0, 1.0, 1.0], [0.0; 4], [0.0; 4]].concat(), @@ -460,7 +460,7 @@ fn pip_hky_likelihood_example_internals() { } let iota = 0.133; let beta = 0.787; - let idx = usize::from(cost.info.tree.get_idx_by_id("E")); + let idx = usize::from(cost.info.tree.get_idx_by_id("E").unwrap()); cost.set_internal_values(idx); assert_values( &cost, @@ -476,7 +476,7 @@ fn pip_hky_likelihood_example_internals() { 0.0, ], ); - let idx = usize::from(cost.info.tree.get_idx_by_id("F")); + let idx = usize::from(cost.info.tree.get_idx_by_id("F").unwrap()); cost.set_internal_values(idx); let iota_f = 0.2; let beta_f = 0.704; @@ -501,7 +501,7 @@ fn pip_hky_likelihood_example_internals() { let beta = 1.0; let iota_e = 0.133; let beta_e = 0.787; - let idx = usize::from(cost.info.tree.get_idx_by_id("R")); + let idx = usize::from(cost.info.tree.get_idx_by_id("R").unwrap()); cost.set_root_values(idx); assert_values( &cost, @@ -561,39 +561,39 @@ fn pip_hky_likelihood_example_c0() { model, tmp: temp_values, }; - cost.set_leaf_values(cost.info.tree.get_idx_by_id("A").into()); + cost.set_leaf_values(cost.info.tree.get_idx_by_id("A").unwrap().into()); assert_c0_values( &cost, - cost.info.tree.get_idx_by_id("A").into(), + cost.info.tree.get_idx_by_id("A").unwrap().into(), &[0.0, 0.0, 0.0, 0.0, 1.0], 0.0, 0.028329, ); - cost.set_leaf_values(cost.info.tree.get_idx_by_id("B").into()); + cost.set_leaf_values(cost.info.tree.get_idx_by_id("B").unwrap().into()); assert_c0_values( &cost, - cost.info.tree.get_idx_by_id("B").into(), + cost.info.tree.get_idx_by_id("B").unwrap().into(), &[0.0, 0.0, 0.0, 0.0, 1.0], 0.0, 0.028329, ); - cost.set_leaf_values(cost.info.tree.get_idx_by_id("C").into()); + cost.set_leaf_values(cost.info.tree.get_idx_by_id("C").unwrap().into()); assert_c0_values( &cost, - cost.info.tree.get_idx_by_id("C").into(), + cost.info.tree.get_idx_by_id("C").unwrap().into(), &[0.0, 0.0, 0.0, 0.0, 1.0], 0.0, 0.007705, ); - cost.set_leaf_values(cost.info.tree.get_idx_by_id("D").into()); + cost.set_leaf_values(cost.info.tree.get_idx_by_id("D").unwrap().into()); assert_c0_values( &cost, - cost.info.tree.get_idx_by_id("D").into(), + cost.info.tree.get_idx_by_id("D").unwrap().into(), &[0.0, 0.0, 0.0, 0.0, 1.0], 0.0, 0.007705, ); - let idx = usize::from(cost.info.tree.get_idx_by_id("E")); + let idx = usize::from(cost.info.tree.get_idx_by_id("E").unwrap()); cost.set_internal_values(idx); assert_c0_values( &cost, @@ -602,7 +602,7 @@ fn pip_hky_likelihood_example_c0() { 0.154, 0.044448334 + 0.028329 * 2.0, ); - let idx = usize::from(cost.info.tree.get_idx_by_id("F")); + let idx = usize::from(cost.info.tree.get_idx_by_id("F").unwrap()); cost.set_internal_values(idx); assert_c0_values( &cost, @@ -611,7 +611,7 @@ fn pip_hky_likelihood_example_c0() { 0.0488, 0.06607104 + 0.007705 * 2.0, ); - let idx = usize::from(cost.info.tree.get_idx_by_id("R")); + let idx = usize::from(cost.info.tree.get_idx_by_id("R").unwrap()); cost.set_root_values(idx); assert_c0_values( &cost, diff --git a/phylo/src/tree/mod.rs b/phylo/src/tree/mod.rs index dca4fd7..dff8b49 100644 --- a/phylo/src/tree/mod.rs +++ b/phylo/src/tree/mod.rs @@ -227,17 +227,17 @@ impl Tree { lengths } - pub fn get_idx_by_id(&self, id: &str) -> NodeIdx { + pub fn get_idx_by_id(&self, id: &str) -> Result { debug_assert!(self.complete); let idx = self.leaves.iter().position(|node| node.id == id); if let Some(idx) = idx { - return NodeIdx::Leaf(idx); + return Ok(NodeIdx::Leaf(idx)); } let idx = self.internals.iter().position(|node| node.id == id); if let Some(idx) = idx { - return NodeIdx::Internal(idx); + return Ok(NodeIdx::Internal(idx)); } - panic!("No node with id {} found in the tree", id); + bail!("No node with id {} found in the tree", id); } } diff --git a/phylo/src/tree/tree_tests.rs b/phylo/src/tree/tree_tests.rs index c437221..baab873 100644 --- a/phylo/src/tree/tree_tests.rs +++ b/phylo/src/tree/tree_tests.rs @@ -34,6 +34,30 @@ fn setup_test_tree() -> Tree { tree } +#[test] +fn get_idx_by_id() { + let tree = from_newick_string(&String::from( + "(((A:1.0,B:1.0)E:2.0,C:1.0)F:1.0,D:1.0)G:2.0;", + )) + .unwrap() + .pop() + .unwrap(); + let nodes = [ + ("A", L(0)), + ("B", L(1)), + ("C", L(2)), + ("D", L(3)), + ("E", I(2)), + ("F", I(1)), + ("G", I(0)), + ]; + for (id, idx) in nodes.iter() { + assert!(tree.get_idx_by_id(id).is_ok()); + assert_eq!(tree.get_idx_by_id(id).unwrap(), *idx); + } + assert!(tree.get_idx_by_id("H").is_err()); +} + #[test] fn subroot_preorder() { let tree = setup_test_tree();