<p>Did you try to fetch from the database <code>nucleotide</code> instead of <code>gene</code> ?</p>
<pre><code># load modules
from Bio import SeqIO
from Bio import Entrez

# Lookup ID
search = Entrez.esearch(db='nucleotide', term='Tobacco mosaic virus[Orgn] AND replicase')
read = Entrez.read(search)
idlist = read[&quot;IdList&quot;]

# Fetch sequence
search = Entrez.efetch(db='nucleotide', id=idlist[0], retmode='text', rettype='gb')
read = SeqIO.read(search, &quot;genbank&quot;)
tmv_replicase = read.seq
</code></pre>
<p>It returns</p>
<pre><code>&gt;&gt;&gt; read
SeqRecord(seq=Seq('GTATTTTTACAACAATTACCAACAACAACAAACAACAAACAACATTACAATTAC...GGG'), id='MN912489.1', name='MN912489', description='Tobacco mosaic virus isolate TMV:BR:SC:02:01, complete genome', dbxrefs=[])
&gt;&gt;&gt; tmv_replicase
Seq('GTATTTTTACAACAATTACCAACAACAACAAACAACAAACAACATTACAATTAC...GGG')

</code></pre>

CC BY-SA 4.0
2021-06-03T09:34:50.510Z
Thank you, this gets me what I wanted. Out of interest though, do you know why using the gene database for this wouldn't work? (I'm new to Entrez).
blammo69
CC BY-SA 4.0
2021-06-04T08:03:34.937Z
It is simply 2 differents databases. The ID into `nucleotide` is not the same into `gene`. Thus from the website NCBI it is possible to extract a fasta from a gene query it doesn't seem possible with biopython https://biopython.readthedocs.io/en/latest/chapter_entrez.html
Pierre-Edouard Guerin
CC BY-SA 4.0
2021-06-04T08:05:37.367Z
If you want to filter your queries from the `nucleotide` database in order to get only the specific genes you can check my code as an example: https://gitlab.mbb.univ-montp2.fr/bev/target_genes_from_ncbi/-/blob/master/select_query_gene_sequences_ncbi.py
Pierre-Edouard Guerin
CC BY-SA 4.0
2021-06-02T09:23:23.740Z
2021-06-02T09:23:23.740Z
Pierre-Edouard Guerin
<p>I'm trying to use Entrez (through Biopython) to download the sequence of a TMV replicase gene.</p>
<p>I have the following code:</p>
<pre class="lang-py prettyprint-override"><code># Lookup ID
search = Entrez.esearch(db='gene', term='Tobacco mosaic virus[Orgn] AND replicase')
read = Entrez.read(search)
idlist = read[&quot;IdList&quot;]

# Fetch sequence
search = Entrez.efetch(db='gene', id=idlist[0], retmode='text', rettype='gb')
read = SeqIO.read(search, &quot;genbank&quot;)
tmv_replicase = read.seq
</code></pre>
<p>But when I run it it throws the exception on the <code>SeqIO.read</code> line: <code>ValueError: No records found in handle</code>.</p>
<p>The IDs provided in idlist are: <code>['1494081', '1494082']</code>.</p>

<python><covid-19><genbank><entrez><virus>
Entrez (Biopython) esearch and efetch not returning sequence as expected
# Entrez (Biopython) esearch and efetch not returning sequence as expected

(Asked by: username_0 on 2021-05-30)

I'm trying to use Entrez (through Biopython) to download the sequence of a TMV replicase gene.
I have the following code:
# Lookup ID
search = Entrez.esearch(db='gene', term='Tobacco mosaic virus[Orgn] AND replicase')
read = Entrez.read(search)
idlist = read["IdList"]

# Fetch sequence
search = Entrez.efetch(db='gene', id=idlist[0], retmode='text', rettype='gb')
read = SeqIO.read(search, "genbank")
tmv_replicase = read.seq

But when I run it it throws the exception on the SeqIO.read line: ValueError: No records found in handle.
The IDs provided in idlist are: ['1494081', '1494082'].

**Tags:** python, covid-19, genbank, entrez, virus

---

## 1 answers

### Answer by username_1 on 2021-06-02. Score: 2 | Accepted answer

Did you try to fetch from the database nucleotide instead of gene ?
# load modules
from Bio import SeqIO
from Bio import Entrez

# Lookup ID
search = Entrez.esearch(db='nucleotide', term='Tobacco mosaic virus[Orgn] AND replicase')
read = Entrez.read(search)
idlist = read["IdList"]

# Fetch sequence
search = Entrez.efetch(db='nucleotide', id=idlist[0], retmode='text', rettype='gb')
read = SeqIO.read(search, "genbank")
tmv_replicase = read.seq

It returns
>>> read
SeqRecord(seq=Seq('GTATTTTTACAACAATTACCAACAACAACAAACAACAAACAACATTACAATTAC...GGG'), id='MN912489.1', name='MN912489', description='Tobacco mosaic virus isolate TMV:BR:SC:02:01, complete genome', dbxrefs=[])
>>> tmv_replicase
Seq('GTATTTTTACAACAATTACCAACAACAACAAACAACAAACAACATTACAATTAC...GGG')

> **Comments:**
> **username_0**: Thank you, this gets me what I wanted. Out of interest though, do you know why using the gene database for this wouldn't work? (I'm new to Entrez).  
> **username_1**: It is simply 2 differents databases. The ID into `nucleotide` is not the same into `gene`. Thus from the website NCBI it is possible to extract a fasta from a gene query it doesn't seem possible with biopython https://biopython.readthedocs.io/en/latest/chapter_entrez.html  
> **username_1**: If you want to filter your queries from the `nucleotide` database in order to get only the specific genes you can check my code as an example: https://gitlab.mbb.univ-montp2.fr/bev/target_genes_from_ncbi/-/blob/master/select_query_gene_sequences_ncbi.py  

---

