Smith-Waterman algorithm#

Define a score function and two input sequences for alignment#

score=c(1,-1,-1)
seq1="AGTC"
seq2="ACGTC"

Create matrix M#

ncol = nchar(seq1)+1
nrow = nchar(seq2)+1
M = matrix(0, ncol = ncol, nrow = nrow)
colnames(M) = c("",unlist(strsplit(seq1,split="")))
rownames(M) = c("",unlist(strsplit(seq2,split="")))

coln = c("",unlist(strsplit(seq1,split="")))
rown = c("",unlist(strsplit(seq2,split="")))

move = M

Initialize matrix M#

for(i in 2:ncol) M[1,i] = M[1,i-1] + max(0,score[3])
for(i in 2:nrow) M[i,1] = M[i-1,1] + max(0,score[3])

for(i in 2:ncol) move[1,i] = 1
for(i in 2:nrow) move[i,1] = 2

M
Hide code cell output
A matrix: 6 × 5 of type dbl
AGTC
00000
A00000
C00000
G00000
T00000
C00000

Fill in matrix M#

for(i in 2:nrow){
  for(j in 2:ncol){
    h_score = M[i,j-1] + score[3]
    v_score = M[i-1,j] + score[3]
    if(coln[j] == rown[i]) d_score = M[i-1,j-1] + score[1]
    else d_score = M[i-1,j-1] + score[2]
    x = c(h_score,v_score,d_score)
    M[i,j] = max(x)
    move[i,j] = which(x == M[i,j])[1]
    if(M[i,j]<0) M[i,j]=0
  }
}
M
A matrix: 6 × 5 of type dbl
AGTC
00000
A01000
C00001
G00100
T00021
C00013

Tracing back to find the alignment#

alignment = matrix("",nrow=2,ncol=nrow+ncol)
i = nrow
j = ncol
x = move[i,j]
index = nrow+ncol
while(x != 0){
  if(x == 3) {
    alignment[1,index] = coln[j]
    alignment[2,index] = rown[i]
    i = i-1
    j = j-1
    index = index-1
  }else if(x == 2){
    alignment[1,index] = "-"
    alignment[2,index] = rown[i]
    i = i-1
    index = index-1
  }else{
    alignment[1,index] = coln[j]
    alignment[2,index] = "-"
    j = j-1
    index = index-1
  }
  x = move[i,j]
}

Show the alignment#

alg=rep("",2)
alg[1]=paste(alignment[1,], collapse="")
alg[2]=paste(alignment[2,], collapse="")

result = list(M=as.matrix, score = as.numeric, alignment=as.character)

result$M = M
result$score = M[nrow,ncol]
result$alignment = alg

result
$M
A matrix: 6 × 5 of type dbl
AGTC
00000
A01000
C00001
G00100
T00021
C00013
$score
3
$alignment
  1. 'A-GTC'
  2. 'ACGTC'

A wrapper function for Smith-Waterman algorithm#

Smith_Waterman <- function(seq1, seq2, score){
  ncol = nchar(seq1)+1
  nrow = nchar(seq2)+1
  
  
  #create matrix M
  M = matrix(0, ncol = ncol, nrow = nrow)
  colnames(M) = c("",unlist(strsplit(seq1,split="")))
  rownames(M) = c("",unlist(strsplit(seq2,split="")))
  
  coln = c("",unlist(strsplit(seq1,split="")))
  rown = c("",unlist(strsplit(seq2,split="")))
  
  move = M
  
  #fill in the first row and first column of M,N
  for(i in 2:ncol) M[1,i] = M[1,i-1] + max(0,score[3])
  for(i in 2:nrow) M[i,1] = M[i-1,1] + max(0,score[3])
  
  for(i in 2:ncol) move[1,i] = 1
  for(i in 2:nrow) move[i,1] = 2
  
  #fill in M
  for(i in 2:nrow){
    for(j in 2:ncol){
      h_score = M[i,j-1] + score[3]
      v_score = M[i-1,j] + score[3]
      if(coln[j] == rown[i]) d_score = M[i-1,j-1] + score[1]
      else d_score = M[i-1,j-1] + score[2]
      x = c(h_score,v_score,d_score)
      M[i,j] = max(x)
      move[i,j] = which(x == M[i,j])[1]
      if(M[i,j]<0) M[i,j]=0
    }
  }
  
  alignment = matrix("",nrow=2,ncol=nrow+ncol)
  i = nrow
  j = ncol
  x = move[i,j]
  index = nrow+ncol
  while(x != 0){
    if(x == 3) {
      alignment[1,index] = coln[j]
      alignment[2,index] = rown[i]
      i = i-1
      j = j-1
      index = index-1
    }else if(x == 2){
      alignment[1,index] = "-"
      alignment[2,index] = rown[i]
      i = i-1
      index = index-1
    }else{
      alignment[1,index] = coln[j]
      alignment[2,index] = "-"
      j = j-1
      index = index-1
    }
    x = move[i,j]
  }
  
  alg=rep("",2)
  alg[1]=paste(alignment[1,], collapse="")
  alg[2]=paste(alignment[2,], collapse="")
  
  result = list(M=as.matrix, score = as.numeric, alignment=as.character)
  
  result$M = M
  result$score = M[nrow,ncol]
  result$alignment = alg
  
  
  result

}

seq1="ATTCCTGTTCCCGTC"
seq2="ATCCTGCGTTCGTC"
Smith_Waterman(seq1,seq2,score=c(1,-1,-1))
$M
A matrix: 15 × 16 of type dbl
ATTCCTGTTCCCGTC
0000000000000000
A0100000000000000
T0021001011000010
C0011210000211002
C0000232100132101
T0011124321022121
G0000013543211321
C0000112443432223
G0000001333332322
T0011001244322243
T0012101135432133
C0001321024654324
G0000221213554543
T0011113232444465
C0000222222355457
$score
7
$alignment
  1. 'ATTCCTG--TTCCCGTC'
  2. 'AT-CCTGCGTTC--GTC'