Advertisement
reeps

Table

May 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. import numpy
  2. import scipy
  3. from string import *
  4.  
  5. #return value of each variation
  6. def Diagonal(n1, n2, pt):
  7.     if (n1 == n2):
  8.         return pt['MATCH']
  9.     else:
  10.         return pt['MISMATCH']
  11. #the function gets the optional elements of the alignment matrix and returns pointer's elements
  12. def Pointers( di, ho, ve):
  13.     pointer = max(di, ho, ve)
  14.     if (di == pointer):
  15.         return 'D'
  16.     elif (ho == pointer):
  17.         return 'H'
  18.     else:
  19.         return 'V'
  20.  
  21. def NW(s1, s2, match = 1, mismatch = -1, gap = -2):
  22.     penalty = { 'MATCH': match, 'MISMATCH' : mismatch, 'GAP': gap}
  23.     n = len(s1) + 1 #dimension of the matrix columns
  24.     m = len(s2) + 1  #dimension of the matrix rows
  25.     al_mat = numpy.zeros((m, n), dtype = int) # alignments matrix with zeros
  26.     p_mat = numpy.zeros((m, n), dtype = str) # alignments matrix with zeros
  27. # checking on gaps
  28.     for i in range(m):
  29.         al_mat[i][0] = penalty['GAP'] * i
  30.         p_mat[i][0] = 'V'
  31.     for j in range(n):
  32.         al_mat[0][j] = penalty['GAP'] * j
  33.         p_mat[0][j] = 'H'
  34.     p_mat[0][0] = 0 #return the first element of pointer matrix back to 0
  35.     for i in range(1, m):
  36.         for j in range(1, n):
  37.             di = al_mat[i - 1][j - 1] + Diagonal(s1[j - 1], s2[i - 1], penalty)
  38. #value of matching on diagonal
  39.             ho = al_mat[i][j - 1] + penalty['GAP']
  40.             #horizontal
  41.             ve = al_mat[i - 1][j] + penalty['GAP']
  42.             #vertical
  43.             al_mat[i][j] = max(di, ho, ve)
  44.             p_mat[i][j] = Pointers(di, ho, ve)
  45.     f1 = open('output1.txt', 'w')
  46.     f2 = open('output2.txt', 'w')
  47.    
  48.     f1.write(numpy.matrix(al_mat))
  49.     f2.write(numpy.matrix(p_mat))
  50.  
  51. NW('TCCCCCCCCGGGGGGGGGGAAAAAAAAAAAAAAAATCGATCGATCGAGGATCGA', 'TCGGAGAGAGGGAGAGCGGCAGCAGCGAGCAGTTTTTTT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement