PhyNetPy Documentation

Library for the Development and Use of Phylogenetic Network Methods

Network Module v1.0.0

Core phylogenetic network data structures: Node, Edge, and Network classes.

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

Exceptions

exception NetworkError(Exception)

This exception is raised when a network is malformed, or if a network operation fails.

exception NodeError(Exception)

This exception is raised when a Node operation fails.

exception EdgeError(Exception)

This exception is raised when an Edge operation fails.

Node

class Node

Node class that provides support for managing network constructs like reticulation nodes and other phylogenetic attributes.

Properties

label -> str property

Returns the name of the node

Returns: str: Node label.

Constructor

__init__(name: str, is_reticulation: bool = False, attr: dict[Union[str, Any], Any] = dict(), seq: Union[DataSequence, None] = None, t: Union[float, None] = None) -> None

Initialize a node with a name, attribute mapping, and a hybrid flag.

Parameter Type Description
name str A Node label.
is_reticulation bool, optional Flag that marks a node as a reticulation node if set to True. Defaults to False.
attr dict[Union[str, Any], Any], optional Fill a mapping with any other user defined values. Defaults to an empty dictionary.
seq Union[DataSequence, None], optional A data sequence wrapper.
t Union[float, None], optional A speciation time for this node.

Methods

get_attributes -> dict[Union[str, Any], Any]

Retrieve the attribute mapping.

Returns: dict[Union[str, Any], Any]: A storage of key value pairs that correspond to user defined node attributes. Typically keys are string labels, but they can be anything.
set_attributes(new_attr: dict[Union[str, Any], Any]) -> None

Set a node's attributes to a mapping of key labels to attribute values.

Parameter Type Description
new_attr dict[Union[str, Any], Any] Attribute storage mapping.
get_time -> float

Get the speciation time for this node. Closer to 0 implies a time closer to the origin (the root). A larger time implies a time closer to the present (leaves).

Returns: float: Speciation time, typically in coalescent units.
set_time(new_t: float) -> None

Set the speciation time for this node. The arg 't' must be a non-negative number.

Parameter Type Description
new_t float The new speciation/hybridization time for this node.
Raises: NodeError: If the new time is negative.
__str__ -> str

Create a string representation of a node.

Returns: str: A string representation of the node.
__eq__(other) -> bool

Equality comparison for Node objects. Two nodes are equal if they have the same name (identity). Note: This is based on node identity, not biological distance.

Parameter Type Description
other Another object to compare against
Returns: bool: True if both are Node objects with the same name
__hash__ -> int

Hash function for Node objects. Uses the node name for hashing to maintain consistency with equality.

Returns: int: Hash value based on the node name
to_string -> str

Create a description of a node and summarize its attributes.

Returns: str: A string description of the node.
set_name(new_name: str) -> None

Sets the name of the node to new_name.

Parameter Type Description
new_name str A new string label for this node.
set_is_reticulation(new_is_retic: bool) -> None

Sets whether a node is a reticulation Node (or not).

Parameter Type Description
new_is_retic bool Hybrid flag. True if this node is a reticulation node, false otherwise
is_reticulation -> bool

Retrieves whether a node is a reticulation Node (or not)

Returns: bool: True, if this node is a reticulation. False otherwise.
add_attribute(key: Any, value: Any, append: bool = False) -> None

Put a key and value pair into the node attribute dictionary. If the key is already present, it will overwrite the old value.

Parameter Type Description
key Any Attribute key.
value Any Attribute value for the key.
append bool, optional If True, appends the given value to the existing value for the key. If false, simply replaces. Defaults to False.
attribute_value(key: Any) -> object

If key is a key in the attributes mapping, then its value will be returned. Otherwise, returns None.

Parameter Type Description
key Any A lookup key.
Returns: object: The value of key, if key is present.
set_seq(new_sequence: DataSequence) -> None

Associate a data sequence with this node, if this node is a leaf in a network.

Parameter Type Description
new_sequence DataSequence A data sequence wrapper. Grab from MSA object upon parsing.
get_seq -> DataSequence

Gets the data sequence associated with this node.

Returns: DataSequence: Data sequence wrapper.
Raises: NodeError: If no sequence record has been associated with this node.
copy -> Node

Duplicate this node by copying all data into a separate Node object. Useful for crafting copies of networks without having to deep copy.

Returns: Node: An equivalent node to this node, with all the same data but technically are not "=="

NodeSet

class NodeSet

Data structure that is in charge of managing the nodes that are in a given Network

Constructor

__init__(directed: bool = True) -> None

Initialize an empty set of network nodes

Parameter Type Description
directed bool, optional True if the network is directed, False otherwise. Defaults to True.

Methods

__contains__(n: Node) -> bool

Check if a node is in the network node set.

Parameter Type Description
n Node A node to check for in the network.
Returns: bool: True if the node is in the network, False otherwise.
add(*nodes: Node | list[Node]) -> None

Add a list of nodes to the network node set.

Parameter Type Description
node Node | list[Node] A list of new nodes to put in the network.
ready(edge: Union[Edge, UEdge]) -> bool

Check if an edge is allowed to be added to the network (both nodes must be in the node set before an edge can be added). A undirected edge is not allowed to be inserted into a directed network, and a directed edge is not allowed to be inserted into an undirected graph. False will be returned in such instances.

Parameter Type Description
edge Union[Edge, UEdge] A potential new network edge.
Returns: bool: True if edge can be safely added, False otherwise.
in_deg(node: Node) -> int

Gets the in degree of a node in this set. Returns 0 if the node has no in edges, or if the node is not in the node set. It is up to the user to make sure the parameter node is in the node set.

Parameter Type Description
node Node any Node obj
Returns: int: the in degree of the node
out_deg(node: Node) -> int

Gets the out degree of a node in this set. Returns 0 if the node has no out edges, or if the node is not in the node set. It is up to the user to make sure the parameter node is in the node set.

Parameter Type Description
node Node any Node obj
Returns: int: the out degree of the node
in_edges(node: Node) -> set[Any]

Gets the in edges of a node in this set. Returns an empty set if the node has no in edges, or if the node is not in the node set. It is up to the user to make sure the parameter node is in the node set. Note: Returns a reference to the internal set. Copy before iterating if you plan to modify the graph during iteration.

Parameter Type Description
node Node any Node obj
Returns: set[Any]: the in edges of the node
out_edges(node: Node) -> set[Any]

Gets the out edges of a node in this set. Returns an empty set if the node has no out edges, or if the node is not in the node set. It is up to the user to make sure the parameter node is in the node set. Note: Returns a reference to the internal set. Copy before iterating if you plan to modify the graph during iteration.

Parameter Type Description
node Node any Node obj
Returns: set[Any]: the out edges of the node
process(edge: Union[Edge, UEdge], removal: bool = False) -> None

Keep track of network data (in/out degrees, in/out edge maps) upon the addition or removal of an edge for a network.

Parameter Type Description
edge Union[Edge, UEdge] The edge that is being added or removed
removal bool, optional False if edge is being added, True if edge is being removed. Defaults to False.
get_set -> set[Node]

Grab the set of nodes.

Returns: set[Node]: V, the node set of a network.
remove(node: Node) -> None

Remove a node from V, and update necessary mappings.

Parameter Type Description
node Node Node to remove from the network.
update(node: Node, new_name: str) -> None

Protected method that processes updates to node labels within the NodeSet. You may not set a Node's name to None.

Parameter Type Description
node Node The Node that is being renamed
new_name str The new name.
Raises: NodeError: If @new_name is None or if @node is not present in this NodeSet.
get(name: str) -> Union[Node, None]

Get a node by name.

Parameter Type Description
name str The name of the node to get.
Returns: Union[Node, None]: The node with the given name, or None if the node does not exist.

UEdge

class UEdge

Undirected dge class that is essentially a wrapper class for a set {a, b} where a and b are Node objects. There is no ordering/direction.

Properties

n1 -> Node property

Gets the "first" node in this edge. (No ordering, but for efficient retrieval, we designate one as the first).

Returns: Node: The "first" node in this edge.
n2 -> Node property

Gets the "second" node in this edge. (No ordering, but for efficient retrieval, we designate one as the second).

Returns: Node: The "second" node in this edge.

Constructor

__init__(n1: Node, n2: Node, length: float | None = None, weight: float | None = None) -> None

Initialize an undirected Edge.

Parameter Type Description
n1 Node A Node (designated member #1 for efficient retrieval)
n2 Node A Node (designated member #2 for efficient retrieval)
length float, optional Branch length of this edge. Defaults to None.
weight float, optional Edge weight value. Defaults to None.
Raises: EdgeError: If @length does not match the difference in node times should both n1 and n2 have set time attributes.

Methods

__contains__(x: Node) -> bool

Check if a node is a member of this edge.

Parameter Type Description
x Node A node to check for membership in this edge.
Returns: bool: True if x is a member of this edge, False otherwise.
get_length -> float

Gets the branch length of this edge. If it is not equivalent to the current times of the source and destination nodes, then a warning is raised that the branch lengths are not equivalent, and that either the times are outdated or the branch length field is outdated.

Returns: float: branch length.
set_length(branch_length: float, enforce_times: bool = True) -> None

Sets the branch length of this edge.

Parameter Type Description
branch_length float The new branch length.
enforce_times bool, optional If True, checks that the branch length is equivalent to the difference in times of the source and destination nodes. Defaults to True.
Raises: EdgeError: If @branch_length is not equivalent to the difference in times of the source and destination nodes
copy(new_n1: Node | None = None, new_n2: Node | None = None) -> UEdge

Duplicate this edge by copying all data into a separate UEdge object.

Parameter Type Description
new_n1 Node | None, optional A new node to replace the first node in this edge. Defaults to None.
new_n2 Node | None, optional A new node to replace the second node in this edge. Defaults to None.
Returns: UEdge: An equivalent edge to this undirected edge.
to_directed(src: Node) -> Edge

Convert this edge to a directed edge.

Parameter Type Description
src Node Must be one of the Nodes that is in this edge. Sets this Node as the source node, and the other node as the destination
Returns: Edge: An equivalent edge to this undirected edge, but with a direction now enforced.
Raises: EdgeError: If 'src' is not a node in this Edge.
get_weight -> float

Gets the weight of this edge.

Returns: float: Edge weight.
set_weight(weight: float) -> None

Sets the weight of this edge.

Parameter Type Description
weight float The new edge weight.
to_names -> tuple[str, str]

Get the names of the nodes in this edge.

Returns: tuple[str, str]: A tuple of the names of the nodes in this edge.

Edge

class Edge

Class for directed edges. Instead of being a wrapper for a "set" of member nodes, we can now think about an Edge as a wrapper for a tuple of member nodes (a, b), where now the direction is encoded in the ordering (where a is the source, and b the destination).

Properties

src -> Node property

Get the source node of this edge.

Returns: Node: The source node.
dest -> Node property

Get the destination node of this edge.

Returns: Node: The destination node.

Constructor

__init__(source: Node, destination: Node, length: float | None = None, gamma: float | None = None, weight: float | None = None, tag: str | None = None) -> None

An edge has a source (parent) and a destination (child). Edges in a phylogenetic context are *generally* directed. source -----> destination

Parameter Type Description
source Node The parent node.
destination Node The child node.
length float, optional Branch length value. Defaults to None.
gamma float, optional Inheritance Probability, MUST be from [0,1]. Defaults to None.
weight float, optional Edge weight, can be any real number. Defaults to None.
tag str, optional A name tag for identifiability of hybrid edges should each have a gamma of 0.5. Defaults to None.
Raises: ValueError: If @gamma is not a probabilistic value between 0 and 1 (inclusive, but if a hybrid edge pair has 0 and 1 then those hybrid edges may as well not exist)., EdgeError: If @length does not match the difference between @source.get_time() and @destination.get_time().

Methods

set_tag(new_tag: str) -> None

Set the name/identifiability tag of this edge.

Parameter Type Description
new_tag str a unique string identifier.
get_tag -> str

Get the name/identifiability tag for this edge.

Returns: str: The name/identifiabity tag for this edge. High change of being None!
set_gamma(gamma: float) -> None

Set the inheritance probability of this edge. Only applicable to hybrid edges, but no warning will be raised if you attempt to set the probability of a non-hybrid edge.

Parameter Type Description
gamma float A probability (between 0 and 1 inclusive).
Raises: ValueError: If gamma is not between 0 and 1 inclusive.
get_gamma -> float

Gets the inheritance probability for this edge.

Returns: float: A probability (between 0 and 1).
copy(new_src: Node | None = None, new_dest: Node | None = None) -> Edge

Craft an identical edge to this edge object, just in a new object. Useful in building subnetworks of a network that is in hand.

Parameter Type Description
new_src Node | None, optional A new source node for this edge.
new_dest Node | None, optional A new destination node for this edge.
Returns: Edge: An identical edge to this one, with respect to the data they hold.
set_length(branch_length: float, warn_times: bool = False, enforce_times: bool = False) -> None

Set the branch length of this edge. If enforce_times is True, then the branch length must be equivalent to the difference in times of the source and destination nodes. If warn_times is True, then a warning will be raised if the branch length is not equivalent to the difference in times of the source and destination nodes.

Parameter Type Description
branch_length float The new branch length.
warn_times bool, optional If True, raises a warning if the branch length is not equivalent to the difference in times of the source and destination nodes. Defaults to False.
enforce_times bool, optional If True, raises an error if the branch length is not equivalent to the difference in times of the source and destination nodes. Defaults to False.
Raises: EdgeError: If enforce_times is True and the branch length is not equivalent to the difference in times of the source and destination nodes. Warning: If warn_times is True and the branch length is not equivalent to the difference in times of the source and destination nodes.
get_length -> float

Get the branch length of this edge.

Returns: float: The branch length of this edge.
set_weight(new_weight: float) -> None

Set the weight of this edge.

Parameter Type Description
new_weight float The new weight of this edge.
get_weight -> float

Get the weight of this edge.

Returns: float: The weight of this edge.
to_names -> tuple[str, str]

Get the names of the nodes in this edge.

Returns: tuple[str, str]: A tuple of the names of the nodes in this edge.
__len__ -> float

Get the length of this edge.

to_branch -> Branch

Convert this edge to a Branch object for use in phylogenetic computations.

Returns: Branch: A Branch with this edge's length, gamma, and source node label.

EdgeSet

class EdgeSet

Data structure that serves the purpose of keeping track of edges that belong to a network. We call this set E.

Constructor

__init__(directed: bool = True) -> None

Initialize the set of edges, E, for a network.

Parameter Type Description
directed bool, optional True if the network is directed, False otherwise. Defaults to True.

Methods

__contains__(e: Union[Edge, UEdge]) -> bool

An undirected edge (Edge) is in the EdgeSet if there is an equivalent edge object OR if there is anUEdgeobject with equivalent node members. A directed edge (DiEdge) is in the EdgeSet only if the EdgeSet has a reference to @e.

Parameter Type Description
e Union[Edge, UEdge] An undirected or directed edge.
Returns: bool: If the Edge @e is represented in the graph
add(*edges: Union[Edge, UEdge]) -> None

Add any number of edges to E.

Raises: TypeError: If the edge type (undirected or directed) doesn't match the designated type of network/graph associated with this edge set.
remove(edge: Union[Edge, UEdge]) -> None

Remove an edge from E.

Parameter Type Description
edge Edge An edge that is currently in E.
get(n1: Node, n2: Node, gamma: float | None = None, tag: str | None = None) -> Union[Edge, UEdge]

Given the nodes that make up the edge and an inheritance probability, get the edge in E that matches the data. Inheritance probability is only required for when a directed network has two edges with the same source and destination nodes, and it is known that a bubble edge is being looked up. Inheritance probabilities are insufficient for identifiability of directed bubble edges should gamma be .5, in which case please tag your edges with some sort of key. If a bubble edge is being looked up and there is no gamma and/or tag provided, then one of the two bubble edges will be returned at random. There are no identifiability problems with undirected graphs, since duplicate edges are not a thing, and there exists only one edge with a given pair of member nodes.

Parameter Type Description
n1 Node If directed, this is "src" / the parent. If undirected, node ordering does not matter.
destination Node If directed, this is "dest" / the child. If
undirected node ordering does not matter.
gamma float, optional Inheritance probability, for bubble identifiability. Defaults to None.
tag str, optional In the event that gamma is .5 AND THERE IS A
BUBBLE then edges should be tagged with a unique identifier string.
Returns: Edge: The edge in E that matches the given data.
Raises: EdgeError: If there are any problems looking up the desired edge, or if there is no such edge in the graph/network.
get_set -> set[Union[Edge, UEdge, Any]]

Get the set, E, for a network.

Returns: set[Union[Edge, UEdge]]: The set of edges in this network.

Graph

class Graph

Superclass that defines the general constructs for working with graphs in a phylogenetic context. This object assumes that edges are undirected and the graph is therefore unrooted. Nodes may have as many neighbors as needed, and no topological restrictions are assumed.

Constructor

__init__(edges: EdgeSet | None = None, nodes: NodeSet | None = None) -> None

Initialize a Network object. You may initialize with any combination of edges/nodes, or provide none at all.

Parameter Type Description
edges EdgeSet, optional A set of Edges. Defaults to an empty EdgeSet.
nodes NodeSet, optional A set of Nodes. Defaults to an empty NodeSet.

Methods

__contains__(obj: Union[Node, UEdge, Edge]) -> bool

Allows a simple pythonic "n in graph" or "e in graph" check.

Parameter Type Description
obj Node | UEdge |Edge A node or edge.
Returns: bool: True if obj is a node or edge in the graph
Raises: TypeError: If @obj is of any type other than Node, Edge, orEdge.
add_nodes(*nodes: Node) -> None

Add any amount of nodes to this graph (or Network).

add_uid_node(node: Union[Node, None] = None) -> Node

Ensure a node has a unique name that hasn't been used before/is not currently in use for this graph. May be used with a node that is or is not yet an element of V. The node will be added to V as a result of this function.

Parameter Type Description
node Node | None Any node object. Defaults to None.
Returns: Node: the added/edited node that has been added to the graph.
remove_nodes(*nodes: Node) -> None

Removes any amount of nodes from the list of nodes. Also prunes all edges from the graph that are connected to the removed node(s). Has no effect if a node is not in this network.

remove_edge(edge: Union[Edge, UEdge, list[Node]]) -> None

Removes an edge from the EdgeSet. Does not delete nodes with no edges Has no effect if 'edge' is not in the graph.

Parameter Type Description
edge Edge | UEdge | list[Node] An edge to remove from the graph, either represented as an Edge/UEdge object or as a list of Nodes (length 2).
Raises: NetworkError: If given as a list, and the list of nodes is malformed in any way.
get_edge(n1: Node, n2: Node) -> Any

Gets the edge in the graph with the given members.

Parameter Type Description
n1 Node A Node that is a member of an edge with @n2
n2 Node A Node that is a member of an edge with @n1
Returns: Any: The edge in the graph that has @n1 and @n2 as its members.
V -> list[Node]

Get all nodes in V.

Returns: list[Node]: the set V, in list form.
E -> list[Any]

Get the set E (in list form).

Returns: list[Edge]: The list of all edges in the graph
get_item(key: str) -> object

Access the blob storage with a key. CONSIDER REMOVAL OF THIS FUNCTION...

Parameter Type Description
key str a string key to access the blob storage
Returns: object: the object stored in the blob storage
put_item(key: str, item: Any) -> None

Add an item to the blob storage.

Parameter Type Description
key str a string key to access the blob storage
item Any the object to store in the blob storage
update_node_name(node: Node, name: str) -> None

Rename a node and update the bookkeeping.

Parameter Type Description
node Node a node in the graph
name str the new name for the node.
has_node_named(name: str) -> Union[Node, None]

Check whether the graph has a node with a certain name. Strings must be exactly equal (same white space, capitalization, etc.)

Parameter Type Description
name str the name to search for
Returns: Node | None: the node with the given name, or None if there is no such node
in_degree(node: Node) -> int

Get the in-degree of a node.

Parameter Type Description
node Node A node in V
Returns: int: the in degree count
out_degree(node: Node) -> int

Get the out-degree(number of edges where the given node is a parent) of a node in the graph.

Parameter Type Description
node Node a node in V
Returns: int: the out-degree count
in_edges(node: Node) -> list[Any]

Get the in-edges of a node in V. The in-edges are the edges in E, where the given node is the child.

Parameter Type Description
node Node a node in V
Returns: list[Any]: the list of in-edges
out_edges(node: Node) -> list[Any]

Get the out-edges of a node in V. The out-edges are the edges in E, where the given node is the parent.

Parameter Type Description
node Node a node in V
Returns: list[Any]: the list of out-edges

Network

class Network(Graph)

This class represents a directed (and potentially acyclic) graph containing nodes and edges. An 'Edge' object is a wrapper class for a tuple of two nodes, (a, b), where a and b are Node objects, and the direction of the edge is from a to b (a is b's parent) -- thus (a, b) is NOT the same as (b, a). Use of 'UEdge' objects is strictly prohibited here. Notes and Allowances: 1) You may create cycles -- however we have provided a method to check if this graph object is acyclic. This method is internally called on methods that assume that a network has no cycles, so be mindful of the state of networks that are passed as arguments. 2) You may have multiple roots. Be mindful of whether this graph is connected and what root you wish to operate on. 3) You may end up with floater nodes/edges, ie this...

Constructor

__init__(edges: EdgeSet | set[Edge] = None, nodes: NodeSet | set[Node] = None) -> None

Initialize a Network object. You may initialize with any combination of edges/nodes, or provide none at all. If you provide an EdgeSet and no nodes, each node present in the EdgeSet *WILL* be added to the network.

Parameter Type Description
edges EdgeSet, optional A set of Edges. Defaults to an empty EdgeSet.
nodes NodeSet, optional A set of Nodes. Defaults to an empty NodeSet.

Methods

from_newick(newick: str) -> Network @classmethod

Construct a Network object by passing in a newick string and parsing out the topology from there.

Parameter Type Description
newick str An extended newick string.
Returns: Network: An initialized network object
add_edges(edges: Union[Edge, list[Edge]]) -> None

If edges is a list of Edges, then add each Edge to the list of edges. If edges is a singleton Edge then just add to the edge array. Note: Each edge that you attempt to add must be between two nodes that exist in the network. Otherwise, an error will be thrown.

Parameter Type Description
edges Edge | list[Edge] a single edge, or multiple.
Raises: NetworkError: if input edge/edges are malformed in any way
remove_nodes(node: Node) -> None

Removes node from the list of nodes. Also prunes all edges from the graph that are connected to the node. Has no effect if node is not in this network.

Parameter Type Description
node Node a Node obj
remove_edge(edge: Union[Edge, UEdge, list[Node]], gamma: float | None = None) -> None

Removes edge from the list of edges. Does not delete nodes with no edges Has no effect if 'edge' is not in the graph.

Parameter Type Description
edge Union[Edge, UEdge, list[Node] an edge to remove from the graph
gamma float an inheritance probability from [0,1], if the edge is provided as a list of nodes, and there is an identifiability issue that needs resolving (ie, the edge that needs to be removed is a bubble edge). Optional. Defaults to None.
get_edge(n1: Node, n2: Node, gamma: float | None = None, tag: str | None = None) -> Edge

Note, that in the event of bubbles, 2 edges will exist with the same source and destination. If this is possible, please supply the inheritance probability of the correct branch. If both edges are known to be identical (gamma = 0.5), then one will be chosen at random.

Parameter Type Description
n1 Node parent node
n2 Node child node
gamma float inheritance probability. Optional. Defaults to None
tag str A name/identifiability tag for hybrid edges should both gammas be = .5. Optional. Defaults to None.
Returns: Edge: the edge containing n1 and n2 and has the proper gamma value (if applicable).
root -> Node

Return the root of the Network. Phylogenetic networks only have one root, but for generality and practical use, multiple roots have been allowed. To get all roots, should multiple exist, call the function "roots". This function only returns 1 root.

Returns: Node: The root of the network.
Raises: NetworkError: If there are no roots in the network (cycle, or empty), or if there are multiple roots in the network.
roots -> list[Node]

Return all root(s) of the Network. Phylogenetic networks typically have one root, but for generality and practical use, multiple roots have been allowed. Use ``root()`` to retrieve a single root.

Returns: list[Node]: A list of all root nodes (nodes with in-degree 0 and out-degree > 0).
get_leaves -> list[Node]

Returns the set X (a subset of V), the set of all leaves (nodes with out-degree 0). Only returns the leaves that are connected/reachable from the root.

Returns: list[Node]: the connected elements of X, in list format.
get_parents(node: Node) -> list[Node]

Returns a list of the parents of a node. There is no hard cap on the length of this array.

Parameter Type Description
node Node any node in V.
Returns: list[Node]: the parents of the node.
Raises: NetworkError: If the node is not in the graph.
get_children(node: Node) -> list[Node]

Returns a list of the children of a node. There is no hard cap on the length of this array.

Parameter Type Description
node Node any node in V.
Returns: list[Node]: the children of the node.
Raises: NetworkError: If the node is not in the graph.
get_branches(node: Node) -> dict[str, Any]

Returns a dictionary of branches connected to the node.

Parameter Type Description
node Node a node in the graph.
Returns: dict[str, Any]: a dictionary of branches connected to the node. The keys are "parent_branches" and "child_branches". The values are lists of branches.
clean(options: list[bool] = [True, True, True]) -> None

All the various ways that the graph can be cleaned up and streamlined while not altering topology or results of algorithms. Algorithm Indeces: 0) Remove nodes that have in/out degree of 0 (floater nodes) 1) Remove a spurious root/root edge (root node with only one out edge) 2) Consolidate all chains of nodes with in/out degree of 1 into 1 edge. Default behavior is to run all three. To not run a certain routine, set the options list at the indeces listed above to False. Ie. To run the first and third algo, use [True, False, True].

Parameter Type Description
options list[bool], optional A list of booleans that signal which algorithms to run. Defaults to [True, True, True].
mrca(set_of_nodes: set[Node] | set[str]) -> Node

Computes the Least Common Ancestor of a set of graph nodes.

Parameter Type Description
set_of_nodes set[Node] | set[str] A set of Nodes, or node names.
Returns: Node: The node that is the LCA of the set.
Raises: NetworkError: If any node in the set is not in the graph, or if elements are of an unexpected type.
leaf_descendants(node: Node) -> set[Node]

Compute the set of all leaf nodes that are descendants of the parameter node. Uses DFS to find paths to leaves.

Parameter Type Description
node Node The node for which to compute leaf children.
Returns: set[Node]: The set of all leaves that descend from 'node'.
Raises: NetworkError: If node is not found in the graph.
diff_subtree_edges(rng: np.random.Generator) -> list[Edge]

Returns 2 random edges such that there does not exist a directed path from one edge source node to the other edge source node.

Parameter Type Description
rng np.random.Generator an rng object.
Returns: list[Edge]: a list of 2 edges such that neither edge is reachable from either starting point.
subgenome_count(n: Node) -> int

Given a node in this graph, return the subgenome count.

Parameter Type Description
n Node Any node in the graph. It is an error to input a node that is not in the graph.
Returns: int: subgenome count
Raises: NetworkError: If the input node is not in the graph.
edges_downstream_of_node(n: Node) -> list[Edge]

Returns the set (as a list) of edges that are in the subgraph of a node.

Parameter Type Description
n Node A node in a graph.
Returns: list[Edge]: The set of all edges in the subgraph of n.
Raises: NetworkError: If the input node is not in the graph.
edges_upstream_of_node(n: Node) -> list[Edge]

Returns the set (as a list) of edges that are in all paths from the root to this node. Useful in avoiding the creation of cycles when adding edges.

Parameter Type Description
n Node A node in a graph.
Returns: list[Edge]: The set of all edges on paths from the root to n.
Raises: NetworkError: If the input node is not in the graph.
subgenome_ct_edges(downstream_node: Union[Node, None] = None, delta: float = math.inf, start_node: Union[Node, None] = None) -> dict[Edge, int]

Maps edges to their subgenome counts.

Parameter Type Description
downstream_node Node | None, optional No edges will be included in the map that are in a subgraph of this node. Defaults to None.
delta float, optional Only include edges in the mapping that have subgenome counts <= delta. Defaults to math.inf.
start_node Node | None, optional Provide a node only if you don't want to start at the root. Defaults to None.
Returns: dict[Edge, int]: a map from edges to subgenome counts
Raises: NetworkError: If the graph has more than one root to start.
edges_to_subgenome_count(downstream_node: Union[Node, None] = None, delta: float = math.inf, start_node: Union[Node, None] = None) -> dict[int, list[Edge]]

Maps edges to their subgenome counts.

Parameter Type Description
downstream_node Node | None, optional No edges will be included in the map that are in a subgraph of this node. Defaults to None.
delta float, optional Only include edges in the mapping that have subgenome counts <= delta. Defaults to math.inf.
start_node Node | None, optional Provide a node only if you don't want to start at the root. Defaults to None.
Returns: dict[int, list[Edge]]: a map from edges to subgenome counts
Raises: NetworkError: If the graph has more than one root to start.
leaf_descendants_all -> dict[Node, set[Node]]

Map each node in the graph to its set of leaf descendants

Returns: dict[Node, set[Node]]: map from graph nodes to their leaf descendants
newick -> str

Generate the newick string of the network.

Returns: str: The newick representation of the network.
is_acyclic -> bool

Checks if each of this graph's connected components is acyclic

Returns: bool: True if acyclic, False if cyclic.
bfs_dfs(start_node: Union[Node, None] = None, dfs: bool = False, is_connected: bool = False, accumulator: Callable[..., None] | None = None, accumulated: Any = None) -> tuple[dict[Node, int], Any]

General bfs-dfs routine, with the added utility of checking whether or not this graph is made up of multiple connected components.

Parameter Type Description
start_node Node | None, optional Give a node to start the search from. Defaults to None, in which case the search will start at the root.
dfs bool, optional Flag that specifies whether to use bfs or dfs. Defaults to False (bfs), if true is passed, will run dfs.
is_connected bool, optional Flag that, if enabled, will check for the connected component status. Defaults to False (won't run).
accumulator Callable[..., None] | None, optional A function that takes the currently searched Node in the graph and does some sort of bookkeeping.
accumulated Any Any type of structure that stores the data given by the accumulator function.
Returns: tuple[dict[Node, int], Any]: Mapping from nodes to their distance from the start node.
subnet(retic_node: Node) -> Network

Make a copy of a subnetwork of this DAG, rooted at 'retic_node', with unique node names.

Parameter Type Description
retic_node Node A node in this network that is a reticulation node
Returns: Network: A subnetwork of the DAG being operated on
copy -> tuple[Network, dict[Node, Node]]

Copy this network into a new network object, also with new node and edge objects.

Returns: tuple[Network, dict[Node, Node]]: A tuple of the copied network and a map from old nodes to new nodes.
to_networkx -> nx.Graph

Convert this network to a NetworkX graph object.

Returns: nx.Graph: A NetworkX graph object.
compare_network(net: Network, measure: str) -> float

Compares the topology of this network compared to another network. The following are options for the distance measure: [tree|tri|cluster|luay|rnbs|apd|normapd|wapd|normwapd] If [tree | tri | cluster] is used, the return value will be the average of the false positive and false negative rates. If [luay] is used, the return value will be the distance between the two networks. If [rnbs | apd | normapd | wapd | normwapd] is used, the return value is the dissimilarity between the two networks.

Parameter Type Description
net Network The other network.
measure str Distance measure to use. One of ``tree``, ``tri``, ``cluster``, ``luay``, ``rnbs``, ``apd``, ``normapd``, ``wapd``, or ``normwapd``.
Returns: float: Measure of the two networks similarity/distance.
Raises: NetworkError: If an unrecognized measure string is provided.
is_isomorphic(other: Network) -> bool

Check if this network is topologically isomorphic to another network. Two networks are isomorphic if they have the same topology, regardless of node labels or branch lengths. This method calls the GraphUtils.is_isomorphic function.

Parameter Type Description
other Network The other network to compare against.
Returns: bool: True if the networks are topologically isomorphic, False otherwise.
nakhleh_distance(other: Network) -> float

Computes the rooted triplet distance (Nakhleh distance) between two networks. This distance measure compares the topological relationships between all triplets of leaves in both networks.

Parameter Type Description
other Network The other network to compare against.
Returns: float: The Nakhleh distance (0 = identical topology, 1 = completely different).
get_subtree_at(node: Node) -> set[Node]

Collect all nodes in the subtree rooted at the given node.

Parameter Type Description
node Node The root of the subtree.
Returns: set[Node]: A set of nodes in the subtree.
dist_from_root(node: Node) -> int

Get the distance from the root to the given node, measured in edge count (hop count) via BFS.

Parameter Type Description
node Node A node in this network.
Returns: int: The number of edges on the BFS-shortest path from the root to the given node.
topological_order -> list[Node]

Compute a topological order of nodes in an acyclic network.

Returns: list[Node]: Nodes in topological order from roots to leaves.
Raises: NetworkError: If the network contains a directed cycle and no valid topological order exists.
distance_from_root(node: Node, use_time: bool = True) -> float

Compute distance from root to a given node.

Parameter Type Description
node Node The target node
use_time bool If True, use branch lengths/times. If False, use edge count.
Returns: float: Distance from root (time units or edge count)
Raises: NetworkError: If node is not in the network or no path to root exists
set_node_times_from_root -> None

Set time attributes for all nodes based on their distance from root. Uses cumulative branch lengths from root. Useful for enabling biologically meaningful node comparisons. The root node is assigned time 0.0 and each descendant's time is the sum of branch lengths along the BFS path from the root.

Returns: None
set_node_times_by_edge_count -> None

Set time attributes for all nodes based on their edge count from root. This provides biologically meaningful comparison when actual times are not available. Edge counts are converted to time values for comparison purposes. The root node is assigned time 0.0 and each descendant's time equals the number of edges on the BFS path from the root.

Returns: None

MUL

class MUL(Network)

A subclass of a Network, that is a binary tree that results from the transformation of a standard network into a Multilabeled Species Tree.

Constructor

__init__(gene_map: dict[str, list[str]], rng: np.random.Generator) -> None

Initializes a MUL object.

Parameter Type Description
gene_map dict[str, list[str]] A mapping from gene names to a set of gene copy names.
rng np.random.Generator A numpy random number generator.

Methods

network_to_mul_recursive(net: Network) -> Network

Recursively expand reticulations, handling nested reticulations correctly. Each reticulation node with multiple parents is expanded by duplicating the downstream subnetwork for each additional parent, processing the deepest reticulations first.

Parameter Type Description
net Network A phylogenetic network potentially containing reticulation nodes.
Returns: Network: A tree (MUL tree) with all reticulations removed through subnetwork duplication.
to_mul(net: Network) -> Network

Creates a (MU)lti-(L)abeled Species Tree from a network

Parameter Type Description
net Network A Network
Returns: Network: a MUL tree (as a Network obj)
Raises: NetworkError: If the network is malformed with regards to ploidy

Module Functions

using_cython -> bool

Check if Cython acceleration is enabled.

Returns: bool: True if the Cython-accelerated NodeSet implementation is available and in use, False if using the pure-Python fallback.
print_graph(g: Union[Graph, Network]) -> None

For each node in g's V set (node set), print out the node's information and attributes.

Parameter Type Description
g Union[Graph, Network] A network or graph object.
pretty_print_edges(g: Union[Graph, Network]) -> None

Prints the two node names as a tuple for all the edges in g's E set (edge set).

Parameter Type Description
g Union[Graph, Network] A network or graph object.

Navigation

Modules

This Page