<p>using a http request.</p>

<p>if there is a DAS server, you can always use this protocol to download the xml -> fasta. see <a href="https://www.biostars.org/p/56/" rel="noreferrer">https://www.biostars.org/p/56/</a></p>

<pre><code>$ curl -s "http://genome.ucsc.edu/cgi-bin/das/hg19/dna?segment=chrM:100,200"  | xmllint --xpath '/DASDNA/SEQUENCE/DNA/text()' - | tr -d '\n'
ggagccggagcaccctatgtcgcagtatctgtctttgattcctgcctcattctattatttatcgcacctacgttcaatattacaggcgaacatacctacta
</code></pre>

<p>or use the UCSC utility <strong>twoBitToFa</strong> which works with remote files.</p>

<p>If you have an NCBI accession number you can use the E-utilities with seq_start and seq_end.</p>

<pre><code>$ wget -q  -O -  "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&amp;id=AE014134.1&amp;seq_start=100&amp;seq_stop=300&amp;rettype=fasta"
&gt;AE014134.1:100-300 Drosophila melanogaster chromosome 2L complete sequence
TGCCAACATATTGTGCTCTTTGATTTTTTGGCAACCCAAAATGGTGGCGGATGAACGAGATGATAATATA
TTCAAGTTGCCGCTAATCAGAAATAAATTCATTGCAACGTTAAATACAGCACAATATATGATCGCGTATG
CGAGAGTAGTGCCAACATATTGTGCTAATGAGTGCCTCTCGTTCTCTGTCTTATATTACCG
</code></pre>

CC BY-SA 3.0
2017-09-20T07:27:01.660Z
2017-09-20T07:27:01.660Z
Pierre
<p><em>I am not aware of any tools that offer this functionality out-of-the-box, although that doesn't necessarily mean it doesn't exist.</em></p>

<p>The tool you describe would have to be some kind of web service, where you submit a request with something like the following:</p>

<ul>
<li>species</li>
<li>assembly version</li>
<li>list of intervals, each with:

<ul>
<li>chromosome ID</li>
<li>start position</li>
<li>end position</li>
</ul></li>
</ul>

<p>The response would be one or more subsequences in Fasta format that you could save to a file.</p>

<p>This approach pre-supposes that there will be a relatively small number of subsequences, that those subsequences won't be too large, and you won't be requesting them very frequently. Otherwise, this approach will offer very little practical benefit over your current approach.</p>

CC BY-SA 3.0
2017-09-20T02:56:05.820Z
2017-09-20T07:03:28.920Z
2017-09-20T07:03:28.920Z
Daniel Standage
<p>If you are comfortable with Linux command line, try Entrez Direct and GNU parallel.</p>
<p>First, I recommend acquiring a <a href="https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/" rel="nofollow noreferrer">NCBI API key</a> to increase the maximum requests per second.</p>
<p>After that, if you already have the accession numbers that you want to download, just create a tab delimited file (eg arg.tsv) contains 3 columns (no header), including accession number, sequence start and sequence stop.</p>
<p>To fetch the desired sequence region and save it in separate file in parallel:</p>
<pre><code>parallel -a arg.tsv -C '\t' -j8 &quot;efetch -db nuccore -id {1} -seq_start {2} -seq_stop {3} -format fasta &gt;{1}.fa&quot;
</code></pre>
<p>Or you can use Biopython <code>from Bio import Entrez</code>. But I have not tried yet.</p>

CC BY-SA 4.0
2022-06-27T12:56:36.657Z
2022-06-27T12:56:36.657Z
Forrest Vigor
<p>So I have a list of start and stop positions along chromosomes in different species, and I'd like to get the corresponding DNA sequence for each set of coordinates. In the past, I've just download the genome as a fasta file and then use pyfaidx to extract the sequences at the given positions. But now that I'm working with several species at once, I was wondering if there's any kind of tool in Python or R that can fetch your sequences of interest without downloading a bunch of large files. Thanks</p>

CC BY-SA 3.0
2017-09-20T07:42:35.713Z
Please do not cross-post to BioStars and Stack Exchange https://www.biostars.org/p/273588/
Emily_Ensembl
CC BY-SA 3.0
2017-09-20T08:07:31.160Z
@Emily_Ensembl it would be great if you could flesh out your answer from BioStars so it could be posted as an answer here! Maybe with a simple example like Pierre has done below?
terdon
<r><python><genome><phylogenetics><sequence-analysis>
Way to get genomic sequences at given coordinates without downloading fasta files of whole chromosomes/genomes first?
# Way to get genomic sequences at given coordinates without downloading fasta files of whole chromosomes/genomes first?

(Asked by: username_0 on 2017-09-20)

So I have a list of start and stop positions along chromosomes in different species, and I'd like to get the corresponding DNA sequence for each set of coordinates. In the past, I've just download the genome as a fasta file and then use pyfaidx to extract the sequences at the given positions. But now that I'm working with several species at once, I was wondering if there's any kind of tool in Python or R that can fetch your sequences of interest without downloading a bunch of large files. Thanks

**Tags:** r, python, genome, phylogenetics, sequence-analysis
> **Comments:**
> **username_1**: Please do not cross-post to BioStars and Stack Exchange https://www.biostars.org/p/273588/  
> **username_2**: @Emily_Ensembl it would be great if you could flesh out your answer from BioStars so it could be posted as an answer here! Maybe with a simple example like Pierre has done below?  

---

## 3 answers

### Answer by username_3 on 2017-09-20. Score: 8 | Accepted answer

using a http request.
if there is a DAS server, you can always use this protocol to download the xml -> fasta. see https://www.biostars.org/p/56/
$ curl -s "http://genome.ucsc.edu/cgi-bin/das/hg19/dna?segment=chrM:100,200"  | xmllint --xpath '/DASDNA/SEQUENCE/DNA/text()' - | tr -d '\n'
ggagccggagcaccctatgtcgcagtatctgtctttgattcctgcctcattctattatttatcgcacctacgttcaatattacaggcgaacatacctacta

or use the UCSC utility twoBitToFa which works with remote files.
If you have an NCBI accession number you can use the E-utilities with seq_start and seq_end.
$ wget -q  -O -  "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=AE014134.1&seq_start=100&seq_stop=300&rettype=fasta"
>AE014134.1:100-300 Drosophila melanogaster chromosome 2L complete sequence
TGCCAACATATTGTGCTCTTTGATTTTTTGGCAACCCAAAATGGTGGCGGATGAACGAGATGATAATATA
TTCAAGTTGCCGCTAATCAGAAATAAATTCATTGCAACGTTAAATACAGCACAATATATGATCGCGTATG
CGAGAGTAGTGCCAACATATTGTGCTAATGAGTGCCTCTCGTTCTCTGTCTTATATTACCG


---

### Answer by username_4 on 2017-09-20. Score: 1

*I am not aware of any tools that offer this functionality out-of-the-box, although that doesn't necessarily mean it doesn't exist.*

The tool you describe would have to be some kind of web service, where you submit a request with something like the following:

* species
* assembly version
* list of intervals, each with:
  + chromosome ID
  + start position
  + end position

The response would be one or more subsequences in Fasta format that you could save to a file.

This approach pre-supposes that there will be a relatively small number of subsequences, that those subsequences won't be too large, and you won't be requesting them very frequently. Otherwise, this approach will offer very little practical benefit over your current approach.


---

### Answer by username_5 on 2022-06-27. Score: 0

If you are comfortable with Linux command line, try Entrez Direct and GNU parallel.
First, I recommend acquiring a NCBI API key to increase the maximum requests per second.
After that, if you already have the accession numbers that you want to download, just create a tab delimited file (eg arg.tsv) contains 3 columns (no header), including accession number, sequence start and sequence stop.
To fetch the desired sequence region and save it in separate file in parallel:
parallel -a arg.tsv -C '\t' -j8 "efetch -db nuccore -id {1} -seq_start {2} -seq_stop {3} -format fasta >{1}.fa"

Or you can use Biopython from Bio import Entrez. But I have not tried yet.


---

