<p>The following should work:</p>

<pre><code>cat(file="out.fa", paste(paste0("&gt;",names(dnabin_to_fasta)),
          sapply(dnabin_to_fasta, paste, collapse=""), sep="\n"), sep="\n");
</code></pre>

<p>A bit of explanation, starting from the middle outwards:</p>

<ol>
<li>Collapse the letter arrays inside dnabin_to_fasta into single strings</li>
<li>Append ">" to the front of the names of each sequence</li>
<li>Concatenate the results of steps 1 &amp; 2 together, separating with a line break</li>
<li>Write each concatenated string to a file, separating by a line break</li>
</ol>

CC BY-SA 3.0
2017-08-30T14:08:25.743Z
Thank you so much! It solved a major part of my problem. When I print it to the screen, I get the exact output I want. However, it does not produce any output files. Could you please help me with this, too?
Nikita
CC BY-SA 3.0
2017-08-30T14:18:54.690Z
Note that R will save files in the "working directory", which is not necessarily the directory that the last file was loaded from. If you're using `cat` with the `file` option, it's probably saving *somewhere*. You can find out where by using the `getwd()` command.
gringer
CC BY-SA 3.0
2017-08-30T14:19:31.803Z
Thank you so much! It solved my problem! I was stuck in it since a long time.
Nikita
CC BY-SA 3.0
2017-08-30T01:40:35.883Z
2017-08-30T01:40:35.883Z
gringer
<p>I am trying to convert DNAbin files to fasta format. Rationale: I want to use the fasta file to calculate non-synonymous/synonymous mutation rate. </p>

<p><code>my_dnabin1</code> is a DNAbin file of 55 samples and I am using the following code to convert it into a fasta file. </p>

<p><code>dnabin_to_fasta &lt;- lapply(my_dnabin1, function(x) as.character(x[1:length(x)]))</code>
This generates a list of 55 samples which looks like:</p>

<p>$SS.11.01</p>

<p>[1] "t" "t" "a" "c" "c" "t" "a" "a" "a" "a" "a" "g" "c" "c" "g" "c" "t" "t" "c" "c" "c" "t" "c" "c" "a" "a"
 [27] "c" "c" "c" "t" "a" "g" "a" "a" "g" "c" "a" "a" "a" "c" "c" "t" "t" "t" "c" "a" "a" "c" "c" "c" "c" "a"</p>

<p>$SS.11.02</p>

<p>[1] "t" "t" "a" "c" "c" "t" "a" "a" "a" "a" "a" "g" "c" "c" "g" "c" "t" "t" "c" "c" "c" "t" "c" "c" "a" "a"
 [27] "c" "c" "c" "t" "a" "g" "a" "a" "g" "c" "a" "a" "a" "c" "c" "t" "t" "t" "c" "a" "a" "c" "c" "c" "c" "a"</p>

<p>and so on...</p>

<p>However, I want a fasta formatted file as the output that may look something like:</p>

<p><code>&gt;SS.11.01 ttacctga</code></p>

<p><code>&gt;SS.11.02 ttacctga</code></p>

<r><fasta>
How to convert DNAbin to FASTA in R?
# How to convert DNAbin to FASTA in R?

(Asked by: username_0 on 2017-08-30)

I am trying to convert DNAbin files to fasta format. Rationale: I want to use the fasta file to calculate non-synonymous/synonymous mutation rate.

`my_dnabin1` is a DNAbin file of 55 samples and I am using the following code to convert it into a fasta file.

`dnabin_to_fasta <- lapply(my_dnabin1, function(x) as.character(x[1:length(x)]))`
This generates a list of 55 samples which looks like:

$SS.11.01

[1] "t" "t" "a" "c" "c" "t" "a" "a" "a" "a" "a" "g" "c" "c" "g" "c" "t" "t" "c" "c" "c" "t" "c" "c" "a" "a"
[27] "c" "c" "c" "t" "a" "g" "a" "a" "g" "c" "a" "a" "a" "c" "c" "t" "t" "t" "c" "a" "a" "c" "c" "c" "c" "a"

$SS.11.02

[1] "t" "t" "a" "c" "c" "t" "a" "a" "a" "a" "a" "g" "c" "c" "g" "c" "t" "t" "c" "c" "c" "t" "c" "c" "a" "a"
[27] "c" "c" "c" "t" "a" "g" "a" "a" "g" "c" "a" "a" "a" "c" "c" "t" "t" "t" "c" "a" "a" "c" "c" "c" "c" "a"

and so on...

However, I want a fasta formatted file as the output that may look something like:

`>SS.11.01 ttacctga`

`>SS.11.02 ttacctga`

**Tags:** r, fasta

---

## 1 answers

### Answer by username_1 on 2017-08-30. Score: 5 | Accepted answer

The following should work:
cat(file="out.fa", paste(paste0(">",names(dnabin_to_fasta)),
          sapply(dnabin_to_fasta, paste, collapse=""), sep="\n"), sep="\n");

A bit of explanation, starting from the middle outwards:

Collapse the letter arrays inside dnabin_to_fasta into single strings
Append ">" to the front of the names of each sequence
Concatenate the results of steps 1 & 2 together, separating with a line break
Write each concatenated string to a file, separating by a line break

> **Comments:**
> **username_0**: Thank you so much! It solved a major part of my problem. When I print it to the screen, I get the exact output I want. However, it does not produce any output files. Could you please help me with this, too?  
> **username_1**: Note that R will save files in the "working directory", which is not necessarily the directory that the last file was loaded from. If you're using `cat` with the `file` option, it's probably saving *somewhere*. You can find out where by using the `getwd()` command.  
> **username_0**: Thank you so much! It solved my problem! I was stuck in it since a long time.  

---

