It works.
Hi everyone,
I am trying to batch download FASTA sequences for Nipah virus using Biopython's Entrez module. However, the resulting output file nipah_seq.fasta is created empty.
Here is the script I am currently using:
from Bio import Entrez
from Bio import SeqIO
import os
Entrez.email="xyz@gmail.com"
filename="nipah_seq.fasta"
if not os.path.isfile(filename):
record=Entrez.read(Entrez.esearch(db="nuccore",term="Henipavirus nipahense[Organism] AND India[country]",retmax=10))
seq_data=Entrez.efetch(db="nuccore",term="Henipavirus nipahense[Organism] AND India[country]",id=record['IdList'],rettype="fasta",retmode="text")
sequences=seq_data.read()
seq_data.close()
with open(filename, 'w') as output:
output.write(sequences)
Issue Details:
Entrez.esearch seems to run, but Entrez.efetch is not writing any data into the FASTA file.
- Noticeably, Entrez.efetch does not take a term argument, so passing term alongside id might be causing issues or returning an empty handle.
How should I modify Entrez.efetch to properly retrieve and write the FASTA records for the matching IDs?
Any suggestions or corrections would be greatly appreciated!
2 answers
The code works, and it downloads 10 sequences. Make sure you are not hitting the rate limits or being blocked.
Hi,
It's not an answer to your question, but a different way to retrieve the same data. For viruses, you can use the NCBI Datasets command-line tool to retrieve sequences and metadata from the nucleotide database. Generally speaking, anything you can find on NCBI Virus, you can download using the Datasets CLI tool.
Here's the command:
datasets download virus genome taxon "Henipavirus nipahense" --geo-location India --filename Henipavirus.zip
unzip Henipavirus.zip -d henipavirus
This command will download a zip file that contains a data report in JSONL format with metadata information and a file with all FASTA sequences (genomic.fna). Here's the folder structure:
henipavirus/
|-- README.md
|-- md5sum.txt
`-- ncbi_dataset
`-- data
|-- data_report.jsonl
|-- dataset_catalog.json
`-- genomic.fna
Let me know if you have any questions or need any assistance with this.
Thanks!
Thank you for your helpful response. I will be sure to reach out to you when I need assistance with computational biology.
Log in to answer this question.