def pd(tree, tips): traveled = set() traveled.add(tree_utils.get_mrca(list(tips), tree)) measure = 0 for i in tips: c = i while c not in traveled: measure += c.length if c.parent == None: #root break traveled.add(c) c = c.parent return measure
def phylo_dist(tip1,tip2,tree): mrca = tree_utils.get_mrca([tip1,tip2],tree) d1 = 0 d2 = 0 cn = tip1 while cn != mrca: d1 += cn.length cn = cn.parent cn = tip2 while cn != mrca: d2 += cn.length cn = cn.parent return d1+d2
print >> sys.stderr, "not matched", i.label continue done = set() dates = {} # key is node, value is date dates_names = {} #key is node, value is mrca string for i in dated.iternodes(order="preorder"): if len(i.children) == 0: continue else: nds = [] for j in i.leaves(): if "node" in j.data: nds.append(j.data["node"]) if len(nds) > 1: x = tree_utils.get_mrca(nds, ott) if x in done: continue done.add(x) dates[x] = i.height dates_names[x] = x.children[0].leaves( )[0].label + "," + x.children[1].leaves()[0].label # check backwards dele = set() for i in dates: if i in dele: continue cur = i while cur != None: try:
if i.name not in removetaxa: tr.write(i.get_fasta()) tr.close() tree = tree_reader.read_tree_file_iter(sys.argv[2]).next() nodes = {} mrca_pars = [] #vector of the parents of each remove constraint child_constraints = [ ] #vector of child_constraints for each remove constraint for i in tree.leaves(): nodes[i.label] = i for i in remove_mrca: tnods = [] for j in i: tnods.append(nodes[j]) mr = tree_utils.get_mrca(tnods, tree) par = mr.parent tchild_constraints = [] for j in mr.children: if len(j.children) > 0: tchild_constraints.append(j) j.parent = par par.add_child(j) child_constraints.append(tchild_constraints) par.remove_child(mr) mrca_pars.append(par) for i in removetaxa: # in case you remove if i not in nodes: continue par = nodes[i].parent
mrca_checks[spls[0]].append(i) except: mrca_checks[spls[0]] = [] mrca_checks[spls[0]].append(i) of = open(sys.argv[2], "r") for i in of: nm = i.strip() nm1 = nm.split("_")[0] if nm1 in mrca_checks: print nm1, mrca_checks[nm1] x = None if len(mrca_checks[nm1]) == 1: x = mrca_checks[nm1][0] else: x = tree_utils.get_mrca(mrca_checks[nm1], tree) print x.get_newick_repr() n2 = node.Node() n2.label = nm # is a tip if len(x.children) == 0: p = x.parent # add a child n = node.Node() n.length = x.length / 2. x.length = n.length p.remove_child(x) n.add_child(x) n2.length = x.length n.add_child(n2) p.add_child(n)