Skip to content

Method `baltic.tree.add_leaf`

Barney Potter edited this page Oct 14, 2024 · 1 revision

tree.add_leaf() Method

Description

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.

Syntax

def add_leaf(self, i, name)

Parameters

  • i (int): The index of the new leaf, representing its position along the tree string.
  • name (str): The name of the new leaf.

Functionality

  1. Creates a new leaf object.
  2. Sets the index of the new leaf to the provided value i.
  3. 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.
  4. Sets the parent of the new leaf to the current node (self.cur_node).
  5. Adds the new leaf as a child of the current node.
  6. Sets the name of the leaf to the provided name.
  7. Updates the current node to be this new leaf.
  8. Appends the new leaf to the tree's Objects list.

Use Cases

  1. Adding terminal nodes (tips) to the tree when constructing it from a Newick string or similar representation.
  2. Manually adding samples or species to an existing tree structure.
  3. Extending a tree with new data points or taxa.

Example

# 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"

Notes

  • 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.
Clone this wiki locally