-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Newick C code to new file. Added code that will eventually crea…
…te a Tree struct from a rapidNJ newick string. I do this by modifying the rapidNJ Newick string and passing it to the C code. Currently the C code throws an error
- Loading branch information
1 parent
f07b3b6
commit 1e25e95
Showing
5 changed files
with
82 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
fn main() { | ||
cxx_build::bridge("src/phylo2vec.rs") | ||
cxx_build::bridge("src/newick.rs") | ||
.file("src/phylo2vec.cpp") | ||
.std("c++17") | ||
.compile("phylo2vec"); | ||
|
||
println!("cargo:rerun-if-changed=src/phylo2vec.rs"); | ||
println!("cargo:rerun-if-changed=src/newick.rs"); | ||
println!("cargo:rerun-if-changed=src/phylo2vec.cpp"); | ||
println!("cargo:rerun-if-changed=src/phylo2vec.hpp"); | ||
} |
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,74 @@ | ||
use cxx::let_cxx_string; | ||
use std::collections::HashMap; | ||
|
||
#[cxx::bridge] | ||
pub mod ffi { | ||
unsafe extern "C++" { | ||
include!("bactrees/include/phylo2vec.hpp"); | ||
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 y: Vec<usize> = x.iter().map(|el| *el as usize).collect(); | ||
y | ||
} | ||
|
||
pub fn parse_rapidNJ_newick(nw: &String) -> String { | ||
|
||
// Chop the Newick string into nodes | ||
let v: Vec<&str> = nw.split_inclusive(|c: char| c.eq(&')') || c.eq(&',')) | ||
.filter(|s| !s.is_empty() && s.ne(&";")) | ||
.collect(); | ||
|
||
// Initiate data structures | ||
let n_nodes = v.len(); | ||
let mut branch_len: Vec<f64> = Vec::with_capacity(n_nodes); | ||
let mut name_dict: HashMap<usize, String> = HashMap::with_capacity(n_nodes); | ||
let mut node_index: usize = 0; | ||
let mut new_newick: Vec<String> = Vec::with_capacity(n_nodes); | ||
|
||
// Iterate over nodes | ||
for node in v { | ||
// Cut outbrackets and commas | ||
let cleaned_node: String = node.replace(&['(', '\'', ')', ','], ""); | ||
|
||
// Split into label and branch length | ||
let split_node: Vec<&str> = cleaned_node.split(':').collect(); | ||
|
||
// Get old node label (an old label is assigned if no previous | ||
// label due to being internal node) | ||
let node_name: String = match split_node[0].is_empty() { | ||
true => node_index.to_string(), | ||
false => split_node[0].to_string(), | ||
}; | ||
// Put into name HashMap | ||
name_dict.insert(node_index, node_name); | ||
|
||
// Get branch length | ||
let blen: f64 = split_node[1].parse().unwrap(); | ||
branch_len.push(blen); | ||
|
||
// Put new node labels into old newick string | ||
if split_node[0].is_empty() { | ||
let new_el: String = node.replace(":", &format!("{}{}", node_index.to_string(), ":")); | ||
new_newick.push(new_el); | ||
|
||
} else { | ||
let new_el: String = node.replace(&format!("'{}'", name_dict.get(&node_index).unwrap()), | ||
&format!("{}", node_index.to_string())); | ||
new_newick.push(new_el); | ||
} | ||
|
||
// Iterate node index | ||
node_index += 1; | ||
} | ||
|
||
// Put newick string with new labels together | ||
let mut new_str: String = new_newick.join(""); | ||
new_str.push_str(";"); | ||
|
||
new_str | ||
} |
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