def __init__(self, t, s0, i0, r0, beta, gamma): # First call the gillespy2.Model initializer. gillespy2.Model.__init__(self, name='StochasticSIR') # Define parameters for the rates of creation and dissociation. k_i = gillespy2.Parameter(name='k_i', expression=beta / N) k_r = gillespy2.Parameter(name='k_r', expression=gamma) self.add_parameter([k_i, k_r]) # Define variables for the molecular species representing M and D. s = gillespy2.Species(name='S', initial_value=s0) i = gillespy2.Species(name='I', initial_value=i0) r = gillespy2.Species(name='R', initial_value=r0) self.add_species([s, i, r]) # The list of reactants and products for a Reaction object are each a # Python dictionary in which the dictionary keys are Species objects # and the values are stoichiometries of the species in the reaction. r_i = gillespy2.Reaction(name="infection", rate=k_i, reactants={ s: 1, i: 1 }, products={i: 2}) r_r = gillespy2.Reaction(name="recovery", rate=k_r, reactants={i: 1}, products={r: 1}) self.add_reaction([r_i, r_r]) # Set the timespan for the simulation. self.timespan(t)
def __init__(self, parameter_values=None): # Initialize the model. gillespy2.Model.__init__(self, name="simple1") # Parameters k1 = gillespy2.Parameter(name='k1', expression=parameter_values[0]) k2 = gillespy2.Parameter(name='k2', expression=parameter_values[1]) k3 = gillespy2.Parameter(name='k3', expression=parameter_values[2]) self.add_parameter([k1, k2, k3]) # Species S1 = gillespy2.Species(name='S1', initial_value=100) S2 = gillespy2.Species(name='S2', initial_value=0) self.add_species([S1, S2]) # Reactions rxn1 = gillespy2.Reaction(name='S1 production', reactants={}, products={S1: 1}, rate=k1) rxn2 = gillespy2.Reaction(name='dimer formation', reactants={S1: 2}, products={S2: 1}, rate=k2) rxn3 = gillespy2.Reaction(name='dimer degradation', reactants={S2: 1}, products={}, rate=k3) self.add_reaction([rxn1, rxn2, rxn3]) self.timespan(np.linspace(0, 100, 101))
def __init__(self, parameter_values=None, init_v=1): #initialize Model gillespy2.Model.__init__(self, name="Simple_Hybrid_Model") #Species A = gillespy2.Species(name='A', initial_value=init_v) V = gillespy2.Species(name='V', initial_value=init_v) self.add_species([A, V]) #parameters rate1 = gillespy2.Parameter(name='rate1', expression=20.0) rate2 = gillespy2.Parameter(name='rate2', expression=10.0) # rate_rule1 = gillespy2.RateRule(V, "cos(t)") self.add_parameter([rate1, rate2]) # self.add_rate_rule(rate_rule1) #reactions r1 = gillespy2.Reaction(name="r1", reactants={}, products={A: 1}, propensity_function="rate1 * V") r2 = gillespy2.Reaction(name="r2", reactants={A: 1}, products={}, rate=rate2) self.add_reaction([r1, r2]) self.timespan(numpy.linspace(0, 100, 101))
def create_boundary_condition_test_model(parameter_values=None): model = gillespy2.Model(name="BoundaryConditionTestModel") s1 = gillespy2.Species(name="S1", boundary_condition=True, initial_value=0.001, mode='continuous') s2 = gillespy2.Species(name="S2", boundary_condition=False, initial_value=0.002, mode='continuous') s3 = gillespy2.Species(name="S3", boundary_condition=False, initial_value=0.001, mode='continuous') s4 = gillespy2.Species(name="S4", boundary_condition=True, initial_value=0.002, mode='continuous') model.add_species([s1, s2, s3, s4]) k1 = gillespy2.Parameter(name="k1", expression="0.75") k2 = gillespy2.Parameter(name="k2", expression="0.25") model.add_parameter([k1, k2]) reaction1 = gillespy2.Reaction(name="reaction1", propensity_function="vol*k1*S1*S2", reactants={s1: 1, s2: 1}, products={s3: 1}) reaction2 = gillespy2.Reaction(name="reaction2", propensity_function="vol*k2*S3", reactants={s3: 1}, products={s1: 1, s2: 1}) reaction3 = gillespy2.Reaction(name="reaction3", propensity_function="vol*k2*S3", reactants={s1: 1, s2: 2, s3: 1}, products={s4: 1}) model.add_reaction([reaction1, reaction2, reaction3]) model.timespan(numpy.linspace(0, 20, 51)) return model
def __init__(self, parameter_values=None, volume=1.0): # Initialize the model. gillespy2.Model.__init__(self, name="simple1", volume=volume) # Parameters k1 = gillespy2.Parameter(name='k1', expression=0.03) self.add_parameter(k1) # Species r1 = gillespy2.Species(name='r1', initial_value=100) self.add_species(r1) r2 = gillespy2.Species(name='r2', initial_value=100) self.add_species(r2) # Reactions rxn1 = gillespy2.Reaction(name='r1d', reactants={r1: 2}, products={}, rate=k1) self.add_reaction(rxn1) rxn2 = gillespy2.Reaction(name='r2d', reactants={r2: 2}, products={}, propensity_function='k1/2 * r2*(r2-1)/vol') self.add_reaction(rxn2)
def test_compile_w_spaces(self): import gillespy2 self.solvers = [ gillespy2.ODECSolver, gillespy2.SSACSolver, gillespy2.TauLeapingCSolver, gillespy2.TauHybridCSolver ] # create a model model = gillespy2.Model(name="Michaelis_Menten") # parameters rate1 = gillespy2.Parameter(name='rate1', expression=0.0017) rate2 = gillespy2.Parameter(name='rate2', expression=0.5) rate3 = gillespy2.Parameter(name='rate3', expression=0.1) model.add_parameter([rate1, rate2, rate3]) # Species A = gillespy2.Species(name='A', initial_value=301) B = gillespy2.Species(name='B', initial_value=120) C = gillespy2.Species(name='C', initial_value=0) D = gillespy2.Species(name='D', initial_value=0) model.add_species([A, B, C, D]) # reactions r1 = gillespy2.Reaction(name="r1", reactants={ A: 1, B: 1 }, products={C: 1}, rate=rate1) r2 = gillespy2.Reaction(name="r2", reactants={C: 1}, products={ A: 1, B: 1 }, rate=rate2) r3 = gillespy2.Reaction(name="r3", reactants={C: 1}, products={ B: 1, D: 1 }, rate=rate3) model.add_reaction([r1, r2, r3]) model.timespan(gillespy2.TimeSpan.linspace(t=100, num_points=101)) # run the model for solver in self.solvers: with self.subTest(solver=solver.name): if platform.system() == "Windows": with self.assertRaises(gillespy2.SimulationError): model.run(solver=solver) else: result = model.run(solver=solver) self.assertTrue(gillespy2.__file__ == os.path.join( self.prefix_base_dir, 'A SPACE/gillespy2/__init__.py'))
def __init__(self, parameter_values=None): # First call the gillespy2.Model initializer. super().__init__(self) # Define parameters for the rates of creation and dissociation. k_c = gillespy2.Parameter(name='k_c', expression=0.005) k_d = gillespy2.Parameter(name='k_d', expression=0.08) self.add_parameter([k_c, k_d]) # Define variables for the molecular species representing M and D. m = gillespy2.Species(name='monomer', initial_value=30) d = gillespy2.Species(name='dimer', initial_value=0) self.add_species([m, d]) # Define the reactions representing the process. In GillesPy2, # the list of reactants and products for a Reaction object are each a # Python dictionary in which the dictionary keys are Species objects # and the values are stoichiometries of the species in the reaction. r_creation = gillespy2.Reaction(name="r_creation", rate=k_c, reactants={m: 2}, products={d: 1}) r_dissociation = gillespy2.Reaction(name="r_dissociation", rate=k_d, reactants={d: 1}, products={m: 2}) self.add_reaction([r_creation, r_dissociation]) # Set the timespan for the simulation. self.timespan(numpy.linspace(0, 100, 101))
def __init__(self, parameter_values=None): # Initialize the model. gillespy2.Model.__init__(self, name="toggle_switch") # Parameters alpha1 = gillespy2.Parameter(name='alpha1', expression=1) alpha2 = gillespy2.Parameter(name='alpha2', expression=1) beta = gillespy2.Parameter(name='beta', expression="2.0") gamma = gillespy2.Parameter(name='gamma', expression="2.0") mu = gillespy2.Parameter(name='mu', expression=1.0) self.add_parameter([alpha1, alpha2, beta, gamma, mu]) # Species U = gillespy2.Species(name='U', initial_value=10) V = gillespy2.Species(name='V', initial_value=10) self.add_species([U, V]) # Reactions cu = gillespy2.Reaction(name="r1", reactants={}, products={U: 1}, propensity_function="alpha1/(1+pow(V,beta))") cv = gillespy2.Reaction(name="r2", reactants={}, products={V: 1}, propensity_function="alpha2/(1+pow(U,gamma))") du = gillespy2.Reaction(name="r3", reactants={U: 1}, products={}, rate=mu) dv = gillespy2.Reaction(name="r4", reactants={V: 1}, products={}, rate=mu) self.add_reaction([cu, cv, du, dv]) self.timespan(np.linspace(0, 50, 101))
def create_event_test_model(parameter_values=None): model = gillespy2.Model(name='Event Test Model') model.add_species([gillespy2.Species(name='S', initial_value=0)]) model.add_parameter( gillespy2.Parameter(name='event_tracker', expression=99)) model.add_parameter( gillespy2.Parameter(name='event_tracker2', expression=0)) model.add_reaction( gillespy2.Reaction( name='r1', products={'S': 1}, rate=model.listOfParameters['event_tracker2'])) eventTrig1 = gillespy2.EventTrigger(expression='t>=2') event1 = gillespy2.Event(name='event1', trigger=eventTrig1) event1.add_assignment( gillespy2.EventAssignment(variable='event_tracker', expression='t')) eventTrig2 = gillespy2.EventTrigger( expression='t >= event_tracker + 2') event2 = gillespy2.Event(name='event2', trigger=eventTrig2) event2.add_assignment( gillespy2.EventAssignment(variable='event_tracker2', expression='t')) model.add_event([event1, event2]) return model
def __init__(self, parameter_values=None): # First call the gillespy2.Model initializer. super().__init__(self) # Define a parameter that represents the rate of decay. decayrate = gillespy2.Parameter(name='decayrate', expression=0.05) self.add_parameter([decayrate]) # Define a molecular species representing the protein; "initial_value" # sets the number of molecules initial present of simulation. protein = gillespy2.Species(name='protein', initial_value=50) self.add_species([protein]) # Define the reaction representing the decay process. In GillesPy2, # the list of reactants and products for a Reaction object are each a # Python dictionary in which the dictionary keys are Species objects # and the values are stoichiometries of the species in the reaction. # (In this example, we have only one reactant, and no products.) The # "rate" for the Reaction object is a Parameter representing the # propensity of this reaction firing.) reaction = gillespy2.Reaction(name="decay", rate=decayrate, reactants={protein: 1}, products={}) self.add_reaction([reaction]) # Set the timespan for the simulation. self.timespan(numpy.linspace(0, 100, 101))
def create_protein_decay(parameter_values=None): # Instantiate Model model = gillespy2.Model(name="Protein Decay") # Define Variables (GillesPy2.Species) protein = gillespy2.Species(name='protein', initial_value=50) # Add Variables to Model model.add_species(protein) # Define Parameters decayrate = gillespy2.Parameter(name='decayrate', expression=0.05) # Add Parameters to Model model.add_parameter(decayrate) # Define Reactions reaction = gillespy2.Reaction( name="decay", rate='decayrate', reactants={'protein': 1}, products={} ) # Add Reactions to Model model.add_reaction(reaction) # Define Timespan tspan = gillespy2.TimeSpan.linspace(t=100, num_points=101) # Set Model Timespan model.timespan(tspan) return model
def cbsa2gillespy(cbsa_model): import gillespy2 as glp gilles = glp.Model(name="Model") k = [glp.Parameter(name='k'+str(i), expression=cbsa_model.exp_k[i]) for i in range(1,cbsa_model.exp_n_reactions)] gilles.add_parameter(k) mols = [glp.Species(name='M'+str(i), initial_value=int(cbsa_model.exp_x0[i])) for i in range(1,cbsa_model.exp_n_molecules)] gilles.add_species(mols) reactions = [] for i in range(1,cbsa_model.exp_n_reactions): reactants = list(np.where(cbsa_model.expS[:,i] < 0)[0]) reactants_sto = list(cbsa_model.expS[:,i][reactants]*-1) modifiers = list(np.where(cbsa_model.expR[:,i] > 0)[0]) modifiers_sto = list(cbsa_model.expR[:,i][modifiers]) products = list(np.where(cbsa_model.expS[:,i] > 0)[0]) products_sto = list(cbsa_model.expS[:,i][products]) reactants += modifiers reactants_sto += modifiers_sto products += modifiers products_sto += modifiers_sto reactions.append(glp.Reaction(name="R"+str(i), rate=k[i-1], reactants={mols[reactants[j]-1]:reactants_sto[j] for j in range(len(reactants))}, products={mols[products[j]-1]:products_sto[j] for j in range(len(products))} ) ) gilles.add_reaction(reactions) return gilles
def test_model_add__multiple_components__in_order(self): import gillespy2 s1 = gillespy2.Species(name="s1", initial_value=29) k1 = gillespy2.Parameter(name="k1", expression=29) r1 = gillespy2.Reaction(name="r1", reactants={"s1": 1}, rate="k1") rr1 = gillespy2.RateRule(name="rr1", variable="k1", formula="29") ar1 = gillespy2.AssignmentRule(name="ar1", variable="s1", formula="29") ea = gillespy2.EventAssignment(name="ea", variable="k1", expression="29") et = gillespy2.EventTrigger(expression="t > 29") e1 = gillespy2.Event(name="e1", trigger=et, assignments=[ea]) divide = gillespy2.FunctionDefinition(name="divide", function="x / y", args=["x", "y"]) tspan = gillespy2.TimeSpan(range(100)) model = gillespy2.Model(name="Test Model") model.add([s1, k1, r1, rr1, ar1, e1, divide, tspan]) self.assertIn("ar1", model.listOfAssignmentRules) self.assertIn("e1", model.listOfEvents) self.assertIn("divide", model.listOfFunctionDefinitions) self.assertIn("k1", model.listOfParameters) self.assertIn("rr1", model.listOfRateRules) self.assertIn("r1", model.listOfReactions) self.assertIn("s1", model.listOfSpecies) self.assertEqual(tspan, model.tspan)
def test_math_name_overlap(self): gamma = gillespy2.Species('gamma',initial_value=2, mode='continuous') self.model.add_species([gamma]) k2 = gillespy2.Parameter(name='k2', expression=1) self.model.add_parameter([k2]) gamma_react = gillespy2.Reaction(name='gamma_react', reactants={'gamma':1}, products={}, rate=k2) self.model.add_reaction([gamma_react]) self.model.run(solver=BasicTauHybridSolver)
def process_reactions(self, reactions): """ Set model reactions extracted from SBML definition. Reversible reactions are and their kinetic laws are split into forward and backward. SBML keyword `compartment` is removed from the expressions of kinetic laws. Parameters ---------- reactions : list of rections (libsbml.Reaction) Returns ------- None """ for reaction in reactions: r_name = reaction.id r_reactants_dict = self.get_reactants_dict(reaction) r_products_dict = self.get_products_dict(reaction) r_kinetic_law = self._kinetic_law_conversion(reaction, forward=True) print("Reaction: {}".format(r_name)) print("reactants: {}".format(r_reactants_dict)) print("products: {}".format(r_products_dict)) print("Original: {}".format(reaction.getKineticLaw().formula)) self.add_reaction( gillespy.Reaction(name=r_name, reactants=r_reactants_dict, products=r_products_dict, propensity_function=r_kinetic_law)) if reaction.reversible is True: print("Forward: {}".format(r_kinetic_law)) r_kinetic_law = self._kinetic_law_conversion(reaction, forward=False) print("Backward: {}".format(r_kinetic_law)) self.add_reaction( gillespy.Reaction(name=r_name + '_inverse', reactants=r_products_dict, products=r_reactants_dict, propensity_function=r_kinetic_law)) else: print("Converted: {}".format(r_kinetic_law)) print()
def __get_reactions(sbml_model, gillespy_model, errors): # reactions for i in range(sbml_model.getNumReactions()): reaction = sbml_model.getReaction(i) name = reaction.getId() tree = __get_kinetic_law(sbml_model, gillespy_model, reaction) propensity = __get_math(tree) reactants = {} products = {} r_set = set() p_set = set() # get reactants for j in range(reaction.getNumReactants()): reactant = reaction.getReactant(j) species = reactant.getSpecies() if species == "EmptySet": continue else: stoichiometry = reactant.getStoichiometry() if isinstance(stoichiometry, float): if int(stoichiometry) != stoichiometry: logmsg = f"Reaction {name} contains a float stoichiometry for reactant {species}. " logmsg += "Please check your model as this may cause inaccuracies in the results." gillespy2.log.warning(logmsg) stoichiometry = int(stoichiometry) if species in r_set: reactants[species] += stoichiometry else: r_set.add(species) reactants[species] = stoichiometry # get products for j in range(reaction.getNumProducts()): product = reaction.getProduct(j) species = product.getSpecies() if species == "EmptySet": continue else: stoichiometry = product.getStoichiometry() if isinstance(stoichiometry, float): if int(stoichiometry) != stoichiometry: logmsg = f"Reaction {name} contains a float stoichiometry for product {species}. " logmsg += "Please check your model as this may cause inaccuracies in the results." gillespy2.log.warning(logmsg) stoichiometry = int(stoichiometry) if species in p_set: products[species] += stoichiometry else: p_set.add(species) products[species] = stoichiometry gillespy_reaction = gillespy2.Reaction(name=name, reactants=reactants, products=products, propensity_function=propensity) gillespy_model.add_reaction([gillespy_reaction])
def __init__(self, endtime, timestep): """ Initialize the model. Parameters ---------- endtime : endtime of simulations timestep : time-step of simulations """ super().__init__( endtime=endtime, timestep=timestep, model_name="SIR", ) beta = gillespy.Parameter(name='beta', expression=self.params['beta']) gamma = gillespy.Parameter(name='gamma', expression=self.params['gamma']) self.add_parameter([beta, gamma]) # Species S = gillespy.Species(name='S', initial_value=100) I = gillespy.Species(name='I', initial_value=100) R = gillespy.Species(name='R', initial_value=100) self.add_species([S, I, R]) # Reactions infection = gillespy.Reaction( name='infection', reactants={ S: 1, I: 1 }, products={I: 2}, propensity_function='beta*S*I/(S+I+R)', ) recover = gillespy.Reaction( name='recover', reactants={I: 1}, products={R: 1}, rate=gamma, ) self.add_reaction([infection, recover]) nb_of_steps = int(math.ceil((endtime / timestep))) + 1 self.timespan(np.linspace(0, endtime, nb_of_steps))
def __init__(self, parameter_values=None): gillespy2.Model.__init__(self, name='StochTest1') A = gillespy2.Species(name='A', initial_value=10) B = gillespy2.Species(name='B', initial_value=0) self.add_species([A, B]) k = gillespy2.Parameter(name='k', expression=10) self.add_parameter([k]) r = gillespy2.Reaction(name='r', reactants={A: 2}, products={B:1}, rate=k) self.add_reaction([r]) self.timespan(np.linspace(0, 100, 101))
def create_stoch_test_1(parameter_values=None): model = gillespy2.Model(name='StochTest1') A = gillespy2.Species(name='A', initial_value=10) B = gillespy2.Species(name='B', initial_value=0) model.add_species([A, B]) k = gillespy2.Parameter(name='k', expression=10) model.add_parameter([k]) r = gillespy2.Reaction(name='r', reactants={A: 2}, products={B: 1}, rate=k) model.add_reaction([r]) model.timespan(np.linspace(0, 100, 101)) return model
def create_dimerization(parameter_values=None): # Initialize Model model = gillespy2.Model(name="Dimerization") # Define Variables (GillesPy2.Species) m = gillespy2.Species(name='monomer', initial_value=30) d = gillespy2.Species(name='dimer', initial_value=0) # Add Variables to Model model.add_species([m, d]) # Define Parameters k_c = gillespy2.Parameter(name='k_c', expression=0.005) k_d = gillespy2.Parameter(name='k_d', expression=0.08) # Add Parameters to Model model.add_parameter([k_c, k_d]) # Define Reactions r_creation = gillespy2.Reaction(name="r_creation", reactants={'monomer': 2}, products={'dimer': 1}, rate='k_c') r_dissociation = gillespy2.Reaction(name="r_dissociation", reactants={'dimer': 1}, products={'monomer': 2}, rate='k_d') # Add Reactions to Model model.add_reaction([r_creation, r_dissociation]) # Define Timespan tspan = gillespy2.TimeSpan.linspace(t=100, num_points=101) # Set Model Timespan model.timespan(tspan) return model
def create_stoch_test_1(parameter_values=None): model = gillespy2.Model(name='StochTest1') A = gillespy2.Species(name='A', initial_value=10) B = gillespy2.Species(name='B', initial_value=0) model.add_species([A, B]) k = gillespy2.Parameter(name='k', expression=10) model.add_parameter([k]) r = gillespy2.Reaction(name='r', reactants={A: 1}, products={B: 1}, propensity_function="k*A/vol" ) # testing if 'vol' is a pre-set variable model.add_reaction([r]) model.timespan(np.linspace(0, 100, 101)) return model
def create_truncated_state_model(parameter_values=None): model = gillespy2.Model(name="TruncatedStateModel") S1 = gillespy2.Species(name="S1", initial_value=0, mode="discrete") rate = gillespy2.Species(name="rate", initial_value=0.9999, mode="continuous") model.add_species([S1, rate]) model.add_rate_rule( gillespy2.RateRule(variable="rate", formula="-1/((t+0.9999)**2)")) model.add_reaction( # Because S1 is a "discrete" species, our reaction will be marked "stochastic." gillespy2.Reaction(products={S1: 1}, propensity_function="10.0*rate")) return model
def __init__(self): gillespy2.Model.__init__(self, name='Event Test Model') self.add_species([gillespy2.Species(name='S', initial_value=0)]) self.add_parameter(gillespy2.Parameter(name='event_tracker', expression=99)) self.add_parameter(gillespy2.Parameter(name='event_tracker2', expression=0)) self.add_reaction(gillespy2.Reaction(name='r1', products={'S':1}, rate=self.listOfParameters['event_tracker2'])) eventTrig1 = gillespy2.EventTrigger(expression='t>=2') event1 = gillespy2.Event(name='event1', trigger=eventTrig1) event1.add_assignment(gillespy2.EventAssignment( variable='event_tracker', expression='t')) eventTrig2 = gillespy2.EventTrigger(expression='t >= event_tracker + 2') event2 = gillespy2.Event(name='event2', trigger=eventTrig2) event2.add_assignment(gillespy2.EventAssignment( variable='event_tracker2', expression='t')) self.add_event([event1, event2])
def create_base_event_model(s1, s2, rate): model = gillespy2.Model(name="BasicEventModel") s1 = gillespy2.Species(name="S1", initial_value=s1, mode="continuous") s2 = gillespy2.Species(name="S2", initial_value=s2, mode="continuous") model.add_species([s1, s2]) rate = gillespy2.Parameter(name="k1", expression=rate) model.add_parameter(rate) r1 = gillespy2.Reaction( name="r1", reactants={s1: 1}, products={s2: 1}, rate=rate, ) model.add_reaction(r1) return model
def __get_reactions(sbml_model, gillespy_model, errors): # reactions for i in range(sbml_model.getNumReactions()): reaction = sbml_model.getReaction(i) name = reaction.getId() tree = __get_kinetic_law(sbml_model, gillespy_model, reaction) propensity = __get_math(tree) reactants = {} products = {} r_set = set() p_set = set() # get reactants for j in range(reaction.getNumReactants()): species = reaction.getReactant(j) if species.getSpecies() == "EmptySet": continue else: if species.getSpecies() in r_set: reactants[ species.getSpecies()] += species.getStoichiometry() else: r_set.add(species.getSpecies()) reactants[ species.getSpecies()] = species.getStoichiometry() # get products for j in range(reaction.getNumProducts()): species = reaction.getProduct(j) if species.getSpecies() == "EmptySet": continue else: if species.getSpecies() in p_set: products[ species.getSpecies()] += species.getStoichiometry() else: p_set.add(species.getSpecies()) products[species.getSpecies()] = species.getStoichiometry() gillespy_reaction = gillespy2.Reaction(name=name, reactants=reactants, products=products, propensity_function=propensity) gillespy_model.add_reaction([gillespy_reaction])
def test_random_seed_unnamed_reactions(self): model = self.model k2 = gillespy2.Parameter(name='k2', expression=1.0) model.add_parameter([k2]) unnamed_rxn = gillespy2.Reaction(reactants={}, products={'Sp':1}, rate=k2) model.add_reaction(unnamed_rxn) for solver in self.solvers: with self.subTest(solver=solver.name): solver = solver(model=self.model) if "ODE" in solver.name: same_results = self.model.run(solver=solver) compare_results = self.model.run(solver=solver) else: same_results = self.model.run(solver=solver, seed=1) compare_results = self.model.run(solver=solver,seed=1) self.assertTrue(np.array_equal(same_results.to_array(), compare_results.to_array())) if solver.name in ["ODESolver", "ODECSolver"]: continue diff_results = self.model.run(solver=solver, seed=2) self.assertFalse(np.array_equal(diff_results.to_array(), same_results.to_array()))
def __init__(self, endtime, timestep): """ Initialize the model. Parameters ---------- endtime : endtime of simulations timestep : time-step of simulations """ super().__init__( endtime=endtime, timestep=timestep, model_name="Gene", ) # Parameters Kp = gillespy.Parameter(name='Kp', expression=self.params['Kp']) Kt = gillespy.Parameter(name='Kt', expression=self.params['Kt']) Kd1 = gillespy.Parameter(name='Kd1', expression=self.params['Kd1']) Kd2 = gillespy.Parameter(name='Kd2', expression=self.params['Kd2']) Kb = gillespy.Parameter(name='Kb', expression=self.params['Kb']) Ku = gillespy.Parameter(name='Ku', expression=self.params['Ku']) self.add_parameter([Kp, Kt, Kd1, Kd2, Kb, Ku]) # Species G0 = gillespy.Species(name='G0', initial_value=0) G1 = gillespy.Species(name='G1', initial_value=1) M = gillespy.Species(name='M', initial_value=1) P = gillespy.Species(name='P', initial_value=500) self.add_species([G0, G1, M, P]) # Reactions prodM = gillespy.Reaction(name='prodM', reactants={G1: 1}, products={ G1: 1, M: 1 }, rate=Kp) prodP = gillespy.Reaction(name='prodP', reactants={M: 1}, products={ M: 1, P: 1 }, rate=Kt) degM = gillespy.Reaction(name='degM', reactants={M: 1}, products={}, rate=Kd1) degP = gillespy.Reaction(name='degP', reactants={P: 1}, products={}, rate=Kd2) reg1G = gillespy.Reaction(name='reg1G', reactants={ G1: 1, P: 1 }, products={G0: 1}, rate=Kb) reg2G = gillespy.Reaction(name='reg2G', reactants={G0: 1}, products={ G1: 1, P: 1 }, rate=Ku) self.add_reaction([prodM, prodP, degM, degP, reg1G, reg2G]) nb_of_steps = int(math.ceil((endtime / timestep))) + 1 self.timespan(np.linspace(0, endtime, nb_of_steps))
def create_tyson_2_state(parameter_values=None): """ Here, as a test case, we run a simple two-state oscillator (Novak & Tyson 2008) as an example of a stochastic reaction system. """ system_volume = 300 #system volume model = gillespy2.Model(name="tyson-2-state", volume=system_volume) model.timespan(np.linspace(0, 100, 101)) # ============================================= # Define model species, initial values, parameters, and volume # ============================================= # Parameter values for this biochemical system are given in # concentration units. However, stochastic systems must use population # values. For example, a concentration unit of 0.5mol/(L*s) # is multiplied by a volume unit, to get a population/s rate # constant. Thus, for our non-mass action reactions, we include the # parameter "vol" in order to convert population units to concentration # units. Volume here = 300. P = gillespy2.Parameter(name='P', expression=2.0) kt = gillespy2.Parameter(name='kt', expression=20.0) kd = gillespy2.Parameter(name='kd', expression=1.0) a0 = gillespy2.Parameter(name='a0', expression=0.005) a1 = gillespy2.Parameter(name='a1', expression=0.05) a2 = gillespy2.Parameter(name='a2', expression=0.1) kdx = gillespy2.Parameter(name='kdx', expression=1.0) model.add_parameter([P, kt, kd, a0, a1, a2, kdx]) # Species # Initial values of each species (concentration converted to pop.) X = gillespy2.Species(name='X', initial_value=int(0.65609071 * system_volume)) Y = gillespy2.Species(name='Y', initial_value=int(0.85088331 * system_volume)) model.add_species([X, Y]) # ============================================= # Define the reactions within the model # ============================================= # creation of X: rxn1 = gillespy2.Reaction( name='X production', reactants={}, products={X: 1}, propensity_function='vol*1/(1+(Y*Y/((vol*vol))))') # degradadation of X: rxn2 = gillespy2.Reaction(name='X degradation', reactants={X: 1}, products={}, rate=kdx) # creation of Y: rxn3 = gillespy2.Reaction(name='Y production', reactants={X: 1}, products={ X: 1, Y: 1 }, rate=kt) # degradation of Y: rxn4 = gillespy2.Reaction(name='Y degradation', reactants={Y: 1}, products={}, rate=kd) # nonlinear Y term: rxn5 = gillespy2.Reaction( name='Y nonlin', reactants={Y: 1}, products={}, propensity_function='Y/(a0 + a1*(Y/vol)+a2*Y*Y/(vol*vol))') model.add_reaction([rxn1, rxn2, rxn3, rxn4, rxn5]) return model
def convert_reactions(self): ''' Method to convert reactions from non-gillespy2 Random DNA Chemistry to the gillespy2 Random DNA Chemistry ''' gillespy2_reactions = [] for i_bind, r_bind in enumerate( self.randomDNAChem.reaction_lookup['R_BIND']): for species1 in self.listOfSpecies: if species1 == r_bind[0:2]: bind_reactant1 = species1 for species2 in self.listOfSpecies: if species2 == r_bind[5:7]: bind_reactant2 = species2 for species3 in self.listOfSpecies: if species3 == r_bind[11:15]: bind_product = species3 gillespy2_bind = gillespy2.Reaction( name=r_bind, rate=list(self.listOfParameters.values())[i_bind], reactants={ bind_reactant1: 1, bind_reactant2: 1 }, products={bind_product: 1}) gillespy2_reactions.append(gillespy2_bind) for i_displace, r_displace in enumerate( self.randomDNAChem.reaction_lookup['R_DISPLACE']): for species1 in self.listOfSpecies: if species1 == r_displace[0:2]: displace_reactant1 = species1 for species2 in self.listOfSpecies: if species2 == r_displace[5:9]: displace_reactant2 = species2 for species3 in self.listOfSpecies: if species3 == r_displace[13:17]: displace_product1 = species3 for species3 in self.listOfSpecies: if species3 == r_displace[20:22]: displace_product2 = species3 gillespy2_displace = gillespy2.Reaction( name=r_displace, rate=list(self.listOfParameters.values())[i_displace], reactants={ displace_reactant1: 1, displace_reactant2: 1 }, products={ displace_product1: 1, displace_product2: 1 }) gillespy2_reactions.append(gillespy2_displace) for i_in, r_in in enumerate( self.randomDNAChem.reaction_lookup['R_IN']): if len(r_in) == 9: for species1 in self.listOfSpecies: if species1 == r_in[5:9]: in_product = species1 elif len(r_in) == 7: for species1 in self.listOfSpecies: if species1 == r_in[5:7]: in_product = species1 else: raise BaseException gillespy2_in = gillespy2.Reaction( name=r_in, rate=list(self.listOfParameters.values())[i_in], reactants={}, products={in_product: 1}) gillespy2_reactions.append(gillespy2_in) for i_out, r_out in enumerate( self.randomDNAChem.reaction_lookup['R_OUT']): if len(r_out) == 9: for species1 in self.listOfSpecies: if species1 == r_out[0:4]: out_reactant = species1 elif len(r_out) == 7: for species1 in self.listOfSpecies: if species1 == r_out[0:2]: out_reactant = species1 else: raise BaseException gillespy2_out = gillespy2.Reaction( name=r_out, rate=list(self.listOfParameters.values())[i_out], reactants={out_reactant: 1}, products={}) gillespy2_reactions.append(gillespy2_out) self.add_reaction(gillespy2_reactions)
def __init__(self, parameters): gillespy2.Model.__init__(self, "Well Mixed Model") self.list_species = ['Gf', 'Gb', 'RNA', 'P'] self.dict_species = { species: gillespy2.Species( name=species, initial_value=0) for species in self.list_species} # Force species order to follow list_species # Necessary to extract results correctly self.add_species([self.dict_species[s] for s in self.list_species]) # TODO unit test order preservation, e.g. all rates to 0... #Parameters parameters = {name: gillespy2.Parameter(name=name, expression=value) for name, value in parameters.items() if isinstance(value, float)} self.add_parameter(list(parameters.values())) #Reactions #Degradation dict_reactions = {} for name, species in [(name, self.dict_species[name]) for name in ["RNA", "P"]]: dict_reactions["dgd"+name] = gillespy2.Reaction( name = "dgd"+name, reactants = {species:1}, products={}, rate = parameters['gamma']) #Transcription dict_reactions["tcp"] = gillespy2.Reaction( name = "tcp", reactants = {self.dict_species['Gf']:1}, products={self.dict_species['Gf']:1, self.dict_species['RNA']:1}, rate = parameters['mu']) #Translation dict_reactions["tlt"] = gillespy2.Reaction( name="tlt", reactants={self.dict_species['RNA']:1}, products={self.dict_species['RNA']:1, self.dict_species['P']:1}, rate=parameters['kappa']) #Binding with P dict_reactions["bdg"] = gillespy2.Reaction( name="bdg", reactants={self.dict_species['Gf']:1, self.dict_species['P']:1}, products={self.dict_species['Gb']:1}, rate=parameters['k_a']) #Unbinding from her dict_reactions["ubdg"+name] = gillespy2.Reaction( name="ubdg"+name, reactants={self.dict_species['Gb']:1}, products={self.dict_species['Gf']:1, self.dict_species['P']:1}, rate=parameters['k_d']) self.add_reaction(list(dict_reactions.values()))