Creating a Distance Matrix from a Phylogenetic Tree

What will you learn?

In this detailed guide, you will learn how to extract or reconstruct a distance matrix from an existing phylogenetic tree using Python. By leveraging the Biopython’s Phylo module, you will understand the process of converting evolutionary relationships represented in trees into a structured distance matrix format.

Introduction to the Problem and Solution

In the realm of phylogenetics, researchers often work with phylogenetic trees that visualize evolutionary distances between species or sequences. After constructing such trees, there arises a need to analyze these relationships in various formats, one of which is the distance matrix. The challenge we tackle here is extracting the original distance information from a phylogenetic tree.

To address this problem, we delve into utilizing Biopython’s Phylo module. While directly obtaining a distance matrix from an existing tree may not be explicitly documented within Phylo, we can navigate around this limitation by understanding how distances are encoded within trees and manually compiling these values into a matrix structure.


from Bio import Phylo
from scipy.spatial import distance_matrix
import numpy as np

# Load your tree (assuming it's in Newick format)
tree ="your_tree_file.newick", "newick")

# Extract terminal nodes' names (leaf labels)
terminals = [ for terminal in tree.get_terminals()]

# Initialize an empty dictionary to hold distances
distances = {name: {} for name in terminals}

# Calculate pairwise distances and populate dictionary
for i, term1 in enumerate(terminals):
    for j, term2 in enumerate(terminals[i+1:], i+1):
        dist = tree.distance(term1, term2)
        distances[term1][term2] = dist
        distances[term2][term1] = dist

# Convert dictionary into 2D array (distance matrix)
names_ordered = sorted(distances.keys())
matrix = np.array([[distances[name1].get(name2) if name1 != name2 else 0 
                    for name2 in names_ordered] 
                   for name1 in names_ordered])

print("Distance Matrix:\n", matrix)

# Copyright PHD


The solution involves reading the phylogenetic tree file using and extracting terminal node names as they represent leaf-to-leaf evolutionary divergences. Pairwise distances among these terminals are computed using tree.distance(), storing these values in a nested dictionary structure based on terminal names. Finally, this dictionary is transformed into a numerical array representing an NxN square distance matrix.

    • How do I install BioPython? To use Phylo and other BioPython modules, install them via pip:

    • pip install biopython
    • # Copyright PHD
    • What file formats can I use with BioPython supports formats like Newick, Nexus, PhyloXML etc., offering flexibility based on your data source.

    • Can I visualize my matrices/trees? Yes! Both original trees and resulting matrices can be visualized using libraries like Matplotlib alongside BioPython�s visualization capabilities.

    • How accurate is the derived distance matrix compared to pre-tree matrices? Accuracy depends on initial conditions; close approximations are expected assuming minimal computation errors.

    • Can I save my generated distance matrix easily? Yes! Use NumPy�s savetxt function:

    • np.savetxt("distance_matrix.csv", matrix , delimiter=",")
    • # Copyright PHD
    • Are there alternative methods/libraries capable of similar tasks? Other libraries like ETE Toolkit offer functionalities around phylogeny but come with their learning curves.


Reconstructing or generating a distance matrix from a phylogenetic tree provides valuable insights into evolutionary relationships depicted visually. By utilizing powerful libraries like Biopython�s Phylo module and computational tools such as NumPy, transitioning between representation forms enriches analytical perspectives available for broader scientific inquiries.

Leave a Comment