Last active
August 30, 2019 16:00
-
-
Save Ken-Kuroki/92c8839ecfe11c0cda3cc98e92d430d1 to your computer and use it in GitHub Desktop.
Generate distance matrix from newick tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import product | |
import pandas as pd | |
from ete3 import Tree | |
t = Tree("some_tree_file.nwk") | |
leaves = t.get_leaves() | |
leaf_names = t.get_leaf_names() | |
pair = product(leaves, leaves) | |
dist = {(each[0].name, each[1].name): each[0].get_distance(each[1]) for each in pair} | |
df = pd.DataFrame(index=leaf_names, columns=leaf_names).fillna(0).stack().reset_index() | |
df["dist"] = df.apply(lambda x: dist[(x["level_0"], x["level_1"])], axis=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment