Home > Software engineering >  Convert multi-fasta file into seperate individual fasta files in R
Convert multi-fasta file into seperate individual fasta files in R

Time:05-29

I have a multi-fasta file with 1333 individual fasta files in txt fomrat

>header1
ACGATGCACAAGGT.....
>header2
CCAAACGCAGGGGT.....
>header3
CCAATAAGTAGCCC.....
>header4
AAAGTCGGATTTAG.....

continuing till >header1333

I want to split the multi-fasta into separate individual fasta files so that it fits in my R code for some biological analysis which was exclusively made for a single fasta file.

I want the outcome to be like file1.txt will contain

>header1
ACGATGCACAAGGT.....

file2.txt will contain

>header2
CCAAACGCAGGGGT.....

And so on. Is there any possible way to do this?

CodePudding user response:

You can read it in data.frame then save it in folder named fastafolder using a for loop :

fasta <- read.table("~/fasta", quote="\"", comment.char="")

dir.create("~/fastafolder")

for (line in 1:nrow(fasta)) {
  rows <- 2 * line - 1
  if (rows < nrow(fasta)) {
    write(fasta[rows:(rows   1), 1] , paste0("~/fastafolder/fasta" , line))
  }
}

Created on 2022-05-28 by the reprex package (v2.0.1)

  • Related