<p>You can use eutils to get this information as shown below:</p>

<pre><code>esearch -db assembly -q 'GCF_000337855.1' | esummary | xtract -pattern DocumentSummary -element AssemblyAccession,Taxid
GCF_000337855.1 469382
</code></pre>

CC BY-SA 4.0
2018-10-27T02:07:25.620Z
2018-10-27T02:07:25.620Z
vkkodali
<p>As the question mentions python, here is a python example using <a href="https://github.com/biopython/biopython" rel="nofollow noreferrer">biopython</a>.</p>
<pre class="lang-py prettyprint-override"><code>import json
from Bio import Entrez


def accession2taxid(acc: str, db=&quot;nucleotide&quot;) -&gt; str:
    handle = Entrez.esearch(db=db, term=acc)
    record = Entrez.read(handle)
    gi = record[&quot;IdList&quot;][0]
    handle = Entrez.esummary(db=db, id=gi, retmode=&quot;json&quot;)
    result = json.load(handle)[&quot;result&quot;]
    taxid = result[gi][&quot;taxid&quot;]
    return str(taxid)
</code></pre>
<p>So if you want to use an assembly accession, as in your example</p>
<pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; accession2taxid(&quot;GCA_000005845.2&quot;, db=&quot;assembly&quot;)
'511145'
</code></pre>
<p>In your example, you said that <code>GCA_000005845.2</code> should return <code>79781</code>, but that Taxon ID is for &quot;uncultured archaeon AOC672&quot;. The taxid in the example I just showed returns &quot;Escherichia coli str. K-12 substr. MG1655&quot;, which is what you want.</p>
<p>Your question also mentions GenBank accessions, so here is another example</p>
<pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; accession2taxid(&quot;CU458896.1&quot;, db=&quot;nucleotide&quot;)
'561007'
</code></pre>

CC BY-SA 4.0
2023-07-03T23:35:08.380Z
2023-07-03T23:35:08.380Z
Michael Hall
<p>Bumped into this question and tried @michael-hall 's answer, which works, but takes a very long time to execute if you only do one accession number per query, and there is a limit to how many you can do passing the numbers directly to esummary, so here is a version that takes a list of accession numbers, uploads them in bulk with epost, then fetches all the records at once, returning a dict mapping accession number to taxid. (I did occasionally run into random errors with the epost command, but adding some retry logic will fix it, if needed)</p>
<pre><code>from Bio import Entrez

def accession_to_taxid_bulk(acc: list[str], db='nuccore') -&gt; dict[str, str]:
    handle = Entrez.epost(db=db, id=','.join(refs))
    record = Entrez.read(handle)

    handle = Entrez.esummary(db=db,
                             query_key = record['QueryKey'], WebEnv=record['WebEnv'])
    result = Entrez.read(handle)

    return {i['AccessionVersion']: int(i['TaxId']) for i in result}
</code></pre>

CC BY-SA 4.0
2024-06-13T23:49:17.213Z
2024-06-13T23:49:17.213Z
Michael D. M. Dryden
<p>Turns out I'd already written some code that did it, human memory is a funny thing.</p>

<p>This uses biopython to split the field description to where the species is. May not work for all NCBI files, but seems to work on most.</p>

<pre><code>import Bio
from Bio import SeqIO
from Bio import AlignIO    

for record in SeqIO.parse (FILE, "fasta"):
    Speciesname = record.description.split('[', 1)[1].split(']', 1)[0]
</code></pre>

CC BY-SA 4.0
2018-07-02T10:44:13.333Z
2018-07-02T10:44:13.333Z
Biomage
<p>I have about 10,000 genome files all named by either refseq or genbank accession number, do you know if it's possible to convert these numbers to the corresponding NCBI taxon ID or species?</p>

<p>for example:</p>

<pre><code>GCA_000005845.2 to 79781
</code></pre>

<p>In the case of <a href="https://www.ncbi.nlm.nih.gov/assembly/GCF_000005845.2" rel="nofollow noreferrer" title="E.coli"><em>E.coli</em></a></p>

<p>Edit:</p>

<p>The file names look like this:</p>

<pre><code>GCF_000337855.1_ASM33785v1_protein.faa.gz
GCF_000091125.1_ASM9112v1_protein.faa.gz
GCF_000184535.1_ASM18453v1_protein.faa.gz
</code></pre>

<p>My operating system is Ubuntu 16.04</p>

CC BY-SA 4.0
2018-06-28T12:27:42.750Z
Hi and welcome to the site. Please [edit] your question and show us a few examples of the actual file names. If you have both refseq and genbank accessions, show examples of both. Also, please mention your operating system since some solutions might depend on it.
terdon
CC BY-SA 4.0
2018-06-28T12:35:52.153Z
Thanks for the edit. So, are your filenames always `accession`.`version`_`assembly`_`protein.faa.gz`?
terdon
CC BY-SA 4.0
2018-06-28T12:38:33.030Z
Yes, although they are all in different folders, for example: `/refseq/archaea/GCF_000337855.1/GCF_000337855.1_ASM33785v1_protein.faa.gz`
Biomage
<python><phylogenetics>
Retrieving NCBI Taxa IDs from refseq or GenBank assembly accession
# Retrieving NCBI Taxa IDs from refseq or GenBank assembly accession

(Asked by: username_0 on 2018-06-28)

I have about 10,000 genome files all named by either refseq or genbank accession number, do you know if it's possible to convert these numbers to the corresponding NCBI taxon ID or species?
for example:
GCA_000005845.2 to 79781

In the case of E.coli
Edit:
The file names look like this:
GCF_000337855.1_ASM33785v1_protein.faa.gz
GCF_000091125.1_ASM9112v1_protein.faa.gz
GCF_000184535.1_ASM18453v1_protein.faa.gz

My operating system is Ubuntu 16.04

**Tags:** python, phylogenetics
> **Comments:**
> **username_1**: Hi and welcome to the site. Please [edit] your question and show us a few examples of the actual file names. If you have both refseq and genbank accessions, show examples of both. Also, please mention your operating system since some solutions might depend on it.  
> **username_1**: Thanks for the edit. So, are your filenames always `accession`.`version`_`assembly`_`protein.faa.gz`?  
> **username_0**: Yes, although they are all in different folders, for example: `/refseq/archaea/GCF_000337855.1/GCF_000337855.1_ASM33785v1_protein.faa.gz`  

---

## 4 answers

### Answer by username_0 on 2018-07-02. Score: 2 | Accepted answer

Turns out I'd already written some code that did it, human memory is a funny thing.
This uses biopython to split the field description to where the species is. May not work for all NCBI files, but seems to work on most.
import Bio
from Bio import SeqIO
from Bio import AlignIO    

for record in SeqIO.parse (FILE, "fasta"):
    Speciesname = record.description.split('[', 1)[1].split(']', 1)[0]


---

### Answer by username_2 on 2018-10-27. Score: 2

You can use eutils to get this information as shown below:
esearch -db assembly -q 'GCF_000337855.1' | esummary | xtract -pattern DocumentSummary -element AssemblyAccession,Taxid
GCF_000337855.1 469382


---

### Answer by username_3 on 2023-07-03. Score: 2

As the question mentions python, here is a python example using biopython.
import json
from Bio import Entrez


def accession2taxid(acc: str, db="nucleotide") -> str:
    handle = Entrez.esearch(db=db, term=acc)
    record = Entrez.read(handle)
    gi = record["IdList"][0]
    handle = Entrez.esummary(db=db, id=gi, retmode="json")
    result = json.load(handle)["result"]
    taxid = result[gi]["taxid"]
    return str(taxid)

So if you want to use an assembly accession, as in your example
>>> accession2taxid("GCA_000005845.2", db="assembly")
'511145'

In your example, you said that GCA_000005845.2 should return 79781, but that Taxon ID is for "uncultured archaeon AOC672". The taxid in the example I just showed returns "Escherichia coli str. K-12 substr. MG1655", which is what you want.
Your question also mentions GenBank accessions, so here is another example
>>> accession2taxid("CU458896.1", db="nucleotide")
'561007'


---

### Answer by username_4 on 2024-06-13. Score: 1

Bumped into this question and tried @michael-hall 's answer, which works, but takes a very long time to execute if you only do one accession number per query, and there is a limit to how many you can do passing the numbers directly to esummary, so here is a version that takes a list of accession numbers, uploads them in bulk with epost, then fetches all the records at once, returning a dict mapping accession number to taxid. (I did occasionally run into random errors with the epost command, but adding some retry logic will fix it, if needed)
from Bio import Entrez

def accession_to_taxid_bulk(acc: list[str], db='nuccore') -> dict[str, str]:
    handle = Entrez.epost(db=db, id=','.join(refs))
    record = Entrez.read(handle)

    handle = Entrez.esummary(db=db,
                             query_key = record['QueryKey'], WebEnv=record['WebEnv'])
    result = Entrez.read(handle)

    return {i['AccessionVersion']: int(i['TaxId']) for i in result}


---

