text: user982: 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? 
  
  user776: 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.
  
  user73: 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")
  
  
  user77: It's not a fasta file, but:
  > 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
  
  
original_text: 982: <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>
   776: <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>
   73: <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>
   77: <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>
  