Skip to content

Add lineage_ids and descendant_ids, fix descendants panicking on nonexistent tax ID - #86

Open
audy wants to merge 6 commits into
masterfrom
austin/dev-12348-descendants-unknown-id
Open

audy wants to merge 6 commits into
masterfrom
austin/dev-12348-descendants-unknown-id

Conversation

@audy

@audy audy commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add Taxonomy.lineage_ids(tax_id) and Taxonomy.descendant_ids(tax_id). They do the same traversal as lineage() and descendants() but return list[str] IDs instead of building a TaxonomyNode per node. On large trees, building those node objects is most of the cost, and some callers only need the IDs.
  • Fixes descendants() (and so descendant_ids()), which panicked on an unknown tax ID. It now raises TaxonomyError.
  • Optimization: The Python lineage/descendants bindings (and the new _ids variants) now resolve the tax ID to an internal index once and traverse by index, instead of hashing the tax ID string for every field and every step of the traversal. This resulted in a slight speedup.
  • Add a contains(tax_id) -> bool method to the Rust Taxonomy trait as the single way to check whether a node exists. A tax ID exists if name() returns Ok for it; there are no overrides. traverse() uses it to reject unknown start nodes, and Python's tax_id in tax and add_node duplicate check now call it.
  • Behaviour change: Python's descendants() (and descendant_ids()) now return nodes in depth-first order, each node before its own descendants, instead of sorted by tax ID as a string. The lexicographic sort wasn't a meaningful order and was a large part of the cost. The Rust &str descendants still sorts.
  • Breaking change (Newick): from_newick now stores node labels as names and gives each node a positional tax ID (the root is "0"), and to_newick writes names (whitespace replaced with underscores) instead of tax IDs. Newick labels are optional and often repeated (unlabelled internal nodes, support values), so using them as tax IDs produced duplicates: traversals by ID could skip nodes or loop forever on nested unlabelled nodes. Look nodes up by label with find_all_by_name. Newick trees now always have unique IDs, so descendants() never returns duplicates.
  • Documentation improvements

Release build, current NCBI taxdump, master (0ce37a9) vs. this PR (e78df5b). Median of 3 runs, each in a fresh process; memory is RSS held by the result (varies by up to ~35 MB between builds for identical code). Lineage is over 100k random bacterial taxa; descendants is of "2" (564k nodes).

master this PR
lineage 0.38 s, 223 MB 0.24 s, ~230 MB
lineage_ids – 0.11 s, 50 MB
descendants 0.46 s, 189 MB 0.13 s, 230 MB
descendant_ids – 0.05 s, 53 MB
tax_id in tax (1M lookups) 0.16 s 0.18 s

@audy audy changed the title Raise TaxonomyError from descendants for unknown IDs Add lineage_ids and descendant_ids Nov 24, 2026
@audy
audy force-pushed the austin/dev-12348-descendants-unknown-id branch from d6132e3 to 78be915 Compare November 24, 2026 22:30
Comment thread src/python.rs Resolved
@audy audy changed the title Add lineage_ids and descendant_ids Add lineage_ids and descendant_ids, fix descendants panicking on nonexistent tax ID Nov 25, 2026
@audy
audy marked this pull request as ready for review November 25, 2026 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant