PTreeGenerator  1.0
Simple phylogenetic tree generation from multiple sequence alignment.
 All Classes Namespaces Files Functions Variables
Functions
ptreegen.utilities.tree_utils Namespace Reference

Functions

def findShortestEdge
 Finds the two vertices with minimal edge weight.
def initEdgeLengths
 Initializes all edges in the tree to a given value.
def findConsensusTree
 Returns weighted consensus tree.

Function Documentation

def ptreegen.utilities.tree_utils.findConsensusTree (   trees,
  weights = [],
  lim = 0 
)

Returns weighted consensus tree.

Uses 50% majority rule.

It is a very slightly modified version of Francois-Jose Serra's code (accesible from here: https://github.com/fransua/utils/blob/master/pmodeltest/consensus.py).

Definition at line 44 of file tree_utils.py.

44 
45 def findConsensusTree(trees,weights=[],lim=0):
46  if weights == []:
47  weights = [1] * len (trees)
48  dic = {}
49  outgroup_name = trees[0].get_leaf_names()[1]
50  tlen = 0
51  for (tree, weight) in zip (trees, weights):
52  if tlen == 0: tlen = len(tree)
53  elif len (tree) != tlen: exit('ERROR: trees with different length')
54  outgroup = tree.search_nodes(name=outgroup_name)[0]
55  tree.set_outgroup(outgroup)
56  dad = outgroup.get_sisters()[0]
57  for node in dad.traverse():
58  if node.is_root(): continue
59  cluster = ','.join (sorted (node.get_leaf_names()))
60  if dic.has_key(cluster):
61  dic[cluster] += weight
62  else:
63  dic[cluster] = weight
64 
65  sorted_nodes = map(lambda x: [x[2], x[1]], sorted (\
66  map (lambda x: (len (x.split(',')), x, dic[x]), \
67  dic.keys()), reverse = True))
68  if lim < sorted (sorted_nodes, reverse=True)[:tlen*2 - 3][-1][0]:
69  lim = sorted (sorted_nodes, reverse=True)[:tlen*2 - 3][-1][0]
70  sorted_nodes = filter (lambda x: x[0] >= lim, sorted_nodes)
71  sorted_nodes = map (lambda x: x[1], sorted_nodes)
72  if len (sorted_nodes) > tlen*2 - 3:
73  print >> stderr, \
74  'WARNING: two nodes with same support, will remove: ' + \
75  sorted_nodes[-1]
76  sorted_nodes = sorted_nodes[:-1]
77  cons_tree = Tree()
78  cons_tree.add_child(name=outgroup_name)
79  node = cons_tree.add_child(name='NoName')
80  node.add_feature('childrens', \
81  set (sorted_nodes.pop(0).split(','))
82  - set([outgroup_name]))
83  while len (sorted_nodes) > 0:
84  for name in sorted_nodes:
85  if not name in sorted_nodes: continue
86  for node in cons_tree.traverse(strategy='postorder'):
87  if node.is_root(): continue
88  if node.name is not 'NoName': continue
89  if len (node.childrens & set(name.split(','))) == 0:
90  continue
91  # check if ther is better solution in one of the child
92  for rest in sorted_nodes:
93  if len (set(rest.split(','))) < \
94  len (set(name.split(','))):
95  continue
96  if len (set(rest.split(',')) & set(name.split(','))) > 0:
97  name = rest
98  weight = dic[name]
99  children = set(name.split(','))
100  if len (children) == 1:
101  node.add_child(name=name)
102  else:
103  n = node.add_child(name='NoName')
104  n.add_feature('childrens', children)
105  n.support = weight
106  break
107  sorted_nodes.pop(sorted_nodes.index(name))
108  sister = node.childrens - children
109  name = ','.join (sorted ( list (sister)))
110  if not name in sorted_nodes:
111  continue
112  weight = dic[name]
113  if len (sister) == 1:
114  node.add_child(name=name)
115  else:
116  n = node.add_child(name='NoName')
117  n.add_feature('childrens', sister)
118  n.support = weight
119  sorted_nodes.pop(sorted_nodes.index(name))
120  break
121 
122  return cons_tree
def ptreegen.utilities.tree_utils.findShortestEdge (   tree)

Finds the two vertices with minimal edge weight.

If there are more minimal edges with the same weight, the first one found is returned.

Parameters
treereference to any vertex of the processed tree
Returns
a tuple of size two which contains the two vertices connected with the shortest edge

Definition at line 18 of file tree_utils.py.

18 
19 def findShortestEdge(tree):
20  shortest_edge = [None, None]
21  min_dist = float("inf")
22  for node in tree.iter_descendants():
23  if node.dist < min_dist:
24  shortest_edge[0] = node.up
25  shortest_edge[1] = node
26  return tuple(shortest_edge)
def ptreegen.utilities.tree_utils.initEdgeLengths (   tree,
  value 
)

Initializes all edges in the tree to a given value.

Parameters
treereference to any vertex of the processed tree
valuea single value for the edge lengths to be initialized to

Definition at line 32 of file tree_utils.py.

32 
33 def initEdgeLengths(tree, value):
34  tree.dist = value
35  for desc in tree.iter_descendants():
36  desc.dist = value