Godoakos

Untitled

Nov 2nd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. import copy as cp
  2. import re
  3.  
  4. alph = list()
  5. blosum = list()
  6. mat = list()
  7. tb = list()
  8. T = 40
  9. _seq = ""
  10. _dom = ""
  11.  
  12.  
  13. with open("blosum62.txt","r") as blofile:
  14.     alph = re.findall("\w",blofile.readline())
  15.     for line in blofile.readlines():
  16.         blosum.append(re.findall("\-?\d+", line))
  17. gapval = int(blosum[len(alph)][len(alph)-1])
  18.  
  19. def nws(s): #ez csúnya. nagyon.
  20.     ns = ""
  21.     for i in s:
  22.         if i == '\r' or i == '\n':
  23.             pass
  24.         else:
  25.             ns+=i
  26.     return ns
  27.  
  28. def makemat(seq,dom,mat):
  29.     zeroes = list()
  30.     for i in range(len(seq)+1):
  31.         zeroes.append(0)
  32.     for j in range(len(dom)+1):
  33.         mat.append(cp.deepcopy(zeroes))
  34.  
  35. def printmat(mat):
  36.     for i in range(len(mat)):
  37.         print mat[i]
  38.  
  39. def fillval(i,j,s,d): #sor,oszlop,seq,dom
  40.     return max(mat[i-1][j-1]+int(blosum[s][d]),mat[i-1][j]+int(gapval),mat[i][j-1]+int(gapval),max(mat[0][j-1],mat[i][j-1]-T))
  41.  
  42. def fill_line(i,j):
  43.     return max(mat[0][j-1],mat[i][j-1]-T)
  44.  
  45. def fillmat(seq,dom):
  46.     for i in range(1,len(mat)): #sor
  47.         for j in range(1,len(mat[i])): #oszlop
  48.             mat[i][j] = fillval(i,j,alph.index(seq[j-1]),alph.index(dom[i-1]))
  49.  
  50. def traceback(i,j):
  51.     s = ""
  52.     dd = ""
  53.     while i>0 and j>0:
  54.         l = mat[i][j-1]
  55.         u = mat[i-1][j]
  56.         d = mat[i-1][j-1]
  57.         if max(l,u,d) == l:
  58.             s = _seq[j-1] + s
  59.             dd = '-' + dd
  60.             j = j-1
  61.             tb[i][j] = 1
  62.         elif max(l,u,d) == u:
  63.  
  64.             s = _seq[j-1] + s
  65.             dd = '.' + dd
  66.             i = i-1
  67.             tb[i][j] = 1
  68.         elif max(l,u,d) == d:
  69.  
  70.             s = _seq[j-1] + s
  71.             dd = _dom[i-1] + dd
  72.             i = i-1
  73.             j = j-1
  74.             tb[i][j] = 1
  75.     print s
  76.     print dd
  77.     print "////////////////////////////"
  78.  
  79.  
  80. def getmax(j):
  81.     for i in range(len(mat)):
  82.         mval = max(mat[0][j-1],mat[i][j-1]-T)
  83.     return mval
  84.  
  85.            
  86. def findmax(j,val):
  87.     for o in range(j,0,-1):
  88.         for s in range(len(mat)):
  89.             if mat[s][o-1] == val+T:
  90.                 return [s,o]
  91.     mval = [0,0,0]
  92.     for i in range(len(mat)):
  93.         for j in range(len(mat[i])):
  94.             if mval[0]<mat[i][j]:
  95.                 mval = [mat[i][j],i,j]
  96.     return [mval[1],mval[2]]
  97.  
  98.  
  99.    
  100. def tbproper(coords,dom):
  101.     while coords[0]>0 and coords[1]>0:
  102.         l = mat[coords[0]][coords[1]-1]
  103.         u = mat[coords[0]-1][coords[1]]
  104.         d = mat[coords[0]-1][coords[1]-1]
  105.         if max(l,u,d) == l:
  106.             coords[1] = coords[1]-1
  107.             dom = '-' + dom
  108.             tb[coords[0]][coords[1]] = 1 #jobbról jött
  109.         elif max(l,u,d) == u:
  110.             coords[0] = coords[0]-1
  111.             tb[coords[0]][coords[1]] = 2
  112.             dom = '.' + dom #lentről
  113.         elif max(l,u,d) == d:
  114.             coords[0] = coords[0]-1
  115.             coords[1] = coords[1]-1
  116.             tb[coords[0]][coords[1]] = 3
  117.             dom = _dom[coords[0]]+dom #átlósan
  118.     if coords[0] ==0 and coords[1]!=0: #vagyis csak a tetejét értük el
  119.         print "called again at " + str(coords[0]) +'-'+ str(coords[1])
  120.         tbproper(findmax(coords[1],getmax(coords[1])),dom) #for the lulz
  121.     return dom
  122.  
  123. def move(crd,dom):
  124.     _outdom = ""
  125.     i = crd[0]
  126.     j = crd[1]
  127.     while i<len(mat)-1 and j<len(mat[i])-1 and tb[i][j]!=0:
  128.         print [i,j]
  129.         if tb[i][j] == 1:
  130.             print "j"
  131.             j = j+1
  132.             _outdom = '-' + _outdom
  133.         if tb[i][j] == 2:
  134.             print "l"
  135.             i = i+1
  136.             _outdom = '.' + _outdom
  137.         if tb[i][j] == 3:
  138.             print "á"
  139.             _outdom = dom[i-1] + _outdom
  140.             i = i + 1
  141.             j = j + 1
  142.     print _outdom
  143.    
  144. def reconstr(dom):
  145.     for i in range(len(mat)):
  146.         if tb[i][0] != 0:
  147.             print "let's do this shit"
  148.             move([i,0],dom)
  149.            
  150.  
  151. #INTMÉJN
  152. with open("q2tay7.fasta") as seqfile, open("q2tay7_domain.fasta") as domfile:
  153.     seqfile.readline()
  154.     domfile.readline()
  155.     _seq = nws(seqfile.read())
  156.     _dom = nws(domfile.read())
  157. #    _seq = "HEAGAWGHEE"
  158. #    _dom = "PAWHEAE"
  159.    
  160. makemat(_seq, _dom,mat)
  161. makemat(_seq, _dom,tb)
  162. fillmat(_seq,_dom)
  163. print getmax(len(mat[0]))
  164. _crd = findmax(len(mat[0]),getmax(len(mat[0])))
  165. print _crd
  166. _od = ""
  167. print _seq
  168. #print tbproper(_crd,_od)
  169. traceback(_crd[0],_crd[1])
  170. #printmat(tb)
  171. #reconstr(_od)
Advertisement
Add Comment
Please, Sign In to add comment