Library for the Development and Use of Phylogenetic Network Methods
Graph and network utility functions for topology analysis, manipulation, and ASCII rendering.
Compile a list of clusters that make up this graph. Ie: for a graph ((A, B)C, D); , set of all clusters is {(A,B), (A,B,C)}. Can optionally allow the trivial leaf clusters in the set as desired.
| Parameter | Type | Description |
|---|---|---|
| net | Network | the network to operate on |
| node | Node | For any user call, this should be the root. For internal calls, it is the starting point for search. |
| include_trivial | bool | If set to True, includes clusters of size 1. Defaults to False. |
Combine two networks into a single network object by making the roots of each network the children of a new root node.
| Parameter | Type | Description |
|---|---|---|
| left | Network | Left subnetwork |
| right | Network | Right subnetwork |
Compute the minimally sized subnetwork of a network such that the leaf set of the subnetwork is a subset of the original network's leaf set.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network |
| leaf_set | list[Node] | A set of leaf nodes of the given network |
Generate all possible trees that can be derived from the given network by removing hybrid edges and creating copies with subtrees that start at each non-reticulation node.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object |
Generate the dominant tree from a given network by retaining only the reticulation edges with the highest inheritance probability and removing all other reticulation edges.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object |
Count the number of reticulation nodes in a network.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
Validate standard binary constraints. - Tree/internal nodes: indegree=1 (except root with 0), outdegree=2 - Reticulation nodes: indegree=2, outdegree=1 - Leaves: outdegree=0
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
| require_tree_nodes | bool | Enforce tree node constraints. |
| require_retic_nodes | bool | Enforce retic node constraints. |
Detect bubble structures as parallel directed edges (same (src, dest)).
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
Sample one displayed tree by selecting, for each reticulation, a single incoming edge with probability proportional to its inheritance probability.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
| rng | Union[np.random.Generator, None] | Optional RNG; defaults to new generator. |
Contract an internal edge by merging its endpoints.
| Parameter | Type | Description |
|---|---|---|
| net | Network | Network to modify in place. |
| edge | Edge | The edge (u->v) to contract. |
| keep | str | Which endpoint to keep as the merged node: "parent" keeps u, "child" keeps v. |
NetworkError: On invalid keep value.Return a copy of the network re-rooted at `new_root` by orienting edges away from the new root using undirected BFS levels. This preserves node/edge attributes; reticulation indegrees may change depending on topology.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
| new_root | Node | Node in `net` to be the new root. |
NetworkError: If `new_root` is not a node in the network.Compute pairwise distances between leaves on the underlying undirected graph. If use_branch_lengths is True, sum edge lengths; otherwise, use unit weights.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
| use_branch_lengths | bool | Whether to sum edge lengths. |
Compute bridges and articulation points on the underlying undirected graph using Tarjan's algorithm.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
Compute the transitive reduction of an acyclic network: remove edges (u,v) for which an alternate directed path u -> ... -> v exists.
| Parameter | Type | Description |
|---|---|---|
| net | Network | Input network (must be acyclic). |
NetworkError: If the network contains a directed cycle.Return the biconnected components ("blobs") of the underlying undirected graph.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
Decompose a phylogenetic network into its biconnected components (blobs). Each blob is a maximal biconnected subgraph of the underlying undirected graph. Returns a list of Network objects, one per biconnected component, containing copies of the original nodes and directed edges. Node names are preserved but all objects are distinct from the original network.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A phylogenetic network. |
Compute the level of a phylogenetic network. The level is the maximum number of reticulation nodes contained in any biconnected component ("blob") of the underlying undirected graph. Returns 0 for trees.
| Parameter | Type | Description |
|---|---|---|
| net | Network | Input network. |
Estimate the number of displayed trees of a network. Computed as the product, over reticulation nodes, of their inbound edge counts (typically 2). For general networks, this is an upper bound when some choices may be incompatible; for level-1 networks this often matches the exact count.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
Check whether a network is a tree (i.e., has no reticulation nodes).
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
Validate node times and branch lengths consistency across the network. For each edge (parent u -> child v), verify: - child_time - parent_time ≈ edge.length within tolerance. - child_time >= parent_time (monotonicity).
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
| tol | float | Allowed absolute tolerance for length mismatch. |
Compute the ancestor and descendant sets for each node in a network.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
Compute a topological order of nodes in an acyclic network. Delegates to :pymeth:`Network.topological_order`.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
NetworkError: If the network contains a directed cycle.Produce an acyclic copy by removing a small set of cycle-forming edges. Strategy 'greedy' removes the first encountered back edge per DFS pass until the graph is acyclic. This is not guaranteed minimal but is fast and effective for cleaning up accidental cycles.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
| strategy | str | Currently only 'greedy' is supported. |
NetworkError: If an unsupported strategy is specified.Map each reticulation node to its inbound edges, sorted by gamma.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
Construct the subnetwork induced by a set of leaf labels. The induced subnetwork is formed by taking the MRCA of the target leaves and retaining only those descendant branches that lead to at least one target leaf. Node/edge attributes (gamma, length) are copied where applicable.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A network object. |
| taxa | list[str] | List of leaf labels to induce on. |
NetworkError: If any leaf name is not found in the network.Extract the topology from a newick string by removing branch lengths, inheritance probabilities, and other metadata. This function handles PhyloNet-style newick strings that may contain: - Branch lengths after single colons (:) - Inheritance probabilities after double colons (::) - Additional metadata after triple colons (:::) - Comments in square brackets [...]
| Parameter | Type | Description |
|---|---|---|
| newick_str | str | A newick string potentially with PhyloNet extensions |
Extract topology from all newick strings in a file.
| Parameter | Type | Description |
|---|---|---|
| filename | str | Path to file containing newick strings. |
| output_filename | str, optional | Path to save cleaned newick strings. If None, results are only returned without writing to disk. |
FileNotFoundError: If `filename` does not exist., IOError: If reading or writing the file fails.Returns True, if net1 and net2 are topologically identical networks. Even if branch lengths are different, or node labels are different, networks can be isomorphic. ie-- all trees with 3 taxa are isomorphic. quartet trees have only a few different variations. Networks of any size, have infinitely many possible topologies. (A,B)C; is isomorphic with (D,E)F; (A,B),(C,D); is not isomorphic with ((A,B)C, D);
| Parameter | Type | Description |
|---|---|---|
| net1 | Network | A Network object |
| net2 | Network | Another Network object |
Prints out an ascii art depiction of this Network object as a vertical tree/network with the root at the top and leaves at the bottom.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A Network |
| show_edge_lengths | bool | If True, display edge lengths. Defaults to False. |
Prints a more detailed vertical ASCII art depiction of the Network with extended connecting lines between levels for wide trees.
| Parameter | Type | Description |
|---|---|---|
| net | Network | A Network |