Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-30T14:08:25.743Z
Answers.Text: 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?
Answers.UserDisplayName: Nikita
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-30T14:18:54.690Z
Answers.Text: 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.
Answers.UserDisplayName: gringer
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-30T14:19:31.803Z
Answers.Text: Thank you so much! It solved my problem! I was stuck in it since a long time.
Answers.UserDisplayName: Nikita
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-30T01:40:35.883Z
Answers.LastActivityDate: 2017-08-30T01:40:35.883Z
Answers.UserDisplayName: gringer
Body: <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>
  
Tags: <r><fasta>
Title: How to convert DNAbin to FASTA in R?
ThreadText: # 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:
  
  1. Collapse the letter arrays inside dnabin\_to\_fasta into single strings
  2. Append ">" to the front of the names of each sequence
  3. Concatenate the results of steps 1 & 2 together, separating with a line break
  4. 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.  
  
  ---
  
  