This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract Tree Topology From Ncbi Taxonomy Database

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

bioperl taxonomy

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.

from ete3 import NCBITaxa
ncbi = NCBITaxa()

tree = ncbi.get_topology([9606, 9598, 10090, 7707, 8782])
print tree.get_ascii(attributes=["sci_name", "rank"])

Can someone tell me how I can convert the output to a Newick file?

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"]))

Thanks! This works as well. Is there a way to replace the TaxIDs with the scientific names?

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]);

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:

ete3_ncbiquery_tree

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.