From b9cbf779ea1f390841ce8b2348354b2551bb5978 Mon Sep 17 00:00:00 2001 From: dagou Date: Thu, 5 Sep 2024 21:44:00 +0800 Subject: [PATCH] bootstrap u32 to f64 --- Cargo.lock | 2 +- Cargo.toml | 2 +- pyproject.toml | 5 ++--- setup.py | 2 +- src/node.rs | 4 ++-- src/python.rs | 2 +- src/tree.rs | 24 ++++++++++++------------ 7 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2fac0d4..3ae9744 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,7 +16,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "gtdb_tree" -version = "0.1.7" +version = "0.1.8" dependencies = [ "memchr", "pyo3", diff --git a/Cargo.toml b/Cargo.toml index 11201c5..3121644 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gtdb_tree" -version = "0.1.7" +version = "0.1.8" edition = "2021" description = "A library for parsing Newick format files, especially GTDB tree files." homepage = "https://github.com/eric9n/gtdb_tree" diff --git a/pyproject.toml b/pyproject.toml index d91dc16..2184bd4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ features = ["python"] [project] name = "gtdb_tree" -version = "0.1.7" +version = "0.1.8" description = "A Python package for parsing GTDB trees using Rust" readme = "README.md" authors = [{ name = "dagou", email = "eric9n@gmail.com" }] @@ -22,8 +22,7 @@ classifiers = [ ] keywords = ["gtdb", "tree", "parser", "rust"] dependencies = [ - "setuptools-rust>=1.5.2", - "cffi" + "setuptools-rust>=1.5.2" ] requires-python = ">=3.8" diff --git a/setup.py b/setup.py index c396878..1eedf6f 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name="gtdb_tree", - version="0.1.7", + version="0.1.8", rust_extensions=[RustExtension("gtdb_tree.gtdb_tree", binding=Binding.PyO3)], packages=["gtdb_tree"], # rust extensions are not zip safe, just like C-extensions. diff --git a/src/node.rs b/src/node.rs index d4c0a97..0b34d35 100644 --- a/src/node.rs +++ b/src/node.rs @@ -6,12 +6,12 @@ /// * `name` - The name of the node. /// * `length` - The length of the branch leading to this node. /// * `parent` - The identifier of the parent node. -/// * `bootstrap` - The bootstrap value of the node. 0-100 +/// * `bootstrap` - The bootstrap value of the node. 0.0-100.0 #[derive(Debug, PartialEq, Clone)] pub struct Node { pub id: usize, pub name: String, - pub bootstrap: u32, + pub bootstrap: f64, pub length: f64, pub parent: usize, } diff --git a/src/python.rs b/src/python.rs index 79cb239..cf0f368 100644 --- a/src/python.rs +++ b/src/python.rs @@ -24,7 +24,7 @@ impl Node { } #[getter] - fn bootstrap(&self) -> PyResult { + fn bootstrap(&self) -> PyResult { Ok(self.node.bootstrap) } diff --git a/src/tree.rs b/src/tree.rs index 8d5538b..2991c18 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -1,27 +1,27 @@ use crate::node::{Node, ParseError}; use memchr::memchr2; -fn parse_label(label: &str) -> Result<(String, u32), ParseError> { +fn parse_label(label: &str) -> Result<(String, f64), ParseError> { let label = label.trim_end_matches(";").trim_matches('\'').to_string(); let parts: Vec<&str> = label.splitn(2, ':').collect(); if parts.len() == 1 { - let label_u32 = label.parse::(); - if let Ok(bootstrap) = label_u32 { - if bootstrap <= 100 { + let label_f64 = label.parse::(); + if let Ok(bootstrap) = label_f64 { + if bootstrap <= 100.1 { return Ok(("".into(), bootstrap)); } } - return Ok((label, 0)); + return Ok((label, 0.0)); } - let parts_0_u32 = parts.get(0).unwrap_or(&"0").parse::(); + let parts_0_f64 = parts.get(0).unwrap_or(&"0").parse::(); let name = parts.get(1).unwrap_or(&"").to_string(); - if let Ok(bootstrap) = parts_0_u32 { - if bootstrap <= 100 { + if let Ok(bootstrap) = parts_0_f64 { + if bootstrap <= 100.1 { return Ok((name, bootstrap)); } } - Ok((label, 0)) + Ok((label, 0.0)) } /// Parse the name and length of a node from a Newick tree string. @@ -46,10 +46,10 @@ fn parse_label(label: &str) -> Result<(String, u32), ParseError> { /// let node_bytes = b"A:0.1"; /// let (name, bootstrap, length) = parse_node(node_bytes).unwrap(); /// assert_eq!(name, "A"); -/// assert_eq!(bootstrap, 0); +/// assert_eq!(bootstrap, 0.0); /// assert_eq!(length, 0.1); /// ``` -pub fn parse_node(node_bytes: &[u8]) -> Result<(String, u32, f64), ParseError> { +pub fn parse_node(node_bytes: &[u8]) -> Result<(String, f64, f64), ParseError> { let node_str = std::str::from_utf8(node_bytes).expect("UTF-8 sequence"); // gtdb // Check if node_str contains single quotes and ensure they are together @@ -64,7 +64,7 @@ pub fn parse_node(node_bytes: &[u8]) -> Result<(String, u32, f64), ParseError> { if parts.len() == 1 { let label = node_str.trim_end_matches(";").to_string(); - return Ok((label, 0, 0.0)); + return Ok((label, 0.0, 0.0)); } let label = parts.get(1).unwrap_or(&"");