<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>

CC BY-SA 3.0
2017-07-22T10:53:27.133Z
2017-07-22T10:53:27.133Z
gringer
<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>

CC BY-SA 3.0
2017-07-21T18:05:08.483Z
2017-09-17T00:22:57.303Z
2017-09-17T00:22:57.303Z
Alex Reynolds
<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>

CC BY-SA 3.0
2017-07-21T13:34:01.813Z
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.
AudileF
CC BY-SA 3.0
2017-07-21T13:20:17.730Z
2017-07-21T13:20:17.730Z
Devon Ryan
<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>

CC BY-SA 3.0
2017-07-21T13:39:05.337Z
What do you mean by "*technically* aligned"?
bli
CC BY-SA 3.0
2017-07-21T14:47:26.027Z
Well technically aligned as in similar to DNA alignments.
AudileF
<r><fasta><format-conversion>
How to convert a binary matrix of gene presence or absence into a fasta sequence
# 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:
> 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 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.  

---

### 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.


---

