我正在尝试从R中的BLOSUM62创建一个矩阵。
library(Biostrings)
seq_test <- c("PAWHEAE", "HEAGAWGHEE", "CAWEKDRRTEAFF", "CASSLVFGQGDNIQYF")
aligned <- pairwiseAlignment(seq_test, seq_test, substitutionMatrix="BLOSUM62")
score(aligned)
#44 62 76 86
它只是将每个序列与自身进行比较。我想让每个序列循环并比较整个序列列表,创建一个4x4矩阵。它看起来更像是这样:
44 -7 -23 -52
-7 62 -34 -40
-23 -34 76 -13
-52 -40 -13 86
答案 0 :(得分:2)
创建所有组合然后比较:
#all combos
seq1 <- expand.grid(seq_test, seq_test)
#compare, convert to matrix
matrix(score(pairwiseAlignment(seq1$Var1, seq1$Var2,
substitutionMatrix = "BLOSUM62")),
nrow = length(seq_test))
# [,1] [,2] [,3] [,4]
# [1,] 44 -7 -23 -52
# [2,] -7 62 -34 -40
# [3,] -23 -34 76 -13
# [4,] -52 -40 -13 86
或循环遍历向量:
sapply(seq_test, function(i){
sapply(seq_test, function(j){
score(pairwiseAlignment(i, j, substitutionMatrix = "BLOSUM62"))
})
})
# PAWHEAE HEAGAWGHEE CAWEKDRRTEAFF CASSLVFGQGDNIQYF
# PAWHEAE 44 -7 -23 -52
# HEAGAWGHEE -7 62 -34 -40
# CAWEKDRRTEAFF -23 -34 76 -13
# CASSLVFGQGDNIQYF -52 -40 -13 86