0

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.

Quinten
  • 35,235
  • 5
  • 20
  • 53
thole
  • 117
  • 6
  • Have you tried increasing `nbchar` to greater than the default `60`? – Chris Sep 25 '22 at 20:42
  • Greetings! Usually it is helpful to provide a minimally reproducible dataset for questions here. One way of doing this is by using the `dput` function. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Sep 26 '22 at 00:27
  • @Chris, I tried increasing the nbchar but it still did not work. However I tried a different method which worked. Clustal_A1<-RNAStringSet(myClustalWAlignment) Biostrings::writeXStringSet(Clustal_A1, "aligned_1.fasta") . – thole Sep 26 '22 at 09:25
  • If you can figure out 'why the difference', even if taking the first approach was wrong, you should write up your solution (and why it was right) as an answer and accept it, as it is likely others will experience the same problem, and solutions are easier to find in answers than in comments. – Chris Sep 26 '22 at 11:29

1 Answers1

0

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")
thole
  • 117
  • 6