Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- import scipy
- from string import *
- #return value of each variation
- def Diagonal(n1, n2, pt):
- if (n1 == n2):
- return pt['MATCH']
- else:
- return pt['MISMATCH']
- #the function gets the optional elements of the alignment matrix and returns pointer's elements
- def Pointers( di, ho, ve):
- pointer = max(di, ho, ve)
- if (di == pointer):
- return 'D'
- elif (ho == pointer):
- return 'H'
- else:
- return 'V'
- def NW(s1, s2, match = 1, mismatch = -1, gap = -2):
- penalty = { 'MATCH': match, 'MISMATCH' : mismatch, 'GAP': gap}
- n = len(s1) + 1 #dimension of the matrix columns
- m = len(s2) + 1 #dimension of the matrix rows
- al_mat = numpy.zeros((m, n), dtype = int) # alignments matrix with zeros
- p_mat = numpy.zeros((m, n), dtype = str) # alignments matrix with zeros
- # checking on gaps
- for i in range(m):
- al_mat[i][0] = penalty['GAP'] * i
- p_mat[i][0] = 'V'
- for j in range(n):
- al_mat[0][j] = penalty['GAP'] * j
- p_mat[0][j] = 'H'
- p_mat[0][0] = 0 #return the first element of pointer matrix back to 0
- for i in range(1, m):
- for j in range(1, n):
- di = al_mat[i - 1][j - 1] + Diagonal(s1[j - 1], s2[i - 1], penalty)
- #value of matching on diagonal
- ho = al_mat[i][j - 1] + penalty['GAP']
- #horizontal
- ve = al_mat[i - 1][j] + penalty['GAP']
- #vertical
- al_mat[i][j] = max(di, ho, ve)
- p_mat[i][j] = Pointers(di, ho, ve)
- f1 = open('output1.txt', 'w')
- f2 = open('output2.txt', 'w')
- f1.write(numpy.matrix(al_mat))
- f2.write(numpy.matrix(p_mat))
- NW('TCCCCCCCCGGGGGGGGGGAAAAAAAAAAAAAAAATCGATCGATCGAGGATCGA', 'TCGGAGAGAGGGAGAGCGGCAGCAGCGAGCAGTTTTTTT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement