Insert Parameters

Parameters can be inserted automatically into a Copasi model.

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.

[1]:
import os
import site
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
[1]:
Model(name=Michaelis-Menten, time_unit=s, volume_unit=ml, quantity_unit=mmol)

Insert Parameters from Python Dictionary

[2]:
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()
[2]:
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

[3]:
model.InsertParameters(mm, parameter_dict=params, inplace=True)
[3]:
<pycotools3.model.InsertParameters at 0x15c51a255c0>

Insert Parameters from Pandas DataFrame

[4]:
import pandas
params = {'(S bind E).kf': 50,
          '(S unbind E).kb': 96}
df = pandas.DataFrame(params, index=[0])
df
[4]:
(S bind E).kf (S unbind E).kb
0 50 96
[ ]:

[5]:
model.InsertParameters(mm, df=df, inplace=True)
[5]:
<pycotools3.model.InsertParameters at 0x15c519f8dd8>

Insert Parameters from Parameter Estimation Output

First we’ll get some parameter estimation data by fitting a model to simulated data.

[6]:
fname = os.path.join(os.path.abspath(''), 'timecourse.txt')
print(fname)
data = mm.simulate(0, 50, 1, report_name=fname)
assert os.path.isfile(fname)
D:\pycotools3\docs\source\Tutorials\timecourse.txt
[7]:
with  tasks.ParameterEstimation.Context(copasi_file, fname, context='s', parameters='l') as context:
    context.set('randomize_start_values', True)
    context.set('lower_bound', 0.01)
    context.set('upper_bound', 100)
    context.set('run_mode', True)
    config = context.get_config()
print(config)
PE = tasks.ParameterEstimation(config)
datasets:
    experiments:
        timecourse:
            affected_models:
            - MichaelisMenten
            filename: D:\pycotools3\docs\source\Tutorials\timecourse.txt
            mappings:
                E:
                    model_object: E
                    object_type: Metabolite
                    role: dependent
                ES:
                    model_object: ES
                    object_type: Metabolite
                    role: dependent
                P:
                    model_object: P
                    object_type: Metabolite
                    role: dependent
                S:
                    model_object: S
                    object_type: Metabolite
                    role: dependent
                Time:
                    model_object: Time
                    role: time
            normalize_weights_per_experiment: true
            separator: "\t"
    validations: {}
items:
    fit_items:
        (ES produce P).kcat:
            affected_experiments:
            - timecourse
            affected_models:
            - MichaelisMenten
            affected_validation_experiments: []
            lower_bound: 1.0e-06
            start_value: model_value
            upper_bound: 1000000.0
        (S bind E).kf:
            affected_experiments:
            - timecourse
            affected_models:
            - MichaelisMenten
            affected_validation_experiments: []
            lower_bound: 1.0e-06
            start_value: model_value
            upper_bound: 1000000.0
        (S unbind E).kb:
            affected_experiments:
            - timecourse
            affected_models:
            - MichaelisMenten
            affected_validation_experiments: []
            lower_bound: 1.0e-06
            start_value: model_value
            upper_bound: 1000000.0
models:
    MichaelisMenten:
        copasi_file: D:\pycotools3\docs\source\Tutorials\MichaelisMenten.cps
        model: Model(name=Michaelis-Menten, time_unit=s, volume_unit=ml, quantity_unit=mmol)
settings:
    calculate_statistics: false
    config_filename: config.yml
    context: s
    cooling_factor: 0.85
    copy_number: 1
    create_parameter_sets: false
    cross_validation_depth: 1
    fit: 1
    iteration_limit: 50
    lower_bound: 0.01
    max_active: 3
    method: genetic_algorithm
    number_of_generations: 200
    number_of_iterations: 100000
    overwrite_config_file: false
    pe_number: 1
    pf: 0.475
    pl_lower_bound: 1000
    pl_upper_bound: 1000
    population_size: 50
    prefix: null
    problem: Problem1
    quantity_type: concentration
    random_number_generator: 1
    randomize_start_values: true
    report_name: PEData.txt
    results_directory: ParameterEstimationData
    rho: 0.2
    run_mode: true
    save: false
    scale: 10
    seed: 0
    start_temperature: 1
    start_value: 0.1
    std_deviation: 1.0e-06
    swarm_size: 50
    tolerance: 1.0e-05
    update_model: false
    upper_bound: 100
    use_config_start_values: false
    validation_threshold: 5
    validation_weight: 1
    weight_method: mean_squared
    working_directory: D:\pycotools3\docs\source\Tutorials

Now we can insert the estimated parameters using:

[8]:
##index=0 for best parameter set (i.e. lowest RSS)
model.InsertParameters(mm, parameter_path=PE.results_directory['MichaelisMenten'], index=0, inplace=True)
[8]:
<pycotools3.model.InsertParameters at 0x15c6fb8c2e8>

Insert Parameters using the model.Model().insert_parameters method

The same means of inserting parameters can be used from the model object itself

[9]:
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

[10]:
mm.set('metabolite', 'S', 55, 'name', 'concentration')

## or

mm.set('metabolite', 'S', 55, 'name', 'particle_numbers')
[10]:
Model(name=Michaelis-Menten, time_unit=s, volume_unit=ml, quantity_unit=mmol)