DendroPy

raw JSON →
5.0.8 verified Mon Apr 27 auth: no python

A Python library for phylogenetics and phylogenetic computing: reading, writing, simulation, processing and manipulation of phylogenetic trees (phylogenies) and characters. Current version 5.0.8 (requires Python >=3.7). The library has undergone major API changes from v4 to v5 (e.g., removal of deprecated modules, new tree structure classes). Release cadence is irregular, with v5.0.0 released in late 2023 and subsequent bugfix releases.

pip install dendropy
error dendropy.exception.DendroPyError: No trees found in file.
cause Incorrect schema argument or file contains no trees (e.g., only taxon definitions).
fix
Specify the correct schema, e.g., Tree.get(..., schema='nexus') for NEXUS files. Ensure file contains tree data.
error AttributeError: module 'dendropy' has no attribute 'tree'
cause Old code trying to access 'dendropy.tree' directly (v4 pattern). 'tree' is no longer a submodule; use 'dendropy.Tree'.
fix
Replace 'dendropy.tree' with 'dendropy.Tree'.
breaking DendroPy v5 introduced breaking changes: removal of deprecated modules (e.g., 'dendropy.utility', 'dendropy.dataio') and renaming of some classes. Code written for v4 may not work directly.
fix Update imports to use the new 'dendropy' top-level namespace. Refer to migration guide on GitHub.
gotcha When reading trees, ensure the schema argument matches the file format exactly (e.g., 'newick', 'nexus', 'nexml'). Incorrect schema often leads to 'No tree found' or parsing errors.
fix Double-check the file format and use the correct schema string. For example, use schema='nexus' for NEXUS files.

Load a tree from a Newick file and print it.

import dendropy
tree = dendropy.Tree()
# Read from a file (Newick example)
tree = dendropy.Tree.get(path='example.tre', schema='newick')
print(tree.as_string(schema='newick'))