Skip to content

How to get initial tree with ete3

Sina Majidian edited this page Mar 7, 2025 · 2 revisions

You can use python package ete3 and run this in python. Note that ete3 is already installed in fastoma environment.

Example when you want to find the tree for a list of species with these taxanomic IDs

list_tax=["2777116", "135651" ,"6239", "2018661", "96644"]

from ete3 import NCBITaxa
ncbi = NCBITaxa()

list_tax=["2777116", "135651" ,"6239", "2018661", "96644"]

taxid2name = ncbi.get_taxid_translator(list_tax)
print(taxid2name)
list_sp= list(taxid2name.values())

#tree = ncbi.get_descendant_taxa(list_sp, collapse_subspecies=True, return_tree=True)
tree = ncbi.get_topology(list_tax)
tree.write()

#print tree.get_ascii(attributes=['sci_name', 'taxid'])
node_tax=[]
for  node in tree.traverse():
    node_tax.append(node.name)
node_tax.append(tree.name)

taxid2name = ncbi.get_taxid_translator(node_tax)
for  node in tree.traverse():
    node_name=node.name
    node.name="_".join(str(taxid2name[int(node_name)]).split(" "))
tree.write(format=1,format_root_node=True)

The output will be

{6239: 'Caenorhabditis elegans', 96644: 'Mesorhabditis spiculigera', 135651: 'Caenorhabditis brenneri', 2018661: 'Diploscapter pachys', 2777116: 'Caenorhabditis auriculariae'}
'((Caenorhabditis_brenneri:1,Caenorhabditis_auriculariae:1,Caenorhabditis_elegans:1)Caenorhabditis:1,Mesorhabditis_spiculigera:1,Diploscapter_pachys:1)Rhabditidae:1;'

You can change this python script to start with species name. See here for details.

Clone this wiki locally