Beispiel #1
0
    def create_bioscrape_model(self):
        from bioscrape.types import Model

        species_list = []
        initial_condition_dict = {}
        for s in self.species:
            species_list.append(repr(s))
            if s.initial_concentration is None:
                initial_condition_dict[repr(s)] = 0
            else:
                initial_condition_dict[repr(s)] = s.initial_concentration

        reaction_list = []
        reaction_counter = 0
        rate_list = []
        for rxn in self.reactions:

            reactants = []
            for i in range(len(rxn.inputs)):
                reactants += [repr(rxn.inputs[i])] * int(rxn.input_coefs[i])
            products = []
            for i in range(len(rxn.outputs)):
                products += [repr(rxn.outputs[i])] * int(rxn.output_coefs[i])

            prop_type = rxn.propensity_type
            if rxn.propensity_params == None:
                prop_params = {}
            else:
                prop_params = {}
                for k in rxn.propensity_params:
                    v = rxn.propensity_params[k]
                    if isinstance(v, Species):
                        prop_params[k] = repr(v)
                    elif isinstance(v, str):
                        prop_params[k] = v
                    else:
                        prop_params[k] = float(v)

            prop_params['propensity_type'] = rxn.propensity_type
            prop_params['k'] = rxn.k

            reaction_list.append(
                (reactants, products, prop_type, dict(prop_params)))

            if rxn.reversible and rxn.propensity_type == "massaction":
                prop_params['k'] = rxn.k_r
                reaction_list.append(
                    (products, reactants, prop_type, dict(prop_params)))
            elif rxn.reversible:
                raise ValueError(
                    "Only massaction irreversible reactions are "
                    "supported for automatic bioscrape simulation."
                    " Consider creating two seperate reactions.")
        model = Model(species=species_list,
                      reactions=reaction_list,
                      initial_condition_dict=initial_condition_dict)
        return model
    def simulate_with_bioscrape_via_sbml(self,
                                         timepoints,
                                         filename=None,
                                         initial_condition_dict=None,
                                         return_dataframe=True,
                                         stochastic=False,
                                         safe=False,
                                         return_model=False,
                                         check_validity=True,
                                         **kwargs):
        """Simulate CRN model with bioscrape via writing a SBML file temporarily.
        [Bioscrape on GitHub](https://github.com/biocircuits/bioscrape).

        Returns the data for all species as Pandas dataframe.
        """
        result = None
        m = None
        try:
            from bioscrape.simulator import py_simulate_model
            from bioscrape.types import Model

            if filename is None:
                self.write_sbml_file(file_name="temp_sbml_file.xml",
                                     stochastic_model=stochastic,
                                     for_bioscrape=True,
                                     check_validity=check_validity)
                file_name = "temp_sbml_file.xml"
            elif isinstance(filename, str):
                file_name = filename
            else:
                raise ValueError(
                    f"filename must be None or a string. Recievied: {filename}"
                )

            if 'sbml_warnings' in kwargs:
                sbml_warnings = kwargs.get('sbml_warnings')
            else:
                sbml_warnings = False
            m = Model(sbml_filename=file_name, sbml_warnings=sbml_warnings)
            # m.write_bioscrape_xml('temp_bs'+ file_name + '.xml') # Uncomment if you want a bioscrape XML written as well.
            if initial_condition_dict is not None:
                processed = process_initial_concentration_dict(
                    initial_condition_dict)
                m.set_species(processed)
            result = py_simulate_model(timepoints,
                                       Model=m,
                                       stochastic=stochastic,
                                       safe=safe,
                                       return_dataframe=return_dataframe)
        except ModuleNotFoundError:
            warnings.warn('bioscrape was not found, please install bioscrape')

        if return_model:
            return result, m
        else:
            return result
def test_bioscrape_instantiation():

    #Deterministic Case
    bioscrape_process = Bioscrape({'sbml_file': 'Notebooks/model1.xml'})
    assert isinstance(bioscrape_process.model, Model)
    assert isinstance(bioscrape_process.interface, ModelCSimInterface)
    assert bioscrape_process.stochastic == False
    assert isinstance(bioscrape_process.simulator, DeterministicSimulator)

    #Stochastic Case
    bioscrape_process = Bioscrape({
        'sbml_file': 'Notebooks/model1.xml',
        "stochastic": True
    })
    assert isinstance(bioscrape_process.model, Model)
    assert isinstance(bioscrape_process.interface, ModelCSimInterface)
    assert bioscrape_process.stochastic == True
    assert isinstance(bioscrape_process.simulator, VolumeSSASimulator)

    #Custom Model Case
    M = Model(species=["S"])
    bioscrape_process2 = Bioscrape({'bioscrape_model': M})
    assert M == bioscrape_process2.model
    assert bioscrape_process2.sbml_file is None
Beispiel #4
0
#
# Below, we load in the simple model of gene expression. We then simulate it determinstically and stochastically. The parameters have been chosen so that the mean mRNA level should be 10 and the mean protein level should be 1000.
#
# Thus, the deterministic simulation should quickly go to a steady state of mRNA = 10 and protein = 1000 and the stochastic simulation should bounce around that number.

# In[ ]:

# Code for simple gene expression without delay

# Import relevant types
from bioscrape.types import Model
from bioscrape.simulator import DeterministicSimulator, SSASimulator
from bioscrape.simulator import ModelCSimInterface

# Load the model by creating a model with the file name containing the model
m = Model('models/sbml_to_txt.xml')
# Expose the model's core characteristics for simulation. (i.e. stoichiometry,
# delays, and propensities)
s = ModelCSimInterface(m)

# Set the initial simulation time
s.py_set_initial_time(0)

# This function uses sparsity to further optimize the speed of deterministic
# simulations. You must call it before doing deterministic simulations.
s.py_prep_deterministic_simulation()

det_simulator = DeterministicSimulator()
# Set up our desired timepoints for which to simulate.
# Must match with initial time.
timepoints = np.linspace(0, 14 * 60 * 60, 100)
fname = "CRN.xml"
CRNs = [CRN0, CRN1, CRN2, CRN4, CRN3, CRN5]
LSs = ["-", "--", ":"]
plt.figure()
for i in range(6):

    plt.subplot(3, 2, i + 1)

    CRN = CRNs[i]
    CRN.write_sbml_file(file_name=fname)
    print("Saved")

    from bioscrape.types import Model
    from bioscrape.simulator import py_simulate_model
    from bioscrape.sbmlutil import *
    M = Model(sbml_filename=fname)

    A_list = [0, 1, 2, 5, 10]
    for ind in range(len(A_list)):
        x0[repr(A)] = A_list[ind]

        M.set_species(x0)

        R = py_simulate_model(timepoints, Model=M)

        plt.plot(timepoints,
                 R["protein_X"],
                 label="A=" + str(A_list[ind]),
                 color='blue',
                 alpha=(ind + 1) / len(A_list))
# Code for simple gene expression without delay

# Import relevant types
from bioscrape.types import Model
from bioscrape.simulator import DeterministicSimulator, SSASimulator
from bioscrape.simulator import ModelCSimInterface

import numpy as np
from libsbml import SBMLReader

# Load the model by creating a model with the file name containing the model
reader = SBMLReader()
# m = reader.readSBMLFromFile('splitEnzymeKinietics1.xml')
# m = m.getModel();
# print str(m.getNumSpecies()) + " species"
m = Model('splitEnzymeKinietics1.xml')
# Expose the model's core characteristics for simulation. (i.e. stoichiometry,
# delays, and propensities)
s = ModelCSimInterface(m)

# Set the initial simulation time
s.py_set_initial_time(0)

# This function uses sparsity to further optimize the speed of deterministic
# simulations. You must call it before doing deterministic simulations.
s.py_prep_deterministic_simulation()

# Set up our desired timepoints for which to simulate.
# Must match with initial time.
timepoints = np.linspace(0, 1000, 1000)
Beispiel #7
0
mpl.rc('xtick', labelsize=12) 
mpl.rc('ytick', labelsize=12)


import numpy as np

# Code for simple gene expression without delay

# Import relevant types
from bioscrape.types import Model
from bioscrape.simulator import DeterministicSimulator, SSASimulator
from bioscrape.simulator import ModelCSimInterface

# Load the model by creating a model with the file name containing the model
m = Model('DP.xml')
# Expose the model's core characteristics for simulation. (i.e. stoichiometry,
# delays, and propensities)
s = ModelCSimInterface(m)

s.py_set_initial_time(0)

# This function uses sparsity to further optimize the speed of deterministic
# simulations. You must call it before doing deterministic simulations.
s.py_prep_deterministic_simulation()

# Set up our desired timepoints for which to simulate. 
# Must match with initial time.
timepoints = np.linspace(0,100,1000)

# Create a DeterministicSimulator as well as an SSASimulator
Beispiel #8
0
def import_sbml(sbml_file, bioscrape_model=None, input_printout=False):
    """
    Convert SBML document to bioscrape Model object. Note that events, compartments, non-standard function definitions,
    and some kinds of rules will be ignored. 
    Adds mass action kinetics based reactions with the appropriate mass action propensity in bioscrape. 
    Propensities with the correct annotation are added as compiled propensity types.
    All other propensities are added as general propensity.
    Local parameters are renamed if there is a conflict since bioscrape does not have a local environment.
    """
    # Attempt to import libsbml and read the SBML model.
    try:
        import libsbml
    except:
        raise ImportError(
            "libsbml not found. See sbml.org for installation help!\n" +
            'If you are using anaconda you can run the following:\n' +
            'conda install -c SBMLTeam python-libsbml\n\n\n')

    reader = libsbml.SBMLReader()
    doc = reader.readSBML(sbml_file)

    if doc.getNumErrors() > 1:
        raise SyntaxError(
            "SBML File {0} cannot be read without errors".format(sbml_file))

    model = doc.getModel()

    # Parse through species and parameters and keep a set of both along with their values.
    allspecies = {}
    allparams = {}
    allreactions = []
    for s in model.getListOfSpecies():
        sid = s.getId()
        if sid == "volume" or sid == "t":
            warnings.warn(
                "You have defined a species called '" + sid +
                ". This species is being ignored and treated as a keyword.")
            continue
        allspecies[sid] = 0.0
        if np.isfinite(s.getInitialAmount()):
            allspecies[sid] = s.getInitialAmount()
        if np.isfinite(s.getInitialConcentration()) and allspecies[sid] == 0:
            allspecies[sid] = s.getInitialConcentration()

    for p in model.getListOfParameters():
        pid = p.getId()
        allparams[pid] = 0.0
        if np.isfinite(p.getValue()):
            allparams[pid] = p.getValue()

    # Now go through reactions one at a time to get stoich and rates, then append to reaction_list.
    for reaction in model.getListOfReactions():
        # get the propensity
        kl = reaction.getKineticLaw()
        # capture any local parameters
        for p in kl.getListOfParameters():
            pid = p.getId()
            if pid in allparams:
                # If local parameter ID already exists in allparams due to another local/global parameter with same ID
                oldid = pid
                newid = oldid + '_' + reaction.getId()
                # Rename the ID everywhere it's used (such as in the Kinetic Law)
                kl.renameSIdRefs(oldid, newid)
                p.setId(newid)
                # Rename its usages
                for element in reaction.getListOfAllElements():
                    element.renameSIdRefs(oldid, newid)
                pid = newid

            allparams[pid] = 0.0
            if np.isfinite(p.getValue()):
                allparams[pid] = p.getValue()

        # get the formula as a string and then add
        # a leading _ to parameter names
        kl_formula = libsbml.formulaToL3String(kl.getMath())
        rate_string = _add_underscore_to_parameters(kl_formula, allparams)

        if reaction.getReversible():
            warnings.warn(
                'SBML model contains reversible reaction!\n' +
                'Please check rate expressions and ensure they are non-negative before doing '
                + 'stochastic simulations.')

        #Get Reactants and Products
        reactant_list = []
        product_list = []
        for reactant in reaction.getListOfReactants():
            reactantspecies = model.getSpecies(reactant.getSpecies())
            reactantspecies_id = reactantspecies.getId()
            if reactantspecies_id in allspecies:
                reactant_list.append(reactantspecies_id)
            else:
                warnings.warn(
                    'Reactant in reaction {0} not found in the list of species in the SBML model.'
                    + ' The species will be added with zero initial amount'.
                    format(reaction.getId()))
                allspecies[reactantspecies_id] = 0.0
                reactant_list.append(reactantspecies_id)

        for product in reaction.getListOfProducts():
            productspecies = model.getSpecies(product.getSpecies())
            productspecies_id = productspecies.getId()
            if productspecies_id in allspecies:
                product_list.append(productspecies_id)
            else:
                warnings.warn(
                    'Reactant in reaction {0} not found in the list of species in the SBML model.'
                    + ' The species will be added with zero initial amount'.
                    format(reaction.getId()))
                allspecies[productspecies_id] = 0.0
                product_list.append(productspecies_id)

        #Identify propensities based upon annotations
        annotation_string = reaction.getAnnotationString()
        if "PropensityType" in annotation_string:
            ind0 = annotation_string.find("<PropensityType>")
            ind1 = annotation_string.find("</PropensityType>")
            # propensity_definition = {}
            annotation_list = annotation_string[ind0:ind1].split(" ")
            key_vals = [(i.split("=")[0], i.split("=")[1])
                        for i in annotation_list if "=" in i]
            propensity_params = {}
            for (k, v) in key_vals:
                try:
                    propensity_params[k] = float(v)
                except ValueError:
                    propensity_params[k] = v
            if input_printout:
                print("Reaction found:", reactant_list, "->", product_list)
                print("Annotated propensity found with params:",
                      propensity_params)
            rxn = (reactant_list, product_list, propensity_params['type'],
                   propensity_params)

        else:  #No annotation found
            propensity_type = 'general'
            general_kl_formula = {}
            general_kl_formula['rate'] = rate_string
            rxn = (reactant_list, product_list, propensity_type,
                   general_kl_formula)
            if input_printout:
                print("Reaction found:", reactant_list, "->", product_list)
                print("Unnotated propensity found with ratestring:",
                      rate_string)
        allreactions.append(rxn)

    # Go through rules one at a time
    allrules = []
    #"Rules must be a tuple: (rule_type (string), rule_attributes (dict), rule_frequency (optional))")
    for rule in model.getListOfRules():
        rule_formula = libsbml.formulaToL3String(rule.getMath())
        rulevariable = rule.getVariable()
        if rulevariable in allspecies:
            rule_string = rulevariable + '=' + _add_underscore_to_parameters(
                rule_formula, allparams)
        elif rulevariable in allparams:
            rule_string = '_' + rulevariable + '=' + _add_underscore_to_parameters(
                rule_formula, allparams)
        else:
            warnings.warn(
                'SBML: Attempting to assign something that is not a parameter or species %s'
                % rulevariable)
            continue
        if rule.getElementName() == 'algebraicRule':
            warnings.warn('Unsupported rule type: %s' % rule.getElementName())
            continue
        elif rule.getElementName() == 'assignmentRule':
            rule_type = 'assignment'
        elif rule.getElementName() == 'rateRule':
            rate_rule_formula = _add_underscore_to_parameters(
                rule_formula, allparams)
            rule_rxn = ([''], [rulevariable], 'general', rate_rule_formula
                        )  # Create --> X type reaction to model rate rules.
            allreactions.append(rule_rxn)
            continue
        else:
            raise ValueError('Invalid SBML Rule type.')
        rule_dict = {}
        rule_dict['equation'] = rule_string
        rule_frequency = 'repeated'
        rule_tuple = (rule_type, rule_dict, rule_frequency)
        allrules.append(rule_tuple)

    #print('allparams = {0}'.format(allparams))
    #print('allspecies = {0}'.format(allspecies))
    #print('allreactions = {0}'.format(allreactions))
    #print(allrules)

    # Check and warn if there are any unrecognized components (function definitions, packages, etc.)
    if len(model.getListOfCompartments()) > 0 or len(
            model.getListOfUnitDefinitions()) > 0 or len(
                model.getListOfEvents()) > 0:
        warnings.warn(
            'Compartments, UnitDefintions, Events, and some other SBML model components are not recognized by bioscrape. '
            + 'Refer to the bioscrape wiki for more information.')

    #If no Model is passed into the function, a Model is returned
    if bioscrape_model == None:
        bioscrape_model = Model()
    #If a Model is passed into the function, that Model is modified
    if isinstance(bioscrape_model, Model):
        for species in allspecies.keys():
            bioscrape_model._add_species(species)

        for (param, val) in allparams.items():
            bioscrape_model._add_param(param)
            bioscrape_model.set_parameter(param, val)
            if input_printout:
                print("Adding Parameter:", param, "=", val)

        for rxn in allreactions:
            if len(rxn) == 4:
                reactants, products, propensity_type, propensity_param_dict = rxn
                delay_type, delay_reactants, delay_products, delay_param_dict = None, None, None, None
            elif len(rxn) == 8:
                reactants, products, propensity_type, propensity_param_dict, delay_type, delay_reactants, delay_products, delay_param_dict = rxn
            bioscrape_model.create_reaction(reactants,
                                            products,
                                            propensity_type,
                                            propensity_param_dict,
                                            delay_type,
                                            delay_reactants,
                                            delay_products,
                                            delay_param_dict,
                                            input_printout=input_printout)

        for rule in allrules:
            if len(rule) == 2:
                rule_type, rule_attributes = rule
                bioscrape_model.create_rule(rule_type,
                                            rule_attributes,
                                            input_printout=input_printout)
            elif len(rule) == 3:
                rule_type, rule_attributes, rule_frequency = rule
                bioscrape_model.create_rule(rule_type,
                                            rule_attributes,
                                            rule_frequency=rule_frequency,
                                            input_printout=input_printout)
        bioscrape_model.set_species(allspecies)
        bioscrape_model.py_initialize()
        return bioscrape_model
    else:
        raise ValueError(
            "bioscrape_model keyword must be a Bioscrape Model object or None (in which case a Model object is returned)."
        )
Beispiel #9
0
       prop_cycle=(mpl.cycler('color', ['r', 'k', 'b', 'g', 'y', 'm', 'c'])))

mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)

import numpy as np

# Code for simple gene expression without delay

# Import relevant types
from bioscrape.types import Model
from bioscrape.simulator import DeterministicSimulator, SSASimulator
from bioscrape.simulator import ModelCSimInterface

# Load the model by creating a model with the file name containing the model
m = Model('IFFL.xml')
# Expose the model's core characteristics for simulation. (i.e. stoichiometry,
# delays, and propensities)
s = ModelCSimInterface(m)

s.py_set_initial_time(0)

# This function uses sparsity to further optimize the speed of deterministic
# simulations. You must call it before doing deterministic simulations.
s.py_prep_deterministic_simulation()

# Set up our desired timepoints for which to simulate.
# Must match with initial time.
timepoints = np.linspace(0, 50, 1000)

# Create a DeterministicSimulator as well as an SSASimulator
       prop_cycle=(mpl.cycler('color', ['r', 'k', 'b', 'g', 'y', 'm', 'c'])))

mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)

import numpy as np

# Code for simple gene expression without delay

# Import relevant types
from bioscrape.types import Model
from bioscrape.simulator import DeterministicSimulator, SSASimulator
from bioscrape.simulator import ModelCSimInterface

# Load the model by creating a model with the file name containing the model
m = Model('models/gene_expression_with_delay.xml')
# Expose the model's core characteristics for simul
s = ModelCSimInterface(m)

s.py_set_initial_time(0)

# This function uses sparsity to further optimize the speed of deterministic
# simulations. You must call it before doing deterministic simulations.
s.py_prep_deterministic_simulation()

# Set up our desired timepoints for which to simulate.
# Must match with initial time.
timepoints = np.linspace(0, 1000, 1000)

# Create a DeterministicSimulator as well as an SSASimulator
ssa_simulator = SSASimulator()
    def __init__(self, parameters=None):
        super(Bioscrape, self).__init__(parameters)

        # get the parameters out of self.parameters if available, or use defaults
        if 'sbml_file' not in self.parameters and 'bioscrape_model' not in self.parameters:
            raise ValueError(
                "Bioscrape Process requires either an sbml_file or bioscrape_model parameter."
            )
        elif 'sbml_file' not in self.parameters and isinstance(
                self.parameters['bioscrape_model'], Model):
            # load the sbml file to create the model
            self.sbml_file = None
            self.model = self.parameters['bioscrape_model']
        elif isinstance(self.parameters['sbml_file'],
                        str) and 'bioscrape_model' not in self.parameters:
            self.sbml_file = self.parameters['sbml_file']
            if self.parameters["lineage"]:
                self.model = LineageModel(sbml_filename=self.sbml_file, )
            else:
                self.model = Model(sbml_filename=self.sbml_file,
                                   sbml_warnings=False)
        elif isinstance(self.parameters['sbml_file'], str) and isinstance(
                self.parameters['bioscrape_model'], Model):
            raise ValueError(
                "Bioscrape recieved an sbml_file and a bioscrape_model. Please use one or the other."
            )
        else:
            raise ValueError(
                f"Bioscrape did not recieve a valid bioscrape_model "
                f"(recieved: {self.parameters['bioscrape_model']} or a "
                f"valid sbml_file (recieved: {self.parameters['sbml_file']}).")

        self.internal_dt = self.parameters['internal_dt']
        self.stochastic = self.parameters['stochastic']

        #Toggle using Lineage Model
        if self.parameters["lineage"]:
            if not self.stochastic:
                raise ValueError(
                    "Bioscrape lineage only available with stochastic = True")
            self.simulator = LineageSSASimulator()
            self.interface = LineageCSimInterface(self.model)
            self.volume = LineageVolumeCellState(
            )  #Create an internal bioscrape Volume
        #Otherwise use normal bioscrape models
        else:
            # create the interface
            if self.parameters["safe_mode"]:
                self.interface = SafeModelCSimInterface(
                    self.model, max_species_count=10**8)
            else:
                self.interface = ModelCSimInterface(self.model)

            #Stochastic
            if self.stochastic:
                self.simulator = VolumeSSASimulator()
            #Not Stochastic
            elif not self.stochastic:
                self.interface.py_prep_deterministic_simulation()
                # create a Simulator
                self.simulator = DeterministicSimulator()
            self.volume = Volume()  #Create an internal bioscrape Volume

        #Set dt
        self.interface.py_set_dt(self.internal_dt)
        #Set the volume
        self.volume.py_set_volume(self.parameters['initial_volume'])