PhyNetPy Documentation

Library for the Development and Use of Phylogenetic Network Methods

ModelGraph Module v0.1.0

Probabilistic graphical model for phylogenetics with typed model nodes and visitor pattern support.

Author:
Mark Kessler
Last Edit:
3/11/25
Source:
ModelGraph.py

Exceptions

exception ModelError(Exception)

Class to handle any errors related to building the model or running likelihoods computations on the model.

Model

class Model

Class that implements a version of probabilistic graphical modeling, for phylogenetics. Generally, it is made up of a network, along with various parameters and components that are attached to the model in order to compute either a model likelihood or simulate data over a set of model parameters.

Constructor

__init__()

Initialize a model object.

Methods

get_root -> ModelNode

Returns the root node of the model.

execute_move(move: Move) -> Model

The operator move has asked for permission to work on this model. Pass the move this model and get the model that is the result of the operation on this model.

Parameter Type Description
move Move A concrete subclass instance of Move.
Returns: Model: This same object. The model will have changed based on the result of the move.
summary(tree_filename: str, summary_filename: str) -> None

Writes summary of calculations to a file, and gets the current state of the model and creates a network obj so that the newick format can be output.

Parameter Type Description
tree_filename str A string that is the name of the file to output a newick string to. If the filename does not
exist a new file will be created in the directory in which one is operating in.
summary_filename str A string that is the name of the file to output logging information. If the filename does not exist, a new file will be created in the current directory.

ModelNode

class ModelNode(ABC)

Class that defines the graphical structure and shared interactions between any node in the Model.

Constructor

__init__(children: list = None, parents: list = None) -> None

Initialize a ModelNode object.

Parameter Type Description
children list, optional Children of this node. Defaults to None.
parents list, optional Parents of this node. Defaults to None.
node_type str, optional A string that describes the type of node.

Methods

add_child(model_node: ModelNode) -> None

Adds a successor to this node.

Parameter Type Description
model_node ModelNode A ModelNode to add as a child.
add_parent(model_node: ModelNode) -> None

Adds a predecessor to this node.

Parameter Type Description
model_node ModelNode A ModelNode to add as a parent.
join(other_node: ModelNode) -> None

Adds other_node as a parent, and adds this node as a child of other_node.

Parameter Type Description
other_node ModelNode A ModelNode to join this ModelNode to.
unjoin(other_node: ModelNode) -> None

Removes other_node as a parent, and removes this node as a child of other_node.

Parameter Type Description
other_node ModelNode A ModelNode to unjoin this ModelNode from.
remove_child(model_node: ModelNode) -> None

Removes a child to this node.

Parameter Type Description
model_node ModelNode A ModelNode to remove as a child.
remove_parent(model_node: ModelNode) -> None

Removes a parent to this node.

Parameter Type Description
model_node ModelNode A ModelNode to remove as a parent.
get_model_parents(of_type: type = None) -> list[ModelNode]

Get the parent nodes to this node, but only the ModelNodes.

Parameter Type Description
of_type type, optional A type to filter the parent nodes by.
Returns: list[ModelNode]: The list of parent nodes to this node
get_model_children(of_type: type = None) -> list[ModelNode]

Get the children nodes to this node, but only the ModelNodes.

Parameter Type Description
of_type type, optional A type to filter the child nodes by.
Returns: list[ModelNode]: The list of child nodes to this node
in_degree -> int

Calculates the in degree of the current node (ie number of parents) If 0, this node is a root of the Model.

Returns: int: The number of parents to this node.
out_degree -> int

Calculates the out degree of the current node (ie number of children) If 0, this node is a leaf.

Returns: int: The number of children to this node.
get_node_type -> str

Get the type of this node.

accept(visitor: Visitor) -> Any

Accept a visitor and return the result of the visit.

Parameter Type Description
visitor ModelVisitor A visitor to visit this node.
Returns: Any: The result of the visit.

LeafNode

class LeafNode(ModelNode)

A leaf node in the model graph.

Constructor

__init__(name: str, branch_length: Branch, data: list[DataSequence] = None, samples: int = 1) -> None

Initialize a LeafNode object.

Parameter Type Description
name str The name of this leaf node.

Methods

get_samples -> int

Returns the number of samples for this leaf node.

get_name -> str

Returns the name of this leaf node.

branch -> Branch

Returns information about the branch associated with this leaf node. Since it is a leaf node, there must be exactly one network parent and thus only one branch

Returns: Branch: A Branch object containing information about the branch.
set_branch(branch_length: float) -> None

Set the branch length of the branch associated with this leaf node.

Parameter Type Description
branch_length float The new branch length.
set_data(data: list[DataSequence]) -> None

Set the data for this leaf node.

InternalNode

class InternalNode(ModelNode)

An internal node in the model graph.

Constructor

__init__(name: str, branch_length: Branch) -> None

Initialize an InternalNode object.

Parameter Type Description
branch_length Branch, optional The branch associated with this internal node. Defaults to a new Branch object.

Methods

branch -> Branch

Returns information about the branch associated with this internal node. Since it is a internal node, there must be exactly one network parent and thus only one branch

Returns: Branch: A Branch object containing information about the branch.
set_branch(branch_length: float) -> None

Set the branch length of the branch associated with this leaf node.

Parameter Type Description
branch_length float The new branch length.
get_name -> str

Returns the name of this leaf node.

ReticulationNode

class ReticulationNode(ModelNode)

A reticulation node in the model graph.

Constructor

__init__(name: str, branch_1: Branch, branch_2: Branch) -> None

Initialize a ReticulationNode object.

Parameter Type Description
branch_1 Branch, optional The first branch. Defaults to 0.5.
branch_2 Branch, optional The second branch. Defaults to 0.5.

Methods

get_name -> str

Returns the name of this leaf node.

branches -> tuple[Branch, Branch]

Returns information about the branch associated with this reticulation node. Since it is a reticulation node, there must be exactly two network parents and thus two branches

Returns: tuple[Branch, Branch]: A tuple of Branch objects containing information about the branches.
set_branch(parent_id: str, branch_length: float = None, inheritance_probability: float = None) -> None

Set the branch length and inheritance probability of the branch associated with this reticulation node.

Parameter Type Description
parent_id str The id of the parent node.
branch_length float, optional The new branch length. Defaults to None.
inheritance_probability float, optional The new inheritance probability. Defaults to None.

RootNode

class RootNode(ModelNode)

A root node in the model graph.

Constructor

__init__(name: str) -> None

Initialize a RootNode object.

Methods

get_name -> str

Returns the name of this leaf node.

RootAggregatorNode

class RootAggregatorNode(ModelNode)

A root aggregator node in the model graph.

Constructor

__init__() -> None

Initialize a RootAggregatorNode object.

Methods

get_name -> str

Returns the name of this leaf node.

get_result -> Any

Returns the result of this root aggregator node.

Module Functions

vec_bin_array(arr: np.array, m: int) -> np.array

Convert an array of integers into a binary array of bits.

Parameter Type Description
arr np.array Numpy array of positive integers
m int Number of bits of each integer to retain (ie DNA, 4)
Returns: (np.array): a copy of arr with every element replaced with a bit vector. Bits encoded as int8's, read from left to right. [1, 0, 1, 1] is 13.

Navigation

Modules

This Page