This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Read, concatenate and merge the multiple fasta sequence files in R

Hi everyone,

I am using Windows and R-Studio. I am looking for a particular R-package or commands to read, write, concatenate and relabel the sequences in a fasta formatted file. I know other GUI softwares but I need to perform this in R.

Please help me. Regards Awan

r genome sequence

1 answer

For writing fasta to disk, you can use:

Write.Fasta <- function(Sequences, Names, FastaPath){
  if (length(Sequences) != length(Names)) stop="Sequences and Names of unequal length!"
  X <- data.frame(paste(">", Names, sep=""), Sequences)
  D <- do.call(rbind, lapply(seq(nrow(X)), function(i) t(X[i, ])))
  write.table(x = D, sep="\n", col.names = F, row.names = F, quote = F, file = FastaPath)
}

## Example:
Write.Fasta(Sequences = my.seqs, Names = my.names, FastaPath = "/Path/To/file.fa")

Log in to answer this question.