phylox.newick_parser

The module handles parsing of Newick strings.

The edges can have attached properties, each edge can have 0, 1, or 3 properties. If there is just one, it will be read as a branch length. If there are three, then the first is the branch length, the second is the bootstrap value, and the third is the inheritance probability along that edge (useful for incoming edges of reticulation nodes).

Functions

dinetwork_to_extended_newick(network[, simple])

Converts a phylogenetic network to a Newick string.

extended_newick_to_dinetwork(newick[, ...])

Converts a Newick string to a networkx DAG with leaf labels.

phylox.newick_parser.dinetwork_to_extended_newick(network, simple=False)

Converts a phylogenetic network to a Newick string. The newick string has :length:bootstrap:probability annotations if any edge has a bootstrap or probability. If only lengths are available, the newick string has :length annotations.

Parameters:
  • network – a phylogenetic network, i.e., a phylox DiNetwork.

  • simple – Boolean, indicating whether to create a simple newick string without parameters

Returns:

a string in extended Newick format for phylogenetic networks.

Example:

>>> from phylox import DiNetwork
>>> from phylox.constants import LENGTH_ATTR
>>> from phylox.newick_parser import dinetwork_to_extended_newick
>>> network = DiNetwork(
...     edges=[
...         (0, 1, {LENGTH_ATTR: 1.0}),
...         (0, 2, {LENGTH_ATTR: 1.0}),
...         (1, 3, {LENGTH_ATTR: 1.0}),
...         (2, 3, {LENGTH_ATTR: 1.0}),
...         (1, 4, {LENGTH_ATTR: 1.0}),
...         (2, 5, {LENGTH_ATTR: 1.0}),
...         (3, 6, {LENGTH_ATTR: 1.0}),
...     ],
...     labels=((0, "A"), (4, "B"), (5, "C"), (6, "D"), (3, "E")),
... )
>>> newick = dinetwork_to_extended_newick(network)
>>> "C:1.0" in newick
True
phylox.newick_parser.extended_newick_to_dinetwork(newick, internal_labels=False)

Converts a Newick string to a networkx DAG with leaf labels. The newick string may or may not have length:bootstrap:probability annotations. The newick string may or may not have internal node labels. The newick string may or may not have hybrid nodes.

Parameters:
  • newick – a string in extended Newick format for phylogenetic networks.

  • internal_labels – a boolean, indicating whether the internal nodes of the network are labeled.

Returns:

a phylogenetic network, i.e., a networkx digraph with leaf labels represented by the `label’ node attribute.

Example:

>>> newick = "(A:1.1,B:1.2,(C:1.3,D:1.4)E:1.6)F;"
>>> network = extended_newick_to_dinetwork(newick)
>>> {network.nodes[leaf].get("label") for leaf in network.leaves} == {'A', 'B', 'C', 'D'}
True
>>> node_for_label_A = network.label_to_node_dict['A']
>>> p = network.parent(node_for_label_A)
>>> network[p][node_for_label_A]['length']
1.1