From 79b64858dad1bf6089894d2e7581f03bf777454a Mon Sep 17 00:00:00 2001 From: Landung 'Don' Setiawan Date: Fri, 20 Dec 2024 10:24:02 -0800 Subject: [PATCH] refactor: Change 'from_newick' to 'to_vector' --- phylo2vec/src/tree_vec/ops/mod.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/phylo2vec/src/tree_vec/ops/mod.rs b/phylo2vec/src/tree_vec/ops/mod.rs index 212479e..2d9d8f0 100644 --- a/phylo2vec/src/tree_vec/ops/mod.rs +++ b/phylo2vec/src/tree_vec/ops/mod.rs @@ -18,18 +18,15 @@ pub fn to_newick(v: &Vec) -> String { } /// Recover a Phylo2Vec vector from a rooted tree (in Newick format) -pub fn from_newick(newick: &str, has_parents: bool) -> Vec { +pub fn to_vector(newick: &str) -> Vec { let mut ancestry: Ancestry; - match has_parents { - true => { - ancestry = newick::get_cherries(newick); - order_cherries(&mut ancestry); - } - false => { - ancestry = newick::get_cherries_no_parents(newick); - order_cherries_no_parents(&mut ancestry); - } + if newick::has_parents(&newick) { + ancestry = newick::get_cherries(newick); + order_cherries(&mut ancestry); + } else { + ancestry = newick::get_cherries_no_parents(newick); + order_cherries_no_parents(&mut ancestry); } return build_vector(&ancestry); @@ -59,8 +56,8 @@ mod tests { #[case(vec![0, 0, 0, 1, 3], "(((0,(3,5)6)8,2)9,(1,4)7)10;")] #[case(vec![0, 1, 2, 3, 4], "(0,(1,(2,(3,(4,5)6)7)8)9)10;")] #[case(vec![0, 0, 1], "((0,2)5,(1,3)4)6;")] - fn test_from_newick(#[case] expected: Vec, #[case] newick: &str) { - let vector = from_newick(&newick, true); + fn test_to_vector(#[case] expected: Vec, #[case] newick: &str) { + let vector = to_vector(&newick); assert_eq!(vector, expected); } @@ -71,8 +68,8 @@ mod tests { #[case(vec![0, 0, 0, 1, 3], "(((0,(3,5)),2),(1,4));")] #[case(vec![0, 1, 2, 3, 4], "(0,(1,(2,(3,(4,5)))));")] #[case(vec![0, 0, 1], "((0,2),(1,3));")] - fn test_from_newick_no_parents(#[case] expected: Vec, #[case] newick: &str) { - let vector = from_newick(&newick, false); + fn test_to_vector_no_parents(#[case] expected: Vec, #[case] newick: &str) { + let vector = to_vector(&newick); assert_eq!(vector, expected); } }