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.
Top comments (0)