I have a list of scientific names of species. Is there a python module that can fetch NCBI taxonomy IDs?
3 Answers
This isn't a Python library, but it's easily invoked from Python code, easy to install, and has a lot of useful features.
I just discovered the amazing taxonkit library this week. In particular, the taxonkit name2taxid
command seems to be what you're looking for.
$ cat NAMES
Polistes dominula
Sinorhizobium meliloti
Gossypium hirsutum
$ cat NAMES | taxonkit name2taxid
Polistes dominula 743375
Sinorhizobium meliloti 382
Gossypium hirsutum 3635
Easily installed using conda install taxonkit
.
Biopython has an efetch frontend.
from Bio import Entrez
Entrez.email = "[email protected]"
handle = Entrez.efetch(db="nucleotide", id="KR052012.1", rettype="gb", retmode="text")
print(handle.read())
OUT
LOCUS KR052012 10714 bp RNA linear VRL 09-MAY-2015
DEFINITION Dengue virus strain Hb33/CHN/2014, complete genome.
ACCESSION KR052012
VERSION KR052012.1
KEYWORDS
.... etc
You will likely need to perform an EFetch search and then extract out the accession numbers for import (as done above)
If you want to perform a species search, here's the raw efetch code. I use this within an alias and the complex quotes are needed for complicated reasons. The reg-ex may need to be adapted to your search. $virus here is your species of interest, e.g. set virus='some species'
(csh or tcsh), `virus='some species' (bash). You'll just need to translate this to Biopython.
alias esummary-acc \
'esearch -db protein -query "$virus" |\
efetch -format docsum |\
xtract -pattern DocumentSummary -element Title,AccessionVersion |\
grep "$virus" |\
perl -p -e '"'"'s/^.*\[.*\]\s+(\w+)/$1/'"'"''