-
Notifications
You must be signed in to change notification settings - Fork 29
Method `baltic.tree.collapseSubtree`
Barney Potter edited this page Oct 14, 2024
·
1 revision
The collapseSubtree()
method in the BALTIC tree
class is used to collapse an entire subtree into a single clade object. This method is useful for simplifying complex tree structures, focusing on specific parts of the tree, or reducing the level of detail in certain analyses.
def collapseSubtree(self, cl, givenName, verbose=False, widthFunction=lambda k: len(k.leaves))
-
cl
(node): The node representing the root of the subtree to collapse. -
givenName
(str): The name to assign to the new clade. -
verbose
(bool): If True, prints verbose output during the process. Default is False. -
widthFunction
(function): A function to determine the width of the clade when computing branch y coordinates indrawTree()
. Default calculates width based on the number of leaves.
-
clade
: The newly created clade object representing the collapsed subtree.
- Creates a new
clade
object with the given name. - Transfers relevant attributes from the subtree root to the new clade (index, leaves, length, height, parent, absoluteTime, traits).
- Calculates the width of the new clade using the provided
widthFunction
. - Removes all nodes in the subtree from the tree's
Objects
list. - Replaces the subtree with the new clade in the parent's children list.
- Updates the tree structure (traverses the tree and sorts branches).
- Simplifying complex trees by collapsing less relevant subtrees.
- Focusing analysis or visualization on specific parts of the tree.
- Creating summary representations of large phylogenies.
- Preparing trees for comparative analyses where some clades are treated as single units.
# Assuming we have a tree and want to collapse a subtree
subtree_root = tree.getInternal()[5] # Some internal node
# Collapse the subtree
collapsed_clade = tree.collapseSubtree(subtree_root, "Collapsed_Clade_1", verbose=True)
print(f"Collapsed clade name: {collapsed_clade.name}")
print(f"Number of leaves in collapsed clade: {len(collapsed_clade.leaves)}")
# Using a custom width function
def custom_width(node):
return len(node.leaves) * 2 # Make the clade twice as wide
tree.collapseSubtree(another_subtree_root, "Collapsed_Clade_2", widthFunction=custom_width)
- This method modifies the tree in-place. If you need to preserve the original tree, make a deep copy before collapsing subtrees.
- The collapsed clade retains information about its constituent leaves, which can be accessed through the
leaves
attribute. - The
widthFunction
parameter allows customization of how the collapsed clade is represented visually when the tree is drawn. - Collapsing subtrees can significantly reduce the complexity of the tree, which may be beneficial for both computational efficiency and visual clarity.
- The method includes checks to ensure you're not attempting to collapse the entire tree.
- After collapsing, you may need to update any analyses or visualizations that relied on the detailed structure of the collapsed subtree.
- The
verbose
option can be useful for debugging or understanding the collapsing process, especially for large or complex subtrees.
Wiki written with the assistance of claude.ai 3.5 "Sonnet".
- Core
baltic
classes:-
Class
tree
- Tree Construction and Manipulation methods
- Tree Analysis methods
- Tree Conversion and Output methods
- Tree Visualization
- Utility Methods
- Class
leaf
- Class
node
- Class
clade
- Class
reticulation
-
Class