Skip to content

Commit

Permalink
feat(tree): Add distance matrix to newick conversion method.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmussig committed Apr 11, 2022
1 parent 07106da commit 9146bbb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ This has been written with the intention of personal use, but feel free to use/c

util/io
util/accession
util/tree


.. toctree::
Expand Down
5 changes: 5 additions & 0 deletions docs/source/util/tree.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
****
Tree
****

.. autofunction:: magna.util.tree.dm_to_newick
31 changes: 31 additions & 0 deletions magna/util/tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Collection

import numpy as np
from scipy.cluster import hierarchy


def _get_newick(node, parent_dist, leaf_names, newick='') -> str:
if node.is_leaf():
return "%s:%.2f%s" % (leaf_names[node.id], parent_dist - node.dist, newick)
else:
if len(newick) > 0:
newick = "):%.2f%s" % (parent_dist - node.dist, newick)
else:
newick = ");"
newick = _get_newick(node.get_left(), node.dist, leaf_names, newick=newick)
newick = _get_newick(node.get_right(), node.dist, leaf_names, newick=",%s" % newick)
newick = "(%s" % newick
return newick


def dm_to_newick(arr: np.ndarray, labels: Collection[str]) -> str:
"""Convert a pairwise distance matrix into Newick format.
Args:
arr: The symmetrical pairwise distance matrix.
labels: The labels of the leaf nodes.
"""
Z = hierarchy.linkage(arr)
tree = hierarchy.to_tree(Z)
newick = _get_newick(tree, tree.dist, labels)
return newick
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def readme():
packages=find_packages(),
include_package_data=True,
install_requires=['tqdm', 'pandas>=1.1.0', 'pyarrow', 'numpy',
'dendropy', 'biopython'],
'dendropy', 'biopython', 'scipy'],
python_requires='>=3.6',
)

0 comments on commit 9146bbb

Please sign in to comment.