# calculating phylogenetic tree from pairwise distances

I have a file with a 3-column list of pairwise distances:

A B distance
A C distance
B C distance
...


And I would like to calculate a phylogenetic tree for all the nodes in the list. I've seen phylip neighbor and similar options, but it seems like the length of the ids is a limit, which I rather not have to deal with. What would be a straightforward way of getting a newick tree output out of an input as above?

I believe there are a number of ways to construct a tree metric from a distance metric. A very straightforward method, neighbor joining, is available in the Sciki-Bio package. A less straightforward option with more freedom is by using the scipy.cluster.hierarchy module to obtain a linkage matrix, then using the to_tree() method to obtain a scipy Tree object, and finally writing a script to convert a scipy Tree object to a newick string.

I've copied the example from scikit-bio below:

>>> from skbio import DistanceMatrix
>>> from skbio.tree import nj
>>>
>>> data = [[0,  5,  9,  9,  8],
...         [5,  0, 10, 10,  9],
...         [9, 10,  0,  8,  7],
...         [9, 10,  8,  0,  3],
...         [8,  9,  7,  3,  0]]
>>> ids = list('abcde')
>>>
>>> dm = DistanceMatrix(data, ids)
>>> # Or from a tsv file with header, no column 0