High-level description
TheCompleteBinarySimulator class simulates a complete binary tree, where each node has exactly two children except for the leaves. It provides a way to generate a balanced tree topology with specified depth or number of cells, which can be used for in silico simulations of single-cell phylogenies.
Code Structure
TheCompleteBinarySimulator class inherits from the TreeSimulator abstract class and implements the simulate_tree method. It uses the networkx library to generate a balanced binary tree and then converts it into a CassiopeiaTree object.
References
This code references the following symbols:TreeSimulator: The abstract base class for tree simulators.CassiopeiaTree: The data structure representing a tree in Cassiopeia.TreeSimulatorError: An exception class for tree simulator errors.nx.balanced_tree: A function from thenetworkxlibrary to generate a balanced binary tree.
Symbols
CompleteBinarySimulator
Description
This class simulates a complete binary tree with a specified depth or number of cells. It uses thenetworkx library to generate a balanced binary tree and then converts it into a CassiopeiaTree object. The branch lengths are normalized by the height of the tree, resulting in a tree with a height of 1.
Inputs
| Name | Type | Description |
|---|---|---|
| num_cells | Optional[int] | Number of cells to simulate (must be a power of 2). |
| depth | Optional[int] | Depth of the tree. |
Outputs
This class doesn’t directly return any output. It simulates a tree and stores it internally.Internal Logic
- Initialization:
- Checks if exactly one of
num_cellsordepthis provided. - If
num_cellsis provided, calculates thedepthand validates thatnum_cellsis a power of 2. - If
depthis provided, validates that it is greater than 0.
- Checks if exactly one of
- Tree Simulation (
simulate_treemethod):- Generates a balanced binary tree using
nx.balanced_treewith a branching factor of 2. - Creates a mapping from the default integer node names to unique string names.
- Adds a root node (“root”) and connects it to the root of the generated binary tree.
- Relabels the nodes using the generated mapping.
- Creates a
CassiopeiaTreeobject from the generated tree. - Initializes branch lengths by dividing the time of each node by the total depth of the tree plus 1.
- Generates a balanced binary tree using
Side Effects
- Creates and stores a
CassiopeiaTreeobject internally.
Dependencies
| Dependency | Purpose |
|---|---|
| networkx | Used to generate the balanced binary tree. |
| numpy | Used for logarithmic calculations. |
| cassiopeia.data.CassiopeiaTree | Used to create the CassiopeiaTree object. |
| cassiopeia.mixins | Used to raise TreeSimulatorError in case of invalid input. |
| cassiopeia.simulator.TreeSimulator | The abstract base class for tree simulators. |
Error Handling
- Raises
TreeSimulatorErrorif:- Neither or both
num_cellsanddepthare provided. num_cellsis not a power of 2.depthis not greater than 0.
- Neither or both
