Use ETE module its very easy to use and pretty cool.
I have a CSV with the original names inn one col and new names in the next col. I put this into a dict and rename by traaversing over the tree with ETE.
import csv
from ete3 import Tree, PhyloTree
#read a csv file with species names and abbreviations and replace
with open('speciesNames.csv', mode='rU') as infile:
reader = csv.reader(infile)
mydict = {rows[1]:rows[0] for rows in reader}
for n in leafTree:
leaf=str(n).replace('\n--','')
if leaf in mydict.keys():
newLeafName=mydict[leaf]
n.name=(newLeafName)
else:
print leaf + '\t' + "not in Dict"
leafTree.write(format=9,'abbNamesMetazoanTree.nw')
ADD COMMENT
• link
updated 4.9 years ago by
Ram
44k
•
written 8.8 years ago by
moranr
▴
290
Thank you for suggestion! I'll try it!