phylonetwork package

phylonetwork module

Main module of the package. Defines classes and methods for working with phylogenetic trees and networks.

class phylonetwork.NetworkShape(*args, **kwargs)

Bases: networkx.classes.digraph.DiGraph

Class for unlabeled phylogenetic networks/trees.

In this class we only consider properties that depend on the topology of the network, without a labeling.

We implicitly assume:

  • There is a single root
new_node()

Creates a node as a string of the form _n with unique n, and returns it

new_reticulation_node()

Creates a node as a string of the form #n with unique n, and returns it

is_leaf(u)

Tests if u is a leaf of the network

leaves

The set of all leaves of the network

interior_nodes

The set of all interior (not leaves) nodes of the network

is_reticulation(u)

Tests if node u is a reticulation (indeg >= 2)

reticulations

The set of all reticulations

is_tree_node(u)

Tests if node u is a tree node (indeg <= 1)

tree_nodes

The set of all tree nodes

is_root(u)

Tests if u is a root

roots

The set of all roots of the network

root

Root of the network. Raises exception if no root or >1 roots

is_elementary(u)

Tests if u is elementary

elementary_nodes

The set of elementary nodes

bottom_to_top_nodes

List of nodes ordered from bottom to top (last node is the root)

top_to_bottom_nodes

List of nodes ordered from top to bottom (first node is the root)

depths

Dict associating each node to its depth (distance to root)

heights

Dict associating each node to its height (max. distance to a leaf)

clear_cache()

Clears the cache of all computed properties

remove_node_and_reconnect(u, clear_cache=False)

Removes node u and connects each of its parents to each of its children

remove_elementary_nodes()

Removes all elementary nodes of the network

make_elementary_node(u, v)

Adds an elementary node between u and v. Returns the added node.

level()

Returns the level of the network

class phylonetwork.PhylogeneticNetwork(eNewick=None, sequence=None, mu_repr=None, **kwargs)

Bases: phylonetwork.NetworkShape

Class for (labeled) phylogenetic networks.

We assume:

  • All leaves are labeled
  • Internal nodes _may_ be labeled
  • All labels are different

It can be given by their eNewick, its reduction sequence (for orchard networks), or its mu_repr (for tree-child networks). If none of those are given, it will be created empty.

The (e)Newick features implemented are:

  • Reticulation events [CRV08]
  • Additional data on nodes and arcs. Format:
[&node_metadata]:[&arc_metadata]arc_length
delta(coords, total)

Creates a vector with 1 in the coords specified and 0 elsewhere.

labeling_dict

Dict that maps nodes to labels

taxa

Set of labels used for nodes

ordered_taxa

Ordered list of taxa

cached_taxa

Set of cached labels of taxa.

ordered_cached_taxa

Set of cached labels of taxa.

set_cached_taxa(taxa)

Sets the default cached_taxa.

clear_cached_taxa()

Clears the cached taxa to remove from extend mu-data.

is_labeled(u)

Tests if u is labeled

label(u)

Returns the label of u (or None if it is not labeled)

labeled_nodes

Set of all labeled nodes

unlabeled_nodes

Set of all unlabeled nodes

lookup_label_dict

Dict that maps each label to the corresponding node

node_with_label(taxon)

Returns the node label by taxon (or None if no such label)

is_unlabeled_elementary_or_leaf(u)

Tests if u is elementary (not labeled and (d_in,d_out)=(1,1),(0,1) or (1,0)

eNewick()

Returns the eNewick representation of the network.

mu_dict

Dict that maps each node to its mu-vector

mu(u)

Returns the mu-vector of u

emu_dict

Dict that maps each node to its extended mu-vector

emu_representation

Dict corresponding to the extended mu-data (no reticulations).

emu(u)

Returns the extended mu-vector of u

cluster_dict

Dict that maps each node to its cluster

cluster(u)

Returns the cluster of u

nested_label_dict

Dict that maps each node to its nested label representation

nested_label(u)

Returns the nested label representation of u

remove_taxa(taxa, prune_network=True)

Removes the given taxa from the network and, if prune_network, eliminates elementary nodes

topological_restriction(taxa)

Returns the topological restriction of the network to the subset taxa of labels

has_nested_taxa()

Tests if the network has labeled internal nodes

is_tree_child()

Tests if the network is tree-child

parent(u)

Returns one parent of u or ‘’ if it’s the root.

cherry(a, b)

Set this network as the cherry (a, b).

add_pair(pair)

Adds the (reticulated) cherry pair=`(i,j)`.

reduce_pair(pair)

Reduces the pair=`(i,j)`

reduce_sequence(seq)

Reduces by the given sequence.

add_sequence(seq)

Adds by the given sequence.

reducible_pairs

List of the reducible pairs.

smallest_pair

Returns the smallest reducible pair.

all_sequences_array

Returns a list of all the CPS.

smallest_sequence_array

Returns the smallest reducible sequence, if any.

all_sequences

Returns a list of all the CPS.

smallest_sequence

Returns the smallest reducible sequence, if any.

is_orchard()

Tests if the network is orchard.

distance(other)

Returns the distance between self and other.

draw()

Plots the network with labels

class phylonetwork.PhylogeneticTree(Newick=None, **kwargs)

Bases: phylonetwork.PhylogeneticNetwork

Class for working with phylogenetic trees.

Implements some methods that are not defined for networks (or are easier to implement on trees).

If Newick is given, it will be populated.

LCA_dict

Dict that associate to each pair of taxons its least common ancestor

LCA(u, v)

Returns the LCA of u and v

nodal_matrix()

Not implemented

cophenetic_matrix()

Not implemented

class phylonetwork.LGTNetwork(eNewick=None, **kwargs)

Bases: phylonetwork.PhylogeneticNetwork

Class for representing LGT networks (Not yet implemented)

principal_edges()

Not implemented

secondary_edges()

Not implemented

principal_subtree(simplify=True)

Not implemented

secondary_subtrees(simplify=True)

Not implemented

draw()

Not implemented

phylonetwork.tree_generators module

Module that generates phylogenetic trees either sequentially or randomly. The implemented random models are uniform and Yule distributions

phylonetwork.tree_generators.all_binary_trees_by_num_taxa(num_taxa)
phylonetwork.tree_generators.relabel(tree, lookup_dict, taxa)
phylonetwork.tree_generators.all_binary_trees_by_taxa(taxa)
phylonetwork.tree_generators.tree_with_single_taxon(taxon)
phylonetwork.tree_generators.all_trees(taxa, binary=True)

Returns a sequential generator for all trees with leaves labeled by taxa.

If binary is True, then only binary trees will be generated; otherwise trees will have internal nodes with arbitrary out-degree.

phylonetwork.tree_generators.number_of_trees(n, binary=True)

Returns the number of phylogenetic trees with n labelled leaves. If binary is True, only binary trees are counted.

phylonetwork.tree_generators.random_uniform_tree_generator(taxa, binary=True)

Returns a random uniform generator of trees with leaves labeled by taxa.

If binary is True, then only binary trees will be generated; otherwise trees will have internal nodes with arbitrary out-degree.

phylonetwork.tree_generators.random_yule_tree_generator(taxa)

Returns a random Yule generator of binary trees with leaves labeled by taxa.

phylonetwork.btctc_generators module

Generation of random binary tree-child time-consistent networks under a Yule-like model.

phylonetwork.btctc_generators.speciate(net: phylonetwork.NetworkShape, u)
phylonetwork.btctc_generators.hybridize(net: phylonetwork.NetworkShape, u1, u2)
phylonetwork.btctc_generators.random_combination(base, r)
phylonetwork.btctc_generators.random_yule_btctc_generator(taxa, p)

phylonetwork.balances module

Implementation of different balance indices for phylogenetic trees or networks.

phylonetwork.balances.sackin(t)

Computes the Sackin (un)balance index of t

phylonetwork.balances.extended_sackin(net: phylonetwork.PhylogeneticNetwork)

Computes the extended Sackin (un)balance index of net

phylonetwork.balances.entropy(t)

Computes the entropy balance index of t

phylonetwork.tree_operations module

Auxiliary constructors for phylogenetic trees.

phylonetwork.tree_operations.push_and_hang(net, u, newtaxa)

Builds a new Phylogenetic Tree from net and u by hanging a leaf labeled newtaxa between u and its parent.

 |          |
 |    =>    |\
 u          u \newtaxa
/ \        / \
phylonetwork.tree_operations.hold_and_hang(net, u, newtaxa)

Builds a new Phylogenetic Tree from net and u by hanging a leaf labeled newtaxa from u

 |    =>    |
 u          u
/|         /|\
              \newtaxa
phylonetwork.tree_operations.push_and_label(net, u, newtaxa)

Builds a new Phylogenetic Tree from net and u by inserting an elementary node between u and its parent and labeling it with newtaxa

 |          |
 |          newtaxa
 |    =>    |
 u          u
/ \        / \
phylonetwork.tree_operations.hold_and_label(net, u, newtaxa)

Builds a new Phylogenetic Tree from net and u by labeling u with newtaxa

 |          |
 |    =>    |
 u          u=newtaxa
/ \        / \

phylonetwork.exceptions module

Custom exceptions for the phylonetwork package

exception phylonetwork.exceptions.MalformedNewickException

Bases: Exception

Raised when creating a new network from a malformed eNewick string.

exception phylonetwork.exceptions.TaxaException

Bases: Exception

Raised when trying to compare to networks with incompatible Taxa.

exception phylonetwork.exceptions.NotAPhylogeneticNetwork

Bases: Exception

Raised when the graph is not a Phylogenetic Network

exception phylonetwork.exceptions.NotImplemented

Bases: Exception

Raised when a method/function is not yet implemented

phylonetwork.utils module

phylonetwork.utils.random_weighted(d)

Given d a dictionary, returns a key in it with probability equals to its value (normalized over the sum of all values)

phylonetwork.utils.product(iterable)
class phylonetwork.utils.clearable_cached_property(func)

Bases: object

Adapted from standard library in order to keep track of cached attributes and allow for its clearance.

phylonetwork.utils.clear_cache(obj)

References

[CRV08]Cardona, G., Rosselló, F., & Valiente, G. (2008). Extended Newick: it is time for a standard representation of phylogenetic networks. BMC Bioinformatics, 9(1), 532. https://doi.org/10.1186/1471-2105-9-532