-
Notifications
You must be signed in to change notification settings - Fork 29
Method `baltic.tree.add_leaf`
Barney Potter edited this page Oct 14, 2024
·
1 revision
The add_leaf()
method in the BALTIC tree
class is used to attach a new leaf (tip) to the current node in the tree. Leaves represent the terminal nodes of the phylogenetic tree, typically corresponding to extant species or sampled sequences.
def add_leaf(self, i, name)
-
i
(int): The index of the new leaf, representing its position along the tree string. -
name
(str): The name of the new leaf.
- Creates a new
leaf
object. - Sets the index of the new leaf to the provided value
i
. - If this is the first object being added (i.e., the root doesn't exist yet):
- Sets this leaf as the root of the tree.
- Sets the parent of the new leaf to the current node (
self.cur_node
). - Adds the new leaf as a child of the current node.
- Sets the name of the leaf to the provided
name
. - Updates the current node to be this new leaf.
- Appends the new leaf to the tree's
Objects
list.
- Adding terminal nodes (tips) to the tree when constructing it from a Newick string or similar representation.
- Manually adding samples or species to an existing tree structure.
- Extending a tree with new data points or taxa.
# Assuming we have a tree object with at least one node
tree.add_leaf(1, "Species_A") # Adds a leaf with index 1 and name "Species_A"
tree.add_node(2) # Move back to an internal node
tree.add_leaf(3, "Species_B") # Adds another leaf with index 3 and name "Species_B"
- This method assumes that leaves are being added in a logical order (e.g., as they would appear in a Newick string).
- The method includes an assertion to ensure that you're not attempting to add a child to a non-node object. This helps prevent structural errors in the tree.
- After adding a leaf, it becomes the new current node. However, since leaves cannot have children, you typically need to navigate back up the tree (e.g., by using
add_node()
) before adding more elements. - The index
i
is typically used to keep track of the leaf's position in the original tree representation (e.g., a Newick string) and serves as a unique identifier for the leaf. - The
name
parameter allows you to assign meaningful labels to the leaves, such as species names, sample IDs, or sequence identifiers.
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