hypertreeTree Class DocumentationThe Tree class is designed to manage and manipulate phylogenetic trees using the treeswift package, while offering additional utilities for embedding, logging, and tree operations. It provides methods to load trees from files, compute distances, normalize branch lengths, and embed trees in Euclidean or hyperbolic spaces. This class is particularly useful for handling large-scale tree data, embedding them into various geometric spaces, and generating visualizations, making it an essential tool for researchers working in phylogenetics or comparative genomics. UsageThe Tree class supports two modes of initialization:
Example: Initialize from a file path
from your_package import tree_collections tree = tree_collections.Tree('path/to/treefile.nwk’) Example: Initialize with a name and treeswift.Tree object
import treeswift as ts t = ts.read_tree_newick('path/to/treefile.nwk’) tree = tree_collections.Tree('Tree_Name’, t) LoggingThe class supports logging for debugging and tracing execution. By setting enable_logging=True, a log file will be created in the specified directory (default: as configured in your package’s config) Example: Enable logging when initializing the tree
tree = tree_collections.Tree('path/to/treefile.nwk’, enable_logging=True) Distance Matrix and Tree OperationsOnce a tree is loaded, the Tree class provides functions to compute key properties like the distance matrix, diameter, and terminal node names.
Example: Compute distance matrix and normalize the tree
terminals = tree.terminal_names() distance_matrix = tree.distance_matrix() diameter = tree.diameter() Tree NormalizationThe normalize() method scales the branch lengths of the tree such that the maximum distance (tree diameter) is set to 1. This is particularly useful before performing embeddings, to ensure consistent scaling across different trees. Example: Normalize tree to have a diameter of 1
tree.normalize() EmbeddingThe embed() method allows embedding the phylogenetic tree into either Euclidean or hyperbolic geometry using different dimensions. This is useful for visualizing or analyzing trees in a geometric space. This method has several optional parameters that allow customization of the embedding process. Below is a detailed explanation of each parameter.
Example
def default_scale_learning(epoch: int, total_epochs: int, loss_list = List[float]) -> bool: if total_epochs <= 1: raise ValueError(“Total epochs must be greater than 1.”) return epoch < int(0.5 * total_epochs) Example: Embed the tree into a 3D Euclidean space
embedding = tree.embed(dimension=3, geometry='euclidean’) tree.embed(dimension=2, accurate=True) Saving and Copying TreesThe class provides utility functions to save and copy trees: save_tree(): Save the tree to a file in Newick format. copy(): Create a deep copy of the tree object, useful for making modifications without affecting the original tree. Example: Save and copy a Tree
tree.save_tree(“/path/to/save/treefile.nwk”) tree_copy = tree.copy() VisualizationExample Workflowtree = tree_collections.Tree('path/to/treefile.nwk’) tree.normalize() embedding = tree.embed(dimension=2, geometry='hyperbolic’) >>> print 'Interactive Python code.’ from your_package import tree_collections tree = tree_collections.Tree('path/to/treefile.nwk’) import treeswift as ts t = ts.read_tree_newick('path/to/treefile.nwk’) tree = tree_collections.Tree('Tree_Name’, t, enable_logging=True) |