Library for the Development and Use of Phylogenetic Network Methods
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.
Raised when a network is malformed or a network operation fails.
Raised when a Node operation fails.
Raised when an Edge operation fails.
Node class providing support for network constructs like reticulation nodes and phylogenetic attributes.
| 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 |
Returns the name of the node.
Set a new node label.
Get speciation time (0 = root, larger = present).
Set speciation time (must be non-negative).
Check if node is a reticulation (hybrid) node.
Set the reticulation flag.
Edge class representing directed connections between nodes with associated attributes like branch length and inheritance probability.
| 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") |
Source node of the edge.
Destination node of the edge.
Create a copy of the edge (optionally with new endpoints).
Main class for representing phylogenetic networks as directed graphs with support for reticulation nodes, branch lengths, and inheritance probabilities.
Create an empty network.
Get all nodes in the network.
Get all edges in the network.
Get the root node (indegree 0).
Get all leaf nodes (outdegree 0).
Find most recent common ancestor of a set of taxa.
Deep copy. Returns (new_network, old_to_new_node_map).
Clean up network: remove isolated nodes, degree-1 chains, etc.
Generate Newick string representation.
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}")