<p><strong>Samtools</strong></p>
<p>Samtools is a toolkit that includes a binary called <code>samtoools</code>, which can be used to query a FASTA file — to extract a subsequence based on some coordinate range you specify.</p>
<p>If you do not have <code>samtools</code> installed, you need to add it to your computer. For OS X, you could use <a href="https://brew.sh/" rel="noreferrer">Homebrew</a>, for example:</p>
<pre><code>$ brew install samtools
</code></pre>
<p>For Ubuntu, you could use <code>apt-get</code>:</p>
<pre><code>$ sudo apt-get install samtools
</code></pre>
<p><strong>Reference assembly</strong></p>
<p>The next step is to download a reference assembly for your genome and genomic build-of-interest.</p>
<p>There is a <a href="https://www.xenbase.org/entry/" rel="noreferrer">research group</a> dedicated to <a href="https://www.xenbase.org/other/static-xenbase/ftpDatafiles.jsp" rel="noreferrer">making <em>Xenopus</em> datasets available</a>. If you were interested in v10 of the <code>Xentr</code> genome, for instance, you could do the following or similar to download it:</p>
<pre><code>$ curl --output &quot;XENTR_10.0_genome.fasta.gz&quot; &quot;http://ftp.xenbase.org/pub/Genomics/JGI/Xentr10.0/XENTR_10.0_genome.fasta.gz&quot;
</code></pre>
<p>You could also dig through the NCBI site to get a similar link. It's the same idea — just change the web address, accordingly.</p>
<p><strong>Indexing</strong></p>
<p>Indexing makes it possible to extract an arbitrary subsequence of interest from this FASTA file. You need to index, but you only need to do it once:</p>
<pre><code>$ samtools faidx XENTR_10.0_genome.fasta.gz
</code></pre>
<p>This creates two new files: <code>XENTR_10.0_genome.fasta.gz.fai</code> and <code>XENTR_10.0_genome.fasta.gz.gzi</code>.</p>
<p><em>Note</em>: You need to keep these two files in the same directory as the original, compressed FASTA file <code>XENTR_10.0_genome.fasta.gz</code>.</p>
<p><strong>Querying</strong></p>
<p>Now you are ready to query, or extract a subsequence of interest. If you wanted the first hundred bases of the first chromosome, for instance:</p>
<pre><code>$ samtools faidx XENTR_10.0_genome.fasta.gz Chr1:1-100
&gt;Chr1:1-100
cctcagcgtcttcttctttctgctcccactgcattagcatagggagagagggccacagag
gctaggtagcattgccggctagcaatcttcagcgtcctct
</code></pre>
<p>The search term used here is <code>Chr1:1-100</code>. Replace <code>Chr1</code> with the chromosome of interest and <code>1</code> and <code>100</code> with the start and stop coordinates, respectively, of interest. Coordinates used with <code>samtools</code> queries use a <a href="https://www.biostars.org/p/84686/" rel="noreferrer">one-based indexing scheme</a>.</p>
<p><em>Note</em>: This uses the chromosome naming scheme in the <code>XENTR_10.0_genome.fasta.gz</code> file (<code>Chr1</code>, etc.). Other research groups (NCBI) might use different chromosome names. If you get your FASTA from somewhere else, investigate the FASTA file to determine the naming scheme being used.</p>
<p>If you want to redirect output to a file, use the output redirect operator (<code>&gt;</code>):</p>
<pre><code>$ samtools faidx XENTR_10.0_genome.fasta.gz Chr1:1-100 &gt; mySubsequence.fa
</code></pre>

CC BY-SA 4.0
2020-07-27T20:08:33.387Z
2020-07-27T20:08:33.387Z
Alex Reynolds
<p>I'd download the fasta, and use samtools, or maybe bedtools.</p>

CC BY-SA 4.0
2020-07-26T17:27:28.560Z
2020-07-26T17:27:28.560Z
swbarnes2
<p>I am new to working with the NCBI database so I am not sure how trivial this question is. I wanted to get DNA sequence of a specific version of the Xenopus tropicalis genome of a specific region (ie 1-100bp) without doing it manually each time (going on genome browser). I know there is a faster way and efficient way to do this through the command line but I found the NCBI tutorial hard to follow.</p>

CC BY-SA 4.0
2020-07-26T18:25:56.717Z
Hi @fp1234 perhaps you could post the code you have tried to date? I presume you are using the `efetch` system? There are other alternatives.
M__
CC BY-SA 4.0
2020-07-26T21:46:35.190Z
Hi! Yes I am using `efetch`. So as a test I was trying to run ` efetch -db genome -id 5323661` but I keep getting a 400 bad request error. The id is the UID for this genome version Xenopus tropicalis https://www.ncbi.nlm.nih.gov/assembly/GCF_000004195.4/. But would I hope to do is to be able to get "x - y bp" from z chromosome using efetch.
fp1234
<phylogenetics>
How to retrieve the DNA sequence from a particular species and region through command line?
# How to retrieve the DNA sequence from a particular species and region through command line?

(Asked by: username_0 on 2020-07-26)

I am new to working with the NCBI database so I am not sure how trivial this question is. I wanted to get DNA sequence of a specific version of the Xenopus tropicalis genome of a specific region (ie 1-100bp) without doing it manually each time (going on genome browser). I know there is a faster way and efficient way to do this through the command line but I found the NCBI tutorial hard to follow.

**Tags:** phylogenetics
> **Comments:**
> **username_1**: Hi @fp1234 perhaps you could post the code you have tried to date? I presume you are using the `efetch` system? There are other alternatives.  
> **username_0**: Hi! Yes I am using `efetch`. So as a test I was trying to run ` efetch -db genome -id 5323661` but I keep getting a 400 bad request error. The id is the UID for this genome version Xenopus tropicalis https://www.ncbi.nlm.nih.gov/assembly/GCF_000004195.4/. But would I hope to do is to be able to get "x - y bp" from z chromosome using efetch.  

---

## 2 answers

### Answer by username_2 on 2020-07-27. Score: 5 | Accepted answer

Samtools
Samtools is a toolkit that includes a binary called samtoools, which can be used to query a FASTA file — to extract a subsequence based on some coordinate range you specify.
If you do not have samtools installed, you need to add it to your computer. For OS X, you could use Homebrew, for example:
$ brew install samtools

For Ubuntu, you could use apt-get:
$ sudo apt-get install samtools

Reference assembly
The next step is to download a reference assembly for your genome and genomic build-of-interest.
There is a research group dedicated to making Xenopus datasets available. If you were interested in v10 of the Xentr genome, for instance, you could do the following or similar to download it:
$ curl --output "XENTR_10.0_genome.fasta.gz" "http://ftp.xenbase.org/pub/Genomics/JGI/Xentr10.0/XENTR_10.0_genome.fasta.gz"

You could also dig through the NCBI site to get a similar link. It's the same idea — just change the web address, accordingly.
Indexing
Indexing makes it possible to extract an arbitrary subsequence of interest from this FASTA file. You need to index, but you only need to do it once:
$ samtools faidx XENTR_10.0_genome.fasta.gz

This creates two new files: XENTR_10.0_genome.fasta.gz.fai and XENTR_10.0_genome.fasta.gz.gzi.
Note: You need to keep these two files in the same directory as the original, compressed FASTA file XENTR_10.0_genome.fasta.gz.
Querying
Now you are ready to query, or extract a subsequence of interest. If you wanted the first hundred bases of the first chromosome, for instance:
$ samtools faidx XENTR_10.0_genome.fasta.gz Chr1:1-100
>Chr1:1-100
cctcagcgtcttcttctttctgctcccactgcattagcatagggagagagggccacagag
gctaggtagcattgccggctagcaatcttcagcgtcctct

The search term used here is Chr1:1-100. Replace Chr1 with the chromosome of interest and 1 and 100 with the start and stop coordinates, respectively, of interest. Coordinates used with samtools queries use a one-based indexing scheme.
Note: This uses the chromosome naming scheme in the XENTR_10.0_genome.fasta.gz file (Chr1, etc.). Other research groups (NCBI) might use different chromosome names. If you get your FASTA from somewhere else, investigate the FASTA file to determine the naming scheme being used.
If you want to redirect output to a file, use the output redirect operator (>):
$ samtools faidx XENTR_10.0_genome.fasta.gz Chr1:1-100 > mySubsequence.fa


---

### Answer by username_3 on 2020-07-26. Score: -2

I'd download the fasta, and use samtools, or maybe bedtools.


---

