Guest User

Untitled

a guest
Jan 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.84 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "metadata": {},
  5. "cell_type": "markdown",
  6. "source": "# SBML to Boltzmann"
  7. },
  8. {
  9. "metadata": {
  10. "collapsed": true,
  11. "trusted": true
  12. },
  13. "cell_type": "code",
  14. "source": "import re\nclass sbml2boltzmann:\n def __init__( self, sbml, deltag ):\n self.sbml = sbml\n self.deltag = deltag\n self.ordRE = re.compile('__([0-9]+)__')\n def repl( self, matchobj ):\n return chr(int(matchobj.group(1)))\n \n def unmunge( self, sbmlid, prefix='M_' ):\n return self.ordRE.sub( self.repl, sbmlid[len(prefix):])\n \n def get_met_comp( self, speciesId ):\n s = self.sbml.getSpecies(speciesId)\n if s.getCompartment() == 'CCO__45__CYTOSOL':\n return self.unmunge( s.getId() + ':' + s.getCompartment() )\n else:\n return self.unmunge( s.getId()).replace('[',':').rstrip(']')\n \n def write_compartment( self, filename ):\n with open(filename, 'w') as out:\n i = 1\n for comp in self.sbml.getListOfCompartments():\n out.write('{}\\t{}\\tliters\\t0\\tV\\n'.format(self.unmunge(comp.getId()), float(i)))\n i +=1\n def write_concentrations( self, filename, volume=1.0e-15, conc_units=1.0e-9, default_conc=1.1111111e-01):\n with open(filename, 'w') as out:\n out.write('VOLUME\\t{}\\n'.format(volume))\n out.write('CONC_UNITS\\t{}\\n'.format(conc_units))\n for species in self.sbml.getListOfSpecies():\n if species.getBoundaryCondition():\n boundary = 'F'\n else:\n boundary = 'V'\n out.write('{}\\t{}\\t{}\\n'.format(self.get_met_comp(species.getId()), default_conc, boundary))\n \n def getDeltaG( self, rxnName ):\n if rxnName in self.deltag:\n return self.deltag[rxnName]\n else:\n return 0.0\n def write_reactions( self, filename):\n with open(filename, 'w') as out:\n for rxn in self.sbml.getListOfReactions():\n out.write('REACTION\\t{}\\n'.format(rxn.getName()))\n out.write('LEFT\\t{}\\n'.format(' + '.join([self.get_met_comp(sr.getSpecies()) \n for sr in rxn.getListOfReactants()])))\n out.write('RIGHT\\t{}\\n'.format(' + '.join([self.get_met_comp(sr.getSpecies()) \n for sr in rxn.getListOfProducts()])))\n out.write('DGZERO\\t{}\\n'.format(self.getDeltaG(rxn.getName())))\n out.write('DGZERO_UNITS\\t{}\\n'.format('KCAL/MOL'))\n out.write('//\\n')\n \ns2b = sbml2boltzmann(m, deltag['DeltaG(pH7.3)'].to_dict())\ns2b.get_met_comp('M_CPD__45__548')\ns2b.write_compartment('nc12.cmpts')\ns2b.write_concentrations('nc12.concs.in')\ns2b.write_reactions('nc12.rxns.dat')",
  15. "execution_count": null,
  16. "outputs": []
  17. },
  18. {
  19. "metadata": {},
  20. "cell_type": "markdown",
  21. "source": "# Boltzmann to COBRA"
  22. },
  23. {
  24. "metadata": {
  25. "ExecuteTime": {
  26. "end_time": "2018-01-03T20:39:33.081966Z",
  27. "start_time": "2018-01-03T20:39:32.857510Z"
  28. },
  29. "trusted": true,
  30. "collapsed": true
  31. },
  32. "cell_type": "code",
  33. "source": "import cobra, os,re\nfrom fractions import Fraction\ndef char2ord( matchobj ):\n return '__{}__'.format( ord(matchobj.group(0)) )\nclass boltzmann2cobra:\n def __init__( self ):\n self.model = cobra.Model()\n self.metabolites = set()\n \n \n def munge( self, Id, prefix='M_', regexp=r'[^A-Za-z0-9_]' ):\n return prefix + re.sub(regexp, char2ord, Id )\n \n def dat2model( self, datfile ):\n attributes = ['REACTION','LEFT','RIGHT','LEFT_COMPARTMENT','RIGHT_COMPARTMENT','ENZYME_LEVEL','NREGULATION','COMMENT', 'COMMENTS','PATHWAY']\n with open(datfile) as dat:\n modelname = os.path.splitext(os.path.basename(datfile))[0]\n self.model.id = self.munge(modelname)\n self.model.name = modelname\n rxns = []\n rxn = {}\n for line in dat:\n if line.strip() != '//' and line[0] != '#':\n cols = line.strip().split('\\t')\n if len(cols) >= 2:\n attribute, value = cols[0].strip(), cols[1].strip()\n if attribute in attributes:\n if attribute in rxn:\n rxn[attribute] += value\n else:\n rxn[attribute] = value\n elif cols[0] == 'COMMENT':\n pass\n else:\n print(line)\n print(rxn)\n elif line.strip() == '//':\n if 'REACTION' not in rxn:\n print(rxn)\n reaction = self.create_reaction( rxn )\n rxns.append(reaction)\n rxn = {}\n return self.model\n def parse_mixture( self, rxn ):\n pattern = re.compile(r'([0-9./]+\\s+)?(.*)')\n parts = {}\n for side in ['LEFT','RIGHT']:\n compartment = rxn['{}_COMPARTMENT'.format( side )]\n for part in rxn[side].split(' + '):\n m = pattern.search(part.strip())\n if m:\n if not m.group(1) or m.group(1) == '':\n stoichiometry = 1\n elif '.' in m.group(1):\n stoichiometry = float(m.group(1).strip())\n else:\n stoichiometry = int(m.group(1).strip())\n if side=='LEFT':\n stoichiometry = -stoichiometry\n species = m.group(2).strip()\n met = cobra.Metabolite(\"{}:{}\".format(species, compartment),name=species, compartment=compartment)\n if met not in self.model.metabolites:\n self.model.add_metabolites( [met] )\n parts[met] = stoichiometry\n return parts\n\n def create_reaction( self, rxn, prefix='', regexp='~'):\n reaction = cobra.Reaction()\n reaction.id = self.munge(rxn['REACTION'], 'R_')\n reaction.name = rxn['REACTION'] \n self.model.add_reactions([reaction])\n reaction.add_metabolites( self.parse_mixture( rxn ))\n return reaction\n",
  34. "execution_count": 100,
  35. "outputs": []
  36. },
  37. {
  38. "metadata": {
  39. "ExecuteTime": {
  40. "end_time": "2018-01-03T21:07:26.572784Z",
  41. "start_time": "2018-01-03T21:07:26.506942Z"
  42. },
  43. "scrolled": false,
  44. "trusted": true,
  45. "collapsed": true
  46. },
  47. "cell_type": "code",
  48. "source": "b2c = boltzmann2cobra()\nmodel = b2c.dat2model('neurospora_pentose_phos.glycolysis.tca.2.dat')\ncobra.io.save_json_model(model, 'neurospora_pentose_phos.glycolysis.tca.2.json',sort=True,pretty=True)",
  49. "execution_count": 102,
  50. "outputs": []
  51. },
  52. {
  53. "metadata": {
  54. "ExecuteTime": {
  55. "end_time": "2018-01-03T01:02:46.237996Z",
  56. "start_time": "2018-01-03T01:02:46.078149Z"
  57. },
  58. "trusted": true
  59. },
  60. "cell_type": "code",
  61. "source": "!head neurospora_pentose_phos.glycolysis.tca.2.dat",
  62. "execution_count": 9,
  63. "outputs": [
  64. {
  65. "name": "stdout",
  66. "output_type": "stream",
  67. "text": "REACTION ME1m\r\nLEFT\t(S)-MALATE + NAD+ \r\nRIGHT\tpyruvate + NADH + CO2\r\nLEFT_COMPARTMENT MITOCHONDRIA\r\nRIGHT_COMPARTMENT MITOCHONDRIA\r\nENZYME_LEVEL\t0.0\r\n//\r\nREACTION ME2m\r\nLEFT (S)-MALATE + NADP+\r\nRIGHT PYRUVATE + NADPH + CO2\r\n"
  68. }
  69. ]
  70. }
  71. ],
  72. "metadata": {
  73. "kernelspec": {
  74. "name": "python3",
  75. "display_name": "Python 3",
  76. "language": "python"
  77. },
  78. "language_info": {
  79. "name": "python",
  80. "version": "3.6.1",
  81. "mimetype": "text/x-python",
  82. "codemirror_mode": {
  83. "name": "ipython",
  84. "version": 3
  85. },
  86. "pygments_lexer": "ipython3",
  87. "nbconvert_exporter": "python",
  88. "file_extension": ".py"
  89. },
  90. "toc": {
  91. "threshold": 4,
  92. "number_sections": true,
  93. "toc_cell": false,
  94. "toc_window_display": false,
  95. "toc_section_display": "block",
  96. "sideBar": true,
  97. "navigate_menu": true,
  98. "moveMenuLeft": true,
  99. "widenNotebook": false,
  100. "colors": {
  101. "hover_highlight": "#DAA520",
  102. "selected_highlight": "#FFD700",
  103. "running_highlight": "#FF0000",
  104. "wrapper_background": "#FFFFFF",
  105. "sidebar_border": "#EEEEEE",
  106. "navigate_text": "#333333",
  107. "navigate_num": "#000000"
  108. },
  109. "nav_menu": {
  110. "height": "30px",
  111. "width": "252px"
  112. }
  113. },
  114. "varInspector": {
  115. "window_display": false,
  116. "cols": {
  117. "lenName": 16,
  118. "lenType": 16,
  119. "lenVar": 40
  120. },
  121. "kernels_config": {
  122. "python": {
  123. "library": "var_list.py",
  124. "delete_cmd_prefix": "del ",
  125. "delete_cmd_postfix": "",
  126. "varRefreshCmd": "print(var_dic_list())"
  127. },
  128. "r": {
  129. "library": "var_list.r",
  130. "delete_cmd_prefix": "rm(",
  131. "delete_cmd_postfix": ") ",
  132. "varRefreshCmd": "cat(var_dic_list()) "
  133. }
  134. },
  135. "types_to_exclude": [
  136. "module",
  137. "function",
  138. "builtin_function_or_method",
  139. "instance",
  140. "_Feature"
  141. ]
  142. },
  143. "gist": {
  144. "id": "",
  145. "data": {
  146. "description": "Neurospora/neurospora_no_regulation/MENTOS_comparison_with_boltzmann_on_neurospora.ipynb",
  147. "public": true
  148. }
  149. }
  150. },
  151. "nbformat": 4,
  152. "nbformat_minor": 2
  153. }
Add Comment
Please, Sign In to add comment