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>
where T: PartialEq + Default + Debug,

source

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.

source

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?
examples/lca.rs (line 11)
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
Hide additional examples
examples/build_tree.rs (line 8)
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.");
}
examples/modify_tree.rs (line 9)
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.");
}
source

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.

source

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?
examples/lca_2.rs (line 15)
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
Hide additional examples
examples/lca.rs (line 14)
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.");
}
source

pub fn get_root(&mut self) -> usize

Get the index of the root.

source

pub fn is_leaf(&self, idx: usize) -> bool

Check if the node is a leaf.

source

pub fn is_left(&self, idx: usize) -> bool

Check if the node is a left node.

source

pub fn is_root(&self, idx: usize) -> bool

Check if the node is the root.

source

pub fn depth(&self, idx: usize) -> usize

Get the depth of the node in the tree.

source

pub fn get_largest_x(&mut self) -> f32

Get the largest x value of a tree.

source

pub fn get_largest_y(&mut self) -> f32

Get the largest y value of a tree.

source

pub fn get_largest_y_nofl(&mut self) -> f32

Get the largest y value of a tree execpt the free living part.

source

pub fn get_smallest_x(&mut self) -> f32

Get the smallest x value of a tree.

source

pub fn get_smallest_y(&mut self) -> f32

Get the smallest y value of a tree.

source

pub fn get_smallest_l(&mut self) -> f32

Get the smallest l value of a tree.

source

pub fn get_largest_height(&mut self) -> f32

Get the largest height .

source

pub fn shift_x_subtree(&mut self, idx: usize, shift: f32)

Shift subtree on x .

source

pub fn copie(&mut self) -> ArenaTree<String>

Copy a whole ArenaTree structure.

Examples found in repository?
examples/read_recphyloxml_copie.rs (line 20)
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");

}

Trait Implementations§

source§

impl<T> Debug for ArenaTree<T>
where T: PartialEq + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Default for ArenaTree<T>
where T: PartialEq + Default,

source§

fn default() -> ArenaTree<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for ArenaTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for ArenaTree<T>
where T: Send,

§

impl<T> Sync for ArenaTree<T>
where T: Sync,

§

impl<T> Unpin for ArenaTree<T>
where T: Unpin,

§

impl<T> UnwindSafe for ArenaTree<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V