/samples/mapk/second_phos_ratio.py

https://github.com/ecell/epdp
Python | 281 lines | 160 code | 85 blank | 36 comment | 47 complexity | 716229caca9c6c54ff0dfe55fbc7cec2 MD5 | raw file
  1. #!/usr/bin/env python
  2. import sys
  3. class Particle:
  4. def __init__( self, particle ):
  5. self.speciesName = particle[0]
  6. self.id = particle[1]
  7. def __eq__( self, other ):
  8. return self.speciesName == other.speciesName and self.id == other.id
  9. def __str__( self ):
  10. return "( '%s', %d )" % ( self.speciesName, self.id )
  11. def __repr__( self ):
  12. return self.__str__()
  13. def __hash__( self ):
  14. return hash( self.speciesName ) ^ self.id
  15. class ReactionEvent:
  16. def __init__( self, t, reactants, products ):
  17. self.t = t
  18. self.reactants = reactants
  19. self.products = products
  20. def load_reactions( file ):
  21. reactions = []
  22. for line in file.readlines():
  23. line = eval( line )
  24. t = line[0]
  25. reactants = [ Particle( p ) for p in line[1] ]
  26. products = [ Particle( p ) for p in line[2] ]
  27. reactions.append( ReactionEvent( t, reactants, products ) )
  28. return reactions
  29. def rebind_ratio( reactions ):
  30. KpCreated = {}
  31. KpKK = {}
  32. KpCurrentForm = {}
  33. CurrentFormKp = {}
  34. KKCurrentForm = {}
  35. CurrentFormKK = {}
  36. counter = 0
  37. lasttime = 0
  38. for r in reactions:
  39. #print r.t, r.reactants, r.products
  40. lasttime = r.t
  41. # unbinding
  42. if len( r.reactants ) == 1 and len( r.products ) == 2:
  43. # K_KK -> Kp + KKi
  44. # Kp first site phosphorylated.
  45. if r.reactants[0].speciesName == 'K_KK':
  46. for i, p in enumerate( r.products ):
  47. if p.speciesName == 'Kp':
  48. Kp = p
  49. KKi = r.products[ 1 - i ]
  50. if KKi.speciesName == 'KKi':
  51. #print r.reactants[0], '->', Kp, KKi
  52. KpCreated[Kp] = r.t
  53. KpKK[Kp] = KKi
  54. KpCurrentForm[Kp] = Kp
  55. CurrentFormKp[Kp] = Kp
  56. KKCurrentForm[KKi] = KKi
  57. CurrentFormKK[KKi] = KKi
  58. elif r.reactants[0].speciesName == 'Kp_KK':
  59. Kp_KK = r.reactants[0]
  60. # Kp_KK -> Kp + KK
  61. for i, p in enumerate( r.products ):
  62. if p.speciesName == 'Kp':
  63. Kp = p
  64. KK = r.products[ 1 - i ]
  65. if KK.speciesName == 'KK':
  66. #print r.reactants[0], '->', Kp, KK
  67. if CurrentFormKp.has_key(Kp_KK):
  68. originalKp = CurrentFormKp[Kp_KK]
  69. KpCurrentForm[originalKp] = Kp
  70. del CurrentFormKp[Kp_KK]
  71. CurrentFormKp[Kp] = originalKp
  72. if CurrentFormKK.has_key(Kp_KK):
  73. originalKK = CurrentFormKK[Kp_KK]
  74. KKCurrentForm[originalKK] = KK
  75. del CurrentFormKK[Kp_KK]
  76. CurrentFormKK[KK] = originalKK
  77. break
  78. # Kp_KK -> Kpp + KKi
  79. for i, p in enumerate( r.products ):
  80. if p.speciesName == 'Kpp':
  81. Kpp = p
  82. KKi = r.products[ 1 - i ]
  83. if KKi.speciesName == 'KKi':
  84. #print r.reactants[0], '->', Kpp, KKi
  85. originalKp=None
  86. originalKK=None
  87. if CurrentFormKK.has_key(Kp_KK):
  88. originalKK = CurrentFormKK[Kp_KK]
  89. del KKCurrentForm[originalKK]
  90. del CurrentFormKK[Kp_KK]
  91. if CurrentFormKp.has_key( Kp_KK ):
  92. originalKp = CurrentFormKp[Kp_KK]
  93. del KpCurrentForm[originalKp]
  94. del CurrentFormKp[Kp_KK]
  95. #print originalKp
  96. # second *phosphorylation*
  97. t_create = KpCreated[originalKp]
  98. t = r.t - t_create
  99. partner = KpKK[originalKp]
  100. counter += 1
  101. if originalKK is not None and originalKK == partner:
  102. outfile.write( '%.18g\trebinding\n' % t )
  103. else:
  104. outfile.write( '%.18g\tdiffusion\n' % t )
  105. del KpCreated[originalKp]
  106. del KpKK[originalKp]
  107. # binding
  108. elif len( r.reactants ) == 2 and len( r.products ) == 1:
  109. # Kp + KK -> Kp_KK
  110. for i, p in enumerate( r.reactants ):
  111. if p.speciesName == 'Kp':
  112. Kp = p
  113. KK = r.reactants[ 1 - i ]
  114. if KK.speciesName == 'KK':
  115. Kp_KK = r.products[0]
  116. assert Kp_KK.speciesName == 'Kp_KK'
  117. #print Kp, KK, '->', Kp_KK
  118. if not CurrentFormKp.has_key( Kp ):
  119. break
  120. originalKp = CurrentFormKp[Kp]
  121. KpCurrentForm[originalKp] = Kp_KK
  122. del CurrentFormKp[Kp]
  123. CurrentFormKp[Kp_KK] = originalKp
  124. if CurrentFormKK.has_key(KK):
  125. originalKK = CurrentFormKK[KK]
  126. KKCurrentForm[originalKK] = Kp_KK
  127. del CurrentFormKK[KK]
  128. CurrentFormKK[Kp_KK] = originalKK
  129. else:
  130. originalKK = None
  131. if KpCreated.has_key(originalKp):
  132. pass
  133. # # second *association*
  134. # t_create = KpCreated[originalKp]
  135. # t = r.t - t_create
  136. # partner = KpKK[originalKp]
  137. # if originalKK is not None and originalKK == partner:
  138. # outfile.write( '%.18g\trebinding\n' % t )
  139. # else:
  140. # outfile.write( '%.18g\tdiffusion\n' % t )
  141. # counter += 1
  142. # del KpCreated[originalKp]
  143. # del KpKK[originalKp]
  144. break
  145. # Kp + P -> Kp_P
  146. for i, p in enumerate( r.reactants ):
  147. if p.speciesName == 'Kp':
  148. Kp = p
  149. P = r.reactants[ 1 - i ]
  150. if P.speciesName == 'P':
  151. Kp_P = r.products[0]
  152. assert Kp_P.speciesName == 'Kp_P'
  153. if not CurrentFormKp.has_key( Kp ):
  154. break
  155. originalKp = CurrentFormKp[Kp]
  156. KpCurrentForm[originalKp] = Kp_P
  157. del CurrentFormKp[Kp]
  158. CurrentFormKp[Kp_P] = originalKp
  159. if KpCreated.has_key(originalKp):
  160. # pass
  161. del KpCreated[originalKp]
  162. del KpKK[originalKp]
  163. break
  164. # monomolecular
  165. elif len( r.reactants ) == 1 and len( r.products ) == 1:
  166. if CurrentFormKK.has_key( r.reactants[0] ):
  167. originalform = CurrentFormKK[ r.reactants[0] ]
  168. KKCurrentForm[ originalform ] = r.products[0]
  169. del CurrentFormKK[ r.reactants[0] ]
  170. CurrentFormKK[ r.products[0] ] = originalform
  171. #print 'transition', r.reactants[0], '->', r.products[0]
  172. #
  173. # *second association* needs the following
  174. #
  175. import numpy
  176. nonreactions = [ t for t in KpCreated.values() if lasttime-t > 10]
  177. print 'reactions: ', counter, 'non-reactions', len(nonreactions)
  178. # for t in nonreactions:
  179. # #if t > 60:
  180. # outfile.write( '%.18g\tno-reaction\n' % numpy.inf )
  181. if __name__ == '__main__':
  182. import sys
  183. import os
  184. import glob
  185. for pattern in sys.argv[1:]:
  186. globpattern = pattern.replace('ALL','*')
  187. l = os.path.basename( os.path.splitext( pattern )[0] )
  188. outfilename = l + '.rebind'
  189. outfile = open( outfilename, 'w' )
  190. print >> sys.stderr, 'pattern ', l, '\noutfile', outfilename
  191. filelist = glob.glob( globpattern )
  192. for file in filelist:
  193. reactions = load_reactions( open( file ) )
  194. print >> sys.stderr, 'num reactions: ', len( reactions )
  195. rebind_ratio( reactions )
  196. outfile.close()