← Back to PhyNetPy

PhyNetPy Documentation

Library for the Development and Use of Phylogenetic Network Methods

Network Module v1.0.0

The Network module provides core data structures for representing phylogenetic networks, including Node, Edge, and Network classes. This is the foundational module used throughout PhyNetPy.

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

Exceptions

exception NetworkError(Exception)

Raised when a network is malformed or a network operation fails.

exception NodeError(Exception)

Raised when a Node operation fails.

exception EdgeError(Exception)

Raised when an Edge operation fails.

Node Class

class Node

Node class providing support for network constructs like reticulation nodes and phylogenetic attributes.

Constructor

__init__(self, name: str, is_reticulation: bool = False, attr: dict = {}, seq: DataSequence = None, t: float = None)
Parameter Type Description
name str Node label
is_reticulation bool Mark as reticulation node
attr dict User-defined attributes
seq DataSequence Associated sequence data
t float Speciation time

Core Methods

label property

Returns the name of the node.

set_name(self, new_name: str) -> None

Set a new node label.

get_time(self) -> float

Get speciation time (0 = root, larger = present).

set_time(self, new_t: float) -> None

Set speciation time (must be non-negative).

is_reticulation(self) -> bool

Check if node is a reticulation (hybrid) node.

set_is_reticulation(self, new_is_retic: bool) -> None

Set the reticulation flag.

Attribute Methods

get_attributes(self) -> dict
set_attributes(self, new_attr: dict) -> None
add_attribute(self, key, value, append: bool = False) -> None
attribute_value(self, key) -> object

Edge Class

class Edge

Edge class representing directed connections between nodes with associated attributes like branch length and inheritance probability.

Constructor

__init__(self, src: Node, dest: Node, length: float = 0, gamma: float = 1.0, weight: float = 1.0, tag: str = None)
Parameter Type Description
src Node Source (parent) node
dest Node Destination (child) node
length float Branch length
gamma float Inheritance probability (default 1.0)
weight float Edge weight (for support values)
tag str Optional label (e.g., "left", "right")

Attributes

src attribute

Source node of the edge.

dest attribute

Destination node of the edge.

Methods

get_length(self) / set_length(self, length: float)
get_gamma(self) / set_gamma(self, gamma: float)
get_weight(self) / set_weight(self, weight: float)
get_tag(self) / set_tag(self, tag: str)
copy(self, new_src: Node = None, new_dest: Node = None) -> Edge

Create a copy of the edge (optionally with new endpoints).

Network Class

class Network

Main class for representing phylogenetic networks as directed graphs with support for reticulation nodes, branch lengths, and inheritance probabilities.

Constructor

__init__(self)

Create an empty network.

Node/Edge Access

V(self) -> list[Node]

Get all nodes in the network.

E(self) -> list[Edge]

Get all edges in the network.

add_nodes(self, *nodes) -> None
add_edges(self, *edges) -> None
remove_nodes(self, node) -> None
remove_edge(self, edge) -> None

Traversal

root(self) -> Node

Get the root node (indegree 0).

get_leaves(self) -> list[Node]

Get all leaf nodes (outdegree 0).

get_parents(self, node) -> list[Node]
get_children(self, node) -> list[Node]
leaf_descendants(self, node) -> set[Node]
mrca(self, taxa: set) -> Node

Find most recent common ancestor of a set of taxa.

Degree Methods

in_degree(self, node) -> int
out_degree(self, node) -> int
in_edges(self, node) -> list[Edge]
out_edges(self, node) -> list[Edge]

Graph Properties

is_acyclic(self) -> bool
get_edge(self, src, dest) -> Edge
has_node_named(self, name) -> Node | None

Modification

copy(self) -> tuple[Network, dict]

Deep copy. Returns (new_network, old_to_new_node_map).

clean(self, flags: list[bool] = [True, True, True]) -> None

Clean up network: remove isolated nodes, degree-1 chains, etc.

newick(self) -> str

Generate Newick string representation.

Usage Examples

from PhyNetPy.Network import Network, Node, Edge

# Create a simple tree
net = Network()

# Add nodes
root = Node("root", t=0)
a = Node("A", t=1)
b = Node("B", t=1)

net.add_nodes(root, a, b)

# Add edges
net.add_edges(Edge(root, a, length=1.0), Edge(root, b, length=1.0))

# Query structure
print(f"Root: {net.root().label}")
print(f"Leaves: {[n.label for n in net.get_leaves()]}")

# Create reticulation network
c = Node("C", t=1)
hybrid = Node("H", is_reticulation=True, t=0.5)
net.add_nodes(c, hybrid)

# Hybrid edges with inheritance probabilities
net.add_edges(
    Edge(root, hybrid, gamma=0.4),
    Edge(c, hybrid, gamma=0.6),
    Edge(hybrid, a)
)

# Check acyclicity
print(f"Acyclic: {net.is_acyclic()}")

# Generate Newick
print(net.newick())

# Copy network
new_net, node_map = net.copy()

# Find MRCA
mrca_node = net.mrca({"A", "B"})
print(f"MRCA of A,B: {mrca_node.label}")

See Also

Navigation

Modules

This Page