Skip to main content

Taxdumpy

A high-performance Python toolkit for parsing NCBI Taxonomy databases with lineage resolution and fuzzy search

CI PyPI version License: MIT Python 3.10+ Development Status codecov

Features

  • Cached Loading: Parse NCBI taxdump files once, then reuse pickle or SQLite caches
  • Dual Backends: In-memory (TaxDb) for speed or SQLite (TaxSQLite) for memory efficiency
  • Lineage Resolution: Complete lineage tracing with automatic handling of merged/deleted nodes
  • Subtree Traversal: Walk down the tree with descendants — every taxid below a given one
  • Rank Codes: Comparable RC class for efficient rank-based operations and grouping
  • Fuzzy Search: Rapid approximate name matching using rapidfuzz
  • Batch Grouping: Group taxids by rank level with group_taxids_by_rank
  • CLI: Ready-to-use commands for caching, searching, and lineage resolution

Installation

pip install taxdumpy

Development Installation:

git clone https://github.com/omegahh/taxdumpy.git
cd taxdumpy
pip install -e .

With Development Dependencies:

pip install -e '.[dev]'

Quick Start

Basic Usage

from taxdumpy import TaxDb, Taxon

# Initialize database (uses pickle cache if available)
taxdb = TaxDb("/path/to/taxdump")

# Create taxon objects
human = Taxon(9606, taxdb)  # Homo sapiens
ecoli = Taxon(511145, taxdb)  # E. coli K-12

# Access lineage information
print(human.name_lineage)
# ['Homo sapiens', 'Homo', 'Hominidae', 'Primates', ..., 'cellular organisms']

print(human.rank_lineage)
# ['species', 'genus', 'family', 'order', ..., 'superkingdom']

# Check taxonomic properties
print(f"Rank: {human.rank}")           # species
print(f"Division: {human.division}")   # PRI (division code, not full name)
print(f"Is legacy: {human.is_legacy}") # False

Using the Factory Function

from taxdumpy import create_database, Taxon

# taxdump_dir is the first positional argument
db = create_database("/path/to/taxdump", backend="sqlite")
# Or: backend="dict" for in-memory

taxon = Taxon(9606, db)
print(taxon.name)  # Homo sapiens

Subtree Traversal

from taxdumpy import TaxDb, descendants

taxdb = TaxDb("/path/to/taxdump")

# Every taxid below genus Escherichia (transitively, not just children)
descendants(taxdb, 561)
# [562, 208962, 511145, ...]

# Direct children only
descendants(taxdb, 561, max_depth=1)

# Deleted and merged taxids are excluded; the queried taxid itself is not
# included. Combine with existing helpers to filter:
species = [t for t in descendants(taxdb, 2) if taxdb.get_node(t).rank == "species"]

The first call builds a parent-to-children index in one pass over the database and caches it, so later queries cost time proportional to the subtree returned rather than to the database size.

# Print formatted, highlighted matches to the console
taxdb.fuzzy_search("Escherichia coli", limit=5)

# Or get the matches as data
results = taxdb.search("Escherichia coli", limit=5)
for match in results:
    print(f"{match['name']} (TaxID: {match['taxid']}, Score: {match['score']})")

search returns dictionaries containing name, taxid, and a score from 0 to 100. It preserves separate taxa sharing a name and reuses its candidate index across queries. fuzzy_search prints the results. Both methods propagate validation and database errors to callers.

Rank Codes and Grouping

from taxdumpy import TaxDb, Taxon, RC, group_taxids_by_rank

taxdb = TaxDb("/path/to/taxdump")
human = Taxon(9606, taxdb)

# RC (Rank Code) - comparable rank representation
print(human.rc)              # RC('S') for species
print(human.rankcode_lineage)  # [RC('S'), RC('G'), RC('F1'), RC('F'), ...]

# RC comparison (lower value = higher rank)
RC("R") < RC("D") < RC("G") < RC("S")  # True
RC("F") < RC("F1")  # True (F1 is below F)

# Group taxids by rank
taxids = [9606, 9598, 9597]  # Human, Chimp, Bonobo
by_family = group_taxids_by_rank(taxids, "family", taxdb)
# {9604: [9606, 9598, 9597]}  # All in Hominidae

by_genus = group_taxids_by_rank(taxids, "genus", taxdb)
# {9605: [9606], 9598: [9598], 9597: [9597]}  # Different genera

# MPA-style lineage representation
print(human.mpa_repr)
# 'd__Eukaryota|k__Metazoa|p__Chordata|...|g__Homo|s__Homo_sapiens'

Command Line Interface

By default, search and lineage use SQLite so individual queries do not load the full pickle into memory. --fast loads a smaller, filtered pickle instead; create that cache first with cache -f FILE.

Check Version

taxdumpy --version

Cache Database

# Rebuild both taxdump.sqlite and taxdump.pickle from source files
taxdumpy cache -d /path/to/taxdump

# Build taxdump_fast.pickle from selected taxids (one per line), their
# ancestors, and their descendants; blank lines and # comments are allowed
taxdumpy cache -d /path/to/taxdump -f important_taxids.txt

The filtered command loads the full TaxDb to build the subset. It does not rebuild SQLite. Queries outside the subset require the full database.

Search Operations

# Search for organisms (with fuzzy matching)
taxdumpy search --fast "Escherichia coli"
taxdumpy search "Homo sapiens"

# Limit search results
taxdumpy search --limit 5 "Influenza A"

# Search with custom database path
taxdumpy search -d /custom/path "Influenza A"

Lineage Tracing

# Get complete lineage for TaxID
taxdumpy lineage --fast 511145  # E. coli K-12 MG1655
taxdumpy lineage 9606           # Homo sapiens

# With custom database path
taxdumpy lineage -d /custom/path 9606

Database Setup

1. Download NCBI Taxonomy Data

# Create directory for taxonomy data
mkdir -p ~/.taxonkit

# Download latest taxdump
wget ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdump.tar.gz -P ~/.taxonkit

# Extract files
tar -xzf ~/.taxonkit/taxdump.tar.gz -C ~/.taxonkit

2. Initialize Database

# Create full cache (recommended for regular use)
taxdumpy cache -d ~/.taxonkit

# Or create fast cache with specific organisms
echo -e "9606\n511145\n7227" > important_species.txt
taxdumpy cache -d ~/.taxonkit -f important_species.txt

3. Set Environment Variable (Optional)

export TAXDB_PATH=~/.taxonkit

Advanced Usage

SQLite Backend

from taxdumpy import TaxSQLite, Taxon

# Use SQLite for memory-efficient storage
db = TaxSQLite("/path/to/taxdump")  # Creates taxdump.sqlite in that directory

# Same API as TaxDb
taxon = Taxon(9606, db)
print(taxon.name_lineage)

# Use as context manager
with TaxSQLite("/path/to/taxdump") as db:
    taxon = Taxon(9606, db)
    print(taxon.name)

Batch Processing

Load the full pickle once and reuse the same instance for all Python queries. Its startup cost is paid once per instance, rather than once per taxid.

from taxdumpy import TaxDb, Taxon

# Load all taxa for arbitrary batch queries
taxdb = TaxDb("/path/to/taxdump")

taxids = [9606, 511145, 7227, 4932]  # Human, E.coli, Fly, Yeast
for taxid in taxids:
    taxon = Taxon(taxid, taxdb)
    print(f"{taxon.name}: {' > '.join(taxon.name_lineage[:3])}")

For a known subset, build a filtered cache using the full database, then load that smaller cache in subsequent processes:

count = taxdb.build_fast_cache([9606, 511145, 7227, 4932])
print(f"Cached {count} nodes, including ancestors and descendants")

subset = TaxDb("/path/to/taxdump", fast=True)
human = Taxon(9606, subset)

Cache Freshness and Rebuilding

New caches record source file sizes and modification times. Opening a stale full pickle or SQLite cache rebuilds it from the taxdump files. Missing, stale, or unreadable filtered caches raise an error; recreate them with cache -f FILE or build_fast_cache(taxids) so the selection is explicit.

Original three-part pickles remain readable, and legacy caches use source modification times to detect changes. Valid caches remain usable after the source .dmp files are removed. Rebuilding requires the source files again.

To force a rebuild from Python:

from taxdumpy import TaxDb, TaxSQLite

taxdb = TaxDb("/path/to/taxdump", rebuild=True)
with TaxSQLite("/path/to/taxdump", rebuild=True) as db:
    print(len(db))

Each cache is replaced atomically only after its new contents are written successfully. A failed rebuild preserves the previous file. rebuild=True cannot be combined with fast=True; rebuild filtered caches through the full database's build_fast_cache method instead.

Handling Merged/Legacy TaxIDs

from taxdumpy import TaxDb, Taxon

taxdb = TaxDb("/path/to/taxdump")

# Merged taxids are automatically resolved
taxon = Taxon(old_taxid, taxdb)
print(taxon.is_legacy)  # True if taxid was merged
print(taxon.taxid)      # Original taxid
print(taxon.node.taxid) # Current/resolved taxid

Exception Handling

from taxdumpy import TaxDb, Taxon, TaxidError, TaxRankError, upper_rank_id

taxdb = TaxDb("/path/to/taxdump")

try:
    taxon = Taxon(999999999, taxdb)
except TaxidError as e:
    print(f"Invalid taxid: {e}")
    # Includes suggestions for similar taxids

try:
    # Signature is (tax, taxdb, rank) — a Taxon, not a bare taxid
    rank_id = upper_rank_id(Taxon(9606, taxdb), taxdb, "invalid_rank")
except TaxRankError as e:
    print(f"Invalid rank: {e}")
    # Includes list of valid ranks

API Reference

Core Classes

TaxDb: In-memory dictionary-based database with pickle caching

TaxDb(taxdump_dir: Path | str, fast: bool = False, verbose: bool = False, *, rebuild: bool = False)

TaxSQLite: SQLite-based persistent database

TaxSQLite(taxdump_dir: Path | str, verbose: bool = False, *, rebuild: bool = False)

Taxon: Taxonomic unit with lineage resolution

Taxon(taxid: int | str, taxdb: TaxDb | TaxSQLite)

# Properties
.name: str                # Scientific name
.rank: str                # Taxonomic rank
.division: str            # NCBI division
.parent: int              # Parent taxid
.acronym: str | None      # Acronym, if any
.node: Node               # Underlying node (resolved, if merged)
.rc: RC                   # Rank code for this taxon
.lineage: list[Node]      # Complete lineage as Node objects
.name_lineage: list[str]  # Names from taxid to root
.rank_lineage: list[str]  # Ranks from taxid to root
.taxid_lineage: list[int] # Taxids from taxid to root
.rankcode_lineage: list[RC]  # Rank codes from taxid to root
.mpa_repr: str            # MetaPhlAn-style lineage string
.is_legacy: bool          # True if taxid was merged
.has_species_level: bool  # True if lineage contains species rank
.species_taxid: int | None # Taxid at species level, or None if absent (previously 0)

Factory Function

from taxdumpy import create_database

# backend: "sqlite", "sql", "dict", "memory", or "pickle"
db = create_database("/path/to/taxdump", backend="sqlite")

Rank Functions

from taxdumpy import (
    upper_rank_id,       # Get taxid at rank (raises TaxRankError if not found)
    get_rank_taxid,      # Get taxid at rank (returns None if not found)
    get_canonical_ranks, # Get dict of all canonical ranks in lineage
    get_closest_rank,    # Get closest rank at or above target level
    get_rank_or_closest, # Same, returning the taxid alone
    get_all_ranks,       # Every rank in the lineage as a dict
    find_rank_in_lineage,# First of several candidate ranks present
    get_rank_distance,   # Calculate distance between two ranks
    group_taxids_by_rank,# Group a list of taxids at a given rank
)

# The rank tables live in taxdumpy.functions, not the package root:
from taxdumpy.functions import RANKNAMES, EXTENDED_RANKS
# RANKNAMES:      9 canonical levels, species → realm (domain/superkingdom are aliases)
# EXTENDED_RANKS: 32 names including sub-ranks and aliases

domain and superkingdom resolve to the same rank level. Realm uses r__ in MPA output and the root-level R rank code, preserving existing integer RC values. Grouping by root always selects taxid 1, including viral lineages.

upper_rank_id requires the requested higher rank or its subrank to exist in the lineage; otherwise it raises TaxRankError. Use get_closest_rank or get_rank_or_closest when fallback to a higher canonical rank is desired.

Subtree Traversal

from taxdumpy import descendants, descendant_nodes, build_children_index

descendants(taxdb, taxid, max_depth=None) -> list[int]
# Every taxid below `taxid`. Excludes `taxid` itself, and excludes deleted
# and merged taxids. max_depth=1 gives direct children only. Breadth-first.

descendant_nodes(taxdb, taxid, max_depth=None) -> list[Node]
# Same, as Node objects. Costs one get_node per result.

build_children_index(taxdb) -> dict[int, list[int]]
# The parent-to-children index the above build and cache. Read-only.

Exceptions

from taxdumpy import (
    TaxdumpyError,          # Base exception
    TaxDbError,             # Database issues
    TaxidError,             # Invalid/unknown taxid
    TaxRankError,           # Invalid rank
    TaxdumpFileError,       # Taxdump file issues
    DatabaseCorruptionError, # Corrupted database
    ValidationError,        # Input validation failures
)

Performance Tips

  • Single CLI Queries: Use the default SQLite backend to avoid loading the full pickle
  • Python Batches: Load one TaxDb instance and reuse it for multiple operations
  • Filtered Data: Use TaxDb(path, fast=True) after building a subset cache; startup cost and memory use depend on the subset size
  • Environment Variables: Set TAXDB_PATH to avoid repeating database paths
  • Choose Backend: Use TaxSQLite for large datasets with limited memory
  • Batch Operations: Process multiple TaxIDs in batches rather than individual calls
  • Subtree Queries: Reuse one database instance so descendants shares its cached children index. Building it costs one pass over the database (~0.4 s for 1M nodes); every query afterwards is proportional to the subtree, and small subtrees resolve in microseconds

Use Cases

  • Metagenomics: Classify and annotate environmental sequences
  • Phylogenetics: Build taxonomic trees and study evolutionary relationships
  • Bioinformatics: Pipeline integration for taxonomy-aware analysis
  • Data Validation: Verify and standardize organism names in datasets
  • Research: Large-scale taxonomic studies and biodiversity analysis

Requirements

  • Python: 3.10+
  • Dependencies: rapidfuzz, tqdm
  • Data: NCBI taxdump files (~300MB compressed, ~2GB extracted)
  • Memory: ~500MB RAM for full database (less with SQLite backend)

Development & Contributing

Development Setup

git clone https://github.com/omegahh/taxdumpy.git
cd taxdumpy
pip install -e '.[dev]'

Testing

# Run all tests (requires 80% coverage)
pytest

# Run specific test categories
pytest -m "not slow and not integration"  # Fast tests only
pytest -m integration                      # Integration tests only

# With coverage report
pytest --cov=taxdumpy --cov-report=html

Code Quality

# Format code and sort imports
ruff format src/ tests/
ruff check src/ tests/ --fix

Release Process

A pushed v* tag creates a GitHub Release. It does not build or publish a package to PyPI. Build and upload separately when publishing a release:

python -m build
python -m twine upload dist/*

The release helper requires a clean worktree, runs tests and Ruff, updates and commits the version, pushes origin main and a version tag, removes dist/, and builds the package. --upload also publishes to PyPI. Run it only when those repository and remote changes are intended:

python scripts/release.py --version 1.2.3 --upload

License

MIT License - see LICENSE for details.


Related Projects: TaxonKit (Go), ete3 (Python), taxizedb (R)

Metadata

Release files for taxdumpy 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for taxdumpy 1.3.0
File Size Uploaded Provenance Actions
taxdumpy-1.3.0.tar.gz 193.2 kB With provenance Details for taxdumpy-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for taxdumpy 1.3.0
File Interpreter ABI Platform Actions
taxdumpy-1.3.0-py3-none-any.whl Python 3 none any Details for taxdumpy-1.3.0-py3-none-any.whl

Total release size: 231.1 kB

Release files / taxdumpy-1.3.0.tar.gz

Download URL taxdumpy-1.3.0.tar.gz
Size 193.2 kB
Tags Source
SHA-256 checksum
How to use checksums
042446420acf317ede9512a3b326deaf708c3045558f572895af7e234fb018c1
BLAKE2b-256 checksum
How to use checksums
667ffae93fc38a69f6928b86db6b3213652fa25c00f4f32587ca9c479efc3059
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.

Transparency log

Release files / taxdumpy-1.3.0-py3-none-any.whl

Download URL taxdumpy-1.3.0-py3-none-any.whl
Size 37.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48dda94fd4c6753f09bd3b11794dbaf615a1b318fb3f0f118014f2a724e25003
BLAKE2b-256 checksum
How to use checksums
c2567ebd325d7b1f7de453f527ef68b45273b544c1049ac7faf86dc6ae418077
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.

Transparency log

Security for taxdumpy 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for taxdumpy-1.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page