<p>Here is a Biopython solution via <code>Entrez</code> utilies, simply copy the code into a python script and execute it:</p>

<pre><code>from Bio import Entrez, SeqIO

Entrez.email = 'me@email.com'

def get_fasta_from_ids(ids_list):
    handle = Entrez.efetch(db='nucleotide', id=','.join(ids_list), rettype='fasta')
    for record in SeqIO.parse(handle, 'fasta'):
        yield record

SeqIO.write(get_fasta_from_ids(['NC_001416.1']), 'NC_001416.1.fa', 'fasta')
</code></pre>

<p>You can add more ids to the input list to retrieve multiple records at once.</p>

CC BY-SA 4.0
2018-09-21T08:39:41.623Z
Thanks for you code snippet, but this is also a solution that needs extra software to be installed (BioPython and this snippet itself), while I'm looking for something that can be run out of the box in any system with no extra software if possible.
Poshi
CC BY-SA 4.0
2018-09-21T08:55:38.237Z
Is the `Entrez.email` actually doing anything? Does the `Entrez` package need it to be set in order to run?
terdon
CC BY-SA 4.0
2018-09-21T08:59:28.193Z
@terdon Yes `Entrez.email` is required (I do use my actual email address although I've never received an email about this). `Entrez.api_key` can be also be added to speed up queries. BTW, I agree that using the E-utilities (as in your solution and mine) is the most flexible approach, whether you do it via python or something else is mostly a matter of taste
Chris_Rands
CC BY-SA 4.0
2018-09-19T19:56:34.670Z
2018-09-19T19:56:34.670Z
Chris_Rands
<p>You can either do this directly as you show in your answer or, for a more sophisticated and flexible approach, use NCBI's <a href="https://www.ncbi.nlm.nih.gov/books/NBK179288/" rel="nofollow noreferrer"><code>edirect</code> tool</a>:</p>

<pre><code>esearch -db nucleotide -query 'NC_001416.1' | efetch -format fasta &gt; NC_001416.1.fa
</code></pre>

CC BY-SA 4.0
2018-09-21T08:37:55.030Z
That's a quite elegant solution, but it involves installing new software, and I'm trying to generate my scripts with as less extra software as possible. Althought this is a more robust solution than having a link that can change over time.
Poshi
CC BY-SA 4.0
2018-09-21T08:56:49.087Z
@Poshi fair enough, I'm glad you found a solution. You might want to accept your answer to indicate that it is the one you preferred.
terdon
CC BY-SA 4.0
2018-09-19T14:33:34.333Z
2018-09-19T14:33:34.333Z
terdon
<p>You can add the <code>--origfmt</code> option, and see what comes out. Either you will have what you want, or perhaps they uploaded the sequences that way.</p>

<p>See: <a href="https://ncbi.github.io/sra-tools/fastq-dump.html" rel="nofollow noreferrer">https://ncbi.github.io/sra-tools/fastq-dump.html</a></p>

CC BY-SA 4.0
2018-09-19T13:11:09.410Z
With that parameter I have the exact same output :-( I think I already tried everything that made sense in my head.
Poshi
CC BY-SA 4.0
2018-09-19T13:31:12.973Z
Did you also try to set the `--fasta` to 0, so that there is no line wrapping?
schmat_90
CC BY-SA 4.0
2018-09-19T13:36:45.300Z
Yes, same effect, oly difference is that instead of a line of ID and several lines of sequence, I have on line of ID and on line of sequence. But still 10 sequences.
Poshi
CC BY-SA 4.0
2018-09-19T13:07:38.733Z
2018-09-19T13:07:38.733Z
schmat_90
<p>Finally, I found an alternative to the SRA translation: a link that works! For those of you interested in knowing how to download FastA files from NCBI using an accession number, try the following link:</p>

<pre><code>https://www.ncbi.nlm.nih.gov/search/api/sequence/${accession}/?report=fasta
</code></pre>

<p>Using <code>wget</code> to download the accession used as example:</p>

<pre><code>wget -O NC_001416.1.fasta "https://www.ncbi.nlm.nih.gov/search/api/sequence/NC_001416.1/?report=fasta"
</code></pre>

CC BY-SA 4.0
2018-12-10T20:38:00.310Z
This works, but is it documented anywhere, and how to get a gzip?
Ciro Santilli OurBigBook.com
CC BY-SA 4.0
2018-12-11T08:08:17.057Z
I didn't found that in the documentation, but playing with the web. If you need a gzip, my advise is to download first and gzip later (or do it at the same time with pipes, but the gzip is on your side either way).
Poshi
CC BY-SA 4.0
2018-12-11T08:13:54.317Z
OK. gzipping locally is problematic because the high cost operation is the network transfer.
Ciro Santilli OurBigBook.com
CC BY-SA 4.0
2018-12-11T08:15:48.947Z
I'm sorry, I didn't found any other option. Try to find information about the `report` parameter. Maybe there is something like `gzfasta`. But I cannot point you to any documentation :-?
Poshi
CC BY-SA 4.0
2018-09-19T13:45:32.473Z
2018-09-19T13:45:32.473Z
Poshi
<p>I'm trying to get the FastA files for some accessions (like NC_001416.1). I did not managed to find an FTP server or direct link to these files (I want to get it from command line with <code>wget</code>, not from a web browser). But I found an "equivalent" file in <a href="ftp://ftp.ncbi.nlm.nih.gov/sra/refseq/NC_001416.1" rel="noreferrer">ftp://ftp.ncbi.nlm.nih.gov/sra/refseq/NC_001416.1</a></p>

<p>This file is an SRA file that should be processed with the SRA-toolkit. In particular, fastq-dump seems to be the tools of choice.</p>

<p>I ran</p>

<pre><code>fastq-dump --fasta NC_001416.1
</code></pre>

<p>and I got a <code>NC_001416.1.fasta</code> file. But the file contains the sequence I'm looking for in chunks of 5000 bases. The IDs in the output file are:</p>

<pre><code>&gt;NC_001416.1.1 length=5000
&gt;NC_001416.1.2 length=5000
&gt;NC_001416.1.3 length=5000
&gt;NC_001416.1.4 length=5000
&gt;NC_001416.1.5 length=5000
&gt;NC_001416.1.6 length=5000
&gt;NC_001416.1.7 length=5000
&gt;NC_001416.1.8 length=5000
&gt;NC_001416.1.9 length=5000
&gt;NC_001416.1.10 length=3502
</code></pre>

<p>I need that sequence under a single ID:</p>

<pre><code>&gt;NC_001416.1 length=48502
</code></pre>

<p>I cannot find the proper parameters to supply to <code>fastq-dump</code> to achieve the desired result.</p>

<p>Does anyone know how could I get the FastA file I'm expecting? How to process properly the SRA file or where I could download the FastA file directly.</p>

CC BY-SA 4.0
2018-09-19T14:34:30.060Z
Out of curiosity, why do you write "FastA"? I've never seen it written that way before, only "FASTA" or "fasta". Why the final capitalized A?
terdon
CC BY-SA 4.0
2018-09-20T09:34:50.353Z
No reason, really. It's a personal way of life XD FASTA comes from Fast-All, because it originally worked with all alphabets. There was also FAST-P for proteins and FAST-N for nucleotides. So FASTA is a shorthand for Fast-All. And I just used CamelCase to name it. FastA. Same for FastQ. First full word capitalized, second word reduced to a single letter and capitalized too.
Poshi
CC BY-SA 4.0
2018-09-21T09:19:00.480Z
@Poshi as much as I follow your logic, I find really bad not to use the "standard" `.fasta`. using your own capitalization generates confusion.
Kamil S Jaron
CC BY-SA 4.0
2018-09-21T09:43:00.813Z
In file names I use the standard `.fasta`, `.fa` or `.fas`. In natural language, I use what it feels better for me. I'm sorry if it confuses you. To me, this capitalization is clearer. If we have to go purists, the proper way to refer the format is `FASTA`, all capitals (check https://en.wikipedia.org/wiki/FASTA_format or https://zhanglab.ccmb.med.umich.edu/FASTA/). But that notation hurt my eyes. It's like it's shouting to me.
Poshi
CC BY-SA 4.0
2018-09-21T22:18:37.690Z
typing it as FastA is kind of fun. It is a nice reminder that the file is pronounced like "fast-eh?" instead of "fast-uh".
conchoecia
<fasta><format-conversion><data-download><sratoolkit>
Convert SRA to FastA
# Convert SRA to FastA

(Asked by: username_0 on 2018-09-19)

I'm trying to get the FastA files for some accessions (like NC_001416.1). I did not managed to find an FTP server or direct link to these files (I want to get it from command line with wget, not from a web browser). But I found an "equivalent" file in ftp://ftp.ncbi.nlm.nih.gov/sra/refseq/NC_001416.1
This file is an SRA file that should be processed with the SRA-toolkit. In particular, fastq-dump seems to be the tools of choice.
I ran
fastq-dump --fasta NC_001416.1

and I got a NC_001416.1.fasta file. But the file contains the sequence I'm looking for in chunks of 5000 bases. The IDs in the output file are:
>NC_001416.1.1 length=5000
>NC_001416.1.2 length=5000
>NC_001416.1.3 length=5000
>NC_001416.1.4 length=5000
>NC_001416.1.5 length=5000
>NC_001416.1.6 length=5000
>NC_001416.1.7 length=5000
>NC_001416.1.8 length=5000
>NC_001416.1.9 length=5000
>NC_001416.1.10 length=3502

I need that sequence under a single ID:
>NC_001416.1 length=48502

I cannot find the proper parameters to supply to fastq-dump to achieve the desired result.
Does anyone know how could I get the FastA file I'm expecting? How to process properly the SRA file or where I could download the FastA file directly.

**Tags:** fasta, format-conversion, data-download, sratoolkit
> **Comments:**
> **username_1**: Out of curiosity, why do you write "FastA"? I've never seen it written that way before, only "FASTA" or "fasta". Why the final capitalized A?  
> **username_0**: No reason, really. It's a personal way of life XD FASTA comes from Fast-All, because it originally worked with all alphabets. There was also FAST-P for proteins and FAST-N for nucleotides. So FASTA is a shorthand for Fast-All. And I just used CamelCase to name it. FastA. Same for FastQ. First full word capitalized, second word reduced to a single letter and capitalized too.  
> **username_2**: @Poshi as much as I follow your logic, I find really bad not to use the "standard" `.fasta`. using your own capitalization generates confusion.  
> **username_0**: In file names I use the standard `.fasta`, `.fa` or `.fas`. In natural language, I use what it feels better for me. I'm sorry if it confuses you. To me, this capitalization is clearer. If we have to go purists, the proper way to refer the format is `FASTA`, all capitals (check https://en.wikipedia.org/wiki/FASTA_format or https://zhanglab.ccmb.med.umich.edu/FASTA/). But that notation hurt my eyes. It's like it's shouting to me.  
> **username_3**: typing it as FastA is kind of fun. It is a nice reminder that the file is pronounced like "fast-eh?" instead of "fast-uh".  

---

## 4 answers

### Answer by username_0 on 2018-09-19. Score: 5 | Accepted answer

Finally, I found an alternative to the SRA translation: a link that works! For those of you interested in knowing how to download FastA files from NCBI using an accession number, try the following link:
https://www.ncbi.nlm.nih.gov/search/api/sequence/${accession}/?report=fasta

Using wget to download the accession used as example:
wget -O NC_001416.1.fasta "https://www.ncbi.nlm.nih.gov/search/api/sequence/NC_001416.1/?report=fasta"

> **Comments:**
> **username_6**: This works, but is it documented anywhere, and how to get a gzip?  
> **username_0**: I didn't found that in the documentation, but playing with the web. If you need a gzip, my advise is to download first and gzip later (or do it at the same time with pipes, but the gzip is on your side either way).  
> **username_6**: OK. gzipping locally is problematic because the high cost operation is the network transfer.  
> **username_0**: I'm sorry, I didn't found any other option. Try to find information about the `report` parameter. Maybe there is something like `gzfasta`. But I cannot point you to any documentation :-?  

---

### Answer by username_4 on 2018-09-19. Score: 2

Here is a Biopython solution via Entrez utilies, simply copy the code into a python script and execute it:
from Bio import Entrez, SeqIO

Entrez.email = 'me@email.com'

def get_fasta_from_ids(ids_list):
    handle = Entrez.efetch(db='nucleotide', id=','.join(ids_list), rettype='fasta')
    for record in SeqIO.parse(handle, 'fasta'):
        yield record

SeqIO.write(get_fasta_from_ids(['NC_001416.1']), 'NC_001416.1.fa', 'fasta')

You can add more ids to the input list to retrieve multiple records at once.

> **Comments:**
> **username_0**: Thanks for you code snippet, but this is also a solution that needs extra software to be installed (BioPython and this snippet itself), while I'm looking for something that can be run out of the box in any system with no extra software if possible.  
> **username_1**: Is the `Entrez.email` actually doing anything? Does the `Entrez` package need it to be set in order to run?  
> **username_4**: @terdon Yes `Entrez.email` is required (I do use my actual email address although I've never received an email about this). `Entrez.api_key` can be also be added to speed up queries. BTW, I agree that using the E-utilities (as in your solution and mine) is the most flexible approach, whether you do it via python or something else is mostly a matter of taste  

---

### Answer by username_1 on 2018-09-19. Score: 1

You can either do this directly as you show in your answer or, for a more sophisticated and flexible approach, use NCBI's edirect tool:
esearch -db nucleotide -query 'NC_001416.1' | efetch -format fasta > NC_001416.1.fa

> **Comments:**
> **username_0**: That's a quite elegant solution, but it involves installing new software, and I'm trying to generate my scripts with as less extra software as possible. Althought this is a more robust solution than having a link that can change over time.  
> **username_1**: @Poshi fair enough, I'm glad you found a solution. You might want to accept your answer to indicate that it is the one you preferred.  

---

### Answer by username_5 on 2018-09-19. Score: 0

You can add the `--origfmt` option, and see what comes out. Either you will have what you want, or perhaps they uploaded the sequences that way.

See: <https://ncbi.github.io/sra-tools/fastq-dump.html>

> **Comments:**
> **username_0**: With that parameter I have the exact same output :-( I think I already tried everything that made sense in my head.  
> **username_5**: Did you also try to set the `--fasta` to 0, so that there is no line wrapping?  
> **username_0**: Yes, same effect, oly difference is that instead of a line of ID and several lines of sequence, I have on line of ID and on line of sequence. But still 10 sequences.  

---

