Insert Parameters¶
Parameters can be inserted automatically into a Copasi model from python code using PyCoTools
Build a demonistration model¶
While antimony or the COPASI user interface are the preferred ways to build a model, PyCoTools does have a mechanism for constructing COPASI models. For variation and demonstration, this method is used here.
[22]:
import os
import site
site.addsitedir('D:\pycotools3')
from pycotools3 import model, tasks, viz
## Choose a working directory for model
working_directory = os.path.abspath('')
copasi_file = os.path.join(working_directory, 'MichaelisMenten.cps')
if os.path.isfile(copasi_file):
os.remove(copasi_file)
kf = 0.01
kb = 0.1
kcat = 0.05
with model.Build(copasi_file) as m:
m.name = 'Michaelis-Menten'
m.add('compartment', name='Cell')
m.add('metabolite', name='P', concentration=0)
m.add('metabolite', name='S', concentration=30)
m.add('metabolite', name='E', concentration=10)
m.add('metabolite', name='ES', concentration=0)
m.add('reaction', name='S bind E', expression='S + E -> ES', rate_law='kf*S*E',
parameter_values={'kf': kf})
m.add('reaction', name='S unbind E', expression='ES -> S + E', rate_law='kb*ES',
parameter_values={'kb': kb})
m.add('reaction', name='ES produce P', expression='ES -> P + E', rate_law='kcat*ES',
parameter_values={'kcat': kcat})
mm = model.Model(copasi_file)
mm
[22]:
Model(name=Michaelis-Menten, time_unit=s, volume_unit=ml, quantity_unit=mmol)
Insert Parameters from Python Dictionary¶
[6]:
params = {'E': 100,
'P': 150}
## Insert into model
I = model.InsertParameters(mm, parameter_dict=params)
##format the parameters for displaying nicely
I.parameters.index = ['Parameter Value']
I.parameters.transpose()
[6]:
Parameter Value | |
---|---|
E | 100 |
P | 150 |
Alternatively use inplace=True
argument (analogous to the pandas
library) to modify the object inplace, rather than needing to assign
[7]:
model.InsertParameters(mm, parameter_dict=params, inplace=True)
[7]:
<pycotools3.model.InsertParameters at 0x1b41f67a9e8>
Insert Parameters from Pandas DataFrame¶
[8]:
import pandas
params = {'(S bind E).kf': 50,
'(S unbind E).kb': 96}
df = pandas.DataFrame(params, index=[0])
df
[8]:
(S bind E).kf | (S unbind E).kb | |
---|---|---|
0 | 50 | 96 |
[9]:
model.InsertParameters(mm, df=df, inplace=True)
[9]:
<pycotools3.model.InsertParameters at 0x1b41f67a048>
Insert Parameters from Parameter Estimation Output¶
First we’ll get some parameter estimation data by fitting a model to simulated data.
[16]:
fname = os.path.join(os.path.abspath(''), 'timecourse.txt')
data = mm.simulate(0, 50, 1, report_name=fname)
assert os.path.isfile(fname)
[23]:
with tasks.ParameterEstimation.Context(copasi_file, fname, context='s', parameters='a') as context:
context.randomize_start_values = True
context.lower_bound = 0.01
context.upper_bound = 100
context.run_mode = True
config = context.get_config()
PE = tasks.ParameterEstimation(config)
[26]:
mm
[26]:
Model(name=Michaelis-Menten, time_unit=s, volume_unit=ml, quantity_unit=mmol)
Now we can insert the estimated parameters using:
[24]:
##index=0 for best parameter set (i.e. lowest RSS)
model.InsertParameters(mm, parameter_path=PE.results_directory, index=0, inplace=True)
---------------------------------------------------------------------------
InputError Traceback (most recent call last)
<ipython-input-24-d908d0566dc2> in <module>()
1 ##index=0 for best parameter set (i.e. lowest RSS)
----> 2 model.InsertParameters(mm, parameter_path=PE.results_directory, index=0, inplace=True)
D:\pycotools3\pycotools3\model.py in __init__(self, model, parameter_dict, df, parameter_path, index, quantity_type, inplace)
4674 self._do_checks()
4675
-> 4676 self.model = self.insert()
4677 if self.inplace:
4678 self.model.save()
D:\pycotools3\pycotools3\model.py in insert(self)
4867
4868 """
-> 4869 self.model = self.insert_locals()
4870 self.model = self.insert_compartments()
4871 self.model = self.insert_global_quantities()
D:\pycotools3\pycotools3\model.py in insert_locals(self)
4774 # print self.parameters
4775
-> 4776 locals = [j for i in self.model.reactions for j in i.parameters if
4777 j.global_name in list(self.parameters.keys())]
4778 if locals == []:
D:\pycotools3\pycotools3\model.py in <listcomp>(.0)
4775
4776 locals = [j for i in self.model.reactions for j in i.parameters if
-> 4777 j.global_name in list(self.parameters.keys())]
4778 if locals == []:
4779 return self.model
D:\pycotools3\pycotools3\cached_property.py in __get__(self, obj, cls)
38 if obj is None:
39 return self
---> 40 value = obj.__dict__[self.func.__name__] = self.func(obj)
41 return value
42
D:\pycotools3\pycotools3\model.py in parameters(self)
4760
4761 if self.parameter_path != None:
-> 4762 P = viz.Parse(self.parameter_path, copasi_file=self.model.copasi_file)
4763 if isinstance(self.index, int):
4764 return pandas.DataFrame(P.data.iloc[self.index]).transpose()
D:\pycotools3\pycotools3\viz.py in __init__(self, cls_instance, log10, copasi_file, alpha, rss_value, num_data_points)
519 raise errors.InputError('{} not in {}'.format(
520 self.cls_instance,
--> 521 accepted_types)
522 )
523
InputError: {'MichaelisMenten': 'D:\\pycotools3\\docs\\source\\Tutorials\\Problem1\\Fit1\\MichaelisMenten\\ParameterEstimationData'} not in [<class 'pycotools3.tasks.TimeCourse'>, <class 'pycotools3.tasks.Scan'>, <class 'pycotools3.tasks.ParameterEstimation'>, <class 'str'>, <class 'pycotools3.viz.Parse'>, <class 'pycotools3.tasks.ProfileLikelihood'>, <class 'pandas.core.frame.DataFrame'>, <class 'pycotools3.tasks.ChaserParameterEstimations'>]
Insert Parameters using the model.Model().insert_parameters
method¶
The same means of inserting parameters can be used from the model object itself
[ ]:
mm.insert_parameters(parameter_dict=params, inplace=True)
Change parameters using model.Model().set
¶
Individual parameters can also be changed using the set
method. For example, we could set the metabolite
with name S
concentration
or particle numbers
to 55
[ ]:
mm.set('metabolite', 'S', 55, 'name', 'concentration')
## or
mm.set('metabolite', 'S', 55, 'name', 'particle_numbers')