DFBA

This section describes how to run dynamic FBA (DFBA) with sbmlutils.


In [1]:
%matplotlib inline
%load_ext autoreload
%autoreload 2

In [2]:
import os
import sbmlutils
from sbmlutils import dfba
from sbmlutils.dfba import utils

Simulate DFBA model

To run an existing DFBA model call use simulate_dfba.


In [3]:
# get the absolute path to the top model
from sbmlutils.dfba.toy_wholecell import settings as toysettings
from sbmlutils.dfba.toy_wholecell import model_factory as toyfactory
from sbmlutils.dfba.toy_wholecell import simulate as toysimulate

sbml_path = os.path.join(utils.versioned_directory(toysettings.OUT_DIR, toysettings.VERSION), 
                         toysettings.TOP_LOCATION)
print(sbml_path)

# run simulation with the top model
from sbmlutils.dfba.simulator import simulate_dfba
df, dfba_model, dfba_simulator = simulate_dfba(sbml_path, tend=50, dt=5.0)
df


/home/mkoenig/git/sbmlutils/sbmlutils/dfba/toy_wholecell/results/v15/toy_wholecell_top.xml
/home/mkoenig/git/sbmlutils/sbmlutils/dfba/model.py:125 UserWarning: No top level model found.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-54c1d0606a90> in <module>
     10 # run simulation with the top model
     11 from sbmlutils.dfba.simulator import simulate_dfba
---> 12 df, dfba_model, dfba_simulator = simulate_dfba(sbml_path, tend=50, dt=5.0)
     13 df

~/git/sbmlutils/sbmlutils/dfba/simulator.py in simulate_dfba(sbml_path, tstart, tend, dt, pfba, abs_tol, rel_tol, lp_solver, ode_integrator, **kwargs)
     40     start_time = timeit.default_timer()
     41     # Load model
---> 42     dfba_model = DFBAModel(sbml_path=sbml_path)
     43 
     44     # simulation

~/git/sbmlutils/sbmlutils/dfba/model.py in __init__(self, sbml_path)
     53             self.dt = None
     54 
---> 55             self._process_top()
     56             self._process_models()
     57             self._process_dt()

~/git/sbmlutils/sbmlutils/dfba/model.py in _process_top(self)
    125             warnings.warn("No top level model found.")
    126 
--> 127         self.framework_top = builder.get_framework(self.model_top)
    128         if self.framework_top is not builder.MODEL_FRAMEWORK_ODE:
    129             warnings.warn("The top level model framework is not ode: {}".format(self.framework_top))

~/git/sbmlutils/sbmlutils/dfba/builder.py in get_framework(model)
     88     if type(model) not in [libsbml.Model, libsbml.ModelDefinition]:
     89         raise ValueError("Framework must be defined on either Model/ModelDefinition, "
---> 90                          "but given: {}".format(model))
     91 
     92     framework = None

ValueError: Framework must be defined on either Model/ModelDefinition, but given: None

Toy example


In [6]:
from sbmlutils.dfba.toy_wholecell import settings as toysettings
from sbmlutils.dfba.toy_wholecell import model_factory as toyfactory
from sbmlutils.dfba.toy_wholecell import simulate as toysimulate

import tempfile
test_dir = tempfile.mkdtemp()

# create the toy model
toyfactory.create_model(test_dir)
# here the files are generated
sbml_path = os.path.join(utils.versioned_directory(test_dir, toysettings.VERSION),
                         toysettings.TOP_LOCATION)
print(sbml_path)
# simulate
dfs = toysimulate.simulate_toy(sbml_path, test_dir, dts=[1.0], figures=False)

toysimulate.print_species(dfs=dfs)
toysimulate.print_fluxes(dfs=dfs)
print(dfs[0].head())


Create directory: /tmp/tmp9dluia05/v15
ERROR:root:Providing model units as dict is deprecated, use 'ModelUnits' instead.
ERROR:root:Using notes strings is deprecated, use 'Notes' instead.
INFO:root:
------------------------------------------------------------------------------------------------------------------------
/tmp/tmp9dluia05/v15/toy_wholecell_fba.xml
valid                    : TRUE
check time (s)           : 0.009
------------------------------------------------------------------------------------------------------------------------
ERROR:root:Providing model units as dict is deprecated, use 'ModelUnits' instead.
ERROR:root:Using notes strings is deprecated, use 'Notes' instead.
INFO:root:
------------------------------------------------------------------------------------------------------------------------
/tmp/tmp9dluia05/v15/toy_wholecell_bounds.xml
valid                    : TRUE
check time (s)           : 0.011
------------------------------------------------------------------------------------------------------------------------
ERROR:root:Providing model units as dict is deprecated, use 'ModelUnits' instead.
ERROR:root:Using notes strings is deprecated, use 'Notes' instead.
INFO:root:
------------------------------------------------------------------------------------------------------------------------
/tmp/tmp9dluia05/v15/toy_wholecell_update.xml
valid                    : TRUE
check time (s)           : 0.007
------------------------------------------------------------------------------------------------------------------------
ERROR:root:Providing model units as dict is deprecated, use 'ModelUnits' instead.
ERROR:root:Using notes strings is deprecated, use 'Notes' instead.
INFO:root:
------------------------------------------------------------------------------------------------------------------------
/tmp/tmp9dluia05/v15/toy_wholecell_top.xml
valid                    : TRUE
check time (s)           : 0.038
------------------------------------------------------------------------------------------------------------------------
INFO:root:
------------------------------------------------------------------------------------------------------------------------
<SBMLDocument>
flattened                : TRUE
flatten time (ms)        : 0.043
------------------------------------------------------------------------------------------------------------------------
INFO:root:Flattened model written to /tmp/tmp9dluia05/v15/toy_wholecell_flattened.xml
INFO:root:/tmp/tmp9dluia05/v15/toy_wholecell_fba.xml
INFO:root:/tmp/tmp9dluia05/v15/toy_wholecell_bounds.xml
INFO:root:/tmp/tmp9dluia05/v15/toy_wholecell_update.xml
INFO:root:/tmp/tmp9dluia05/v15/toy_wholecell_top.xml
INFO:root:/tmp/tmp9dluia05/v15/toy_wholecell_flattened.xml
WARNING:cobra.io.sbml:SBML package 'comp' not supported by cobrapy,information is not parsed
INFO:root:--------------------------------------------------------------------------------
<Model toy_wholecell_top "toy_wholecell (TOP)"> [ode]
--------------------------------------------------------------------------------
ode        : [<Submodel bounds>, <Submodel update>]
fba        : [<Submodel fba>]
dt         : 0.1
flux rules :
	      EX_A <-> pEX_A
	      EX_C <-> pEX_C

<Submodel fba> toy_wholecell_fba.xml
--------------------------------------------------------------------------------
	obj. direction        : maximize
	cobra obj. direction  : max
	fba2top reactions     : {'EX_C': 'EX_C', 'EX_A': 'EX_A'}
	ub_pid2rid            : {'ub_R1': 'R1', 'ub_EX_A': 'EX_A', 'ub_EX_C': 'EX_C'}
	lb_pid2rid            : {'lb_EX_C': 'EX_C', 'lb_EX_A': 'EX_A'}
	top2flat reactions    : {'EX_C': 'EX_C', 'R3': 'fba__R3', 'R1': 'fba__R1', 'R2': 'fba__R2', 'EX_A': 'EX_A'}
--------------------------------------------------------------------------------

INFO:root:        lb    ub reversibility boundary objective_coefficient  \
R1       0     1         False    False                     0   
R2       0  1000         False    False                     0   
R3       0  1000         False    False                     1   
EX_A -1000  1000          True     True                     0   
EX_C -1000  1000          True     True                     0   

         forward_variable                   reverse_variable  
R1         0 <= R1 <= 1.0      0 <= R1_reverse_cda52 <= -0.0  
R2      0 <= R2 <= 1000.0      0 <= R2_reverse_8c6d2 <= -0.0  
R3      0 <= R3 <= 1000.0      0 <= R3_reverse_5c108 <= -0.0  
EX_A  0 <= EX_A <= 1000.0  0 <= EX_A_reverse_b86fb <= 1000.0  
EX_C  0 <= EX_C <= 1000.0  0 <= EX_C_reverse_69b38 <= 1000.0  
INFO:root:dt set from old value <0.1> to new value: dt=1.0
INFO:root:	lower: lb_EX_C    = 0.0 set to 0.0
/tmp/tmp9dluia05/v15/dfba_simulation.xml
/tmp/tmp9dluia05/v15/toy_wholecell_top.xml
--------------------------------------------------------------------------------
ODE integrator settings
--------------------------------------------------------------------------------
<roadrunner.RoadRunner() { 
'this' : 0x642d920
'modelLoaded' : true
'modelName' : toy_wholecell (TOP)
'libSBMLVersion' : LibSBML Version: 5.17.2
'jacobianStepSize' : 1e-05
'conservedMoietyAnalysis' : false
'simulateOptions' : 
< roadrunner.SimulateOptions() 
{ 
'this' : 0x66127e0, 
'reset' : 0,
'structuredResult' : 0,
'copyResult' : 1,
'steps' : 50,
'start' : 0,
'duration' : 5
}>, 
'integrator' : 
< roadrunner.Integrator() >
  name: cvode
  settings:
      relative_tolerance: 0.000001
      absolute_tolerance: 0.000001
                   stiff: true
       maximum_bdf_order: 5
     maximum_adams_order: 12
       maximum_num_steps: 20000
       maximum_time_step: 0
       minimum_time_step: 0
       initial_time_step: 0
          multiple_steps: false
      variable_step_size: false

}>
--------------------------------------------------------------------------------

Simulation time     : 17.754 [s]
Total time          : 17.841 [s]
Overhead time       : 0.087 [s] (0.5 %)

* DFBA Solution is UNIQUE *
INFO:root:print_species: None
INFO:root:print_fluxes: None
     time        [A]       [C]       [D]  [dummy_S]  [fba__A]  [fba__B1]  \
0.0   0.0  10.000000  0.000000  0.000000        0.0       0.0        0.0   
1.0   1.0   9.000000  0.951639  0.048362        0.0       0.0        0.0   
2.0   2.0   8.181278  1.640202  0.178520        0.0       0.0        0.0   
3.0   3.0   7.510973  2.122005  0.367022        0.0       0.0        0.0   
4.0   4.0   6.962185  2.442313  0.595502        0.0       0.0        0.0   

     [fba__B2]  [fba__C]      EX_A  ...  fba__ub_default  fba__zero  k_R4  \
0.0        0.0       0.0 -1.000000  ...           1000.0        0.0   0.1   
1.0        0.0       0.0 -0.818722  ...           1000.0        0.0   0.1   
2.0        0.0       0.0 -0.670305  ...           1000.0        0.0   0.1   
3.0        0.0       0.0 -0.548788  ...           1000.0        0.0   0.1   
4.0        0.0       0.0 -0.449304  ...           1000.0        0.0   0.1   

        lb_EX_A    lb_EX_C     pEX_A     pEX_C  ub_EX_A  ub_EX_C     ub_R1  
0.0 -100.000000   0.000000 -1.000000  1.000000   1000.0   1000.0  1.000000  
1.0  -90.000000  -9.516386 -0.818722  0.818722   1000.0   1000.0  0.818722  
2.0  -81.812784 -16.402019 -0.670305  0.670305   1000.0   1000.0  0.670305  
3.0  -75.109734 -21.220050 -0.548788  0.548788   1000.0   1000.0  0.548788  
4.0  -69.621850 -24.423134 -0.449304  0.449304   1000.0   1000.0  0.449304  

[5 rows x 30 columns]

Diauxic growth


In [ ]:
'''
from sbmlutils.dfba.diauxic_growth import settings as dgsettings
from sbmlutils.dfba.diauxic_growth import model_factory as dgfactory
from sbmlutils.dfba.diauxic_growth import simulate as dgsimulate
from sbmlutils.dfba.diauxic_growth import analyse as dganalyse

import tempfile
test_dir = tempfile.mkdtemp()

# create the model
dgfactory.create_model(test_dir)

# top model file
sbml_path = os.path.join(utils.versioned_directory(test_dir, dgsettings.VERSION),
                         dgsettings.TOP_LOCATION)
print(sbml_path)

# run DFBA
dfs = dgsimulate.simulate_diauxic_growth(sbml_path, test_dir, dts=[0.01], figures=False)

# plot results
dganalyse.print_species(dfs=dfs)
dganalyse.print_fluxes(dfs=dfs)
print(dfs[0].head())
'''

In [ ]: