-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrees.py
34 lines (27 loc) · 1.2 KB
/
trees.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
#######################################################
# Tree reading/writing for Simple name Cleaner
# produced at Phylotastic-II / NESCent::HIP
#
# TNRS Team: Dan Leehr, Andrew Lenards, Guarav Vaidya
#######################################################
import dendropy
class Tree:
def __init__(self,filename,type):
self.tree = dendropy.Tree.get_from_path(filename, type)
def write_nexml_tree(self,outputname):
with open(outputname,'w') as out:
self.tree.write(out,'nexml')
def get_otu_labels(self):
return [t.label for t in self.tree.taxon_set]
def get_node_labels(self):
return [n.label for n in self.tree.get_node_set()]
def replace_otu_labels(self,mapping):
# dict of old: new
# Ideally would include the URIs and fancy things, but this is first stab
for key in mapping:
print "OTU: changing %s to %s" % (key, mapping[key])
self.tree.taxon_set.get_taxon(label=key).label=mapping[key]
def replace_node_labels(self, mapping):
for key in mapping:
print "Node: changing %s to %s" % (key, mapping[key])
self.tree.find_node_with_label(key).label=mapping[key]