0% found this document useful (0 votes)
87 views

Needleman Algo

The document describes the implementation of the Needleman-Wunsch algorithm for global sequence alignment in Python. It includes functions to build an alignment matrix by recursively calculating scores for matches, mismatches and gaps. A directional string is generated from the matrix to trace the optimal alignment. This string is then used to build the alignment by iterating through the characters and concatenating matching/gap characters to the alignment strings.

Uploaded by

Aeman Maqsood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Needleman Algo

The document describes the implementation of the Needleman-Wunsch algorithm for global sequence alignment in Python. It includes functions to build an alignment matrix by recursively calculating scores for matches, mismatches and gaps. A directional string is generated from the matrix to trace the optimal alignment. This string is then used to build the alignment by iterating through the characters and concatenating matching/gap characters to the alignment strings.

Uploaded by

Aeman Maqsood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Code that I implemented:

import ​numpy ​as ​num

def n ​ eedleman(): ​#Initializing all the values and setting up the matrix
s ​ 1 = ​"CACGTAT"
​s2 = ​"CGCA"
​N = ​len​(s1)
M = ​len​(s2)
gap = -​1
​mismatch = -​1
​match = ​1
​matrix = num.zeros((m+​1​,n+​1​))
​#building an alignment matrix
m ​ atrix[​0​][​0​] = ​0
​for ​i ​in ​range​(​1​,N):
matrix[​1​][​0​] = matrix[i-​1​][​0​] + gap
​for ​j ​in ​range​(​1​,M):
matrix[​0​][j] = matrix[​0​][j-​1​] + gap
​for ​i ​in ​range​(​1​,N):
​for ​j ​in ​range​(​1​,M):
​if​(s1[i-​1​] == s2[j-​1​]):
score1 = matrix[i-​1​][j-​1​] + match
​else​:
score1 = matrix[i-​1​][j-​1​] + mismatch
score2 = matrix[i][j-​1​] + gap
score3 = matrix[i-​1​][j] + gap
matrix[i,j] = ​max​(score1,score2,score3)

#creating directional strings


def ​direction(matrix,N,M):
dstring = buildDirectionalString(matrix,N,M)
alignment(matrix,N,M,dstring)

#building alignments using directional string


def ​alignment(matrix,N,M):
seq1pos = N-​1
​seq2pos = M-​1
​dirpos = ​0
​top = ​" "
​bottom = ​" "
​while​(dirpos < ​len​(dstring)):
​if​(dstring[dirpos] == ​'D'​):
top+= s1[seq1pos]
bottom+= s2[seq2pos]
seq1pos-= ​1
​seq2pos-=​1
​elif​(dstring[dirpos]==​'V'​):
top+= s1[seq1pos]
bottom+= ​"-"
​seq1pos-=​1
​else​:
top+=​"-"
​bottom+= s2[seq2pos]
dirpos+=​1

#fucntion to create directional string


def ​buildDirectionalString(matrix,N,M):
dstring = ​""
​currentrow = N
currentcol = M
​while​(currentrow!=​0 ​or ​currentcol!=​0​):
​if​(currentrow==​0​):
dstring+=​"H"
​currentcol+=-​1
​elif​(currentcol==​0​):
dstring+=​"V"
​currentrow+=-​1
​elif​(matrix[currentrow][currentcol-​1​] + ​"-" =​ =matrix[currentrow][currentcol] ):
dstring+=​"H"
​currentcol+=-​1
​elif​(matrix[currentrow-​1​][currentcol] + "
​ -" =​ = matrix[currentrow][currentcol]):
dstring+=​"V"
​currentrow+=-​1
​else​:
dstring+=​"D"
​currentrow+=-​1
​currentcol+=-​1
​return ​dstring

PSEUDOCODE:

// Initialization
Input the two sequences: s1 and s2
N = length of s1
M = length of s2
matrix = array of size[N+1][M+1]
gap = gap score
mismatch = mismatch score
match = match score
// Step 1: Build Alignment Matrix
set matrix[0,0] to 0
for each i from 1 to N, inclusive
matrix[i, 0] = matrix[i-1, 0] + gap
for each j from 1 to M, inclusive
matrix[0, j] = matrix[0, j-1] + gap
for each i from 1 to N, inclusive
for each j from 1 to M, inclusive
if (s1[i-1] equals s2[j-1])
score1 = matrix[i-1, j-1] + match
else
socre1 = matrix[i-1, j-1] + mismatch
score2 = matrix[i, j-1] + gap
score3 = matrix[i-1, j] + gap
matrix[i, j] = max(score1, score2, score3)
// Step 2: Create directional strings
dstring = buildDirectionalString(matrix, N, M)
// Step 3: Build alignments using directional strings
seq1pos = N-1 //position of last character in seq1
seq2pos = M-1 //position of last character in seq2
dirpos = 0
while (dirpos < length of directional string)
if (dstring[dirpos] equals 'D')
align s1[seq1pos] and s2[seq2pos]
subtract 1 from seq1pos and seq2pos
else if (dstring[dirpos] equals 'V')
align s1[seq1pos] and a gap
subtract 1 from seq1pos
else // must be an H
align s2[seq2pos] and a gap
subtract 1 from seq2pos
increment dirpos
//Function to directional string
function buildDirectionalString(matrix, N, M)
dstring = ''
currentrow = N
currentcol = M
while (currentrow != 0 or currentcol != 0)
if (currentrow is 0)
add 'H' to dstring
subtract 1 from currentcol
else if (currentcol is 0)
add 'V' to dstring
subtract 1 from currentrow
else if (matrix[currentrow, currentcol-1] + gap equals matrix[currentrow,
currentcol])
add 'H' to dstring
subtract 1 from currentcol
else if (matrix[currentrow-1, currentcol] + gap equals matrix[currentrow,
currentcol])
add 'V' to dstring
subtract 1 from currentrow
else
add 'D' to dstring
subtract 1 from currentrow
subtract 1 from currentcol
return dstring

You might also like