Answers.Body: <p>A mixture of <code>apply</code>, <code>paste</code> and <code>cat</code> will do this:</p>
  
  <pre><code>&gt; data.mat
         sample1 sample2 sample3 sample4
  fliI         1       1       1       1
  patB_1       1       1       1       1
  pgpA         1       1       1       1
  osmB         1       1       1       1
  cspA         1       0       1       1
  &gt; data.conv &lt;- apply(data.mat,2,paste,collapse="") # aggregate along 2nd dim
  &gt; cat(paste0("&gt;", names(data.conv), "\n", data.conv), sep="\n")
  &gt;sample1
  11111
  &gt;sample2
  11110
  &gt;sample3
  11111
  &gt;sample4
  11111
  </code></pre>
  
  <p>If writing to a file is required, just add a <code>file</code> parameter to the <code>cat</code> command:</p>
  
  <pre><code>cat(paste0("&gt;", names(data.conv), "\n", data.conv), 
      sep="\n", file="out_fastaLike.txt")
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-22T10:53:27.133Z
Answers.LastActivityDate: 2017-07-22T10:53:27.133Z
Answers.UserDisplayName: gringer
Answers.Body: <p>One could also use <code>cut</code> and a pair of Python scripts:</p>
  
  <p><em>transpose.py</em></p>
  
  <pre><code>#!/usr/bin/env python                                                                                                                                                                                                                                                                                                                       
  
  import sys
  
  for c in zip(*(l.split() for l in sys.stdin.readlines() if l.strip())):
      sys.stdout.write("%s\n" % ('\t'.join(c)))
  </code></pre>
  
  <p><em>mock_fasta.py</em></p>
  
  <pre><code>#!/usr/bin/env python                                                                                                                                                                                                                                                                                                                       
  
  import sys
  
  for e in (l.split() for l in sys.stdin.readlines() if l.strip()):
      sys.stdout.write("&gt;%s\n%s\n" % (e[0], ''.join(e[1:])))
  </code></pre>
  
  <p>Then:</p>
  
  <pre><code>$ cut -f2- in.mtx | transpose.py | mock_fasta.py &gt; in.fa
  </code></pre>
  
  <p>Which gives:</p>
  
  <pre><code>$ more in.fa
  &gt;sample1
  11111
  &gt;sample2
  11110
  &gt;sample3
  11111
  &gt;sample4
  11111
  </code></pre>
  
  <p>Why anyone would use R for text parsing is beyond me, but kids these days etc.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-21T18:05:08.483Z
Answers.LastActivityDate: 2017-09-17T00:22:57.303Z
Answers.LastEditDate: 2017-09-17T00:22:57.303Z
Answers.UserDisplayName: Alex Reynolds
Answers.Body: <p>It's not a fasta file, but:</p>
  
  <pre class="lang-r prettyprint-override"><code>&gt; m
         sample1 sample2 sample3 sample4
  fliI         1       1       1       1
  patB_1       1       1       1       1
  pgpA         1       1       1       1
  osmB         1       1       1       1
  cspA         1       0       1       1
  &gt; # Collapse to labeled strings
  &gt; blah = apply(m, 2, function(x) paste(x, collapse=''))
  &gt; blah
  sample1 sample2 sample3 sample4 
  "11111" "11110" "11111" "11111" 
  &gt; # Write that to a file with the appropriate format
  &gt; cat(paste(mapply(function(x, y) sprintf("&gt;%s\n%s\n", x, y), names(blah), blah), collapse=""), file="some file.fa")
  </code></pre>
  
  <p>Make sure to change <code>some file.fa</code>. I have no idea why you would want to do all of this, but this will give you the output you want:</p>
  
  <pre><code>$ cat blah.txt 
  &gt;sample1
  11111
  &gt;sample2
  11110
  &gt;sample3
  11111
  &gt;sample4
  11111
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-21T13:34:01.813Z
Answers.Text: Thanks Devon. This looks great. I want it for phylogenetic analysis so its important the layout comes out as in the matrix so its technically aligned.
Answers.UserDisplayName: AudileF
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-21T13:20:17.730Z
Answers.LastActivityDate: 2017-07-21T13:20:17.730Z
Answers.UserDisplayName: Devon Ryan
Body: <p>I have a binary matrix of gene presence or absence which looks like: [roary output]</p>
  
  <pre><code>Gene    sample1 sample2 sample3 sample4
  fliI        1       1       1        1
  patB_1      1       1       1        1
  pgpA        1       1       1        1
  osmB        1       1       1        1
  cspA        1       0       1        1
  </code></pre>
  
  <p>How can I convert to fasta so it looks like and technically aligned:  </p>
  
  <pre><code>&gt;sample1
  11111
  &gt;sample2
  11110
  </code></pre>
  
  <p>Is it possible to do this in R? </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-21T13:39:05.337Z
Comments.Text: What do you mean by "*technically* aligned"?
Comments.UserDisplayName: bli
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-21T14:47:26.027Z
Comments.Text: Well technically aligned as in similar to DNA alignments.
Comments.UserDisplayName: AudileF
Tags: <r><fasta><format-conversion>
Title: How to convert a binary matrix of gene presence or absence into a fasta sequence
ThreadText: # How to convert a binary matrix of gene presence or absence into a fasta sequence
  
  (Asked by: username_0 on 2017-07-21)
  
  I have a binary matrix of gene presence or absence which looks like: [roary output]
  
  ```
  Gene    sample1 sample2 sample3 sample4
  fliI        1       1       1        1
  patB_1      1       1       1        1
  pgpA        1       1       1        1
  osmB        1       1       1        1
  cspA        1       0       1        1
  
  ```
  
  How can I convert to fasta so it looks like and technically aligned:
  
  ```
  >sample1
  11111
  >sample2
  11110
  
  ```
  
  Is it possible to do this in R?
  
  **Tags:** r, fasta, format-conversion
  
  > **Comments:**
  > **username_1**: What do you mean by "*technically* aligned"?  
  > **username_0**: Well technically aligned as in similar to DNA alignments.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_4 on 2017-07-21. Score: 8 | Accepted answer
  
  It's not a fasta file, but:
  
  ```r
  > m
         sample1 sample2 sample3 sample4
  fliI         1       1       1       1
  patB_1       1       1       1       1
  pgpA         1       1       1       1
  osmB         1       1       1       1
  cspA         1       0       1       1
  > # Collapse to labeled strings
  > blah = apply(m, 2, function(x) paste(x, collapse=''))
  > blah
  sample1 sample2 sample3 sample4 
  "11111" "11110" "11111" "11111" 
  > # Write that to a file with the appropriate format
  > cat(paste(mapply(function(x, y) sprintf(">%s\n%s\n", x, y), names(blah), blah), collapse=""), file="some file.fa")
  
  ```
  
  Make sure to change `some file.fa`. I have no idea why you would want to do all of this, but this will give you the output you want:
  
  ```
  $ cat blah.txt 
  >sample1
  11111
  >sample2
  11110
  >sample3
  11111
  >sample4
  11111
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_4. This looks great. I want it for phylogenetic analysis so its important the layout comes out as in the matrix so its technically aligned.  
  
  ---
  
  ### Answer by username_2 on 2017-07-22. Score: 4
  
  A mixture of `apply`, `paste` and `cat` will do this:
  
  ```
  > data.mat
         sample1 sample2 sample3 sample4
  fliI         1       1       1       1
  patB_1       1       1       1       1
  pgpA         1       1       1       1
  osmB         1       1       1       1
  cspA         1       0       1       1
  > data.conv <- apply(data.mat,2,paste,collapse="") # aggregate along 2nd dim
  > cat(paste0(">", names(data.conv), "\n", data.conv), sep="\n")
  >sample1
  11111
  >sample2
  11110
  >sample3
  11111
  >sample4
  11111
  
  ```
  
  If writing to a file is required, just add a `file` parameter to the `cat` command:
  
  ```
  cat(paste0(">", names(data.conv), "\n", data.conv), 
      sep="\n", file="out_fastaLike.txt")
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2017-07-21. Score: 3
  
  One could also use `cut` and a pair of Python scripts:
  
  *transpose.py*
  
  ```
  #!/usr/bin/env python                                                                                                                                                                                                                                                                                                                       
  
  import sys
  
  for c in zip(*(l.split() for l in sys.stdin.readlines() if l.strip())):
      sys.stdout.write("%s\n" % ('\t'.join(c)))
  
  ```
  
  *mock\_fasta.py*
  
  ```
  #!/usr/bin/env python                                                                                                                                                                                                                                                                                                                       
  
  import sys
  
  for e in (l.split() for l in sys.stdin.readlines() if l.strip()):
      sys.stdout.write(">%s\n%s\n" % (e[0], ''.join(e[1:])))
  
  ```
  
  Then:
  
  ```
  $ cut -f2- in.mtx | transpose.py | mock_fasta.py > in.fa
  
  ```
  
  Which gives:
  
  ```
  $ more in.fa
  >sample1
  11111
  >sample2
  11110
  >sample3
  11111
  >sample4
  11111
  
  ```
  
  Why anyone would use R for text parsing is beyond me, but kids these days etc.
  
  
  ---
  
  