-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_genus_stem_ages.py
39 lines (33 loc) · 1.06 KB
/
get_genus_stem_ages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
__author__ = 'Kinggerm'
# python2
import dendropy
import os
def main():
tree_f = raw_input('Input mcc nexus tree:').strip()
threshold = float(raw_input('Input threshold to cut:'))
if not (0 < threshold < 1):
print 'Error: threshold has to be set in (0, 1)!'
os._exit(0)
tree = dendropy.Tree.get(path=tree_f, schema='nexus')
tree.calc_node_ages()
terminals = tree.leaf_nodes()
# tree.taxon_namespace
stems = {}
for terminal in terminals:
this_age = terminal.parent_node.age
stems[str(terminal.taxon).strip("'")] = this_age
# print list
print ''
taxa = stems.keys()
taxa.sort(key=lambda x:stems[x])
ages = stems.values()
max_t_l = max([len(str(x)) for x in taxa])+4
max_a_l = max([len(str(x)) for x in ages])+1
for taxon in taxa:
print repr(str(taxon)).ljust(max_t_l).replace("'", ''), repr(stems[taxon]).ljust(max_a_l)
# print cut
# ages = list(set(ages))
ages.sort()
print '\nCut age at:', ages[int(len(ages)*threshold)]
if __name__ == '__main__':
main()