Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. import turtle
  2. from folding import score
  3.  
  4. def create_chains_base(path):
  5. """
  6. Reads 'chains' of amino acids from file
  7. and creates a nested list, with an
  8. element==one chain of amino acid
  9. length of the list == number of chains
  10. :param path: path to the data-file
  11. :return: nested list of chains
  12. """
  13. def read_from_file():
  14. global mystring
  15. mystring = ''
  16. myfile = open(path)
  17. while True:
  18. mystring += myfile.readline()
  19. if len(myfile.readline())==0:
  20. break
  21.  
  22. def list_the_string():
  23. global listofvecs, mylist
  24. listofvecs = []
  25. mylist = mystring.split("\n")
  26. for c in range(0,len(mylist)-1):
  27. listofvecs.append(list(mylist[c]))
  28.  
  29. read_from_file()
  30. list_the_string()
  31. return listofvecs
  32.  
  33. def visualize(chain,folding):
  34. """
  35. visualising the fold of an amino-acid
  36. chain. (requires turtle.py)
  37. :param chain: a chain of amino-acid
  38. :param folding: vector of positioning of the
  39. next element of chain
  40. :return: None
  41. Graphicaly visualising the folding of
  42. amino-acid chains.
  43. """
  44.  
  45. def create_screen():
  46. global bubble, wn
  47. wn = turtle.Screen()
  48. wn.bgcolor("white")
  49. wn.title('Visualisation of folding of amino-acids')
  50. bubble = turtle.Turtle()
  51. if len(chain)>20:
  52. wn.screensize(2500,2500)
  53. bubble.speed(1000)
  54. bubble.hideturtle()
  55. bubble.speed(1)
  56.  
  57. def draw_first_bubble():
  58. bubble.color("blue")
  59. bubble.dot(10)
  60. bubble.forward(30)
  61.  
  62. def draw_bubbles():
  63. p = 0
  64. c = 0
  65. draw_first_bubble()
  66. for i in chain:
  67. if i=='0' or i==0:
  68. bubble.color('blue')
  69. else:
  70. bubble.color('red')
  71. bubble.begin_fill()
  72. bubble.dot(10)
  73. if p!=len(folding):
  74. if folding[c]==1:
  75. bubble.seth(0)
  76. if folding[c]==1j:
  77. bubble.seth(90)
  78. if folding[c]==-1:
  79. bubble.seth(180)
  80. if folding[c]==-1j:
  81. bubble.seth(270)
  82. c+=1
  83. p+=1
  84. if p!=(len(chain)):
  85. bubble.forward(30)
  86. bubble.end_fill()
  87.  
  88. create_screen()
  89. draw_bubbles()
  90. wn.mainloop()
  91.  
  92. def solver(chain):
  93. """
  94. Returns the possible 'best' folding of amino-acid
  95. :param chain: current chain of amino-acid
  96. :return: folding of amino-acid
  97. """
  98. folding=[]
  99. coords = []
  100. N = len(chain)*len(chain)
  101. X_cord = len(chain)
  102. Y_cord = len(chain)
  103.  
  104. for i in range(0,len(chain)-1):
  105. folding.append(1)
  106.  
  107. for i in range(0,N):
  108. coords.append([0] * (N))
  109.  
  110.  
  111. for j in range(0,len(chain)-1):
  112. for i in range(0,len(chain)-1):
  113. basic_score = score(chain,folding)
  114. folding[i] = 1j
  115. if score(chain,folding)<=basic_score and coords[X_cord][Y_cord+1]!=1:
  116. continue
  117. folding[i] = -1j
  118. if score(chain,folding)<=basic_score and coords[X_cord][Y_cord-1]!=1:
  119. continue
  120. folding[i] = -1
  121. if score(chain,folding)<=basic_score and coords[X_cord-1][Y_cord]!=1:
  122. continue
  123. folding[i] = 1
  124. if folding[i] == 1j:
  125. coords[X_cord][Y_cord+1] = 1
  126. Y_cord+=1
  127. if folding[i] == -1j:
  128. coords[X_cord][Y_cord-1] = 1
  129. Y_cord-=1
  130. if folding[i] == -1:
  131. coords[X_cord-1][Y_cord] = 1
  132. Y_cord-=1
  133. if folding[i] == 1:
  134. coords[X_cord+1][Y_cord] = 1
  135. X_cord+=1
  136.  
  137.  
  138. for c in coords:
  139. for i in coords:
  140. print(c)
  141. print('\n')
  142.  
  143. return folding
  144.  
  145. def create_foldings_base(chains):
  146. """
  147. Takes all foldings together is a list
  148. :param chains:list of all chains of amino-acids
  149. :return: list of all foldings of amino-acids
  150. """
  151. global foldings
  152. foldings=[]
  153. for chain in chains:
  154. foldings.append(solver(chain))
  155. return foldings
  156.  
  157. def scoring(chains,foldings):
  158. """
  159. Sums the scores of all chain-folding pairs
  160. :param chains: list of all chains of amino-acids
  161. :param foldings: list of all foldings of amino-acids
  162. :return: result of writing this code
  163. """
  164. sum=0
  165. for i in range(0,5):
  166. sum+=score(chains[i],foldings[i])
  167. return sum/len(chains)
  168.  
  169.  
  170. visualize([0,1,1,1,1,0,],solver([0,1,1,1,1,0]))
  171. #chains = create_chains_base("testsuite.txt")
  172. #foldings = create_foldings_base(chains[:5])
  173. #print("%.1f" % scoring(chains,foldings))
  174. #print(foldings)
  175. #print(foldings)
  176. #print(chains)
  177. #visualize(chains[3],foldings[3])
  178. #visualize([0, 1, 1, 1, 1, 0],solver([0,1,1,1,1,0,]))
  179. #print(score([0, 1, 1, 1, 1, 0],[1, 1, 1j, -1, -1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement