Whenever we try to publish an algorithm for speeding up neighbour-joining, at least one reviewer will ask us if we have checked that our new algorithm generates the same tree as the “canonical” neighbour-joining algorithm does.
This sounds reasonable, but it isn’t really. The neighbour-joining method is not as deterministic as you’d think.
Well, it is a deterministic algorithm in the sense that the same input will always produce the same output, but equivalent input will not necessarily produce the same output.
The neighbour-joining algorithm
The neighbour-joining algorithm is phylogenetic inference method that takes as input a matrix of pairwise distances between taxa and outputs a tree connecting the taxa. It is a heuristic for producing “minimum evolution” trees, that is trees where the total branch length is as small as possible (while still, of course, reflecting the distances between the input taxa). A heuristic is needed, since optimising the minimal evolution criteria is computationally intractable, and the neighbour-joining algorithm is a heuristic that strikes a good balance between computational efficiency and inference accuracy.
The heuristic is not a “heuristic optimisation algorithm” in the sense that simulated annealing or evolutionary algorithms is. It is not randomised. It is a greedy algorithm, that in each step makes an optimal choice, which will hopefully eventually leads to a good solution (although locally optimial choices are not guaranteed to lead to a global optimal choice).
A problem with a greedy algorithm is that local choices can drastically change the final result. Not all greedy algorithms, of course. Some are guaranteed to reach a global optimum and if there is only a single global optimum then the local choices will not affect the final outcome (but if there is more than one global optimum the local choices will also influence the final result).
In the neighbour-joining algorithm, the greedy step consists of joining two clusters that minimise a certain criteria. If two or more pairs minimise the criteria, a choice must be made, and that choice will affect the following steps.
Equivalent distance matrices does not mean identical input
Unless the choice is resolved randomly, and I’m assuming that it isn’t, then the algorithm is deterministic. It could, for example, always choose the first or last pair minimising the criteria. How this choice should be resolved is not specified in the algorithm, so anything goes, really.
If the choice is resolved by picking the first or last pair, then exact form of the input matters.
The distance matrix neighbour-joining uses as input gives us all the pair-wise distances between our taxa, but to represent it we need to order the taxa to get row and column numbers. This ordering is completely arbitrary, and any ordering gives us an equivalent distance matrix. Not an identical one, though.
If we resolve the local choice in a way that depend on the row and column order, then two equivalent input matrices might produce very different output trees.
To see how much this matters in practice, I made a small experiment. I took 37 distance matrices based on Pfam sequences, with between 100 and 200 taxa. You can see the matrices here. There are 39 of them, but I had to throw two of them away — AIRC and Acyl_CoA_thio — since they had taxons with the same name, and with that I cannot compare the resulting trees. It’s matrices I created for this paper, but I don’t remember the details… it is not important for the experiment, though.
For each matrix I constructed ten equivalent distance matrices by randomly permuting the order of taxa. Then I used these ten matrices to construct ten neighbour-joining trees and finally computed the Robinson-Foulds between all pairs of these trees.
To see a plot of the result, click on the figure on the left.
As you can see, you can get quite different trees out of equivalent distance matrices and on realistic data too.
I didn’t check if the differences between the trees are correlated with how much support the splits have in the data. Looking at bootstrap values for the trees might tell us something about that. Anyway, that is not the take home message I want to give here. That is that it doesn’t necessarily make sense to talk the neighbour-joining tree for a distance matrix.
Neighbour-joining is less deterministic than you might think!