<p>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 <a href="http://scikit-bio.org/docs/latest/generated/skbio.tree.nj.html?highlight=neighbor%20join" rel="nofollow noreferrer">Sciki-Bio package</a>. A less straightforward option with more freedom is by using the <a href="https://docs.scipy.org/doc/scipy/reference/cluster.hierarchy.html" rel="nofollow noreferrer">scipy.cluster.hierarchy</a> module to obtain a linkage matrix, then using the <code>to_tree()</code> method to obtain a scipy <code>Tree</code> object, and finally writing a script to <a href="https://stackoverflow.com/questions/28222179/save-dendrogram-to-newick-format">convert a scipy Tree object to a newick string</a>.</p>
<p>I've copied the example from scikit-bio below:</p>
<pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; from skbio import DistanceMatrix
&gt;&gt;&gt; from skbio.tree import nj
&gt;&gt;&gt;
&gt;&gt;&gt; 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]]
&gt;&gt;&gt; ids = list('abcde')
&gt;&gt;&gt;
&gt;&gt;&gt; dm = DistanceMatrix(data, ids)
&gt;&gt;&gt; # Or from a tsv file with header, no column 0
&gt;&gt;&gt; reader = csv.reader(open(args.inputfile), delimiter=&quot;\t&quot;)
&gt;&gt;&gt; x = list(reader)                                         
&gt;&gt;&gt; dm = DistanceMatrix(x[1:],x[0])                          
&gt;&gt;&gt;
&gt;&gt;&gt; newick_str = nj(dm, result_constructor=str)
&gt;&gt;&gt; print(newick_str[:55], &quot;...&quot;)
(d:2.000000, (c:4.000000, (b:3.000000, a:2.000000):3.00 ...
</code></pre>
<ul>
<li>Note: Both methods require you to supply the input as a numpy distance matrix</li>
</ul>

CC BY-SA 4.0
2021-08-09T18:36:34.813Z
2021-08-10T14:04:31.100Z
2021-08-10T14:04:31.100Z
Throckmorton
<p>I have a file with a 3-column list of pairwise distances:</p>
<pre><code>A B distance
A C distance
B C distance
...
</code></pre>
<p>And I would like to calculate a phylogenetic tree for all the nodes in the list. I've seen <code>phylip neighbor</code> 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?</p>

<phylogeny>
calculating phylogenetic tree from pairwise distances
# calculating phylogenetic tree from pairwise distances

(Asked by: username_0 on 2021-08-09)

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?

**Tags:** phylogeny

---

## 1 answers

### Answer by username_1 on 2021-08-09. Score: 3 | Accepted answer

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
>>> reader = csv.reader(open(args.inputfile), delimiter="\t")
>>> x = list(reader)                                         
>>> dm = DistanceMatrix(x[1:],x[0])                          
>>>
>>> newick_str = nj(dm, result_constructor=str)
>>> print(newick_str[:55], "...")
(d:2.000000, (c:4.000000, (b:3.000000, a:2.000000):3.00 ...


Note: Both methods require you to supply the input as a numpy distance matrix


---

