def simulate_cobra(
    total_time=100,
    initial_state=None,
):
    # get the configuration for the iAF1260b BiGG model
    iAF1260b_config = get_iAF1260b_config()
    iAF1260b_config.update({'time_step': COBRA_TIMESTEP})

    iAF1260b_config['name'] = 'cobra' #Rename the process
    dynamic_fba = COBRA_FBA(iAF1260b_config)

    cobra_composite = Composite({
        'processes': dynamic_fba, 
        'topology': dynamic_fba.generate_topology()
        })

    # get the initial state
    if initial_state is None:
        initial_state = dynamic_fba.initial_state({})

    # run simulation
    cobra_sim_settings = {
        'initial_state': initial_state,
        'total_time': total_time}
    cobra_timeseries = simulate_composer(dynamic_fba, cobra_sim_settings)

    return cobra_timeseries, dynamic_fba
def simulate_bioscrape(
        stochastic=False,
        initial_glucose=None,
        initial_lactose=None,
        initial_state=None,
        total_time=100,
        initial_volume=1.0,
        sbml_file=None,
):
    
    #create configs
    if not stochastic:
        bioscrape_config = {
            'sbml_file': sbml_file or sbml_deterministic_file,
            'stochastic': False,
            'initial_volume': initial_volume,
            'internal_dt': BIOSCRAPE_TIMESTEP/100,
            'time_step': BIOSCRAPE_TIMESTEP}
    else:
        bioscrape_config = {
            'sbml_file': sbml_file or sbml_stochastic_file,
            'stochastic': True,
            'safe_mode': False,
            'initial_volume': initial_volume,
            'internal_dt': BIOSCRAPE_TIMESTEP/100,
            'time_step': BIOSCRAPE_TIMESTEP
        }
    
    bioscrape_process = Bioscrape(bioscrape_config)


    #Create an Empty Composite for Plotting Purposes
    bioscrape_composite = Composite({
        'processes': bioscrape_process.generate_processes(), 
        'topology': bioscrape_process.generate_topology()
        })

    # get the initial state
    if initial_state is None:
        initial_state = bioscrape_process.initial_state({})

    if initial_glucose is not None:
        initial_state['species']['Glucose_external'] = initial_glucose

    if initial_lactose is not None:
        initial_state['species']['Lactose_external'] = initial_lactose

    # run simulation
    bioscrape_sim_settings = {
        'initial_state': initial_state,
        'total_time': total_time
        }

    bioscrape_timeseries = simulate_composer(bioscrape_process, bioscrape_sim_settings)

    return bioscrape_timeseries, bioscrape_composite
Пример #3
0
def test_death():
    agent_id = '1'

    # make the compartment
    compartment = ToyLivingCompartment({'agent_id': agent_id})

    # initial state
    initial_state = {
        'agents': {
            agent_id: {
                'external': {
                    'A': 1
                },
                'trigger': False
            }
        }
    }

    # timeline turns death on
    time_dead = 5
    time_total = 10
    timeline = [(0, {
        ('agents', agent_id, 'dead'): False
    }), (time_dead, {
        ('agents', agent_id, 'dead'): True
    }), (time_total, {})]

    # simulate
    settings = {
        'outer_path': ('agents', agent_id),
        'timeline': {
            'timeline': timeline
        },
        'initial_state': initial_state
    }
    output = simulate_composer(compartment, settings)

    # external starts at 1, goes down until death, and then back up
    # internal does the inverse
    external_a = output['agents']['1']['external']['A']
    internal_a = output['agents']['1']['internal']['A']
    assert external_a[0] == 1
    assert external_a[time_dead] < external_a[0]
    assert external_a[time_total] > external_a[time_dead]
    assert internal_a[0] == 0
    assert internal_a[time_dead] > internal_a[0]
    assert internal_a[time_total] < internal_a[time_dead]

    return output
def simulate_diffusion(
        total_time=100,
        diffusion_rate=0.001,
        initial_state={},
        bins=[10, 10],
        bounds=[10, 10]
):
    # configure
    

    config = {
        'n_bins':bins,
        'bounds':bounds,
        'diffusion':diffusion_rate,
        'initial_state':{'glc':initial_state}
    }

    diffusion_process = DiffusionField(config)

    diffusion_composer = Composite({
        'processes': diffusion_process.generate_processes(), 
        'topology': diffusion_process.generate_topology()
        })

    diffusion_state = diffusion_process.initial_state({})

    diffusion_sim_settings = {
        'total_time': total_time,
        'return_raw_data': True,
        'initial_state': diffusion_state,
    }

    diffusion_data = simulate_composer(diffusion_process, diffusion_sim_settings)

    #Add empty agents to reuse plotting functionality
    for t in diffusion_data:
        diffusion_data[t]['agents'] = {}

    return diffusion_data, diffusion_composer
Пример #5
0
def test_remove():
    agent_id = '1'

    # make the compartment
    compartment = ToyLivingCompartment({
        'agent_id': agent_id})

    # initial state
    initial_state = {
        'agents': {
            agent_id: {
                'external': {'A': 1},
                'trigger': False}}}

    # timeline turns death on
    time_dead = 5
    time_total = 10
    timeline = [
        (0, {('agents', agent_id, 'dead'): False}),
        (time_dead, {('agents', agent_id, 'dead'): True}),
        (time_total, {})]

    # simulate
    settings = {
        'outer_path': ('agents', agent_id),
        'timeline': {
            'timeline': timeline},
        'initial_state': initial_state}
    output = simulate_composer(
        compartment,
        settings)

    assert len(output['agents']['1']['dead']) == time_dead + 1
    assert len(output['time']) == time_total + 1

    return output
def simulate_cobra_composite(
    total_time=100,
    initial_state=None,
):

    # get the configuration for the iAF1260b BiGG model
    iAF1260b_config = get_iAF1260b_config()
    iAF1260b_config.update({'time_step': COBRA_TIMESTEP})

    #Place the Process into a Composite level dictionary
    composite_config = {'cobra': iAF1260b_config}
    cobra_composite = CobraComposite(composite_config)

    # get the initial state
    if initial_state is None:
        initial_state = cobra_composite.initial_state({})

    # run simulation
    cobra_sim_settings = {
        'initial_state': initial_state,
        'total_time': total_time}
    cobra_timeseries = simulate_composer(cobra_composite, cobra_sim_settings)

    return cobra_timeseries, cobra_composite
Пример #7
0
def test_connector():

    bioscrape_process_1 = Bioscrape(
        parameters={'sbml_file': 'Notebooks/model1.xml'})
    bioscrape_process_3 = Bioscrape(
        parameters={'sbml_file': 'Notebooks/model3.xml'})

    model1_keys = bioscrape_process_1.get_model_species_ids()
    model3_keys = bioscrape_process_3.get_model_species_ids()

    # define the projection
    model1_vector = np.array(['rna_T' == key for key in model1_keys])
    model3_vector = np.array(['rna_T' == key for key in model3_keys])
    projection_1_3 = np.outer(model1_vector / np.sum(model1_vector),
                              model3_vector / np.sum(model3_vector))
    projection_3_1 = np.outer(model3_vector / np.sum(model3_vector),
                              model1_vector / np.sum(model1_vector))

    # define map function
    def map_1_3(states):
        input_array = array_from(states['source_deltas'])
        output_array = np.dot(input_array, projection_1_3)
        return array_to(model3_keys, output_array)

    def map_3_1(states):
        input_array = array_from(states['source_deltas'])
        output_array = np.dot(input_array, projection_3_1)
        return array_to(model1_keys, output_array)

    # configuration
    time_step = 1
    models = {
        '1': {
            'time_step': time_step,
            'sbml_file': 'Notebooks/model1.xml'
        },
        '3': {
            'time_step': time_step,
            'sbml_file': 'Notebooks/model3.xml'
        },
    }
    connections = [{
        'source': '1',
        'target': '3',
        'map_function': map_1_3
    }, {
        'source': '3',
        'target': '1',
        'map_function': map_3_1
    }]
    # make the composite
    composite = BioscrapeConnector(
        models=models,
        connections=connections,
    )

    ## Run a simulation
    # initial state
    config = {'1': {'rna_T': 10.0}}
    initial_state = composite.initial_state(config)

    #This occurs after composite.generate() is called
    assert len(composite.models) == 2
    assert len(composite.connections) == 2

    # run a simulation
    sim_settings = {'total_time': 10, 'initial_state': initial_state}
    output = simulate_composer(composite, sim_settings)

    #DNA should be constant
    assert all([
        output['1_species']['dna_G'][0] == g
        for g in output['1_species']['dna_G']
    ])

    #RNA should be the same between the two simulations
    assert output['1_species']['rna_T'] == output['3_species']['rna_T']

    #total RNAase should be constant
    RNAase_tot = output['3_species']['protein_RNAase'][0]

    rnas = [
        output['3_species']['protein_RNAase'][i] +
        output['3_species']['complex_protein_RNAase_rna_T_'][i] for i in range(
            len(output['3_species']['complex_protein_RNAase_rna_T_']))
    ]

    #Slight numerical errors are possible, but total RNAase should be nearly constant
    assert all([np.abs(s - RNAase_tot) < 10**-6 for s in rnas])