<p><strong>Update</strong></p>
<p>It seems that part of the desired behavior is to write the sequences in the alignment <em>separately</em>. I am not sure that I understand why it is necessary to write them separately. Generally it is inadvisable to write many small files rather than one big file, based purely on issues of performance; you are likely to hit the I/O bound on performance very quickly, and that will be much slower.</p>
<p>One solution that <a href="https://stackoverflow.com/questions/55305833/extracting-subset-alignment-based-in-sequence-name-using-seqinr">has been suggested previously for this kind of problem</a> is to write the whole alignment to a single file, and then post-process that filed in a second step to extract the sequences of interest. This could be as trivial as a <code>samtools faidx</code> call from the shell, or you could work with it in seqinr as suggested in the previous answer.</p>
<p>Of course, it seems like it would be simpler for most purposes to just have the big alignment file and use e.g. samtools indexing / faidx / biopython / whatever to extract the sequences of interest. But it seems that that is specifically not desired in this case.</p>
<p><strong>Original answer</strong></p>
<p>I would strongly suggest using the <a href="https://cran.r-project.org/web/packages/seqinr/index.html" rel="nofollow noreferrer">seqinr</a> package, which is optimized for solving exactly these problems. See e.g. <a href="https://www.biostars.org/p/270009/" rel="nofollow noreferrer">this similar question</a>.</p>
<p>You will be able to read in an alignment (or whatever), cast it to as.matrix.alignment, do whatever your analysis is, then cast it back to as.alignment, which can then be passed to write.fasta to write the files.</p>
<p>Whenever possible it is preferable to work with such libraries, which handle these utility functions well, fast, and easy.</p>

CC BY-SA 4.0
2020-12-25T13:13:58.227Z
Agree, but because I wish to write a lot smaller fasta based on the original one, I was wondering if there might be faster way.
David
CC BY-SA 4.0
2020-12-27T00:06:06.833Z
@DavidS I see, it had not occurred to me that anyone would want many small "aligned" fasta files. I have updated the answer with one suggestion, but I think that this is just going to be slow based on file I/O no matter what if you want to write large numbers of files. Possibly someone has something clever though.
Maximilian Press
CC BY-SA 4.0
2020-12-27T06:50:03.320Z
So I start from a big MSA, but I wish to do some sort of "sub-sampling" of it to create a smaller files which will still holds valuable information to use further in a deep learning model.
David
CC BY-SA 4.0
2020-12-27T19:19:08.000Z
@DavidS Have you considered directly subsampling from the alignment as an in-memory data structure? I think that will definitely be faster than writing files and then reading them in again for the model. If the issue is access to the data structure across different processes, you could write the full alignment as an RData file using `save()`, and then `load()` it into each process. (assuming the DL model is in R; if it's in e.g. Python, I am sure that analogous alignment handling is possible there, but that's a different question.)
Maximilian Press
CC BY-SA 4.0
2020-12-31T14:27:59.610Z
I considered it, but the problem is that by just sampling from the MSA your might sample species that are too similar to the reference. So I apply some logic to select `k` samples which are farther apart by some criteria.
David
CC BY-SA 4.0
2020-12-31T18:24:02.023Z
@DavidS So why not put that logic into an in-memory sampling regime? Granted, we're now getting into an unrelated implementation discussion, which is mostly my fault. I'll just conclude by reiterating 1) I/O of many files intrinsically makes this (relatively) slow, 2) it is not clear why I/O of many files is necessary, from the high-level discussion we're having.
Maximilian Press
CC BY-SA 4.0
2020-12-24T19:01:40.597Z
2020-12-27T00:03:54.753Z
2020-12-27T00:03:54.753Z
Maximilian Press
<p>I have a data frame where the row names are the species names, and each column can either be an Amino-Acid character or &quot;-&quot;.</p>
<p>I wish to write it in a <a href="https://en.wikipedia.org/wiki/FASTA_format" rel="nofollow noreferrer">fasta format</a>. Simple example of the format is:</p>
<pre><code>&gt;UniRef90_A0A1V4QIJ3
------------------------------------------------------------
--------------------------MDISTFSP-----PVAG-----------VE----
------ITAF-------------V------------------------------------
LN--------G-------------------------------------------------
----EKIPA-----------------------------------------IVLSKD----
---LNPFL------------------Q-ELLDQEPAC-----------------------
------------EHDIGTCYG------------------------DA-------------
--------------------------------------------------PC-----IS-
-----------------------GPDAWNHSGFQC----Q---------VC---------
------------GLVKSWNL-------------------VGDFVVYRQE-----------
&gt;UniRef90_A0A0L9V5F8
------------------------------------------------------------
RFHTLFRNEYGHLRVLQRFDQRSKQIQNLENYRLVEFKSKPNTLLLPHHADADFLLVVLN
GRALLTLVNP-------------D---------------------------------G--
RDSYILEQGHA-------------------------------------------------
----QKIPAGTIFFLVNPNDNENLRI------------------------IKIATPINNP
HRFQDFFL-------------SSTEAQQSYLQG--FS-----------------------
----------------KNVLEASFDSEFKEINRVLFGEEGQQQQGEESQQEGVIVELERE
QIRELIKHA-----------------------------------------KSSSRRSLS-
-----------------------SQ-----DEPFNLRNRKP--------IY--SN-----
------------KF-GRWYEITPEKNP------------Q--------------------
</code></pre>
<p>I can do it using the following for loop:</p>
<pre><code>for (n in row.names(df)){
    ss = do.call(paste, c(as.list(mu_df[1, ]), sep = &quot;&quot;))
    write.fasta(ss,
                  names=n,
                  as.string=TRUE,
                  open=&quot;a&quot;,
                  new_file_path)
}
</code></pre>
<p>Is there a faster way of writing it? I have to write millions of those files and I wish to optimize it.</p>
<p>In order to create the data frame:</p>
<pre><code>structure(list(V1 = c(&quot;-&quot;, &quot;-&quot;), V2 = c(&quot;-&quot;, &quot;-&quot;), V3 = c(&quot;-&quot;, 
&quot;-&quot;), V4 = c(&quot;-&quot;, &quot;-&quot;), V5 = c(&quot;-&quot;, &quot;-&quot;), V6 = c(&quot;-&quot;, &quot;-&quot;), V7 = c(&quot;-&quot;, 
&quot;-&quot;), V8 = c(&quot;-&quot;, &quot;-&quot;), V9 = c(&quot;-&quot;, &quot;-&quot;), V10 = c(&quot;-&quot;, &quot;-&quot;), 
    V11 = c(&quot;-&quot;, &quot;-&quot;), V12 = c(&quot;-&quot;, &quot;-&quot;), V13 = c(&quot;-&quot;, &quot;-&quot;), 
    V14 = c(&quot;-&quot;, &quot;-&quot;), V15 = c(&quot;-&quot;, &quot;-&quot;), V16 = c(&quot;-&quot;, &quot;-&quot;), 
    V17 = c(&quot;-&quot;, &quot;-&quot;), V18 = c(&quot;-&quot;, &quot;-&quot;), V19 = c(&quot;-&quot;, &quot;-&quot;), 
    V20 = c(&quot;-&quot;, &quot;-&quot;), V21 = c(&quot;-&quot;, &quot;-&quot;), V22 = c(&quot;-&quot;, &quot;-&quot;), 
    V23 = c(&quot;-&quot;, &quot;-&quot;), V24 = c(&quot;-&quot;, &quot;-&quot;), V25 = c(&quot;-&quot;, &quot;-&quot;), 
    V26 = c(&quot;-&quot;, &quot;-&quot;), V27 = c(&quot;-&quot;, &quot;-&quot;), V28 = c(&quot;-&quot;, &quot;-&quot;), 
    V29 = c(&quot;-&quot;, &quot;-&quot;), V30 = c(&quot;-&quot;, &quot;-&quot;), V31 = c(&quot;-&quot;, &quot;-&quot;), 
    V32 = c(&quot;-&quot;, &quot;-&quot;), V33 = c(&quot;-&quot;, &quot;-&quot;), V34 = c(&quot;-&quot;, &quot;-&quot;), 
    V35 = c(&quot;-&quot;, &quot;-&quot;), V36 = c(&quot;-&quot;, &quot;-&quot;), V37 = c(&quot;-&quot;, &quot;-&quot;), 
    V38 = c(&quot;-&quot;, &quot;-&quot;), V39 = c(&quot;-&quot;, &quot;-&quot;), V40 = c(&quot;-&quot;, &quot;-&quot;), 
    V41 = c(&quot;-&quot;, &quot;-&quot;), V42 = c(&quot;-&quot;, &quot;-&quot;), V43 = c(&quot;-&quot;, &quot;-&quot;), 
    V44 = c(&quot;-&quot;, &quot;-&quot;), V45 = c(&quot;-&quot;, &quot;-&quot;), V46 = c(&quot;-&quot;, &quot;-&quot;), 
    V47 = c(&quot;-&quot;, &quot;-&quot;), V48 = c(&quot;-&quot;, &quot;-&quot;), V49 = c(&quot;-&quot;, &quot;-&quot;), 
    V50 = c(&quot;-&quot;, &quot;-&quot;), V51 = c(&quot;-&quot;, &quot;-&quot;), V52 = c(&quot;-&quot;, &quot;-&quot;), 
    V53 = c(&quot;-&quot;, &quot;-&quot;), V54 = c(&quot;-&quot;, &quot;-&quot;), V55 = c(&quot;-&quot;, &quot;-&quot;), 
    V56 = c(&quot;-&quot;, &quot;-&quot;), V57 = c(&quot;-&quot;, &quot;-&quot;), V58 = c(&quot;-&quot;, &quot;-&quot;), 
    V59 = c(&quot;-&quot;, &quot;-&quot;), V60 = c(&quot;-&quot;, &quot;-&quot;), V61 = c(&quot;-&quot;, &quot;R&quot;), 
    V62 = c(&quot;-&quot;, &quot;F&quot;), V63 = c(&quot;-&quot;, &quot;H&quot;), V64 = c(&quot;-&quot;, &quot;T&quot;), 
    V65 = c(&quot;-&quot;, &quot;L&quot;), V66 = c(&quot;-&quot;, &quot;F&quot;), V67 = c(&quot;-&quot;, &quot;R&quot;), 
    V68 = c(&quot;-&quot;, &quot;N&quot;), V69 = c(&quot;-&quot;, &quot;E&quot;), V70 = c(&quot;-&quot;, &quot;Y&quot;), 
    V71 = c(&quot;-&quot;, &quot;G&quot;), V72 = c(&quot;-&quot;, &quot;H&quot;), V73 = c(&quot;-&quot;, &quot;L&quot;), 
    V74 = c(&quot;-&quot;, &quot;R&quot;), V75 = c(&quot;-&quot;, &quot;V&quot;), V76 = c(&quot;-&quot;, &quot;L&quot;), 
    V77 = c(&quot;-&quot;, &quot;Q&quot;), V78 = c(&quot;-&quot;, &quot;R&quot;), V79 = c(&quot;-&quot;, &quot;F&quot;), 
    V80 = c(&quot;-&quot;, &quot;D&quot;), V81 = c(&quot;-&quot;, &quot;Q&quot;), V82 = c(&quot;-&quot;, &quot;R&quot;), 
    V83 = c(&quot;-&quot;, &quot;S&quot;), V84 = c(&quot;-&quot;, &quot;K&quot;), V85 = c(&quot;-&quot;, &quot;Q&quot;), 
    V86 = c(&quot;-&quot;, &quot;I&quot;), V87 = c(&quot;M&quot;, &quot;Q&quot;), V88 = c(&quot;D&quot;, &quot;N&quot;), 
    V89 = c(&quot;I&quot;, &quot;L&quot;), V90 = c(&quot;S&quot;, &quot;E&quot;), V91 = c(&quot;T&quot;, &quot;N&quot;), 
    V92 = c(&quot;F&quot;, &quot;Y&quot;), V93 = c(&quot;S&quot;, &quot;R&quot;), V94 = c(&quot;P&quot;, &quot;L&quot;), 
    V95 = c(&quot;-&quot;, &quot;V&quot;), V96 = c(&quot;-&quot;, &quot;E&quot;), V97 = c(&quot;-&quot;, &quot;F&quot;), 
    V98 = c(&quot;-&quot;, &quot;K&quot;), V99 = c(&quot;-&quot;, &quot;S&quot;), V100 = c(&quot;P&quot;, &quot;K&quot;), 
    V101 = c(&quot;V&quot;, &quot;P&quot;), V102 = c(&quot;A&quot;, &quot;N&quot;), V103 = c(&quot;G&quot;, &quot;T&quot;
    ), V104 = c(&quot;-&quot;, &quot;L&quot;), V105 = c(&quot;-&quot;, &quot;L&quot;), V106 = c(&quot;-&quot;, 
    &quot;L&quot;), V107 = c(&quot;-&quot;, &quot;P&quot;), V108 = c(&quot;-&quot;, &quot;H&quot;), V109 = c(&quot;-&quot;, 
    &quot;H&quot;), V110 = c(&quot;-&quot;, &quot;A&quot;), V111 = c(&quot;-&quot;, &quot;D&quot;), V112 = c(&quot;-&quot;, 
    &quot;A&quot;), V113 = c(&quot;-&quot;, &quot;D&quot;), V114 = c(&quot;-&quot;, &quot;F&quot;), V115 = c(&quot;V&quot;, 
    &quot;L&quot;), V116 = c(&quot;E&quot;, &quot;L&quot;), V117 = c(&quot;-&quot;, &quot;V&quot;), V118 = c(&quot;-&quot;, 
    &quot;V&quot;), V119 = c(&quot;-&quot;, &quot;L&quot;), V120 = c(&quot;-&quot;, &quot;N&quot;), V121 = c(&quot;-&quot;, 
    &quot;G&quot;), V122 = c(&quot;-&quot;, &quot;R&quot;), V123 = c(&quot;-&quot;, &quot;A&quot;), V124 = c(&quot;-&quot;, 
    &quot;L&quot;), V125 = c(&quot;-&quot;, &quot;L&quot;), V126 = c(&quot;-&quot;, &quot;T&quot;), V127 = c(&quot;I&quot;, 
    &quot;L&quot;), V128 = c(&quot;T&quot;, &quot;V&quot;), V129 = c(&quot;A&quot;, &quot;N&quot;), V130 = c(&quot;F&quot;, 
    &quot;P&quot;), V131 = c(&quot;-&quot;, &quot;-&quot;), V132 = c(&quot;-&quot;, &quot;-&quot;), V133 = c(&quot;-&quot;, 
    &quot;-&quot;), V134 = c(&quot;-&quot;, &quot;-&quot;), V135 = c(&quot;-&quot;, &quot;-&quot;), V136 = c(&quot;-&quot;, 
    &quot;-&quot;), V137 = c(&quot;-&quot;, &quot;-&quot;), V138 = c(&quot;-&quot;, &quot;-&quot;), V139 = c(&quot;-&quot;, 
    &quot;-&quot;), V140 = c(&quot;-&quot;, &quot;-&quot;), V141 = c(&quot;-&quot;, &quot;-&quot;), V142 = c(&quot;-&quot;, 
    &quot;-&quot;), V143 = c(&quot;-&quot;, &quot;-&quot;), V144 = c(&quot;V&quot;, &quot;D&quot;), V145 = c(&quot;-&quot;, 
    &quot;-&quot;), V146 = c(&quot;-&quot;, &quot;-&quot;), V147 = c(&quot;-&quot;, &quot;-&quot;), V148 = c(&quot;-&quot;, 
    &quot;-&quot;), V149 = c(&quot;-&quot;, &quot;-&quot;), V150 = c(&quot;-&quot;, &quot;-&quot;), V151 = c(&quot;-&quot;, 
    &quot;-&quot;), V152 = c(&quot;-&quot;, &quot;-&quot;), V153 = c(&quot;-&quot;, &quot;-&quot;), V154 = c(&quot;-&quot;, 
    &quot;-&quot;), V155 = c(&quot;-&quot;, &quot;-&quot;), V156 = c(&quot;-&quot;, &quot;-&quot;), V157 = c(&quot;-&quot;, 
    &quot;-&quot;), V158 = c(&quot;-&quot;, &quot;-&quot;), V159 = c(&quot;-&quot;, &quot;-&quot;), V160 = c(&quot;-&quot;, 
    &quot;-&quot;), V161 = c(&quot;-&quot;, &quot;-&quot;), V162 = c(&quot;-&quot;, &quot;-&quot;), V163 = c(&quot;-&quot;, 
    &quot;-&quot;), V164 = c(&quot;-&quot;, &quot;-&quot;), V165 = c(&quot;-&quot;, &quot;-&quot;), V166 = c(&quot;-&quot;, 
    &quot;-&quot;), V167 = c(&quot;-&quot;, &quot;-&quot;), V168 = c(&quot;-&quot;, &quot;-&quot;), V169 = c(&quot;-&quot;, 
    &quot;-&quot;), V170 = c(&quot;-&quot;, &quot;-&quot;), V171 = c(&quot;-&quot;, &quot;-&quot;), V172 = c(&quot;-&quot;, 
    &quot;-&quot;), V173 = c(&quot;-&quot;, &quot;-&quot;), V174 = c(&quot;-&quot;, &quot;-&quot;), V175 = c(&quot;-&quot;, 
    &quot;-&quot;), V176 = c(&quot;-&quot;, &quot;-&quot;), V177 = c(&quot;-&quot;, &quot;-&quot;), V178 = c(&quot;-&quot;, 
    &quot;G&quot;), V179 = c(&quot;-&quot;, &quot;-&quot;), V180 = c(&quot;-&quot;, &quot;-&quot;), V181 = c(&quot;L&quot;, 
    &quot;R&quot;), V182 = c(&quot;N&quot;, &quot;D&quot;), V183 = c(&quot;-&quot;, &quot;S&quot;), V184 = c(&quot;-&quot;, 
    &quot;Y&quot;), V185 = c(&quot;-&quot;, &quot;I&quot;), V186 = c(&quot;-&quot;, &quot;L&quot;), V187 = c(&quot;-&quot;, 
    &quot;E&quot;), V188 = c(&quot;-&quot;, &quot;Q&quot;), V189 = c(&quot;-&quot;, &quot;G&quot;), V190 = c(&quot;-&quot;, 
    &quot;H&quot;), V191 = c(&quot;G&quot;, &quot;A&quot;), V192 = c(&quot;-&quot;, &quot;-&quot;), V193 = c(&quot;-&quot;, 
    &quot;-&quot;), V194 = c(&quot;-&quot;, &quot;-&quot;), V195 = c(&quot;-&quot;, &quot;-&quot;), V196 = c(&quot;-&quot;, 
    &quot;-&quot;), V197 = c(&quot;-&quot;, &quot;-&quot;), V198 = c(&quot;-&quot;, &quot;-&quot;), V199 = c(&quot;-&quot;, 
    &quot;-&quot;), V200 = c(&quot;-&quot;, &quot;-&quot;), V201 = c(&quot;-&quot;, &quot;-&quot;), V202 = c(&quot;-&quot;, 
    &quot;-&quot;), V203 = c(&quot;-&quot;, &quot;-&quot;), V204 = c(&quot;-&quot;, &quot;-&quot;), V205 = c(&quot;-&quot;, 
    &quot;-&quot;), V206 = c(&quot;-&quot;, &quot;-&quot;), V207 = c(&quot;-&quot;, &quot;-&quot;), V208 = c(&quot;-&quot;, 
    &quot;-&quot;), V209 = c(&quot;-&quot;, &quot;-&quot;), V210 = c(&quot;-&quot;, &quot;-&quot;), V211 = c(&quot;-&quot;, 
    &quot;-&quot;), V212 = c(&quot;-&quot;, &quot;-&quot;), V213 = c(&quot;-&quot;, &quot;-&quot;), V214 = c(&quot;-&quot;, 
    &quot;-&quot;), V215 = c(&quot;-&quot;, &quot;-&quot;), V216 = c(&quot;-&quot;, &quot;-&quot;), V217 = c(&quot;-&quot;, 
    &quot;-&quot;), V218 = c(&quot;-&quot;, &quot;-&quot;), V219 = c(&quot;-&quot;, &quot;-&quot;), V220 = c(&quot;-&quot;, 
    &quot;-&quot;), V221 = c(&quot;-&quot;, &quot;-&quot;), V222 = c(&quot;-&quot;, &quot;-&quot;), V223 = c(&quot;-&quot;, 
    &quot;-&quot;), V224 = c(&quot;-&quot;, &quot;-&quot;), V225 = c(&quot;-&quot;, &quot;-&quot;), V226 = c(&quot;-&quot;, 
    &quot;-&quot;), V227 = c(&quot;-&quot;, &quot;-&quot;), V228 = c(&quot;-&quot;, &quot;-&quot;), V229 = c(&quot;-&quot;, 
    &quot;-&quot;), V230 = c(&quot;-&quot;, &quot;-&quot;), V231 = c(&quot;-&quot;, &quot;-&quot;), V232 = c(&quot;-&quot;, 
    &quot;-&quot;), V233 = c(&quot;-&quot;, &quot;-&quot;), V234 = c(&quot;-&quot;, &quot;-&quot;), V235 = c(&quot;-&quot;, 
    &quot;-&quot;), V236 = c(&quot;-&quot;, &quot;-&quot;), V237 = c(&quot;-&quot;, &quot;-&quot;), V238 = c(&quot;-&quot;, 
    &quot;-&quot;), V239 = c(&quot;-&quot;, &quot;-&quot;), V240 = c(&quot;-&quot;, &quot;-&quot;), V241 = c(&quot;-&quot;, 
    &quot;-&quot;), V242 = c(&quot;-&quot;, &quot;-&quot;), V243 = c(&quot;-&quot;, &quot;-&quot;), V244 = c(&quot;-&quot;, 
    &quot;-&quot;), V245 = c(&quot;E&quot;, &quot;Q&quot;), V246 = c(&quot;K&quot;, &quot;K&quot;), V247 = c(&quot;I&quot;, 
    &quot;I&quot;), V248 = c(&quot;P&quot;, &quot;P&quot;), V249 = c(&quot;A&quot;, &quot;A&quot;), V250 = c(&quot;-&quot;, 
    &quot;G&quot;), V251 = c(&quot;-&quot;, &quot;T&quot;), V252 = c(&quot;-&quot;, &quot;I&quot;), V253 = c(&quot;-&quot;, 
    &quot;F&quot;), V254 = c(&quot;-&quot;, &quot;F&quot;), V255 = c(&quot;-&quot;, &quot;L&quot;), V256 = c(&quot;-&quot;, 
    &quot;V&quot;), V257 = c(&quot;-&quot;, &quot;N&quot;), V258 = c(&quot;-&quot;, &quot;P&quot;), V259 = c(&quot;-&quot;, 
    &quot;N&quot;), V260 = c(&quot;-&quot;, &quot;D&quot;), V261 = c(&quot;-&quot;, &quot;N&quot;), V262 = c(&quot;-&quot;, 
    &quot;E&quot;), V263 = c(&quot;-&quot;, &quot;N&quot;), V264 = c(&quot;-&quot;, &quot;L&quot;), V265 = c(&quot;-&quot;, 
    &quot;R&quot;), V266 = c(&quot;-&quot;, &quot;I&quot;), V267 = c(&quot;-&quot;, &quot;-&quot;), V268 = c(&quot;-&quot;, 
    &quot;-&quot;), V269 = c(&quot;-&quot;, &quot;-&quot;), V270 = c(&quot;-&quot;, &quot;-&quot;), V271 = c(&quot;-&quot;, 
    &quot;-&quot;), V272 = c(&quot;-&quot;, &quot;-&quot;), V273 = c(&quot;-&quot;, &quot;-&quot;), V274 = c(&quot;-&quot;, 
    &quot;-&quot;), V275 = c(&quot;-&quot;, &quot;-&quot;), V276 = c(&quot;-&quot;, &quot;-&quot;), V277 = c(&quot;-&quot;, 
    &quot;-&quot;), V278 = c(&quot;-&quot;, &quot;-&quot;), V279 = c(&quot;-&quot;, &quot;-&quot;), V280 = c(&quot;-&quot;, 
    &quot;-&quot;), V281 = c(&quot;-&quot;, &quot;-&quot;), V282 = c(&quot;-&quot;, &quot;-&quot;), V283 = c(&quot;-&quot;, 
    &quot;-&quot;), V284 = c(&quot;-&quot;, &quot;-&quot;), V285 = c(&quot;-&quot;, &quot;-&quot;), V286 = c(&quot;-&quot;, 
    &quot;-&quot;), V287 = c(&quot;-&quot;, &quot;-&quot;), V288 = c(&quot;-&quot;, &quot;-&quot;), V289 = c(&quot;-&quot;, 
    &quot;-&quot;), V290 = c(&quot;-&quot;, &quot;-&quot;), V291 = c(&quot;I&quot;, &quot;I&quot;), V292 = c(&quot;V&quot;, 
    &quot;K&quot;), V293 = c(&quot;L&quot;, &quot;I&quot;), V294 = c(&quot;S&quot;, &quot;A&quot;), V295 = c(&quot;K&quot;, 
    &quot;T&quot;), V296 = c(&quot;D&quot;, &quot;P&quot;), V297 = c(&quot;-&quot;, &quot;I&quot;), V298 = c(&quot;-&quot;, 
    &quot;N&quot;), V299 = c(&quot;-&quot;, &quot;N&quot;), V300 = c(&quot;-&quot;, &quot;P&quot;), V301 = c(&quot;-&quot;, 
    &quot;H&quot;), V302 = c(&quot;-&quot;, &quot;R&quot;), V303 = c(&quot;-&quot;, &quot;F&quot;), V304 = c(&quot;L&quot;, 
    &quot;Q&quot;), V305 = c(&quot;N&quot;, &quot;D&quot;), V306 = c(&quot;P&quot;, &quot;F&quot;), V307 = c(&quot;F&quot;, 
    &quot;F&quot;), V308 = c(&quot;L&quot;, &quot;L&quot;), V309 = c(&quot;-&quot;, &quot;-&quot;), V310 = c(&quot;-&quot;, 
    &quot;-&quot;), V311 = c(&quot;-&quot;, &quot;-&quot;), V312 = c(&quot;-&quot;, &quot;-&quot;), V313 = c(&quot;-&quot;, 
    &quot;-&quot;), V314 = c(&quot;-&quot;, &quot;-&quot;), V315 = c(&quot;-&quot;, &quot;-&quot;), V316 = c(&quot;-&quot;, 
    &quot;-&quot;), V317 = c(&quot;-&quot;, &quot;-&quot;), V318 = c(&quot;-&quot;, &quot;-&quot;), V319 = c(&quot;-&quot;, 
    &quot;-&quot;), V320 = c(&quot;-&quot;, &quot;-&quot;), V321 = c(&quot;-&quot;, &quot;-&quot;), V322 = c(&quot;-&quot;, 
    &quot;S&quot;), V323 = c(&quot;-&quot;, &quot;S&quot;), V324 = c(&quot;-&quot;, &quot;T&quot;), V325 = c(&quot;-&quot;, 
    &quot;E&quot;), V326 = c(&quot;-&quot;, &quot;A&quot;), V327 = c(&quot;Q&quot;, &quot;Q&quot;), V328 = c(&quot;-&quot;, 
    &quot;Q&quot;), V329 = c(&quot;E&quot;, &quot;S&quot;), V330 = c(&quot;L&quot;, &quot;Y&quot;), V331 = c(&quot;L&quot;, 
    &quot;L&quot;), V332 = c(&quot;D&quot;, &quot;Q&quot;), V333 = c(&quot;Q&quot;, &quot;G&quot;), V334 = c(&quot;E&quot;, 
    &quot;-&quot;), V335 = c(&quot;P&quot;, &quot;-&quot;), V336 = c(&quot;A&quot;, &quot;F&quot;), V337 = c(&quot;C&quot;, 
    &quot;S&quot;), V338 = c(&quot;-&quot;, &quot;-&quot;), V339 = c(&quot;-&quot;, &quot;-&quot;), V340 = c(&quot;-&quot;, 
    &quot;-&quot;), V341 = c(&quot;-&quot;, &quot;-&quot;), V342 = c(&quot;-&quot;, &quot;-&quot;), V343 = c(&quot;-&quot;, 
    &quot;-&quot;), V344 = c(&quot;-&quot;, &quot;-&quot;), V345 = c(&quot;-&quot;, &quot;-&quot;), V346 = c(&quot;-&quot;, 
    &quot;-&quot;), V347 = c(&quot;-&quot;, &quot;-&quot;), V348 = c(&quot;-&quot;, &quot;-&quot;), V349 = c(&quot;-&quot;, 
    &quot;-&quot;), V350 = c(&quot;-&quot;, &quot;-&quot;), V351 = c(&quot;-&quot;, &quot;-&quot;), V352 = c(&quot;-&quot;, 
    &quot;-&quot;), V353 = c(&quot;-&quot;, &quot;-&quot;), V354 = c(&quot;-&quot;, &quot;-&quot;), V355 = c(&quot;-&quot;, 
    &quot;-&quot;), V356 = c(&quot;-&quot;, &quot;-&quot;), V357 = c(&quot;-&quot;, &quot;-&quot;), V358 = c(&quot;-&quot;, 
    &quot;-&quot;), V359 = c(&quot;-&quot;, &quot;-&quot;), V360 = c(&quot;-&quot;, &quot;-&quot;), V361 = c(&quot;-&quot;, 
    &quot;-&quot;), V362 = c(&quot;-&quot;, &quot;-&quot;), V363 = c(&quot;-&quot;, &quot;-&quot;), V364 = c(&quot;-&quot;, 
    &quot;-&quot;), V365 = c(&quot;-&quot;, &quot;-&quot;), V366 = c(&quot;-&quot;, &quot;-&quot;), V367 = c(&quot;-&quot;, 
    &quot;-&quot;), V368 = c(&quot;-&quot;, &quot;-&quot;), V369 = c(&quot;-&quot;, &quot;-&quot;), V370 = c(&quot;-&quot;, 
    &quot;-&quot;), V371 = c(&quot;-&quot;, &quot;-&quot;), V372 = c(&quot;-&quot;, &quot;-&quot;), V373 = c(&quot;E&quot;, 
    &quot;-&quot;), V374 = c(&quot;H&quot;, &quot;-&quot;), V375 = c(&quot;D&quot;, &quot;-&quot;), V376 = c(&quot;I&quot;, 
    &quot;-&quot;), V377 = c(&quot;G&quot;, &quot;K&quot;), V378 = c(&quot;T&quot;, &quot;N&quot;), V379 = c(&quot;C&quot;, 
    &quot;V&quot;), V380 = c(&quot;Y&quot;, &quot;L&quot;), V381 = c(&quot;G&quot;, &quot;E&quot;), V382 = c(&quot;-&quot;, 
    &quot;A&quot;), V383 = c(&quot;-&quot;, &quot;S&quot;), V384 = c(&quot;-&quot;, &quot;F&quot;), V385 = c(&quot;-&quot;, 
    &quot;D&quot;), V386 = c(&quot;-&quot;, &quot;S&quot;), V387 = c(&quot;-&quot;, &quot;E&quot;), V388 = c(&quot;-&quot;, 
    &quot;F&quot;), V389 = c(&quot;-&quot;, &quot;K&quot;), V390 = c(&quot;-&quot;, &quot;E&quot;), V391 = c(&quot;-&quot;, 
    &quot;I&quot;), V392 = c(&quot;-&quot;, &quot;N&quot;), V393 = c(&quot;-&quot;, &quot;R&quot;), V394 = c(&quot;-&quot;, 
    &quot;V&quot;), V395 = c(&quot;-&quot;, &quot;L&quot;), V396 = c(&quot;-&quot;, &quot;F&quot;), V397 = c(&quot;-&quot;, 
    &quot;G&quot;), V398 = c(&quot;-&quot;, &quot;E&quot;), V399 = c(&quot;-&quot;, &quot;E&quot;), V400 = c(&quot;-&quot;, 
    &quot;G&quot;), V401 = c(&quot;-&quot;, &quot;Q&quot;), V402 = c(&quot;-&quot;, &quot;Q&quot;), V403 = c(&quot;-&quot;, 
    &quot;Q&quot;), V404 = c(&quot;-&quot;, &quot;Q&quot;), V405 = c(&quot;-&quot;, &quot;G&quot;), V406 = c(&quot;D&quot;, 
    &quot;E&quot;), V407 = c(&quot;A&quot;, &quot;E&quot;), V408 = c(&quot;-&quot;, &quot;S&quot;), V409 = c(&quot;-&quot;, 
    &quot;Q&quot;), V410 = c(&quot;-&quot;, &quot;Q&quot;), V411 = c(&quot;-&quot;, &quot;E&quot;), V412 = c(&quot;-&quot;, 
    &quot;G&quot;), V413 = c(&quot;-&quot;, &quot;V&quot;), V414 = c(&quot;-&quot;, &quot;I&quot;), V415 = c(&quot;-&quot;, 
    &quot;V&quot;), V416 = c(&quot;-&quot;, &quot;E&quot;), V417 = c(&quot;-&quot;, &quot;L&quot;), V418 = c(&quot;-&quot;, 
    &quot;E&quot;), V419 = c(&quot;-&quot;, &quot;R&quot;), V420 = c(&quot;-&quot;, &quot;E&quot;), V421 = c(&quot;-&quot;, 
    &quot;Q&quot;), V422 = c(&quot;-&quot;, &quot;I&quot;), V423 = c(&quot;-&quot;, &quot;R&quot;), V424 = c(&quot;-&quot;, 
    &quot;E&quot;), V425 = c(&quot;-&quot;, &quot;L&quot;), V426 = c(&quot;-&quot;, &quot;I&quot;), V427 = c(&quot;-&quot;, 
    &quot;K&quot;), V428 = c(&quot;-&quot;, &quot;H&quot;), V429 = c(&quot;-&quot;, &quot;A&quot;), V430 = c(&quot;-&quot;, 
    &quot;-&quot;), V431 = c(&quot;-&quot;, &quot;-&quot;), V432 = c(&quot;-&quot;, &quot;-&quot;), V433 = c(&quot;-&quot;, 
    &quot;-&quot;), V434 = c(&quot;-&quot;, &quot;-&quot;), V435 = c(&quot;-&quot;, &quot;-&quot;), V436 = c(&quot;-&quot;, 
    &quot;-&quot;), V437 = c(&quot;-&quot;, &quot;-&quot;), V438 = c(&quot;-&quot;, &quot;-&quot;), V439 = c(&quot;-&quot;, 
    &quot;-&quot;), V440 = c(&quot;-&quot;, &quot;-&quot;), V441 = c(&quot;-&quot;, &quot;-&quot;), V442 = c(&quot;-&quot;, 
    &quot;-&quot;), V443 = c(&quot;-&quot;, &quot;-&quot;), V444 = c(&quot;-&quot;, &quot;-&quot;), V445 = c(&quot;-&quot;, 
    &quot;-&quot;), V446 = c(&quot;-&quot;, &quot;-&quot;), V447 = c(&quot;-&quot;, &quot;-&quot;), V448 = c(&quot;-&quot;, 
    &quot;-&quot;), V449 = c(&quot;-&quot;, &quot;-&quot;), V450 = c(&quot;-&quot;, &quot;-&quot;), V451 = c(&quot;-&quot;, 
    &quot;-&quot;), V452 = c(&quot;-&quot;, &quot;-&quot;), V453 = c(&quot;-&quot;, &quot;-&quot;), V454 = c(&quot;-&quot;, 
    &quot;-&quot;), V455 = c(&quot;-&quot;, &quot;-&quot;), V456 = c(&quot;-&quot;, &quot;-&quot;), V457 = c(&quot;-&quot;, 
    &quot;-&quot;), V458 = c(&quot;-&quot;, &quot;-&quot;), V459 = c(&quot;-&quot;, &quot;-&quot;), V460 = c(&quot;-&quot;, 
    &quot;-&quot;), V461 = c(&quot;-&quot;, &quot;-&quot;), V462 = c(&quot;-&quot;, &quot;-&quot;), V463 = c(&quot;-&quot;, 
    &quot;-&quot;), V464 = c(&quot;-&quot;, &quot;-&quot;), V465 = c(&quot;-&quot;, &quot;-&quot;), V466 = c(&quot;-&quot;, 
    &quot;-&quot;), V467 = c(&quot;-&quot;, &quot;-&quot;), V468 = c(&quot;-&quot;, &quot;-&quot;), V469 = c(&quot;-&quot;, 
    &quot;-&quot;), V470 = c(&quot;-&quot;, &quot;-&quot;), V471 = c(&quot;P&quot;, &quot;K&quot;), V472 = c(&quot;C&quot;, 
    &quot;S&quot;), V473 = c(&quot;-&quot;, &quot;S&quot;), V474 = c(&quot;-&quot;, &quot;S&quot;), V475 = c(&quot;-&quot;, 
    &quot;R&quot;), V476 = c(&quot;-&quot;, &quot;R&quot;), V477 = c(&quot;-&quot;, &quot;S&quot;), V478 = c(&quot;I&quot;, 
    &quot;L&quot;), V479 = c(&quot;S&quot;, &quot;S&quot;), V480 = c(&quot;-&quot;, &quot;-&quot;), V481 = c(&quot;-&quot;, 
    &quot;-&quot;), V482 = c(&quot;-&quot;, &quot;-&quot;), V483 = c(&quot;-&quot;, &quot;-&quot;), V484 = c(&quot;-&quot;, 
    &quot;-&quot;), V485 = c(&quot;-&quot;, &quot;-&quot;), V486 = c(&quot;-&quot;, &quot;-&quot;), V487 = c(&quot;-&quot;, 
    &quot;-&quot;), V488 = c(&quot;-&quot;, &quot;-&quot;), V489 = c(&quot;-&quot;, &quot;-&quot;), V490 = c(&quot;-&quot;, 
    &quot;-&quot;), V491 = c(&quot;-&quot;, &quot;-&quot;), V492 = c(&quot;-&quot;, &quot;-&quot;), V493 = c(&quot;-&quot;, 
    &quot;-&quot;), V494 = c(&quot;-&quot;, &quot;-&quot;), V495 = c(&quot;-&quot;, &quot;-&quot;), V496 = c(&quot;-&quot;, 
    &quot;-&quot;), V497 = c(&quot;-&quot;, &quot;-&quot;), V498 = c(&quot;-&quot;, &quot;-&quot;), V499 = c(&quot;-&quot;, 
    &quot;-&quot;), V500 = c(&quot;-&quot;, &quot;-&quot;), V501 = c(&quot;-&quot;, &quot;-&quot;), V502 = c(&quot;-&quot;, 
    &quot;-&quot;), V503 = c(&quot;-&quot;, &quot;-&quot;), V504 = c(&quot;G&quot;, &quot;S&quot;), V505 = c(&quot;P&quot;, 
    &quot;Q&quot;), V506 = c(&quot;D&quot;, &quot;-&quot;), V507 = c(&quot;A&quot;, &quot;-&quot;), V508 = c(&quot;W&quot;, 
    &quot;-&quot;), V509 = c(&quot;N&quot;, &quot;-&quot;), V510 = c(&quot;H&quot;, &quot;-&quot;), V511 = c(&quot;S&quot;, 
    &quot;D&quot;), V512 = c(&quot;G&quot;, &quot;E&quot;), V513 = c(&quot;F&quot;, &quot;P&quot;), V514 = c(&quot;Q&quot;, 
    &quot;F&quot;), V515 = c(&quot;C&quot;, &quot;N&quot;), V516 = c(&quot;-&quot;, &quot;L&quot;), V517 = c(&quot;-&quot;, 
    &quot;R&quot;), V518 = c(&quot;-&quot;, &quot;N&quot;), V519 = c(&quot;-&quot;, &quot;R&quot;), V520 = c(&quot;Q&quot;, 
    &quot;K&quot;), V521 = c(&quot;-&quot;, &quot;P&quot;), V522 = c(&quot;-&quot;, &quot;-&quot;), V523 = c(&quot;-&quot;, 
    &quot;-&quot;), V524 = c(&quot;-&quot;, &quot;-&quot;), V525 = c(&quot;-&quot;, &quot;-&quot;), V526 = c(&quot;-&quot;, 
    &quot;-&quot;), V527 = c(&quot;-&quot;, &quot;-&quot;), V528 = c(&quot;-&quot;, &quot;-&quot;), V529 = c(&quot;-&quot;, 
    &quot;-&quot;), V530 = c(&quot;V&quot;, &quot;I&quot;), V531 = c(&quot;C&quot;, &quot;Y&quot;), V532 = c(&quot;-&quot;, 
    &quot;-&quot;), V533 = c(&quot;-&quot;, &quot;-&quot;), V534 = c(&quot;-&quot;, &quot;S&quot;), V535 = c(&quot;-&quot;, 
    &quot;N&quot;), V536 = c(&quot;-&quot;, &quot;-&quot;), V537 = c(&quot;-&quot;, &quot;-&quot;), V538 = c(&quot;-&quot;, 
    &quot;-&quot;), V539 = c(&quot;-&quot;, &quot;-&quot;), V540 = c(&quot;-&quot;, &quot;-&quot;), V541 = c(&quot;-&quot;, 
    &quot;-&quot;), V542 = c(&quot;-&quot;, &quot;-&quot;), V543 = c(&quot;-&quot;, &quot;-&quot;), V544 = c(&quot;-&quot;, 
    &quot;-&quot;), V545 = c(&quot;-&quot;, &quot;-&quot;), V546 = c(&quot;-&quot;, &quot;-&quot;), V547 = c(&quot;-&quot;, 
    &quot;-&quot;), V548 = c(&quot;-&quot;, &quot;-&quot;), V549 = c(&quot;-&quot;, &quot;-&quot;), V550 = c(&quot;-&quot;, 
    &quot;-&quot;), V551 = c(&quot;-&quot;, &quot;-&quot;), V552 = c(&quot;-&quot;, &quot;-&quot;), V553 = c(&quot;G&quot;, 
    &quot;K&quot;), V554 = c(&quot;L&quot;, &quot;F&quot;), V555 = c(&quot;V&quot;, &quot;-&quot;), V556 = c(&quot;K&quot;, 
    &quot;G&quot;), V557 = c(&quot;S&quot;, &quot;R&quot;), V558 = c(&quot;W&quot;, &quot;W&quot;), V559 = c(&quot;N&quot;, 
    &quot;Y&quot;), V560 = c(&quot;L&quot;, &quot;E&quot;), V561 = c(&quot;-&quot;, &quot;I&quot;), V562 = c(&quot;-&quot;, 
    &quot;T&quot;), V563 = c(&quot;-&quot;, &quot;P&quot;), V564 = c(&quot;-&quot;, &quot;E&quot;), V565 = c(&quot;-&quot;, 
    &quot;K&quot;), V566 = c(&quot;-&quot;, &quot;N&quot;), V567 = c(&quot;-&quot;, &quot;P&quot;), V568 = c(&quot;-&quot;, 
    &quot;-&quot;), V569 = c(&quot;-&quot;, &quot;-&quot;), V570 = c(&quot;-&quot;, &quot;-&quot;), V571 = c(&quot;-&quot;, 
    &quot;-&quot;), V572 = c(&quot;-&quot;, &quot;-&quot;), V573 = c(&quot;-&quot;, &quot;-&quot;), V574 = c(&quot;-&quot;, 
    &quot;-&quot;), V575 = c(&quot;-&quot;, &quot;-&quot;), V576 = c(&quot;-&quot;, &quot;-&quot;), V577 = c(&quot;-&quot;, 
    &quot;-&quot;), V578 = c(&quot;-&quot;, &quot;-&quot;), V579 = c(&quot;-&quot;, &quot;-&quot;), V580 = c(&quot;V&quot;, 
    &quot;Q&quot;), V581 = c(&quot;G&quot;, &quot;-&quot;), V582 = c(&quot;D&quot;, &quot;-&quot;), V583 = c(&quot;F&quot;, 
    &quot;-&quot;), V584 = c(&quot;V&quot;, &quot;-&quot;), V585 = c(&quot;V&quot;, &quot;-&quot;), V586 = c(&quot;Y&quot;, 
    &quot;-&quot;), V587 = c(&quot;R&quot;, &quot;-&quot;), V588 = c(&quot;Q&quot;, &quot;-&quot;), V589 = c(&quot;E&quot;, 
    &quot;-&quot;), V590 = c(&quot;-&quot;, &quot;-&quot;), V591 = c(&quot;-&quot;, &quot;-&quot;), V592 = c(&quot;-&quot;, 
    &quot;-&quot;), V593 = c(&quot;-&quot;, &quot;-&quot;), V594 = c(&quot;-&quot;, &quot;-&quot;), V595 = c(&quot;-&quot;, 
    &quot;-&quot;), V596 = c(&quot;-&quot;, &quot;-&quot;), V597 = c(&quot;-&quot;, &quot;-&quot;), V598 = c(&quot;-&quot;, 
    &quot;-&quot;), V599 = c(&quot;-&quot;, &quot;-&quot;), V600 = c(&quot;-&quot;, &quot;-&quot;)), class = &quot;data.frame&quot;, row.names = c(&quot;UniRef90_A0A1V4QIJ3&quot;, 
&quot;UniRef90_A0A0L9V5F8&quot;))
</code></pre>

<r><fasta><dataframe>
How to write multi-sequence alignment data frame as a fasta format fast
# How to write multi-sequence alignment data frame as a fasta format fast

(Asked by: username_0 on 2020-12-24)

I have a data frame where the row names are the species names, and each column can either be an Amino-Acid character or "-".
I wish to write it in a fasta format. Simple example of the format is:
>UniRef90_A0A1V4QIJ3
------------------------------------------------------------
--------------------------MDISTFSP-----PVAG-----------VE----
------ITAF-------------V------------------------------------
LN--------G-------------------------------------------------
----EKIPA-----------------------------------------IVLSKD----
---LNPFL------------------Q-ELLDQEPAC-----------------------
------------EHDIGTCYG------------------------DA-------------
--------------------------------------------------PC-----IS-
-----------------------GPDAWNHSGFQC----Q---------VC---------
------------GLVKSWNL-------------------VGDFVVYRQE-----------
>UniRef90_A0A0L9V5F8
------------------------------------------------------------
RFHTLFRNEYGHLRVLQRFDQRSKQIQNLENYRLVEFKSKPNTLLLPHHADADFLLVVLN
GRALLTLVNP-------------D---------------------------------G--
RDSYILEQGHA-------------------------------------------------
----QKIPAGTIFFLVNPNDNENLRI------------------------IKIATPINNP
HRFQDFFL-------------SSTEAQQSYLQG--FS-----------------------
----------------KNVLEASFDSEFKEINRVLFGEEGQQQQGEESQQEGVIVELERE
QIRELIKHA-----------------------------------------KSSSRRSLS-
-----------------------SQ-----DEPFNLRNRKP--------IY--SN-----
------------KF-GRWYEITPEKNP------------Q--------------------

I can do it using the following for loop:
for (n in row.names(df)){
    ss = do.call(paste, c(as.list(mu_df[1, ]), sep = ""))
    write.fasta(ss,
                  names=n,
                  as.string=TRUE,
                  open="a",
                  new_file_path)
}

Is there a faster way of writing it? I have to write millions of those files and I wish to optimize it.
In order to create the data frame:
structure(list(V1 = c("-", "-"), V2 = c("-", "-"), V3 = c("-", 
"-"), V4 = c("-", "-"), V5 = c("-", "-"), V6 = c("-", "-"), V7 = c("-", 
"-"), V8 = c("-", "-"), V9 = c("-", "-"), V10 = c("-", "-"), 
    V11 = c("-", "-"), V12 = c("-", "-"), V13 = c("-", "-"), 
    V14 = c("-", "-"), V15 = c("-", "-"), V16 = c("-", "-"), 
    V17 = c("-", "-"), V18 = c("-", "-"), V19 = c("-", "-"), 
    V20 = c("-", "-"), V21 = c("-", "-"), V22 = c("-", "-"), 
    V23 = c("-", "-"), V24 = c("-", "-"), V25 = c("-", "-"), 
    V26 = c("-", "-"), V27 = c("-", "-"), V28 = c("-", "-"), 
    V29 = c("-", "-"), V30 = c("-", "-"), V31 = c("-", "-"), 
    V32 = c("-", "-"), V33 = c("-", "-"), V34 = c("-", "-"), 
    V35 = c("-", "-"), V36 = c("-", "-"), V37 = c("-", "-"), 
    V38 = c("-", "-"), V39 = c("-", "-"), V40 = c("-", "-"), 
    V41 = c("-", "-"), V42 = c("-", "-"), V43 = c("-", "-"), 
    V44 = c("-", "-"), V45 = c("-", "-"), V46 = c("-", "-"), 
    V47 = c("-", "-"), V48 = c("-", "-"), V49 = c("-", "-"), 
    V50 = c("-", "-"), V51 = c("-", "-"), V52 = c("-", "-"), 
    V53 = c("-", "-"), V54 = c("-", "-"), V55 = c("-", "-"), 
    V56 = c("-", "-"), V57 = c("-", "-"), V58 = c("-", "-"), 
    V59 = c("-", "-"), V60 = c("-", "-"), V61 = c("-", "R"), 
    V62 = c("-", "F"), V63 = c("-", "H"), V64 = c("-", "T"), 
    V65 = c("-", "L"), V66 = c("-", "F"), V67 = c("-", "R"), 
    V68 = c("-", "N"), V69 = c("-", "E"), V70 = c("-", "Y"), 
    V71 = c("-", "G"), V72 = c("-", "H"), V73 = c("-", "L"), 
    V74 = c("-", "R"), V75 = c("-", "V"), V76 = c("-", "L"), 
    V77 = c("-", "Q"), V78 = c("-", "R"), V79 = c("-", "F"), 
    V80 = c("-", "D"), V81 = c("-", "Q"), V82 = c("-", "R"), 
    V83 = c("-", "S"), V84 = c("-", "K"), V85 = c("-", "Q"), 
    V86 = c("-", "I"), V87 = c("M", "Q"), V88 = c("D", "N"), 
    V89 = c("I", "L"), V90 = c("S", "E"), V91 = c("T", "N"), 
    V92 = c("F", "Y"), V93 = c("S", "R"), V94 = c("P", "L"), 
    V95 = c("-", "V"), V96 = c("-", "E"), V97 = c("-", "F"), 
    V98 = c("-", "K"), V99 = c("-", "S"), V100 = c("P", "K"), 
    V101 = c("V", "P"), V102 = c("A", "N"), V103 = c("G", "T"
    ), V104 = c("-", "L"), V105 = c("-", "L"), V106 = c("-", 
    "L"), V107 = c("-", "P"), V108 = c("-", "H"), V109 = c("-", 
    "H"), V110 = c("-", "A"), V111 = c("-", "D"), V112 = c("-", 
    "A"), V113 = c("-", "D"), V114 = c("-", "F"), V115 = c("V", 
    "L"), V116 = c("E", "L"), V117 = c("-", "V"), V118 = c("-", 
    "V"), V119 = c("-", "L"), V120 = c("-", "N"), V121 = c("-", 
    "G"), V122 = c("-", "R"), V123 = c("-", "A"), V124 = c("-", 
    "L"), V125 = c("-", "L"), V126 = c("-", "T"), V127 = c("I", 
    "L"), V128 = c("T", "V"), V129 = c("A", "N"), V130 = c("F", 
    "P"), V131 = c("-", "-"), V132 = c("-", "-"), V133 = c("-", 
    "-"), V134 = c("-", "-"), V135 = c("-", "-"), V136 = c("-", 
    "-"), V137 = c("-", "-"), V138 = c("-", "-"), V139 = c("-", 
    "-"), V140 = c("-", "-"), V141 = c("-", "-"), V142 = c("-", 
    "-"), V143 = c("-", "-"), V144 = c("V", "D"), V145 = c("-", 
    "-"), V146 = c("-", "-"), V147 = c("-", "-"), V148 = c("-", 
    "-"), V149 = c("-", "-"), V150 = c("-", "-"), V151 = c("-", 
    "-"), V152 = c("-", "-"), V153 = c("-", "-"), V154 = c("-", 
    "-"), V155 = c("-", "-"), V156 = c("-", "-"), V157 = c("-", 
    "-"), V158 = c("-", "-"), V159 = c("-", "-"), V160 = c("-", 
    "-"), V161 = c("-", "-"), V162 = c("-", "-"), V163 = c("-", 
    "-"), V164 = c("-", "-"), V165 = c("-", "-"), V166 = c("-", 
    "-"), V167 = c("-", "-"), V168 = c("-", "-"), V169 = c("-", 
    "-"), V170 = c("-", "-"), V171 = c("-", "-"), V172 = c("-", 
    "-"), V173 = c("-", "-"), V174 = c("-", "-"), V175 = c("-", 
    "-"), V176 = c("-", "-"), V177 = c("-", "-"), V178 = c("-", 
    "G"), V179 = c("-", "-"), V180 = c("-", "-"), V181 = c("L", 
    "R"), V182 = c("N", "D"), V183 = c("-", "S"), V184 = c("-", 
    "Y"), V185 = c("-", "I"), V186 = c("-", "L"), V187 = c("-", 
    "E"), V188 = c("-", "Q"), V189 = c("-", "G"), V190 = c("-", 
    "H"), V191 = c("G", "A"), V192 = c("-", "-"), V193 = c("-", 
    "-"), V194 = c("-", "-"), V195 = c("-", "-"), V196 = c("-", 
    "-"), V197 = c("-", "-"), V198 = c("-", "-"), V199 = c("-", 
    "-"), V200 = c("-", "-"), V201 = c("-", "-"), V202 = c("-", 
    "-"), V203 = c("-", "-"), V204 = c("-", "-"), V205 = c("-", 
    "-"), V206 = c("-", "-"), V207 = c("-", "-"), V208 = c("-", 
    "-"), V209 = c("-", "-"), V210 = c("-", "-"), V211 = c("-", 
    "-"), V212 = c("-", "-"), V213 = c("-", "-"), V214 = c("-", 
    "-"), V215 = c("-", "-"), V216 = c("-", "-"), V217 = c("-", 
    "-"), V218 = c("-", "-"), V219 = c("-", "-"), V220 = c("-", 
    "-"), V221 = c("-", "-"), V222 = c("-", "-"), V223 = c("-", 
    "-"), V224 = c("-", "-"), V225 = c("-", "-"), V226 = c("-", 
    "-"), V227 = c("-", "-"), V228 = c("-", "-"), V229 = c("-", 
    "-"), V230 = c("-", "-"), V231 = c("-", "-"), V232 = c("-", 
    "-"), V233 = c("-", "-"), V234 = c("-", "-"), V235 = c("-", 
    "-"), V236 = c("-", "-"), V237 = c("-", "-"), V238 = c("-", 
    "-"), V239 = c("-", "-"), V240 = c("-", "-"), V241 = c("-", 
    "-"), V242 = c("-", "-"), V243 = c("-", "-"), V244 = c("-", 
    "-"), V245 = c("E", "Q"), V246 = c("K", "K"), V247 = c("I", 
    "I"), V248 = c("P", "P"), V249 = c("A", "A"), V250 = c("-", 
    "G"), V251 = c("-", "T"), V252 = c("-", "I"), V253 = c("-", 
    "F"), V254 = c("-", "F"), V255 = c("-", "L"), V256 = c("-", 
    "V"), V257 = c("-", "N"), V258 = c("-", "P"), V259 = c("-", 
    "N"), V260 = c("-", "D"), V261 = c("-", "N"), V262 = c("-", 
    "E"), V263 = c("-", "N"), V264 = c("-", "L"), V265 = c("-", 
    "R"), V266 = c("-", "I"), V267 = c("-", "-"), V268 = c("-", 
    "-"), V269 = c("-", "-"), V270 = c("-", "-"), V271 = c("-", 
    "-"), V272 = c("-", "-"), V273 = c("-", "-"), V274 = c("-", 
    "-"), V275 = c("-", "-"), V276 = c("-", "-"), V277 = c("-", 
    "-"), V278 = c("-", "-"), V279 = c("-", "-"), V280 = c("-", 
    "-"), V281 = c("-", "-"), V282 = c("-", "-"), V283 = c("-", 
    "-"), V284 = c("-", "-"), V285 = c("-", "-"), V286 = c("-", 
    "-"), V287 = c("-", "-"), V288 = c("-", "-"), V289 = c("-", 
    "-"), V290 = c("-", "-"), V291 = c("I", "I"), V292 = c("V", 
    "K"), V293 = c("L", "I"), V294 = c("S", "A"), V295 = c("K", 
    "T"), V296 = c("D", "P"), V297 = c("-", "I"), V298 = c("-", 
    "N"), V299 = c("-", "N"), V300 = c("-", "P"), V301 = c("-", 
    "H"), V302 = c("-", "R"), V303 = c("-", "F"), V304 = c("L", 
    "Q"), V305 = c("N", "D"), V306 = c("P", "F"), V307 = c("F", 
    "F"), V308 = c("L", "L"), V309 = c("-", "-"), V310 = c("-", 
    "-"), V311 = c("-", "-"), V312 = c("-", "-"), V313 = c("-", 
    "-"), V314 = c("-", "-"), V315 = c("-", "-"), V316 = c("-", 
    "-"), V317 = c("-", "-"), V318 = c("-", "-"), V319 = c("-", 
    "-"), V320 = c("-", "-"), V321 = c("-", "-"), V322 = c("-", 
    "S"), V323 = c("-", "S"), V324 = c("-", "T"), V325 = c("-", 
    "E"), V326 = c("-", "A"), V327 = c("Q", "Q"), V328 = c("-", 
    "Q"), V329 = c("E", "S"), V330 = c("L", "Y"), V331 = c("L", 
    "L"), V332 = c("D", "Q"), V333 = c("Q", "G"), V334 = c("E", 
    "-"), V335 = c("P", "-"), V336 = c("A", "F"), V337 = c("C", 
    "S"), V338 = c("-", "-"), V339 = c("-", "-"), V340 = c("-", 
    "-"), V341 = c("-", "-"), V342 = c("-", "-"), V343 = c("-", 
    "-"), V344 = c("-", "-"), V345 = c("-", "-"), V346 = c("-", 
    "-"), V347 = c("-", "-"), V348 = c("-", "-"), V349 = c("-", 
    "-"), V350 = c("-", "-"), V351 = c("-", "-"), V352 = c("-", 
    "-"), V353 = c("-", "-"), V354 = c("-", "-"), V355 = c("-", 
    "-"), V356 = c("-", "-"), V357 = c("-", "-"), V358 = c("-", 
    "-"), V359 = c("-", "-"), V360 = c("-", "-"), V361 = c("-", 
    "-"), V362 = c("-", "-"), V363 = c("-", "-"), V364 = c("-", 
    "-"), V365 = c("-", "-"), V366 = c("-", "-"), V367 = c("-", 
    "-"), V368 = c("-", "-"), V369 = c("-", "-"), V370 = c("-", 
    "-"), V371 = c("-", "-"), V372 = c("-", "-"), V373 = c("E", 
    "-"), V374 = c("H", "-"), V375 = c("D", "-"), V376 = c("I", 
    "-"), V377 = c("G", "K"), V378 = c("T", "N"), V379 = c("C", 
    "V"), V380 = c("Y", "L"), V381 = c("G", "E"), V382 = c("-", 
    "A"), V383 = c("-", "S"), V384 = c("-", "F"), V385 = c("-", 
    "D"), V386 = c("-", "S"), V387 = c("-", "E"), V388 = c("-", 
    "F"), V389 = c("-", "K"), V390 = c("-", "E"), V391 = c("-", 
    "I"), V392 = c("-", "N"), V393 = c("-", "R"), V394 = c("-", 
    "V"), V395 = c("-", "L"), V396 = c("-", "F"), V397 = c("-", 
    "G"), V398 = c("-", "E"), V399 = c("-", "E"), V400 = c("-", 
    "G"), V401 = c("-", "Q"), V402 = c("-", "Q"), V403 = c("-", 
    "Q"), V404 = c("-", "Q"), V405 = c("-", "G"), V406 = c("D", 
    "E"), V407 = c("A", "E"), V408 = c("-", "S"), V409 = c("-", 
    "Q"), V410 = c("-", "Q"), V411 = c("-", "E"), V412 = c("-", 
    "G"), V413 = c("-", "V"), V414 = c("-", "I"), V415 = c("-", 
    "V"), V416 = c("-", "E"), V417 = c("-", "L"), V418 = c("-", 
    "E"), V419 = c("-", "R"), V420 = c("-", "E"), V421 = c("-", 
    "Q"), V422 = c("-", "I"), V423 = c("-", "R"), V424 = c("-", 
    "E"), V425 = c("-", "L"), V426 = c("-", "I"), V427 = c("-", 
    "K"), V428 = c("-", "H"), V429 = c("-", "A"), V430 = c("-", 
    "-"), V431 = c("-", "-"), V432 = c("-", "-"), V433 = c("-", 
    "-"), V434 = c("-", "-"), V435 = c("-", "-"), V436 = c("-", 
    "-"), V437 = c("-", "-"), V438 = c("-", "-"), V439 = c("-", 
    "-"), V440 = c("-", "-"), V441 = c("-", "-"), V442 = c("-", 
    "-"), V443 = c("-", "-"), V444 = c("-", "-"), V445 = c("-", 
    "-"), V446 = c("-", "-"), V447 = c("-", "-"), V448 = c("-", 
    "-"), V449 = c("-", "-"), V450 = c("-", "-"), V451 = c("-", 
    "-"), V452 = c("-", "-"), V453 = c("-", "-"), V454 = c("-", 
    "-"), V455 = c("-", "-"), V456 = c("-", "-"), V457 = c("-", 
    "-"), V458 = c("-", "-"), V459 = c("-", "-"), V460 = c("-", 
    "-"), V461 = c("-", "-"), V462 = c("-", "-"), V463 = c("-", 
    "-"), V464 = c("-", "-"), V465 = c("-", "-"), V466 = c("-", 
    "-"), V467 = c("-", "-"), V468 = c("-", "-"), V469 = c("-", 
    "-"), V470 = c("-", "-"), V471 = c("P", "K"), V472 = c("C", 
    "S"), V473 = c("-", "S"), V474 = c("-", "S"), V475 = c("-", 
    "R"), V476 = c("-", "R"), V477 = c("-", "S"), V478 = c("I", 
    "L"), V479 = c("S", "S"), V480 = c("-", "-"), V481 = c("-", 
    "-"), V482 = c("-", "-"), V483 = c("-", "-"), V484 = c("-", 
    "-"), V485 = c("-", "-"), V486 = c("-", "-"), V487 = c("-", 
    "-"), V488 = c("-", "-"), V489 = c("-", "-"), V490 = c("-", 
    "-"), V491 = c("-", "-"), V492 = c("-", "-"), V493 = c("-", 
    "-"), V494 = c("-", "-"), V495 = c("-", "-"), V496 = c("-", 
    "-"), V497 = c("-", "-"), V498 = c("-", "-"), V499 = c("-", 
    "-"), V500 = c("-", "-"), V501 = c("-", "-"), V502 = c("-", 
    "-"), V503 = c("-", "-"), V504 = c("G", "S"), V505 = c("P", 
    "Q"), V506 = c("D", "-"), V507 = c("A", "-"), V508 = c("W", 
    "-"), V509 = c("N", "-"), V510 = c("H", "-"), V511 = c("S", 
    "D"), V512 = c("G", "E"), V513 = c("F", "P"), V514 = c("Q", 
    "F"), V515 = c("C", "N"), V516 = c("-", "L"), V517 = c("-", 
    "R"), V518 = c("-", "N"), V519 = c("-", "R"), V520 = c("Q", 
    "K"), V521 = c("-", "P"), V522 = c("-", "-"), V523 = c("-", 
    "-"), V524 = c("-", "-"), V525 = c("-", "-"), V526 = c("-", 
    "-"), V527 = c("-", "-"), V528 = c("-", "-"), V529 = c("-", 
    "-"), V530 = c("V", "I"), V531 = c("C", "Y"), V532 = c("-", 
    "-"), V533 = c("-", "-"), V534 = c("-", "S"), V535 = c("-", 
    "N"), V536 = c("-", "-"), V537 = c("-", "-"), V538 = c("-", 
    "-"), V539 = c("-", "-"), V540 = c("-", "-"), V541 = c("-", 
    "-"), V542 = c("-", "-"), V543 = c("-", "-"), V544 = c("-", 
    "-"), V545 = c("-", "-"), V546 = c("-", "-"), V547 = c("-", 
    "-"), V548 = c("-", "-"), V549 = c("-", "-"), V550 = c("-", 
    "-"), V551 = c("-", "-"), V552 = c("-", "-"), V553 = c("G", 
    "K"), V554 = c("L", "F"), V555 = c("V", "-"), V556 = c("K", 
    "G"), V557 = c("S", "R"), V558 = c("W", "W"), V559 = c("N", 
    "Y"), V560 = c("L", "E"), V561 = c("-", "I"), V562 = c("-", 
    "T"), V563 = c("-", "P"), V564 = c("-", "E"), V565 = c("-", 
    "K"), V566 = c("-", "N"), V567 = c("-", "P"), V568 = c("-", 
    "-"), V569 = c("-", "-"), V570 = c("-", "-"), V571 = c("-", 
    "-"), V572 = c("-", "-"), V573 = c("-", "-"), V574 = c("-", 
    "-"), V575 = c("-", "-"), V576 = c("-", "-"), V577 = c("-", 
    "-"), V578 = c("-", "-"), V579 = c("-", "-"), V580 = c("V", 
    "Q"), V581 = c("G", "-"), V582 = c("D", "-"), V583 = c("F", 
    "-"), V584 = c("V", "-"), V585 = c("V", "-"), V586 = c("Y", 
    "-"), V587 = c("R", "-"), V588 = c("Q", "-"), V589 = c("E", 
    "-"), V590 = c("-", "-"), V591 = c("-", "-"), V592 = c("-", 
    "-"), V593 = c("-", "-"), V594 = c("-", "-"), V595 = c("-", 
    "-"), V596 = c("-", "-"), V597 = c("-", "-"), V598 = c("-", 
    "-"), V599 = c("-", "-"), V600 = c("-", "-")), class = "data.frame", row.names = c("UniRef90_A0A1V4QIJ3", 
"UniRef90_A0A0L9V5F8"))

**Tags:** r, fasta, dataframe

---

## 1 answers

### Answer by username_1 on 2020-12-24. Score: 3 | Accepted answer

**Update**

It seems that part of the desired behavior is to write the sequences in the alignment *separately*. I am not sure that I understand why it is necessary to write them separately. Generally it is inadvisable to write many small files rather than one big file, based purely on issues of performance; you are likely to hit the I/O bound on performance very quickly, and that will be much slower.

One solution that [has been suggested previously for this kind of problem](https://stackoverflow.com/questions/55305833/extracting-subset-alignment-based-in-sequence-name-using-seqinr) is to write the whole alignment to a single file, and then post-process that filed in a second step to extract the sequences of interest. This could be as trivial as a `samtools faidx` call from the shell, or you could work with it in seqinr as suggested in the previous answer.

Of course, it seems like it would be simpler for most purposes to just have the big alignment file and use e.g. samtools indexing / faidx / biopython / whatever to extract the sequences of interest. But it seems that that is specifically not desired in this case.

**Original answer**

I would strongly suggest using the [seqinr](https://cran.r-project.org/web/packages/seqinr/index.html) package, which is optimized for solving exactly these problems. See e.g. [this similar question](https://www.biostars.org/p/270009/).

You will be able to read in an alignment (or whatever), cast it to as.matrix.alignment, do whatever your analysis is, then cast it back to as.alignment, which can then be passed to write.fasta to write the files.

Whenever possible it is preferable to work with such libraries, which handle these utility functions well, fast, and easy.

> **Comments:**
> **username_0**: Agree, but because I wish to write a lot smaller fasta based on the original one, I was wondering if there might be faster way.  
> **username_1**: @DavidS I see, it had not occurred to me that anyone would want many small "aligned" fasta files. I have updated the answer with one suggestion, but I think that this is just going to be slow based on file I/O no matter what if you want to write large numbers of files. Possibly someone has something clever though.  
> **username_0**: So I start from a big MSA, but I wish to do some sort of "sub-sampling" of it to create a smaller files which will still holds valuable information to use further in a deep learning model.  
> **username_1**: @DavidS Have you considered directly subsampling from the alignment as an in-memory data structure? I think that will definitely be faster than writing files and then reading them in again for the model. If the issue is access to the data structure across different processes, you could write the full alignment as an RData file using `save()`, and then `load()` it into each process. (assuming the DL model is in R; if it's in e.g. Python, I am sure that analogous alignment handling is possible there, but that's a different question.)  
> **username_0**: I considered it, but the problem is that by just sampling from the MSA your might sample species that are too similar to the reference. So I apply some logic to select `k` samples which are farther apart by some criteria.  
> **username_1**: @DavidS So why not put that logic into an in-memory sampling regime? Granted, we're now getting into an unrelated implementation discussion, which is mostly my fault. I'll just conclude by reiterating 1) I/O of many files intrinsically makes this (relatively) slow, 2) it is not clear why I/O of many files is necessary, from the high-level discussion we're having.  

---

