phylotreelib
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.1, 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
- Extract and prune subtrees
- Compare trees
- Build trees from distance matrices
- Export labels, annotations and colors
- Parsimony calculations
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.
Metadata
Release files for phylotreelib 3.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| phylotreelib-3.0.1.tar.gz | 4.2 MB | Without provenance | Details for phylotreelib-3.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| phylotreelib-3.0.1-py3-none-any.whl | Python 3 | none | any | Details for phylotreelib-3.0.1-py3-none-any.whl |
Total release size: 4.3 MB
Release files / phylotreelib-3.0.1.tar.gz
| Download URL | phylotreelib-3.0.1.tar.gz |
|---|---|
| Size | 4.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
240d63ba4ea3abffcbeb079ef63a883729c41891cd084ae9bde78b74251d1204
|
|
BLAKE2b-256 checksum How to use checksums |
0db668a8f34725b24086466cab89a5a21606e1f2a400cf221e99db6266a9a64a
|
| 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.1-py3-none-any.whl
| Download URL | phylotreelib-3.0.1-py3-none-any.whl |
|---|---|
| Size | 98.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a2bf9a7b17dca4ed05eb022471a0f3af358d165d25f0d738bb7018cdddaeeaab
|
|
BLAKE2b-256 checksum How to use checksums |
2f069625d3fddb7edc92ef5eb038df9d250ac6475170eb7f617267b5389a27b2
|
| 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.1
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.