← Back to PhyNetPy

PhyNetPy Documentation

Library for the Development and Use of Phylogenetic Network Methods

NetworkParser Module v1.0.0

The NetworkParser module provides classes for parsing phylogenetic networks from Nexus files and Newick strings. Includes pre-parsing validation for better error reporting.

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

Exceptions

exception NetworkParserError(Exception)

Raised when an input file or Newick string contains issues that prevent proper parsing.

NetworkParser Class

class NetworkParser

Class that parses networks from Nexus files. Supports extended Newick format with reticulation nodes and inheritance probabilities. Includes optional pre-parsing validation.

Constructor

__init__(self, filename: str, validate_input: bool = True, print_validation_summary: bool = True)

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)
Raises: NetworkParserError - If file cannot be parsed or has critical validation errors

Methods

parse(self) -> None

Parse all trees/networks from the file. Called automatically by constructor.

get_network(self, index: int) -> Network

Get the network at a specific index.

Parameter Type Description
index int Index of the network to retrieve
Returns: Network - The parsed network
get_all_networks(self) -> list[Network]

Get all parsed networks.

Returns: list[Network] - List of all parsed networks
name_of_network(self, network: Network) -> str

Get the label for a parsed network (as it appears in the Nexus file).

Returns: str - Network label
get_validation_summary(self) -> ValidationSummary

Get the validation summary from input file validation.

Returns: ValidationSummary - Validation results (or None if skipped)
parse_attributes(self, attr_str: str) -> list

Parse extended Newick formatting string (e.g., "H1" -> ["Hybridization", 1]).

Returns: list - [event_type, index]

NewickStringParser Class

class NewickStringParser

Parser for Newick strings directly (without a file).

Constructor

__init__(self, newick_str: str)

Initialize with a Newick string.

Parameter Type Description
newick_str str A Newick format string

Methods

parse(self) -> Network

Parse the Newick string into a Network object.

Returns: Network - The parsed network

Extended Newick Format

The parser supports extended Newick format for phylogenetic networks:

  • Reticulation nodes: Marked with # prefix (e.g., #H1, #LGT2)
  • Event types: H = Hybridization, R = Recombination, LGT = Lateral Gene Transfer
  • Inheritance probabilities: Specified in comments [&gamma=0.4]
# Example extended Newick:
((A:1.0,#H1:0.5::0.4):0.5,(B:0.5,#H1:0.5::0.6):1.0)root;

Usage Examples

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()

See Also

Navigation

Modules

This Page