Struct light_phylogeny::ArenaTree
source · pub struct ArenaTree<T>where
T: PartialEq,{
pub arena: Vec<Noeud<T>>,
}
Expand description
ArenaTree Structure. It describes a binary phylogenetic tree.
Inspirated from https://dev.to/deciduously/no-more-tears-no-more-knots-arena-allocated-trees-in-rust-44k6
Ben Lovy https://deciduously.com/
Fields§
§arena: Vec<Noeud<T>>
Implementations§
source§impl<T> ArenaTree<T>
impl<T> ArenaTree<T>
sourcepub fn node(&mut self, val: T) -> usize
pub fn node(&mut self, val: T) -> usize
Add a node with a given val and send its new index. If the node already exists, send its index.
sourcepub fn new_node(&mut self, val: T) -> usize
pub fn new_node(&mut self, val: T) -> usize
Add a node with a given val and send its new index. If the node already exists, send a panic alert.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let mut tree: ArenaTree<String> = ArenaTree::default();
let mut options: Options = Options::new();
let config: Config = Config::new();
println!("Reading newick file examples/newick.A4.txt...");
let contents = fs::read_to_string("examples/newick.A4.txt")
.expect("Something went wrong reading the newick file");
println!("Create a first node which will be the root...");
let root = tree.new_node("Root".to_string());
println!("Build the tree from the file contents...");
newick2tree(contents, &mut tree, root, &mut 0);
let j = tree.get_index("J".to_string()).expect("Error : unable to find J");
println!("Index of leaf J is {}",j);
let l = tree.get_index("L".to_string()).expect("Error : unable to find L");
println!("Index of leaf L is {}",l);
let n = tree.get_index("N".to_string()).expect("Error : unable to find N");
println!("Index of leaf N is {}",n);
let lca_jl = lca(&mut tree,j,l);
println!("Index of lca betwen J and L is {}",lca_jl);
tree.arena[lca_jl].name = "LCA of J and L".to_string();
let lca_jn = lca(&mut tree,j,n);
println!("Index of lca betwen J and N is {}",lca_jn);
tree.arena[lca_jn].name = "LCA of J and N".to_string();
// Display internal nodes
options.gene_internal = true ;
phyloxml_processing(&mut tree, &options, &config,"lca.svg".to_string());
println!("Please open output file 'lca.svg' with your browser");
println!("OK.");
}
More examples
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let mut tree: ArenaTree<String> = ArenaTree::default();
let mut options: Options = Options::new();
let config: Config = Config::new();
// Create a new node root
let root = tree.new_node("root".to_string());
// Create new nodes
let a1 = tree.new_node("a1".to_string());
let a2 = tree.new_node("a2".to_string());
let a = tree.new_node("a".to_string());
let b = tree.new_node("b".to_string());
let c = tree.new_node("c".to_string());
let d = tree.new_node("d".to_string());
println!("Initial tree :");
summary(&mut tree);
// Set names
tree.arena[root].name = "MyRoot".to_string();
tree.arena[a].name = "Gene A".to_string();
tree.arena[a1].name = "Gene A1".to_string();
tree.arena[a2].name = "Gene A2".to_string();
tree.arena[b].name = "Gene B".to_string();
tree.arena[c].name = "Gene C".to_string();
tree.arena[d].name = "Gene D".to_string();
println!("");
println!("Tree after name attribution:");
summary(&mut tree);
// Set hierarchy
// a1 and a2 are children of a
tree.arena[a1].parent = Some(a);
tree.arena[a2].parent = Some(a);
tree.arena[a].children.push(a1);
tree.arena[a].children.push(a2);
// a and b are children of c
tree.arena[a].parent = Some(c);
tree.arena[b].parent = Some(c);
tree.arena[c].children.push(a);
tree.arena[c].children.push(b);
// c and d are children of root
tree.arena[c].parent = Some(root);
tree.arena[d].parent = Some(root);
tree.arena[root].children.push(c);
tree.arena[root].children.push(d);
println!("");
println!("Tree after hierarchy attribution:");
summary(&mut tree);
// set duplication
tree.arena[a].e = Event::Duplication;
println!("");
println!("Event associated to gene {} ({}) : {:?}",a,tree.arena[a].name,tree.arena[a].e);
// Display internal nodes
options.gene_internal = true ;
phyloxml_processing(&mut tree, &options, &config,"build_tree.svg".to_string());
println!("Please open output file 'build_tree.svg' with your browser");
println!("OK.");
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
fn main() {
let mut tree: ArenaTree<String> = ArenaTree::default();
let mut options: Options = Options::new();
let config: Config = Config::new();
// Create a new node root
let root = tree.new_node("root".to_string());
// Create new nodes
let a1 = tree.new_node("a1".to_string());
let a2 = tree.new_node("a2".to_string());
let a = tree.new_node("a".to_string());
let b1 = tree.new_node("b1".to_string());
let b2 = tree.new_node("b2".to_string());
let b = tree.new_node("b".to_string());
let c = tree.new_node("c".to_string());
let d = tree.new_node("d".to_string());
println!("Initial tree :");
summary(&mut tree);
// Set names
tree.arena[root].name = "MyRoot".to_string();
tree.arena[a].name = "Gene A".to_string();
tree.arena[a1].name = "Gene A1".to_string();
tree.arena[a2].name = "Gene A2".to_string();
tree.arena[b].name = "Gene B".to_string();
tree.arena[b1].name = "Gene B1".to_string();
tree.arena[b2].name = "Gene B2".to_string();
tree.arena[c].name = "Gene C".to_string();
tree.arena[d].name = "Gene D".to_string();
println!("");
println!("Tree after name attribution:");
summary(&mut tree);
// Set hierarchy
// a1 and a2 are children of a
add_child(&mut tree,a,a1);
add_child(&mut tree,a,a2);
// a1 and a2 are children of a
add_child(&mut tree,b,b1);
add_child(&mut tree,b,b2);
// a and b are children of c
add_child(&mut tree,c,a);
add_child(&mut tree,c,b);
// c and d are children of root
add_child(&mut tree,root,c);
add_child(&mut tree,root,d);
println!("");
println!("Tree after hierarchy attribution:");
summary(&mut tree);
// Display internal nodes
options.gene_internal = true ;
phyloxml_processing(&mut tree, &options, &config,"modify_tree_ini.svg".to_string());
// knuth_layout(&mut tree,root, &mut 1);
// cladogramme(&mut tree);
// check_contour_postorder(&mut tree, root);
// shift_mod_xy(&mut tree, root, &mut 0.0, &mut 0.0);
// set_middle_postorder(&mut tree, root);
// draw_tree(&mut tree,"modify_tree_ini.svg".to_string(),&options,&config);
println!("Add a loss to C");
let loss = tree.new_node("loss".to_string());
tree.arena[loss].name = "Loss".to_string();
tree.arena[loss].e = Event::Loss;
add_child(&mut tree,c,loss);
println!("Add a node up to B");
let add = tree.new_node("add".to_string());
tree.arena[add].name = "Added up to B".to_string();
println!("Move A to new node ");
move_child(&mut tree, a, add);
println!("Move B to new node ");
move_child(&mut tree, b, add);
println!("Move new node to C ");
add_child(&mut tree, c, add);
println!("Tree after hierarchy modification:");
summary(&mut tree);
reset_pos(&mut tree);
phyloxml_processing(&mut tree, &options, &config,"modify_tree_mod.svg".to_string());
// knuth_layout(&mut tree,root, &mut 1);
// cladogramme(&mut tree);
// check_contour_postorder(&mut tree, root);
// shift_mod_xy(&mut tree, root, &mut 0.0, &mut 0.0);
// set_middle_postorder(&mut tree, root);
// draw_tree(&mut tree,"modify_tree_mod.svg".to_string(),&options,&config);
println!("Please open output files 'modify_tree_ini.svg' and 'modify_tree_mod.svg' with your browser");
println!("OK.");
}
sourcepub fn get_node(&mut self, val: T) -> usize
pub fn get_node(&mut self, val: T) -> usize
Get the index of a node from its val. If the node does not exists, send a panic alert.
sourcepub fn get_index(&mut self, name: String) -> Result<usize, usize>
pub fn get_index(&mut self, name: String) -> Result<usize, usize>
Get the index of a node from its name. Send a Result(index, error)
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() {
let mut options: Options = Options::new();
let mut sp_tree: ArenaTree<String> = ArenaTree::default();
let mut gene_trees:std::vec::Vec<ArenaTree<String>> = Vec::new();
let mut global_roots: std::vec::Vec<usize> = Vec::new();
read_recphyloxml_multi("examples/gene_parasite_page4.recphylo".to_string(),
&mut sp_tree, &mut gene_trees, &mut global_roots);
options.verbose = true;
env::set_var("RUST_LOG", "info");
env_logger::init();
let j = sp_tree.get_index("big_host0".to_string()).expect("Error : unable to find big_host0");
println!("Index of leaf big_host0 is {}",j);
let l = sp_tree.get_index("p4d".to_string()).expect("Error : unable to find p4d");
println!("Index of leaf p4d is {}",l);
let lca_jl = lca(&mut sp_tree,j,l);
println!("Index of lca is {} ({})",lca_jl,sp_tree.arena[lca_jl].name);
assert_eq!(sp_tree.arena[lca_jl].name,"big_host0".to_string());
}
More examples
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let mut tree: ArenaTree<String> = ArenaTree::default();
let mut options: Options = Options::new();
let config: Config = Config::new();
println!("Reading newick file examples/newick.A4.txt...");
let contents = fs::read_to_string("examples/newick.A4.txt")
.expect("Something went wrong reading the newick file");
println!("Create a first node which will be the root...");
let root = tree.new_node("Root".to_string());
println!("Build the tree from the file contents...");
newick2tree(contents, &mut tree, root, &mut 0);
let j = tree.get_index("J".to_string()).expect("Error : unable to find J");
println!("Index of leaf J is {}",j);
let l = tree.get_index("L".to_string()).expect("Error : unable to find L");
println!("Index of leaf L is {}",l);
let n = tree.get_index("N".to_string()).expect("Error : unable to find N");
println!("Index of leaf N is {}",n);
let lca_jl = lca(&mut tree,j,l);
println!("Index of lca betwen J and L is {}",lca_jl);
tree.arena[lca_jl].name = "LCA of J and L".to_string();
let lca_jn = lca(&mut tree,j,n);
println!("Index of lca betwen J and N is {}",lca_jn);
tree.arena[lca_jn].name = "LCA of J and N".to_string();
// Display internal nodes
options.gene_internal = true ;
phyloxml_processing(&mut tree, &options, &config,"lca.svg".to_string());
println!("Please open output file 'lca.svg' with your browser");
println!("OK.");
}
sourcepub fn get_largest_x(&mut self) -> f32
pub fn get_largest_x(&mut self) -> f32
Get the largest x value of a tree.
sourcepub fn get_largest_y(&mut self) -> f32
pub fn get_largest_y(&mut self) -> f32
Get the largest y value of a tree.
sourcepub fn get_largest_y_nofl(&mut self) -> f32
pub fn get_largest_y_nofl(&mut self) -> f32
Get the largest y value of a tree execpt the free living part.
sourcepub fn get_smallest_x(&mut self) -> f32
pub fn get_smallest_x(&mut self) -> f32
Get the smallest x value of a tree.
sourcepub fn get_smallest_y(&mut self) -> f32
pub fn get_smallest_y(&mut self) -> f32
Get the smallest y value of a tree.
sourcepub fn get_smallest_l(&mut self) -> f32
pub fn get_smallest_l(&mut self) -> f32
Get the smallest l value of a tree.
sourcepub fn get_largest_height(&mut self) -> f32
pub fn get_largest_height(&mut self) -> f32
Get the largest height .
sourcepub fn shift_x_subtree(&mut self, idx: usize, shift: f32)
pub fn shift_x_subtree(&mut self, idx: usize, shift: f32)
Shift subtree on x .
sourcepub fn copie(&mut self) -> ArenaTree<String>
pub fn copie(&mut self) -> ArenaTree<String>
Copy a whole ArenaTree structure.
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let transfers = vec![];
let mut options: Options = Options::new();
let config: Config = Config::new();
// Version portrait
let mut sp_tree: ArenaTree<String> = ArenaTree::default();
let mut gene_trees:std::vec::Vec<ArenaTree<String>> = Vec::new();
let mut global_roots: std::vec::Vec<usize> = Vec::new();
read_recphyloxml_multi("examples/FAM000715_reconciliated_2genes.recphylo".to_string(),
&mut sp_tree, &mut gene_trees, &mut global_roots);
recphyloxml_processing(&mut sp_tree, &mut gene_trees, &mut options, &config, true,
&transfers, "read_recphyloxml_original.svg".to_string());
println!("Please open output file 'read_recphyloxml_original.svg with your browser");
let mut sp_tree_clone = sp_tree.copie();
let mut gene_tree_clones:std::vec::Vec<ArenaTree<String>> = Vec::new();
for mut tree in gene_trees {
gene_tree_clones.push(tree.copie());
}
draw_sptree_gntrees(&mut sp_tree_clone, &mut gene_tree_clones,
"read_recphyloxml_copy.svg".to_string(), &options, &config, &transfers);
println!("Please open output file 'read_recphyloxml_copy.svg with your browser");
}