I have a list of scientific names of species. Is there a python module that can fetch NCBI taxonomy IDs?
Biopython has an efetch frontend.
from Bio import Entrez Entrez.email = "firstname.lastname@example.org" handle = Entrez.efetch(db="nucleotide", id="KR052012.1", rettype="gb", retmode="text") print(handle.read())
LOCUS KR052012 10714 bp RNA linear VRL 09-MAY-2015
DEFINITION Dengue virus strain Hb33/CHN/2014, complete genome.
You will likely need to perform an EFetch search and then extract out the accession numbers for import (as done above)
This isn't a Python library, but it's easily invoked from Python code, easy to install, and has a lot of useful features.
$ 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.
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/'"'"''