Tree Construction Tools

Classes and methods for tree construction.

class DistanceMatrix(names, matrix=None)[source]

Distance matrix class that can be used for distance based tree algorithms.

All diagonal elements will be zero no matter what the users provide.

Initialize the class.

format_phylip(handle)[source]

Write data in Phylip format to a given file-like object or handle.

The output stream is the input distance matrix format used with Phylip programs (e.g. ‘neighbor’). See: http://evolution.genetics.washington.edu/phylip/doc/neighbor.html

Parameters:
handle : file or file-like object

A writeable file handle or other object supporting the ‘write’ method, such as StringIO or sys.stdout. On Python 3, should be open in text mode.

class TreeConstructor[source]

Base class for all tree constructor.

build_tree(msa)[source]

Caller to built the tree from a MultipleSeqAlignment object.

This should be implemented in subclass.

class DistanceTreeConstructor(method='nj')[source]

Distance based tree constructor.

Parameters:
method : str

Distance tree construction method, ‘nj’(default) or ‘upgma’.

Loading a small PHYLIP alignment from which to compute distances, and then build a upgma Tree:

from Bio.Phylo.TreeConstruction import DistanceTreeConstructor
from Bio.Phylo.TreeConstruction import DistanceCalculator
from Bio import AlignIO
aln = AlignIO.read(open('TreeConstruction/msa.phy'), 'phylip')
constructor = DistanceTreeConstructor()
calculator = DistanceCalculator('identity')
dm = calculator.get_distance(aln)
upgmatree = constructor.upgma(dm)
print(upgmatree)

Output:

Tree(rooted=True)
    Clade(branch_length=0, name='Inner4')
        Clade(branch_length=0.18749999999999994, name='Inner1')
            Clade(branch_length=0.07692307692307693, name='Epsilon')
            Clade(branch_length=0.07692307692307693, name='Delta')
        Clade(branch_length=0.11057692307692304, name='Inner3')
            Clade(branch_length=0.038461538461538464, name='Inner2')
                Clade(branch_length=0.11538461538461536, name='Gamma')
                Clade(branch_length=0.11538461538461536, name='Beta')
            Clade(branch_length=0.15384615384615383, name='Alpha')

Build a NJ Tree:

njtree = constructor.nj(dm)
print(njtree)

Output:

Tree(rooted=False)
    Clade(branch_length=0, name='Inner3')
        Clade(branch_length=0.18269230769230765, name='Alpha')
        Clade(branch_length=0.04807692307692307, name='Beta')
        Clade(branch_length=0.04807692307692307, name='Inner2')
            Clade(branch_length=0.27884615384615385, name='Inner1')
                Clade(branch_length=0.051282051282051266, name='Epsilon')
                Clade(branch_length=0.10256410256410259, name='Delta')
            Clade(branch_length=0.14423076923076922, name='Gamma')

Initialize the class.

nj(distance_matrix)[source]

Construct and return a Neighbor Joining tree.

Parameters:
distance_matrix : DistanceMatrix

The distance matrix for tree construction.

upgma(distance_matrix)[source]

Construct and return an UPGMA tree.

Constructs and returns an Unweighted Pair Group Method with Arithmetic mean (UPGMA) tree.

Parameters:
distance_matrix : DistanceMatrix

The distance matrix for tree construction.