Library for the Development and Use of Phylogenetic Network Methods
The NetworkParser module provides classes for parsing phylogenetic networks from Nexus files and Newick strings. Includes pre-parsing validation for better error reporting.
Raised when an input file or Newick string contains issues that prevent proper parsing.
Class that parses networks from Nexus files. Supports extended Newick format with reticulation nodes and inheritance probabilities. Includes optional pre-parsing validation.
Initialize the parser with a Nexus file.
| Parameter | Type | Description |
|---|---|---|
| filename | str | Path to the Nexus file |
| validate_input | bool | Whether to validate before parsing (default: True) |
| print_validation_summary | bool | Whether to print validation results (default: True) |
NetworkParserError - If file cannot be parsed or has critical validation errors
Parse all trees/networks from the file. Called automatically by constructor.
Get the network at a specific index.
| Parameter | Type | Description |
|---|---|---|
| index | int | Index of the network to retrieve |
Get all parsed networks.
Get the label for a parsed network (as it appears in the Nexus file).
Get the validation summary from input file validation.
Parse extended Newick formatting string (e.g., "H1" -> ["Hybridization", 1]).
Parser for Newick strings directly (without a file).
Initialize with a Newick string.
| Parameter | Type | Description |
|---|---|---|
| newick_str | str | A Newick format string |
Parse the Newick string into a Network object.
The parser supports extended Newick format for phylogenetic networks:
# Example extended Newick:
((A:1.0,#H1:0.5::0.4):0.5,(B:0.5,#H1:0.5::0.6):1.0)root;
from PhyNetPy.NetworkParser import NetworkParser, NewickStringParser
# Parse from a Nexus file
parser = NetworkParser("network.nex")
# Get all networks
networks = parser.get_all_networks()
print(f"Found {len(networks)} networks")
# Get a specific network
net = parser.get_network(0)
print(f"Network has {len(net.V())} nodes")
# Get network name
name = parser.name_of_network(net)
print(f"Network name: {name}")
# Parse without validation (faster)
parser_fast = NetworkParser("network.nex", validate_input=False)
# Check validation results
summary = parser.get_validation_summary()
if summary and summary.errors:
print("Validation errors:", summary.errors)
# Parse a Newick string directly
newick = "((A:1,B:1)C:1,(D:1,E:1)F:1)root;"
string_parser = NewickStringParser(newick)
tree = string_parser.parse()