Skip to content

Instantly share code, notes, and snippets.

@Ken-Kuroki
Last active October 13, 2018 11:46
Show Gist options
  • Save Ken-Kuroki/cda987eb4dcec28e3a3d689dfdb22cd0 to your computer and use it in GitHub Desktop.
Save Ken-Kuroki/cda987eb4dcec28e3a3d689dfdb22cd0 to your computer and use it in GitHub Desktop.
Get taxonomic name of the rank of your choice from taxonomy ID
import doctest
import xml.etree.ElementTree as ET
from Bio import Entrez
Entrez.email = "example@example.com" # replace it with your actual email address
def get_taxonomic_name(taxid, rank):
'''Return the taxonomic name of the specified rank for the given taxonomy ID.
Make sure you give a string, not an int value as taxid or efetch throws an exception.
>>> get_taxonomic_name("511145", "phylum") # 511145 for Escherichia Coli
'Proteobacteria'
>>> get_taxonomic_name("511145", "species")
'Escherichia coli'
>>> get_taxonomic_name("1236", "phylum") # 1236 for Gammaproteobacteria
'Proteobacteria'
It fails when the given taxonomy ID doesn't have the specified rank.
>>> get_taxonomic_name("1236", "species") # you can't do this!
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'text'
'''
handle = Entrez.efetch(db="taxonomy", id=taxid)
root = ET.fromstring(handle.read())
return root.find(".//Taxon[Rank='{0}']/ScientificName".format(rank)).text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment