forked from sbhattlab/phylo2vec
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Perform codebase refactor to separate appropriate functions
- Loading branch information
Showing
8 changed files
with
76 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
pub mod avl; | ||
pub mod newick; | ||
pub mod vector; | ||
|
||
#[allow(unused_imports)] | ||
use crate::tree_vec::types::Ancestry; | ||
|
||
pub use vector::{ | ||
build_newick, build_vector, find_coords_of_first_leaf, get_ancestry, get_pairs, get_pairs_avl, | ||
order_cherries, order_cherries_no_parents, to_newick, Ancestry, | ||
build_vector, find_coords_of_first_leaf, get_ancestry, get_pairs, get_pairs_avl, | ||
order_cherries, order_cherries_no_parents, | ||
}; | ||
|
||
pub use newick::build_newick; | ||
|
||
/// Recover a rooted tree (in Newick format) from a Phylo2Vec vector | ||
pub fn to_newick(v: &Vec<usize>) -> String { | ||
let ancestry: Ancestry = get_ancestry(&v); | ||
build_newick(&ancestry) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::tree_vec::types::Ancestry; | ||
|
||
// The recursive function that builds the Newick string | ||
fn _build_newick_recursive_inner(p: usize, ancestry: &Ancestry) -> String { | ||
let leaf_max = ancestry.len(); | ||
|
||
// Extract the children (c1, c2) and ignore the parent from the ancestry tuple | ||
let [c1, c2, _] = ancestry[p - leaf_max - 1]; | ||
|
||
// Recursive calls for left and right children, checking if they are leaves or internal nodes | ||
let left = if c1 > leaf_max { | ||
_build_newick_recursive_inner(c1, ancestry) | ||
} else { | ||
c1.to_string() // It's a leaf node, just convert to string | ||
}; | ||
|
||
let right = if c2 > leaf_max { | ||
_build_newick_recursive_inner(c2, ancestry) | ||
} else { | ||
c2.to_string() // It's a leaf node, just convert to string | ||
}; | ||
|
||
// Create the Newick string in the form (left, right)p | ||
format!("({},{}){}", left, right, p) | ||
} | ||
|
||
/// Build newick string from the ancestry matrix | ||
pub fn build_newick(ancestry: &Ancestry) -> String { | ||
// Get the root node, which is the parent value of the last ancestry element | ||
let root = ancestry.last().unwrap()[2]; | ||
|
||
// Build the Newick string starting from the root, and append a semicolon | ||
format!("{};", _build_newick_recursive_inner(root, ancestry)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// A type alias for the Pair type, which is a tuple representing (child1, child2) | ||
pub type Pair = (usize, usize); | ||
|
||
/// A type alias for the Ancestry type, which is a vector of vectors representing [child1, child2, parent] | ||
pub type Ancestry = Vec<[usize; 3]>; | ||
|
||
/// A type alias for the PairsVec type, which is a vector of tuples representing (child1, child2) | ||
pub type PairsVec = Vec<Pair>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters