rfasta

Convert table into fasta in R


I have a table like this:

>head(X)
column1    column2
sequence1 ATCGATCGATCG
sequence2 GCCATGCCATTG

I need an output in a fasta file, looking like this:

sequence1  
ATCGATCGATCG
sequence2  
GCCATGCCATTG

So, basically I need all entries of the 2nd column to become new rows, interspersing the first column. The old 2nd column can then be discarded.

The way I would normally do that is by replacing a whitespace (or tab) with \n in notepad++, but I fear my files will be too big for doing that.

Is there a way for doing that in R?


Solution

  • D <- do.call(rbind, lapply(seq(nrow(X)), function(i) t(X[i, ])))
    D
    #         1             
    # column1 "sequence1"   
    # column2 "ATCGATCGATCG"
    # column1 "sequence2"   
    # column2 "GCCATGCCATTG"
    

    Then, when you write to file, you could use

    write.table(D, row.names = FALSE, col.names = FALSE, quote = FALSE)
    # sequence1
    # ATCGATCGATCG
    # sequence2
    # GCCATGCCATTG
    

    so that the row names, column names, and quotes will be gone.