This is a test version of Biostars. For the public version, visit https://www.biostars.org.
RefSeq ID to TaxID?

I have a number of RefSeq IDs (like this here NZ_LFWC01000004.1) and I would like to get the tax IDs for the species.

Is there a way to automatize that in Bash, Python or R?

genome

If you have a very long list of RefSeq IDs, you might want to do a local search vs "accession2taxid" files, see : Biostars

Wonderful, that is exactly what I was looking for. Thank you so much.

2 answers

Using NCBI Unix eutilities:

esearch -db nucleotide -query "NZ_LFWC01000004.1"|esummary|xtract -pattern TaxId -element TaxId

Any idea about this error?

$  esearch -db nucleotide -query "NZ_LFWC01000004.1"|esummary|xtract -element TaxId

ERROR: No -pattern in command-line arguments

xtract seems to cause the error.

Try this:

esearch -db nucleotide -query "NZ_LFWC01000004.1"|esummary|xtract -pattern TaxId -element TaxId

ah, wonderful, thank you very much. :-)

In python :

from Bio import Entrez
from Bio import SeqIO
key_list=['NZ_LFWC01000004.1'] ###Add all your IDs

for key in key_list:
    Entrez.email = "myemailaddress"
    handle = Entrez.efetch(db='nucleotide', id=key, rettype='gb')
    record = SeqIO.read(handle,'genbank')
    if record.features[0].qualifiers['db_xref'][0].split(":")[0] == 'taxon':
        print(record.features[0].qualifiers['db_xref'])[0].split(":")[1]

I guess one has to be careful with the db_xref tag, it can often contain identifiers linking to other databases such as UniProtKB.

Yes right, I added a condition to keep 'taxon' id from NCBI's taxonomic identifier only

Log in to answer this question.