Library for the Development and Use of Phylogenetic Network Methods
The GraphUtils module provides utility functions for manipulating and analyzing Network objects, including tree extraction, cluster computation, network metrics, and topology operations.
Compile all clusters in a graph. For ((A, B)C, D), clusters are {(A,B), (A,B,C)}.
| Parameter | Type | Description |
|---|---|---|
| net | Network | The network to operate on |
| node | Node | Starting point (defaults to root) |
| include_trivial | bool | Include size-1 clusters. Default False. |
Generate all possible trees by removing hybrid edges.
Generate the dominant tree by retaining only reticulation edges with highest inheritance probability.
Sample one displayed tree by selecting incoming edges proportional to inheritance probability.
Count reticulation nodes (indegree >= 2).
Estimate number of displayed trees (product of inbound edge counts for reticulation nodes).
Compute network level (max reticulations in any biconnected component). Returns 0 for trees.
Return biconnected components ("blobs") of the underlying undirected graph.
Check if network has no reticulation nodes.
Compute pairwise distances between leaves on the underlying undirected graph.
Validate standard binary constraints (tree nodes: indegree=1, outdegree=2; retic nodes: indegree=2, outdegree=1).
Validate consistency of node times and branch lengths.
Detect bubble structures (parallel directed edges with same endpoints).
Combine two networks by making their roots children of a new root.
Extract subnetwork containing only specified leaves.
Construct subnetwork induced by a set of leaf labels.
Contract an internal edge by merging its endpoints (in-place).
Return a copy of the network re-rooted at specified node.
Remove edges for which an alternate directed path exists (requires acyclic).
Produce an acyclic copy by removing cycle-forming edges.
Compute topological order from roots to leaves (requires acyclic).
Compute ancestor and descendant sets for each node.
Compute bridges and articulation points using Tarjan's algorithm.
Extract topology from Newick string by removing branch lengths and metadata.
Check if two networks are topologically identical (ignoring labels and branch lengths).
Map each reticulation node to its inbound edges, sorted by gamma.
Generate ASCII art representation of the network (root at top, leaves at bottom).
Generate detailed ASCII art with extended connecting lines for wide trees.
from PhyNetPy.GraphUtils import *
from PhyNetPy.NetworkParser import NetworkParser
# Load a network
parser = NetworkParser("network.nex")
net = parser.get_network(0)
# Get all clusters
clusters = get_all_clusters(net)
print(f"Found {len(clusters)} clusters")
# Check network level
lvl = level(net)
print(f"Network level: {lvl}")
# Count reticulations
retics = count_reticulations(net)
print(f"Reticulations: {retics}")
# Extract displayed trees
trees = get_all_subtrees(net)
print(f"Displayed trees: {len(trees)}")
# Get dominant tree
dom = dominant_tree(net)
# Validate binary structure
is_valid, violations = validate_binary(net)
if not is_valid:
print("Violations:", violations)
# Compute pairwise distances
distances = pairwise_leaf_distance(net)
# ASCII visualization
print(ascii(net))