Skip to main content

taxutils

Utilities for working with NCBI taxonomic data, accession-to-taxon mappings, taxonomy branches, corrected ranks, and pathogen target taxa.

Install

Conda Formula

conda install bioconda::taxutils

Pip Formula

pip install taxutils

Setup

taxutils stores downloaded taxonomy files (names.dmp, nodes.dmp), pathogen target metadata, and accession-to-taxon mappings in a global save directory. Set TAXUTILS_GLOBALS before importing the package if you want to control where these files live:

export TAXUTILS_GLOBALS=/path/to/taxutils/saves

If TAXUTILS_GLOBALS is not set, taxutils defaults to ./taxutils/ in the current working directory.

The first run downloads NCBI taxonomy files. Accession lookups also use the NCBI accession-to-taxon mapping, which is large. By default, taxutils uses low-memory mode and scans the compressed mapping directly. For faster repeated lookups, use low_memory=False to build or reuse a local SQLite database:

from taxutils import taxutils

tu = taxutils(low_memory=False)

Core usage

Core functions are listed here. See the example notebook for a fuller walkthrough.

# Build object
tu = taxutils(accessions=None, low_memory=True, targets_json=None, rebuild=False)  # Build the taxonomy utility object.

# Accession parsing and mapping
tu.parse_accession(header_strings, version=True)          # Extract one accession per string.
tu.load_a2t(accessions, low_memory=None, extend=False)    # Load accession-to-taxon mappings into tu.a2t.
tu.get_t2a(taxa, low_memory=None)                         # Return accessions assigned to taxa.

# Tree queries
tu.get_branch(taxon)                                      # Return the root-to-taxon branch.
tu.get_subtree(taxon)                                     # Return taxon plus all descendants.
tu.get_lca(taxon_a, taxon_b)                              # Return the lowest common ancestor.
tu.get_distance(taxon_a, taxon_b)                         # Return tree edge distance through the LCA.
tu.sort_taxa(taxa)                                        # Sort taxa in taxonomic order.
tu.format_tree(taxa)                                      # Return an indented tree Series.
tu.topology(taxon, anchor_rank=None)                      # Return subtree topology metrics.
tu.topology(taxon, anchor_rank=None, stat="topology_scale")  # Return one topology statistic.

# Rank utilities
tu.get_rank_order()                                       # Return canonical rank codes.
tu.higher_than_rank(taxa, rank)                           # Test whether taxa are higher than a rank.

In taxutils, accessions=list/of/accessions can be passed to call load_a2t on construction of the taxutils object. A custom targets_json can similarly be passed in lieu of the default json explained below. rebuild=True redownloads the managed taxonomy, target, and accession files and rebuilds the SQLite accession database. load_a2t overwrites tu.a2t by default; pass extend=True to add missing mappings without discarding existing ones. Method-level low_memory=None uses the mode set when tu was built.

parse_accession accepts strings, lists, arrays, and pandas Series. It returns the first accession found from each string using the same container type where possible; missing accessions are returned as "NA".

get_lca(a, b) returns the lowest common ancestor of two taxa. get_distance(a, b) returns the edge distance between two taxa through their lowest common ancestor. Depths are cached lazily as these methods are called.

topology(taxon, anchor_rank=None, stat=None) returns subtree topology metrics such as taxon count, leaf fraction, depth, branchiness, and topology_scale. Pass anchor_rank="F" to summarize the nearest family-level ancestor. With stat=None, a single taxon returns a Series and a list, array, or Series returns a DataFrame.

Pass stat to return one topology metric. A single taxon returns a scalar; a list, array, or Series returns a Series indexed by taxon.

Topology columns

tu.topology(...) returns these columns:

  • taxon: input taxon.
  • name: input taxon name.
  • rank_code: corrected rank code for the input taxon.
  • anchor_taxon: subtree root used for the topology summary.
  • anchor_name: anchor taxon name.
  • anchor_rank_code: corrected rank code for the anchor.
  • n_taxa: number of taxa in the anchor subtree.
  • n_leaves: number of terminal taxa in the anchor subtree.
  • max_depth: maximum number of edges below the anchor.
  • mean_depth: average number of edges below the anchor.
  • topology_scale: 95th percentile descendant depth, with minimum value 1.
  • max_children: largest number of direct children from any node in the subtree.
  • branching_taxa_fraction: fraction of subtree taxa with at least one child.
  • top_child_fraction: fraction of the anchor subtree contained in its largest immediate child branch.

tu.target_taxa contains the default pathogen-derived target taxa. Use it directly for target filtering or movement checks.

Rank correction

taxutils keeps the raw NCBI rank in rank and adds corrected rank columns. Canonical ranks (R, D, K, P, C, O, F, G, S) are used as anchors only when they move deeper than the corrected parent rank. Noncanonical ranks such as no rank, clade, and other unusual labels inherit position from the tree. If a child would be ranked at the same or a higher level than its parent, it is assigned a subrank such as S2, S3, or F2. The canonical name for the corrected rank is stored in new_rank.

Target taxa

In ZarLab, we are working on metagenomics in the clinical setting, with the goal of creating an "agnostic diagnostic". We often want to look at broad array of taxa (tu.target_taxa) that could cause harm to people. In June 2024, CZI did the work of compiling a list of pathogenic taxa. I did the easy work of turning this into a json and uploading it to my website, so that it is available and easily accessed for all time (in case that link ever breaks). taxutils will extend the taxa list to include subtrees of each of those pathogenic taxa. It will additionally include SARS-CoV2, since it was excluded from CZI's list. If you find any other obvious, missing pathogens, please send me a note, so I can update my json. You can also update the target_taxa member variable yourself, or store an entirely different set of targets, if you wanted.

Contact

Author: Will O'Brien
Affiliation: Computer Science Department, UCLA
Email: wob@cs.ucla.edu

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

taxutils-1.0.1.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

taxutils-1.0.1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file taxutils-1.0.1.tar.gz.

File metadata

  • Download URL: taxutils-1.0.1.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.5 Linux/5.10.244-240.970.amzn2.x86_64

File hashes

Hashes for taxutils-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a963025af4a2f1035273edc79037dd2f382e414e34b6c94f212f06e8115e36c9
MD5 e6897f361b3fcd03ca8b799298847acb
BLAKE2b-256 e26e057c3ad4ba62e507ee4812f2d9f501db1a4e901867bbf4fe9889b81046fc

See more details on using hashes here.

File details

Details for the file taxutils-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: taxutils-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.5 Linux/5.10.244-240.970.amzn2.x86_64

File hashes

Hashes for taxutils-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 81f3f4fcb7b08e801b86765d61dc7219c1c80655e04f2d4c025b2267154fc1da
MD5 7d432f5275e0c0df401f3806cf1e375d
BLAKE2b-256 512454a98257c7d8234fd5b7887597ad334b69307421bd296997d51f590cc321

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.1

8 files

1.0.3

2 files

1.0.2

2 files

This release

1.0.1 This release

2 files

1.0.0

2 files

0.1.0

2 files

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