This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to download from ncbi not a whole FASTA file, but only a certain interval?

Hi! I have a list with accession numbers from ncbi and intervals i need to download. Is there a way to download not a whole FASTA file, but only a certain interval with python/biopython or R (or using some other soft)? An example list is provided below.

id            acc_no         start   stop
10000002717 NZ_GG774949.1   1662245 1662896
10000003767 NZ_GG774949.1   1678553 1679990
10000003783 NZ_GG774796.1   257545  258028
fasta genbank biopython r

I mean FASTA sequences that can be downloaded from ncbi. From software I will try to deal with anyone, but it will be more convenient with python/biopython or R

4 answers

Use ncbi efetch

$ wget -O - -q "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&id=NZ_GG774949.1&retmode=text&rettype=fasta&seq_start=1662245&seq_stop=1662896"

>NZ_GG774949.1:1662245-1662896 Bacteroides sp. 3_1_23 supercont1.1, whole genome shotgun sequence
ACAAGTACTTGTTTTATAGTTACCCGTCCTAAATTACGGGAAGTGTTTGCTGCAAATTTCCGTGACCGTA
TTGTACAACATTGGTTGTGCCTACGCTTAGAGCCACTGTTTGAGGCACGTTTTGTTGAACACGGAAATGT
ATCATTTAACTGTCGAAAGGGTTTTGGAACATTTGCATGTATTGATCAGTTGACAAAAAATACAATTGAA
GTCTCTGATAATTATTCGCACGAGGCTTGGTATGCTCAATTTGATATTAAAGGATTTTTTATGTCAATTG
ATTGCGAACGATTATTAGAACACTTATTACCATTTATCAAAGAAAAATGGAATTATTGGAAAGGGACCAT
ATATGAACAAGATTTAGATTTAGTGCTATGGCTTACAGAAATAATTGTACGACATCGACCACAAGATGAT
TGTATACGTCAAGGAAATTTAAAATTATGGAGAATACTGCCTAAAAACAAAAGCCTGTTTTACAATGAAT
GGATGAAAGGCGAACCAATAGGAAACCTAACTAGTCAATTATTTGCCAATTTTTACATGTCATTTTTTGA
TGAATGGGCTATTAAAGCAGCAGAAGAAAGAGGAGCCAAATATGTACGTTTTGTAGATGATTTTAGCTTT
GTGTGCAAAACTAAGGAAGATG

Using Entrezutils on command line:

$ efetch -db nuccore -id "NZ_GG774949.1" -seq_start 1662245 -seq_stop 1662896 -format fasta

EDirect works well for a relatively small number of sequences. If the total number of accessions (column 2 of your table) you are dealing with is small, you may want to consider first downloading the entire fasta for those accessions and then using something like bedtools or seqkit to extract specific ranges from the complete fasta sequences. This approach will be quicker compared to fetching sequences for each range directly from NCBI servers.

Since this is an important distinction I have moved this to a separate answer.

efetch specific location of an fasta ID in ncbi with Biopython

import Bio
from Bio import Entrez
Entrez.email = 'yourmail@gmail.com'
record = Entrez.efetch(db = 'nucleotide', id = 'NC_051849.1', rettype = 'fasta', retmode = 'text', seq_start = 33845728, seq_stop = 33848021)
print(len(record.read()))

Log in to answer this question.