Advertisement
CasualGamer

Needleman Wusch

Jan 5th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. import numpy as np
  2.  
  3. from cell import cell
  4.  
  5. def alignment_nw(a,b):
  6.     if a==b:
  7.         return 1
  8.     else:
  9.         return -1
  10.  
  11. def recursion(paths,D):
  12.     new_paths = []
  13.     returnvalue = paths
  14.     keep_going = False
  15.     for path in paths:
  16.         pointers = (path[-1]).get_pointers()
  17.         for pointer in pointers:
  18.             if pointers.size==0:
  19.                 continue
  20.             keep_going = True
  21.             indices = np.where(D == pointer)
  22.             path = np.append(path,D[indices])
  23.             if len(new_paths) == 0:
  24.                 new_paths = [path]
  25.             else:
  26.                 new_paths.append(path)
  27.     if keep_going:
  28.         returnvalue=recursion(new_paths,D)
  29.     return returnvalue
  30.  
  31. def main(A,B):
  32.     A = np.array([ a for a in A ])
  33.     B = np.array([ b for b in B ])
  34.     A = np.insert(A,0,'-')
  35.     B = np.insert(B,0,'-')
  36.     D = np.array([[cell() for i in range(B.size)] for j in range(A.size)])
  37.     for i in range(1,A.size):
  38.         D[i][0].set_value(-i)
  39.     for i in range(1,B.size):
  40.         D[0][i].set_value(-i)
  41.     for i in range(1,A.size):
  42.         for j in range(1,B.size):
  43.             candidates = [D[i-1][j-1].get_value()+alignment_nw(A[i],B[j]), D[i-1][j].get_value()-1,D[i][j-1].get_value()-1]
  44.             index_array = np.argwhere(candidates == np.amax(candidates)).flatten().tolist()
  45.             D[i][j].set_value(candidates[index_array[-1]])
  46.             for k in index_array:
  47.                 if k == 0:
  48.                     D[i][j].add_pointer(D[i-1][j-1])
  49.                 elif k == 1:
  50.                     D[i][j].add_pointer(D[i-1][j])
  51.                 elif k == 2:
  52.                     D[i][j].add_pointer(D[i][j-1])
  53.     print(D)
  54.     #Tracing arrows back
  55.     path = np.array([[D[-1][-1]]])
  56.     paths = recursion(path,D)
  57.     print(paths)
  58.  
  59.     previous_step_indices = np.array([-1,-1])
  60.     alignment = ['','']
  61.     score = 0
  62.     for id,path in enumerate(paths):
  63.         for step in path:
  64.             indices = np.where(D == step)
  65.             if(previous_step_indices[0]==-1):
  66.                 previous_step_indices=indices
  67.                 continue
  68.             elif indices[0]<previous_step_indices[0] and indices[1]<previous_step_indices[1]:
  69.                 alignment = [A[previous_step_indices[0][-1]]+alignment[0],B[previous_step_indices[1][-1]]+alignment[1]]
  70.                 if A[previous_step_indices[0][-1]]==B[previous_step_indices[1][-1]]:
  71.                     score=score+1
  72.                 else:
  73.                     score=score-1
  74.             elif indices[0]<previous_step_indices[0]:
  75.                 alignment = [A[previous_step_indices[0][-1]]+alignment[0],'-'+alignment[1]]
  76.                 score=score-1
  77.             elif indices[1]<previous_step_indices[1]:
  78.                 alignment = ['-'+alignment[0],B[previous_step_indices[1][-1]]+alignment[1]]# np.char.add(['-',B[indices[1]]],alignment)#('-'+alignment[0],B[indices[1]]+alignment[1])
  79.                 score=score-1
  80.             previous_step_indices=indices
  81.         print('\npath number: {} score: {}\n{}\n{}\n'.format(id+1,score,alignment[0],alignment[1]))
  82.         alignment = ['','']
  83.         score=0
  84.     return
  85.  
  86. main('ACGTC','AGTCA')
  87. #main('GCATGCU','GATTACA')
  88.  
  89.  
  90.  
  91.  
  92. ###################BACKUP#######################
  93.  
  94. #def main(A,B):
  95. #    A = np.array([ a for a in A ])
  96. #    B = np.array([ b for b in B ])
  97. #    A = np.insert(A,0,'-')
  98. #    B = np.insert(B,0,'-')
  99. #    cell = np.dtype([('symbol', np.unicode_, 3), ('value', np.int32)])
  100. #    D = np.zeros((A.size,B.size),dtype=cell)#([('H', 1), ('V', 0)], dtype=cell)
  101. #    for i in range(1,A.size):
  102. #        D[i][0]=('',-i)
  103. #    for i in range(1,B.size):
  104. #        D[0][i]=('',-i)
  105. #    for i in range(1,A.size):
  106. #        for j in range(1,B.size):
  107. #            candidates = [D[i-1][j-1]['value']+alignment_nw(A[i],B[j]), D[i-1][j]['value']-1,D[i][j-1]['value']-1]
  108. #            #print('intex i: {}, j: {}, candidates: {}'.format(i,j,candidates))
  109. #            index_array = np.argwhere(candidates == np.amax(candidates)).flatten().tolist()
  110. #            print(index_array)
  111. #            D[i][j]['value']=candidates[index_array[-1]]
  112. #            for k in index_array:
  113. #                if k == 0:
  114. #                    D[i][j]['symbol']+="\\"
  115. #                elif k == 1:
  116. #                    D[i][j]['symbol']+='^'
  117. #                elif k == 2:
  118. #                    D[i][j]['symbol']+='<'
  119. #            #print(index_array)
  120. #            #print('A[i-1]: {},B[j-1]: {},B[j]: {})'.format(A[i-1],B[j-1],B[j]))
  121. #    print(D)
  122. #    #Tracing arrows back
  123. #    print(D[3][4]['symbol'])
  124. #    return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement