How to get a gene sequence from entrez using biopython?

To retrieve a gene sequence from the NCBI Entrez database using Biopython, you can use the Entrez module from the Bio package. Below is a step-by-step guide on how to do this:

Step 1: Install Biopython

If you haven’t already installed Biopy…


This content originally appeared on DEV Community and was authored by Steven Walt Li

To retrieve a gene sequence from the NCBI Entrez database using Biopython, you can use the Entrez module from the Bio package. Below is a step-by-step guide on how to do this:

Step 1: Install Biopython

If you haven't already installed Biopython, you can do so using pip:

pip install biopython

Step 2: Import Necessary Modules

You need to import the Entrez module from Biopython.

from Bio import Entrez

Step 3: Set Your Email

NCBI requires you to provide an email address when using their Entrez services.

Entrez.email = "your_email@example.com"  # Replace with your email

Step 4: Search for the Gene

Use the Entrez.esearch function to search for the gene of interest. For example, let's search for the human insulin gene (INS).

search_term = "INS[Gene Name] AND Homo sapiens[Organism]"
handle = Entrez.esearch(db="nucleotide", term=search_term)
record = Entrez.read(handle)
handle.close()

Step 5: Retrieve the Gene Sequence

Once you have the list of IDs from the search, you can use Entrez.efetch to retrieve the sequence.

gene_id = record["IdList"][0]  # Get the first ID from the search results
handle = Entrez.efetch(db="nucleotide", id=gene_id, rettype="fasta", retmode="text")
gene_sequence = handle.read()
handle.close()

Step 6: Print or Save the Sequence

You can now print or save the gene sequence.

print(gene_sequence)

# Optionally, save to a file
with open("gene_sequence.fasta", "w") as file:
    file.write(gene_sequence)

Full Example Code

Here is the complete code:

from Bio import Entrez

# Set your email
Entrez.email = "your_email@example.com"

# Search for the gene
search_term = "INS[Gene Name] AND Homo sapiens[Organism]"
handle = Entrez.esearch(db="nucleotide", term=search_term)
record = Entrez.read(handle)
handle.close()

# Retrieve the gene sequence
gene_id = record["IdList"][0]  # Get the first ID from the search results
handle = Entrez.efetch(db="nucleotide", id=gene_id, rettype="fasta", retmode="text")
gene_sequence = handle.read()
handle.close()

# Print or save the sequence
print(gene_sequence)

# Optionally, save to a file
with open("gene_sequence.fasta", "w") as file:
    file.write(gene_sequence)

Notes:

Database (db): The db parameter in Entrez.esearch and Entrez.efetch can be set to different databases like nucleotide, protein, gene, etc., depending on what you are looking for.

Rate Limiting: Be mindful of NCBI's rate limits. If you are making many requests, consider using Entrez.sleep to pause between requests.

This code will fetch the gene sequence in FASTA format and either print it or save it to a file.


This content originally appeared on DEV Community and was authored by Steven Walt Li


Print Share Comment Cite Upload Translate Updates
APA

Steven Walt Li | Sciencx (2025-03-09T02:47:18+00:00) How to get a gene sequence from entrez using biopython?. Retrieved from https://www.scien.cx/2025/03/09/how-to-get-a-gene-sequence-from-entrez-using-biopython/

MLA
" » How to get a gene sequence from entrez using biopython?." Steven Walt Li | Sciencx - Sunday March 9, 2025, https://www.scien.cx/2025/03/09/how-to-get-a-gene-sequence-from-entrez-using-biopython/
HARVARD
Steven Walt Li | Sciencx Sunday March 9, 2025 » How to get a gene sequence from entrez using biopython?., viewed ,<https://www.scien.cx/2025/03/09/how-to-get-a-gene-sequence-from-entrez-using-biopython/>
VANCOUVER
Steven Walt Li | Sciencx - » How to get a gene sequence from entrez using biopython?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/09/how-to-get-a-gene-sequence-from-entrez-using-biopython/
CHICAGO
" » How to get a gene sequence from entrez using biopython?." Steven Walt Li | Sciencx - Accessed . https://www.scien.cx/2025/03/09/how-to-get-a-gene-sequence-from-entrez-using-biopython/
IEEE
" » How to get a gene sequence from entrez using biopython?." Steven Walt Li | Sciencx [Online]. Available: https://www.scien.cx/2025/03/09/how-to-get-a-gene-sequence-from-entrez-using-biopython/. [Accessed: ]
rf:citation
» How to get a gene sequence from entrez using biopython? | Steven Walt Li | Sciencx | https://www.scien.cx/2025/03/09/how-to-get-a-gene-sequence-from-entrez-using-biopython/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.