如何使用 proxy::dist 应用自定义函数来在 R 中创建距离矩阵
我定义了一个自定义函数并测试了该函数以确保它有效,但我无法将其应用于列表以获得距离矩阵。
我的代码是:
library(Biostrings)
library(proxy)
#import the sequences using Biostrings
indf<-readAAStringSet("C:/Users/jamie/OneDrive/Documents/Junk/SAMPLEFASTA.fasta")
#Assign the names and sequences to different variables
seqAAname<-names(indf)
seqz<-paste(indf)
#Put just the sequences into a dataframe
indf2<-data.frame(seqz)
#Convert the sequences into a list
indf3<-as.list(indf2)
#Define a custom function to return the alignment score between two sequences (pairwise)
customalnfunc <- function(X, Y){
pairwiseAlignment(X, Y,
substitutionMatrix = "BLOSUM45", gapOpening = 1, gapExtension = 3)
}
#Test the function but not as a function (This works fine)
testfreefunc<- pairwiseAlignment(AAString("PEHQRSTVE"),AAString("PQHQRETVE"),
substitutionMatrix = "BLOSUM45", gapOpening = 1, gapExtension = 3)
print(testfreefunc@score)
#Test the function as a fucntion to make sure it works (This works fine)
testfuncout <- customalnfunc(AAString("PEHQRSTVE"),AAString("PQHQRETVE"))
print(testfuncout@score)
#Apply the custom function to all possible pairs using proxy::dist with the custom function (This does not work, it returns 0)
outalnmatrix <- proxy::dist(indf3, method = customalnfunc)
outalnmatrix
SAMPLEFASTA.fasta 文件包含:
>SeqA
PEHQRSTVE
>SeqB
PQHQRETVE
>SeqC
RQHERSEVE
我尝试将输入数据作为列表和矩阵传递给 proxy::dist 。
我怎样才能做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要使用
proxy
包,因为proxy::dist
旨在对矩阵/数据帧的行进行相互比较。由于要比较字符串,因此可以使用outer
。但是,您需要调整customalnfunc
函数,以便它仅返回一个数字 (scoreOnly = TRUE
)。You don't need to use the
proxy
package asproxy::dist
is meant to icompare rows of matrix/dataframes against each other. Since you want to compare strings, you can useouter
. However, you need to tweak yourcustomalnfunc
function, so that it returns only a number (scoreOnly = TRUE
).