writing FASTA file output in R

706 Views Asked by At

I am trying to perform Multiple Sequence Alignment using ClustalW. The code works, and I am able to see the alignments on my terminal in R. Below is the code that I wrote.

library(BiocManager)
library(msa)
library(Biostrings)

mySequences <- readRNAStringSet("C:/Users/School EC/Desktop/MSA 
project/only_unaligned/no_recomb/1_unaligned.fasta")
mySequences

myClustalWAlignment <- msa(mySequences, "ClustalW")
myClustalWAlignment

library(seqinr)
write.fasta(sequences = myClustalWAlignment,names = 
names(myClustalWAlignment), file.out = "aligned_1.fasta", open = 
"w",nbchar = 60, as.string = FALSE)

However when I run the write.fasta function I get an error that states:

Error in sequence[(nbchar * q + 1):l] : object of type 'S4' is not subsettable

Could you please advise on how I can sort this out. When I check my file where everything is stored the output file "aligned_1" is there but it is empty.

1

There are 1 best solutions below

0
On

I am not quite sure what caused the error "Error in sequence[(nbchar * q + 1):l] : object of type 'S4' is not subsettable" and I have not figured out why the first approach to write the fasta file was not working. After running my alignment I used RNAStringSet to turn my alignments into a XStringset for storage. I then used the Xstringset function to write my output (alignments) file into the format desired which was fasta.

Solution:

library(BiocManager)
library(msa)
library(Biostrings)

mySequences <- readRNAStringSet("C:/Users/School EC/Desktop/MSA 
project/only_unaligned/no_recomb/1_unaligned.fasta")
mySequences

myClustalWAlignment <- msa(mySequences, "ClustalW")
myClustalWAlignment

Clustal_A1<-RNAStringSet(myClustalWAlignment) 
Biostrings::writeXStringSet(Clustal_A1, "aligned_1.fasta")