Skip to content

Commit

Permalink
Minimal example of newick string to vector code breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
jhellewell14 committed Apr 16, 2024
1 parent 1e25e95 commit 39922e5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
37 changes: 31 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,36 @@ use crate::cli::*;
pub fn main() {
let args = cli_args();
// Newick string example
let nstr: String = String::from("(((((('9':0.00066056,'4':0.0012556):0.00030238,('25':0.00042479,'19':0.00030083):0.00039731):0.00012351,'7':0.00039526):8.5797e-05,'18':0.00068249):0.00011977,'20':0.00056004):9.6199e-05,(((((((('17':0.0045802,'8':0.0021951):0.00019116,(((('24':0.0021162,'12':0.0017684):0.00074803,'13':0.0043482):0.00026831,('27':0.00063335,'5':0.00071804):0.0024273):4.8521e-05,((('23':0.0012435,'2':0.0012676):0.00011672,('21':0.00085695,'6':0.00096101):0.001509):0.00046989,'10':0.0020611):0.00092726):0.00022606):0.00019029,'26':0.0027488):0.00030355,('14':0.0021462,'0':0.0018751):0.00061029):0.00035469,'1':0.00096653):0.00015559,'22':0.0013558):7.0943e-05,('16':0.00054383,'15':0.00076666):0.00015841):0.00016779,'11':0.0021712):9.9157e-05,'3':0.00081132);");
let x: String = parse_rapidNJ_newick(&nstr);
// println!("{:?}", newick_to_vec(&x, 27));
// let nstr: String = String::from("(((((('9':0.00066056,'4':0.0012556):0.00030238,('25':0.00042479,'19':0.00030083):0.00039731):0.00012351,'7':0.00039526):8.5797e-05,'18':0.00068249):0.00011977,'20':0.00056004):9.6199e-05,(((((((('17':0.0045802,'8':0.0021951):0.00019116,(((('24':0.0021162,'12':0.0017684):0.00074803,'13':0.0043482):0.00026831,('27':0.00063335,'5':0.00071804):0.0024273):4.8521e-05,((('23':0.0012435,'2':0.0012676):0.00011672,('21':0.00085695,'6':0.00096101):0.001509):0.00046989,'10':0.0020611):0.00092726):0.00022606):0.00019029,'26':0.0027488):0.00030355,('14':0.0021462,'0':0.0018751):0.00061029):0.00035469,'1':0.00096653):0.00015559,'22':0.0013558):7.0943e-05,('16':0.00054383,'15':0.00076666):0.00015841):0.00016779,'11':0.0021712):9.9157e-05,'3':0.00081132);");
let nstr: String = String::from("(('7':0.00064945,'3':0.00085552):5.9503e-05,('9':0.00060885,'4':0.0013073):0.00019161,((((('6':0.0023043,'2':0.0011704):0.0017225,'5':0.0035144):8.4584e-05,'8':0.0024813):0.0002661,'0':0.0023835):0.00062217,'1':0.00096265):0.00052941);");
let mut x: String = parse_rapidNJ_newick(&nstr);
println!("{:?}", x);


// Need to add a ( after the correct , heading in from the left
// Then another ) just before the ; at the end
let mut brack_count = 0;
let mut add_ind: usize = 0;
for (ind, el) in x.char_indices() {
// println!("{:?}", el);
if el.eq(&'('){
brack_count += 1;
} else if el.eq(&')') {
brack_count -= 1;
} else if el.eq(&',') && brack_count.eq(&1) {
add_ind = ind;
break;
}
// println!("{brack_count}");
}
// println!("{add_ind}");

x.insert(add_ind + 1, '(');
x.insert(x.len() - 1, ')');
println!("{:?}", x);

println!("{:?}", newick_to_vec(&x, 27));

// 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,
Expand All @@ -55,9 +80,9 @@ pub fn main() {

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.hillclimb(&q, 100);
Expand Down
2 changes: 2 additions & 0 deletions src/newick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub fn parse_rapidNJ_newick(nw: &String) -> String {

// Put newick string with new labels together
let mut new_str: String = new_newick.join("");
new_str.push_str(&node_index.to_string());
new_str.push_str(":0.0001");
new_str.push_str(";");

new_str
Expand Down
10 changes: 8 additions & 2 deletions src/phylo2vec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,15 @@ std::vector<int> toVector(std::string newick, int num_leaves) {

std::vector<int> labels(num_leaves);
std::iota(labels.begin(), labels.end(), 0);

// std::cout << "1";
try {
for (int i = 0; i < num_leaves - 1; ++i) {
// std::cout << "2";
// std::cout << "i:" << i;
std::cout << newick << "\n";
// std::cout << labels << "\n";
std::pair<int, int> tmp = findLeftLeaf(newick, labels, processed, num_leaves);
// std::cout << "3";
int left_leaf = tmp.first;
int idx = tmp.second;

Expand Down Expand Up @@ -436,6 +441,7 @@ void processNewick(std::string &newick) {
}

Newick2VResult newick2v(std::string &newick, int num_leaves) {

processNewick(newick);

if (num_leaves == -1) {
Expand Down Expand Up @@ -485,7 +491,7 @@ std::unique_ptr<std::vector<int>> doToVector(std::string& newick, int num_leaves
Newick2VResult tmp = newick2vWithMapping(newick, num_leaves);
converted_v = tmp.v;

std::map<int, std::string> converted_mapping = convertMapping(tmp.mapping);
// std::map<int, std::string> converted_mapping = convertMapping(tmp.mapping);

// std::cout << "Number of leaves: " << tmp.num_leaves << std::endl;

Expand Down

0 comments on commit 39922e5

Please sign in to comment.