Skip to main content

phylotreelib

PyPI version PyPI downloads

phylotreelib is a Python library for reading, writing, analyzing, comparing and manipulating phylogenetic trees. It can be used in scripts or interactively in notebooks.

Main capabilities include:

  • reading and writing Newick and Nexus trees, including tree annotations
  • querying tree structure, MRCAs, paths and distances
  • rooting, pruning, grafting and other topology edits
  • comparing trees using Robinson–Foulds and path-difference distances
  • building neighbor-joining and UPGMA trees from distance matrices
  • summarizing tree samples as consensus, MCC, MBC and HIPSTR trees

Installation

This README describes phylotreelib 3.0.0, which requires Python 3.10 or newer.

python3 -m pip install phylotreelib

To upgrade an existing installation:

python3 -m pip install --upgrade phylotreelib

Documentation

  • This README gives a quickstart and common examples.
  • Recipes give worked examples for reading, editing, comparing and summarizing trees.
  • The API reference describes arguments, data fields and copying/ownership rules.
  • The changelog records notable changes.

If you are updating scripts, see the 3.0 migration guide and the older 1.x upgrade examples.

Quick start

Construct and query a tree

import phylotreelib as pt

tree = pt.Tree.from_string("(Orangutan:4,(Gorilla:3,(Human:2,(Chimpanzee:1,Bonobo:1):1):1):1);")
print(tree)
print(tree.newick())

mrca = tree.find_mrca({"Human", "Chimpanzee"})
print("Descendant leaves:", tree.remote_children(mrca))
print("Human–Bonobo distance:", tree.nodedist("Human", "Bonobo"))

Node height means distance from the most recent tips to the node. Version 1 called this depth. See the upgrade examples for renamed methods and arguments.

Read trees from a file

Treefile detects Newick or Nexus format. Iterate over a file to process trees without storing them all:

import phylotreelib as pt

with pt.Treefile("posterior.trees") as treefile:
    treefile.skip_trees(100)  # Discard burn-in.
    for tree in treefile:
        print(tree.length())

Use Newicktreefile or Nexustreefile when the format is known. readtree() returns one Tree, or None at the end of the file. More input examples, including raw tree text, are in the reading recipes.

Root and prune a tree

import phylotreelib as pt

tree = pt.Tree.from_string("((A:1,B:1):1,((C:1,D:1):1,(E:1,F:1):1):1);")
tree.rootmid()
tree.remove_leaves(["B", "D"])
print(tree.newick())

See the recipes for other rooting methods, grafting and SPR moves.

Read and write annotations

Use parse_meta=True to read annotation values. Default reading skips optional annotations but retains supported rootedness declarations and root labels/stem lengths.

import phylotreelib as pt

tree = pt.Tree.from_string('[&R](A[&location="Denmark"]:1,B:1);', parse_meta=True)
print(tree.nodedict["A"].extra_meta["location"])
print(tree.nexus(print_meta=True))

Printing options apply to each call. print_meta=True includes populated annotations. Select particular keys with node_attributes and branch_attributes, or use an empty tuple to omit that kind of annotation. Output uses full stored precision unless a numeric precision is requested.

tree.rooted is True, False or None. Nexus output declares known state with [&R] or [&U]. Plain Newick does not include a declaration. See the input and output API for positional annotation rules, missing values and output controls.

Summarize posterior tree samples

TreeSummary collects frequencies and optional length/height statistics. build_sumtree() constructs and annotates the requested summary tree.

For command-line use, the separate tool sumt provides these workflows in a single command.

Compute credible intervals and export a summary tree

This self-contained example builds an MCC tree with mean clade heights and 80% and 95% credible intervals:

import phylotreelib as pt

samples = (
    "((A:1,B:1):2,(C:2,D:2):1);",
    "((A:1.5,B:1.5):2.5,(C:2,D:2):2);",
    "((A:1.2,B:1.2):2.8,(C:2.5,D:2.5):1.5);",
)
summary = pt.TreeSummary(
    trackclades=True,
    trackheight=True,
    tracktopo=True,
    trackci=True,
    ci_probs=(0.8, 0.95),
)
for sample in samples:
    summary.add_tree(pt.Tree.from_string(sample))

sumtree = pt.build_sumtree(summary, treetype="mcc", blen="cladeheight")
print(sumtree.nexus(print_meta=True))

For a real analysis, read samples from a file and exclude burn-in before adding observations. Specify both trackci=True and ci_probs. These are central, equal-tailed intervals. Variable-height quantiles are approximate, while effectively constant node heights retain the observed value.

See the summary-tree recipes for consensus trees, common-ancestor heights and selected interval output. If you merge partial summaries or retain input representatives, follow the ownership guidance.

More examples

Upgrading existing scripts

Version 3.0 removes stored printing settings, reader strip_comments, below_root and public .ci storage. Printing and interval selection are now explicit. Read the migration table before updating callers.

The older upgrade recipes cover the 1.x-to-2.x height renames, grafting/SPR arguments and subtree return values.

Known development work is recorded in the maintenance notes.

Metadata

Release files for phylotreelib 3.0.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 phylotreelib 3.0.0
File Size Uploaded Provenance Actions
phylotreelib-3.0.0.tar.gz 4.2 MB Without provenance Details for phylotreelib-3.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 4.3 MB

Release files / phylotreelib-3.0.0.tar.gz

Download URL phylotreelib-3.0.0.tar.gz
Size 4.2 MB
Tags Source
SHA-256 checksum
How to use checksums
e9b8b11755a7d55716e8590698468b056bed206726309c252f7f4b22d99104cf
BLAKE2b-256 checksum
How to use checksums
ee04535c318c4aeff4233ceb8cb23bc6c37f5c36aad08c0c82ac933b68b8aa42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release files / phylotreelib-3.0.0-py3-none-any.whl

Download URL phylotreelib-3.0.0-py3-none-any.whl
Size 99.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0c8daa5a7bd50b3a3ef214690c404790b8517d3a19141023535afc766fd545c
BLAKE2b-256 checksum
How to use checksums
e09fd0724824d86171730b3facfa7d016f99d83a7cb85b03e24964b623824a6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Security for phylotreelib 3.0.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files No provenance

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