2019-07-24 03:27:54 -07:00
|
|
|
from ete3 import Tree
|
|
|
|
|
import numpy as np
|
|
|
|
|
import progressbar
|
|
|
|
|
|
2019-07-24 04:54:02 -07:00
|
|
|
|
2019-07-24 03:27:54 -07:00
|
|
|
def create_branch_length_padding(bl):
|
2019-07-24 04:54:02 -07:00
|
|
|
maxlen = 29
|
2019-07-24 03:27:54 -07:00
|
|
|
for x in bl:
|
2019-07-24 04:54:02 -07:00
|
|
|
for i in range(len(x), maxlen):
|
2019-07-24 03:27:54 -07:00
|
|
|
x.append(0)
|
|
|
|
|
|
2019-07-24 04:54:02 -07:00
|
|
|
|
|
|
|
|
def create_tree_data(treename, df):
|
|
|
|
|
t = Tree(treename)
|
|
|
|
|
branch_lengths_s = []
|
|
|
|
|
branch_lengths_hs = []
|
|
|
|
|
dist = []
|
|
|
|
|
ns = []
|
|
|
|
|
nhs = []
|
|
|
|
|
for index, row in progressbar.progressbar(df.iterrows()):
|
|
|
|
|
d = 0
|
|
|
|
|
x = row["species"]
|
|
|
|
|
y = row["homology_species"]
|
|
|
|
|
bl = []
|
|
|
|
|
c = 0
|
|
|
|
|
mca = t.get_common_ancestor(x, y)
|
|
|
|
|
node = t & x
|
|
|
|
|
while node.up != mca:
|
|
|
|
|
d += node.dist
|
2019-07-24 03:27:54 -07:00
|
|
|
bl.append(node.dist)
|
2019-07-24 04:54:02 -07:00
|
|
|
node = node.up
|
|
|
|
|
c += 1
|
2019-07-24 03:27:54 -07:00
|
|
|
ns.append(c)
|
2019-07-24 04:54:02 -07:00
|
|
|
c = 0
|
2019-07-24 03:27:54 -07:00
|
|
|
branch_lengths_s.append(bl)
|
2019-07-24 04:54:02 -07:00
|
|
|
bl = []
|
|
|
|
|
node = t & y
|
|
|
|
|
while node.up != mca:
|
|
|
|
|
d += node.dist
|
2019-07-24 03:27:54 -07:00
|
|
|
bl.append(node.dist)
|
2019-07-24 04:54:02 -07:00
|
|
|
node = node.up
|
|
|
|
|
c += 1
|
2019-07-24 03:27:54 -07:00
|
|
|
nhs.append(c)
|
|
|
|
|
branch_lengths_hs.append(bl)
|
|
|
|
|
dist.append(d)
|
|
|
|
|
create_branch_length_padding(branch_lengths_s)
|
|
|
|
|
create_branch_length_padding(branch_lengths_hs)
|
2019-07-24 04:54:02 -07:00
|
|
|
return np.array(branch_lengths_s), np.array(
|
|
|
|
|
branch_lengths_hs), np.array(dist), np.array(ns), np.array(nhs)
|