Skip to content

Commit

Permalink
bootstrap u32 to f64
Browse files Browse the repository at this point in the history
  • Loading branch information
dagou committed Sep 5, 2024
1 parent e1263d7 commit b9cbf77
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }]
Expand All @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Node {
}

#[getter]
fn bootstrap(&self) -> PyResult<u32> {
fn bootstrap(&self) -> PyResult<f64> {
Ok(self.node.bootstrap)
}

Expand Down
24 changes: 12 additions & 12 deletions src/tree.rs
Original file line number Diff line number Diff line change
@@ -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::<u32>();
if let Ok(bootstrap) = label_u32 {
if bootstrap <= 100 {
let label_f64 = label.parse::<f64>();
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::<u32>();
let parts_0_f64 = parts.get(0).unwrap_or(&"0").parse::<f64>();
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.
Expand All @@ -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
Expand All @@ -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(&"");
Expand Down

0 comments on commit b9cbf77

Please sign in to comment.