Skip to content

Commit

Permalink
Removed dropping first zero from tree vector in my code. Wrote a test…
Browse files Browse the repository at this point in the history
… for conversion from random tree vector to newick to tree vector to newick
  • Loading branch information
jhellewell14 committed Apr 4, 2024
1 parent 5688730 commit 97706b6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 54 deletions.
46 changes: 16 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,29 @@ use crate::cli::*;
pub fn main() {
let args = cli_args();

// let start = Instant::now();
let mut tr = phylo2vec_quad(random_tree(2));
let nw = tr.newick();
let n_leaves = tr.nodes.iter().filter(|n| n.tip).count();
println!("Tree vector: {:?} with {} leaves and newick: {:?}", tr.tree_vec, n_leaves, nw);
let_cxx_string!(nw_cpp = nw);
let x = phylo2vec::ffi::doToVector(nw_cpp, n_leaves as i32, false);
let mut y: Vec<usize> = x.iter().map(|el| *el as usize).collect();
y.reverse();
println!("Newick from conversion: {:?}", y);
let trstr = phylo2vec_quad(y).newick();
// println!("{:?}", trstr);

// Define rate matrix
// let q: na::Matrix4<f64> = na::Matrix4::new(
// -1.0, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0,
// 1.0 / 3.0, -1.0, 1.0 / 3.0, 1.0 / 3.0,
// 1.0 / 3.0, 1.0 / 3.0, -1.0, 1.0 / 3.0,
// 1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0, -1.0,
// );
let q: na::Matrix4<f64> = na::Matrix4::new(
-1.0, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0,
1.0 / 3.0, -1.0, 1.0 / 3.0, 1.0 / 3.0,
1.0 / 3.0, 1.0 / 3.0, -1.0, 1.0 / 3.0,
1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0, -1.0,
);

// let mut tr = phylo2vec_quad(random_tree(27));
let mut tr = phylo2vec_quad(random_tree(27));

// let end = Instant::now();
// // let filename = "listeria0.aln";
// tr.add_genetic_data(&args.alignment);
tr.add_genetic_data(&args.alignment);

// tr.update_likelihood_postorder(&q);
tr.update_likelihood_postorder(&q);

// println!("{}", tr.get_tree_likelihood());
// println!("{:?}", tr.newick());
// println!("{:?}", tr.tree_vec);
println!("{}", tr.get_tree_likelihood());
println!("{:?}", tr.newick());
println!("{:?}", tr.tree_vec);

// if !args.no_optimise {
// // tr.optimise(&q, 10);
// tr.hillclimb(&q, 20);
// }
if !args.no_optimise {
// tr.optimise(&q, 10);
tr.hillclimb(&q, 20);
}

// let end = Instant::now();

Expand Down
24 changes: 18 additions & 6 deletions src/phylo2vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use crate::node::Node;
use crate::Tree;
use ndarray::*;
use rand::{seq::SliceRandom, thread_rng, Rng};
use cxx::let_cxx_string;

pub fn phylo2vec_quad(v: Vec<usize>) -> Tree {
let mut tree = Tree::new(v);
let k = tree.tree_vec.len();
let mut sub_vec = tree.tree_vec.clone();
sub_vec.remove(0);
let k = sub_vec.len();
let mut not_processed = [true].repeat(k);
let mut M = Array2::<usize>::zeros((k, 3));
let mut labels = Array2::<usize>::zeros((k + 1, k + 1));
Expand All @@ -27,13 +30,13 @@ pub fn phylo2vec_quad(v: Vec<usize>) -> Tree {
let n = rowmax[0..k]
.iter()
.enumerate()
.rposition(|(index, el)| (tree.tree_vec[index] <= *el) & not_processed[index])
.rposition(|(index, el)| (sub_vec[index] <= *el) & not_processed[index])
.unwrap();

let m = labels
.slice(s![n, ..])
.iter()
.position(|x| *x == tree.tree_vec[n])
.position(|x| *x == sub_vec[n])
.unwrap();

M[[i, 0]] = labels[[k, m]];
Expand Down Expand Up @@ -64,14 +67,16 @@ pub fn phylo2vec_quad(v: Vec<usize>) -> Tree {

pub fn phylo2vec_lin(v: Vec<usize>, permute: bool) -> Tree {
let mut tree = Tree::new(v);
let k = tree.tree_vec.len();
let mut sub_vec = tree.tree_vec.clone();
sub_vec.remove(0);
let k = sub_vec.len();
let mut M = Array2::<usize>::zeros((k, 3));
let mut labels_rowk: Vec<usize> = (0..=k).collect();
let mut rmk = k;

for i in 0..k {
let n = k - i - 1;
let m = tree.tree_vec[n];
let m = sub_vec[n];

M[[i, 0]] = labels_rowk[m];
M[[i, 1]] = labels_rowk[n + 1];
Expand Down Expand Up @@ -106,7 +111,7 @@ pub fn phylo2vec_lin(v: Vec<usize>, permute: bool) -> Tree {
pub fn random_tree(k: usize) -> Vec<usize> {
let mut rng = rand::thread_rng();

vec![0; k]
vec![0; k + 1]
.iter()
.enumerate()
.map(|(i, _el)| if i > 0 { rng.gen_range(0..((2 * i) - 1)) } else { 0 })
Expand Down Expand Up @@ -221,3 +226,10 @@ pub mod ffi {
fn doToVector(newick: Pin<&mut CxxString>, num_leaves: i32, with_mapping: bool) -> UniquePtr<CxxVector<i32>>;
}
}

pub fn newick_to_vec(nw: &String, n_leaves: usize) -> Vec<usize>{
let_cxx_string!(nw_cpp = nw);
let x = ffi::doToVector(nw_cpp, n_leaves as i32, false);
let mut y: Vec<usize> = x.iter().map(|el| *el as usize).collect();
y
}
48 changes: 30 additions & 18 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ mod tests {
use crate::phylo2vec::phylo2vec_lin;
use crate::phylo2vec::phylo2vec_quad;
use crate::tree::Tree;
use crate::newick_to_vec;
use crate::random_tree;

#[test]
fn treemake_quad() {
let mut tree = phylo2vec_quad(vec![0, 0, 0]);
let mut tree = phylo2vec_quad(vec![0, 0, 0, 0]);

assert_eq!(tree.get_node(0).unwrap().parent, Some(4));
assert_eq!(tree.get_node(1).unwrap().parent, Some(6));
Expand All @@ -17,7 +19,7 @@ mod tests {
assert_eq!(tree.get_node(5).unwrap().parent, Some(6));
assert_eq!(tree.get_node(6).unwrap().parent, None);

tree = phylo2vec_quad(vec![0, 0, 1]);
tree = phylo2vec_quad(vec![0, 0, 0, 1]);

assert_eq!(tree.get_node(0).unwrap().parent, Some(5));
assert_eq!(tree.get_node(1).unwrap().parent, Some(4));
Expand All @@ -27,7 +29,7 @@ mod tests {
assert_eq!(tree.get_node(5).unwrap().parent, Some(6));
assert_eq!(tree.get_node(6).unwrap().parent, None);

tree = phylo2vec_quad(vec![0, 1, 0]);
tree = phylo2vec_quad(vec![0, 0, 1, 0]);

assert_eq!(tree.get_node(0).unwrap().parent, Some(4));
assert_eq!(tree.get_node(1).unwrap().parent, Some(5));
Expand All @@ -37,7 +39,7 @@ mod tests {
assert_eq!(tree.get_node(5).unwrap().parent, Some(6));
assert_eq!(tree.get_node(6).unwrap().parent, None);

tree = phylo2vec_quad(vec![0, 1, 1]);
tree = phylo2vec_quad(vec![0, 0, 1, 1]);

assert_eq!(tree.get_node(0).unwrap().parent, Some(6));
assert_eq!(tree.get_node(1).unwrap().parent, Some(4));
Expand All @@ -47,7 +49,7 @@ mod tests {
assert_eq!(tree.get_node(5).unwrap().parent, Some(6));
assert_eq!(tree.get_node(6).unwrap().parent, None);

tree = phylo2vec_quad(vec![0, 1, 2]);
tree = phylo2vec_quad(vec![0, 0, 1, 2]);

assert_eq!(tree.get_node(0).unwrap().parent, Some(6));
assert_eq!(tree.get_node(1).unwrap().parent, Some(5));
Expand All @@ -57,7 +59,7 @@ mod tests {
assert_eq!(tree.get_node(5).unwrap().parent, Some(6));
assert_eq!(tree.get_node(6).unwrap().parent, None);

tree = phylo2vec_quad(vec![0, 1, 3]);
tree = phylo2vec_quad(vec![0, 0, 1, 3]);

assert_eq!(tree.get_node(0).unwrap().parent, Some(6));
assert_eq!(tree.get_node(1).unwrap().parent, Some(4));
Expand All @@ -67,7 +69,7 @@ mod tests {
assert_eq!(tree.get_node(5).unwrap().parent, Some(6));
assert_eq!(tree.get_node(6).unwrap().parent, None);

tree = phylo2vec_quad(vec![0, 0, 3]);
tree = phylo2vec_quad(vec![0, 0, 0, 3]);

assert_eq!(tree.get_node(0).unwrap().parent, Some(4));
assert_eq!(tree.get_node(1).unwrap().parent, Some(6));
Expand All @@ -81,7 +83,7 @@ mod tests {
#[test]
fn phylo2vec_lin_same_as_phylo2vec_quad() {

let vecs: Vec<Vec<usize>> = vec![vec![0, 0, 0], vec![0, 1, 0], vec![0, 1, 2], vec![0, 1, 1]];
let vecs: Vec<Vec<usize>> = vec![vec![0, 0, 0, 0], vec![0, 0, 1, 0], vec![0, 0, 1, 2], vec![0, 0, 1, 1]];
let mut tree_q: Tree;
let mut tree_l: Tree;

Expand Down Expand Up @@ -130,10 +132,10 @@ mod tests {

#[test]
fn update_tree_quad_check() {
let mut tree_q = phylo2vec_quad(vec![0, 1, 0]);
let mut tree_l = phylo2vec_lin(vec![0, 0, 0], false);
let mut tree_q = phylo2vec_quad(vec![0, 0, 1, 0]);
let mut tree_l = phylo2vec_lin(vec![0, 0, 0, 0], false);

let vecs: Vec<Vec<usize>> = vec![vec![0, 0, 0], vec![0, 1, 0], vec![0, 1, 2], vec![0, 1, 1]];
let vecs: Vec<Vec<usize>> = vec![vec![0, 0, 0, 0], vec![0, 0, 1, 0], vec![0, 0, 1, 2], vec![0, 0, 1, 1]];

for vec in vecs {
let v = vec.clone();
Expand All @@ -159,7 +161,7 @@ mod tests {
-3.0, 1.0, 1.0, 1.0, 1.0, -3.0, 1.0, 1.0, 1.0, 1.0, -3.0, 1.0, 1.0, 1.0, 1.0, -3.0,
);

let mut tr = phylo2vec_lin(vec![0, 0, 0], false);
let mut tr = phylo2vec_lin(vec![0, 0, 0, 0], false);

let genetic_data = vec![
vec![
Expand Down Expand Up @@ -198,10 +200,10 @@ mod tests {

let old_likelihood = tr.get_tree_likelihood();

tr.update_quad(vec![0, 0, 1]);
tr.update_quad(vec![0, 0, 0, 1]);
tr.update_likelihood(&q);

tr.update_quad(vec![0, 0, 0]);
tr.update_quad(vec![0, 0, 0, 0]);
tr.update_likelihood(&q);

let new_likelihood = tr.get_tree_likelihood();
Expand All @@ -211,31 +213,31 @@ mod tests {

#[test]
fn newick_test () {
let mut tr = phylo2vec_quad(vec![0, 0, 0]);
let mut tr = phylo2vec_quad(vec![0, 0, 0, 0]);

// Newick string for this tree is (1,(2,(3,0)4)5)6;
// This should be the tree topology according to the ape package in R
assert_eq!(tr.get_node(4).unwrap().children, (Some(0), Some(3)));
assert_eq!(tr.get_node(5).unwrap().children, (Some(4), Some(2)));
assert_eq!(tr.get_node(6).unwrap().children, (Some(5), Some(1)));

let mut tr = phylo2vec_quad(vec![0, 0, 1]);
let mut tr = phylo2vec_quad(vec![0, 0, 0, 1]);

// Newick string for this tree is ((3,1)4,(2,0)5)6;
// This should be the tree topology according to the ape package in R
assert_eq!(tr.get_node(4).unwrap().children, (Some(1), Some(3)));
assert_eq!(tr.get_node(5).unwrap().children, (Some(0), Some(2)));
assert_eq!(tr.get_node(6).unwrap().children, (Some(5), Some(4)));

let mut tr = phylo2vec_quad(vec![0, 1, 1]);
let mut tr = phylo2vec_quad(vec![0, 0, 1, 1]);

// Newick string for this tree is ((2,(3,1)4)5,0)6;
// This should be the tree topology according to the ape package in R
assert_eq!(tr.get_node(4).unwrap().children, (Some(1), Some(3)));
assert_eq!(tr.get_node(5).unwrap().children, (Some(4), Some(2)));
assert_eq!(tr.get_node(6).unwrap().children, (Some(0), Some(5)));

let mut tr = phylo2vec_quad(vec![0, 1, 1, 3]);
let mut tr = phylo2vec_quad(vec![0, 0, 1, 1, 3]);

// Newick string for this tree is ((2,((4,3)5,1)6)7,0)8;
// This should be the tree topology according to the ape package in R
Expand All @@ -248,4 +250,14 @@ mod tests {
// mt <- ape::read.tree(text = "newick_string_here")
// plot(mt)
}

#[test]
fn newick_conversion () {
let mut tr = phylo2vec_quad(random_tree(10));
let nw = tr.newick();
let n_leaves = tr.nodes.iter().filter(|n| n.tip).count();
let y = newick_to_vec(&nw, n_leaves);
let trstr = phylo2vec_quad(y).newick();
assert_eq!(trstr, nw);
}
}

0 comments on commit 97706b6

Please sign in to comment.