El David
El David

Reputation: 395

Creating a matrix from biostrings pairwisealignment BLOSUM

I am trying to create a matrix from BLOSUM62 in R.

library(Biostrings)

seq_test <- c("PAWHEAE", "HEAGAWGHEE", "CAWEKDRRTEAFF", "CASSLVFGQGDNIQYF")

aligned <- pairwiseAlignment(seq_test, seq_test, substitutionMatrix="BLOSUM62")

score(aligned)
#44 62 76 86

It is only comparing each sequence to itself. I would like for each sequence to loop through and compare to the entire list of sequences creating a 4x4 matrix. It would look more like this:

44  -7 -23 -52
-7  62 -34 -40
-23 -34  76 -13
-52 -40 -13  86

Upvotes: 2

Views: 752

Answers (1)

zx8754
zx8754

Reputation: 56229

Create all combos then compare:

#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

Or loop through the vector:

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

Upvotes: 2

Related Questions