Thanks! This works as well. Is there a way to replace the TaxIDs with the scientific names?
Hi There,
I am wondering if i can extract tree for a given list of species using bioperl from NCBI taxonnomy database.
I hope some of you can help me figure this out.
Many thanks in advance.
Kind Regards,
Lhl
3 answers
from ete3 import NCBITaxa
ncbi = NCBITaxa()
tree = ncbi.get_topology([9606, 9598, 10090, 7707, 9782])
print(tree.write(format=9, features=["sci_name", "rank"]))
from ete3 import NCBITaxa
ncbi = NCBITaxa()
tree = ncbi.get_topology([9606, 9598, 10090, 7707, 8782])
tree.write(features=["sci_name", "rank"], outfile="tree.nw")
gives you an output tree.nw:
(7707:1[&&NHX:sci_name=Dendrochirotida:rank=order],(((9606:1[&&NHX:sci_name=Homo sapiens:rank=species],9598:1[&&NHX:sci_name=Pan troglodytes:rank=species])1:1[&&NHX:sci_name=Homininae:rank=subfamily],10090:1[&&NHX:sci_name=Mus musculus:rank=species])1:1[&&NHX:sci_name=Euarchontoglires:rank=superorder],8782:1[&&NHX:sci_name=Aves:rank=class])1:1[&&NHX:sci_name=Amniota:rank=no rank]);
Thanks for the fast responding! Works like a charm :)
Is there a way to replace the TaxIDs with the scientific names?
A handy way would be:
ete3 ncbiquery --tree --search 9606 9598 10090 7707 8782
You'll get a tree in Newick format and scientific names as node names:

You can do:
for node in tree.traverse():
node.name = node.sci_name
Thanks SMK. I tried this but it does not work.
tree.write(features=["sci_name", "rank"], outfile="tree.nw")
t_file = Tree("tree.nw")
for node in t_file.traverse():
node.name = node.sci_name
tree.write(features=["node.name"], outfile="labeled_tree.nw")
AttributeError: 'TreeNode' object has no attribute 'sci_name'
Hi peterlageweg603,
You have to traverse the one that you got from ncbi.get_topology():
from ete3 import NCBITaxa
ncbi = NCBITaxa()
tree = ncbi.get_topology([9606, 9598, 10090, 7707, 8782])
for node in tree.traverse():
node.name = node.sci_name
print(tree.write())
This gives you:
(Dendrochirotida:1,(((Homo sapiens:1,Pan troglodytes:1)1:1,Mus musculus:1)1:1,Aves:1)1:1);
Ah thank you for the fast responding and help!! This is exactly what I needed :)
Lhl,
The Bioperl Bio::Tree::TreeFunctionsI module has a get_lca method. Is this what you're looking for?
Brian O.
HI Briano,
I want to get species tree given a list of species. given a list containing (speciesA, species B and Species C), i want to know how to get tree for these species. Thank you anyway. I think i might need Bio::Tree::Tree and get_lca in the future.
Cheers
Lhl
Log in to answer this question.
Not a BioPerl solution, but this python script will do the job: https://github.com/jhcepas/ncbi_taxonomy . I wrote some more info at http://jhcepas.cgenomics.org/?p=216
something like Last Common Ancestor From Ncbi Taxonomy Using Java ?
Not exactly, but i think it is useful.I might use it in the future. Thanks a lot.
Lhl
Yes. This is exactly what i want.
Many thanks.
Lhl
I'm trying this method as well.
Can someone tell me how I can convert the output to a Newick file?