R语言中ClustalW比对结果写入FASTA文件报错求助
解决ClustalW比对结果写入FASTA时的S4对象报错问题
问题原因
msa()函数返回的myClustalWAlignment是S4类的多序列比对对象(如MsaRNAMultipleAlignment),而seqinr包的write.fasta()仅支持向量、列表这类可直接子集化的普通R对象,因此触发object of type 'S4' is not subsettable错误。
解决方法
方法1:使用msa包自带的导出函数(推荐)
msa包提供了专门适配自身比对结果的msaWrite()函数,无需格式转换即可直接导出:
library(BiocManager) library(msa) library(Biostrings) mySequences <- readRNAStringSet("C:/Users/School EC/Desktop/MSA project/only_unaligned/no_recomb/1_unaligned.fasta") myClustalWAlignment <- msa(mySequences, "ClustalW") # 导出为FASTA格式 msaWrite(myClustalWAlignment, file = "aligned_1.fasta", format = "fasta")
方法2:转换S4对象为seqinr支持的格式
如果一定要使用seqinr的write.fasta(),需将S4比对对象转换为字符向量,并设置as.string = TRUE适配格式:
library(seqinr) # 将S4比对对象转换为字符向量 aligned_seqs <- as.character(myClustalWAlignment) # 调用write.fasta导出 write.fasta(sequences = aligned_seqs, names = names(myClustalWAlignment), file.out = "aligned_1.fasta", open = "w", nbchar = 60, as.string = TRUE) # 必须设为TRUE,匹配字符向量输入格式
内容的提问来源于stack exchange,提问作者thole
相关产品推荐
相关产品推荐

