TreeSwift
raw JSON → 1.1.45 verified Fri May 01 auth: no python
TreeSwift is a fast tree manipulation library for Python 2 and 3, providing a simple interface for reading, writing, and analyzing phylogenetic trees. The current version is 1.1.45, with regular updates and bug fixes.
pip install treeswift Common errors
error ModuleNotFoundError: No module named 'treeswift' ↓
cause Library not installed or installed in a different environment.
fix
Run
pip install treeswift in the correct Python environment. error TypeError: extract_tree_with() argument after * must be an iterable, not str ↓
cause Passing a string directly to `extract_tree_with` where an iterable of labels is expected.
fix
Wrap the string in a list:
tree.extract_tree_with(['label']) instead of tree.extract_tree_with('label'). Warnings
gotcha `extract_tree_with` and `extract_tree_without` convert string input to a set of individual characters. If you pass a single string like 'ABC', it will be treated as {'A','B','C'} rather than preserving the string as a single label. Pass a set or list instead. ↓
fix Use `extract_tree_with(set(['ABC']))` or `extract_tree_with(['ABC'])` to keep the label intact.
deprecated `Tree.read` can read gzip files directly, but file extension handling may be simplified in future versions. ↓
fix Pass an open file handle or a path; gzip is detected internally.
gotcha `distance_between` may return incorrect values when one node is an ancestor of the other in versions prior to 1.1.39. Ensure you have the latest version. ↓
fix Upgrade to >=1.1.39: `pip install --upgrade treeswift`.
Imports
- Tree wrong
from treeswift import read_treecorrectfrom treeswift import Tree - Node wrong
from treeswift.tree import Nodecorrectfrom treeswift import Node
Quickstart
from treeswift import Tree
tree = Tree()
tree.read('((A:0.1,B:0.2):0.3,C:0.4);')
print(tree) # Newick string
dist = tree.distance_between('A', 'C')
print(dist) # 0.8