Ejemplo n.º 1
0
    def addEvent(
        self,
        trigger,
        assignments,
        persistent=True,
        initial_value=False,
        priority=0,
        delay=0,
        event_id="",
    ):
        """Add event supplied with when an event is triggered and
        what happens using assignments in a dictionary.
        """

        e1 = self.model.createEvent()
        self.check(e1, "create event")
        if len(event_id) == 0:
            event_id = "e" + str(self.model.getNumEvents())
        self.check(e1.setId(event_id), "add id to event")
        if self.document.getLevel() == 3 or (self.document.getLevel() == 2 and
                                             self.document.getVersion() == 4):
            self.check(e1.setUseValuesFromTriggerTime(True),
                       "set use values from trigger time")

        tri = e1.createTrigger()
        self.check(tri, "add trigger to event")
        tri_ast = libsbml.parseL3Formula(trigger)
        self.check(tri.setMath(tri_ast), "add formula to trigger")
        if self.document.getLevel() == 3:
            self.check(tri.setPersistent(persistent),
                       "set persistence of trigger")
            self.check(tri.setInitialValue(initial_value),
                       "set initial value of trigger")

        de = e1.createDelay()
        if self.document.getLevel() == 3:
            k = self.addParameter(event_id + "Delay", delay,
                                  self.model.getTimeUnits())
        else:
            k = self.addParameter(event_id + "Delay", delay, "time")
        self.check(de, "add delay to event")
        delay_ast = libsbml.parseL3Formula(k.getId())
        self.check(de.setMath(delay_ast), "set formula for delay")

        for a in assignments.keys():
            assign = e1.createEventAssignment()
            self.check(assign, "add event assignment to event")
            self.check(assign.setVariable(a),
                       "add variable to event assignment")
            val_ast = libsbml.parseL3Formula(assignments.get(a))
            self.check(assign.setMath(val_ast),
                       "add value to event assignment")

        if self.document.getLevel() == 3:
            pri = e1.createPriority()
            pri_ast = libsbml.parseL3Formula(str(priority))
            self.check(pri.setMath(pri_ast), "add priority to event")
        return e1
Ejemplo n.º 2
0
 def addRateRule(self, var, math):
     r = self.model.createRateRule()
     self.check(r, 'create rate rule r')
     self.check(r.setVariable(var), 'set rate rule variable')
     math_ast = libsbml.parseL3Formula(math)
     self.check(r.setMath(math_ast), 'set rate rule equation')
     return r
Ejemplo n.º 3
0
 def addRateRule(self, var, math):
     r = self.model.createRateRule()
     self.check(r,                        'create rate rule r')
     self.check(r.setVariable(var),          'set rate rule variable')
     math_ast = libsbml.parseL3Formula(math)
     self.check(r.setMath(math_ast), 'set rate rule equation')
     return r
Ejemplo n.º 4
0
    def addEvent(self, trigger, assignments, persistent=True, \
                 initial_value=False, priority=0, delay=0, event_id=''):
        e1 = self.model.createEvent()
        self.check(e1, 'create event')
        if len(event_id) == 0:
            event_id = 'e' + str(self.model.getNumEvents())
        self.check(e1.setId(event_id), 'add id to event')
        if self.document.getLevel()==3 or (self.document.getLevel()==2 \
                    and self.document.getVersion()==4):
            self.check(e1.setUseValuesFromTriggerTime(True),
                       'set use values from trigger time')

        tri = e1.createTrigger()
        self.check(tri, 'add trigger to event')
        tri_ast = libsbml.parseL3Formula(trigger)
        self.check(tri.setMath(tri_ast), 'add formula to trigger')
        if self.document.getLevel() == 3:
            self.check(tri.setPersistent(persistent),
                       'set persistence of trigger')
            self.check(tri.setInitialValue(initial_value),
                       'set initial value of trigger')

        de = e1.createDelay()
        if self.document.getLevel() == 3:
            k = self.addParameter(event_id + 'Delay', delay,
                                  self.model.getTimeUnits())
        else:
            k = self.addParameter(event_id + 'Delay', delay, 'time')
        self.check(de, 'add delay to event')
        delay_ast = libsbml.parseL3Formula(k.getId())
        self.check(de.setMath(delay_ast), 'set formula for delay')

        for a in assignments.keys():
            assign = e1.createEventAssignment()
            self.check(assign, 'add event assignment to event')
            self.check(assign.setVariable(a),
                       'add variable to event assignment')
            val_ast = libsbml.parseL3Formula(assignments.get(a))
            self.check(assign.setMath(val_ast),
                       'add value to event assignment')

        if self.document.getLevel() == 3:
            pri = e1.createPriority()
            pri_ast = libsbml.parseL3Formula(str(priority))
            self.check(pri.setMath(pri_ast), 'add priority to event')
        return e1
Ejemplo n.º 5
0
    def addAssignmentRule(self, var, math):
        """To assign a state variable with an expression"""

        r = self.model.createAssignmentRule()
        self.check(r, "create assignment rule r")
        self.check(r.setVariable(var), "set assignment rule variable")
        math_ast = libsbml.parseL3Formula(math)
        self.check(r.setMath(math_ast), "set assignment rule equation")
        return r
Ejemplo n.º 6
0
 def addInitialAssignment(self, symbol, math):
     if self.document.getLevel() == 2 and self.document.getVersion() == 1:
         raise SystemExit('Error: InitialAssignment does not exist for \
                 this level and version.')
     a = self.model.createInitialAssignment()
     self.check(a, 'create initial assignment a')
     self.check(a.setSymbol(symbol), 'set initial assignment a symbol')
     math_ast = libsbml.parseL3Formula(math)
     self.check(a.setMath(math_ast), 'set initial assignment a math')
     return a
Ejemplo n.º 7
0
 def addInitialAssignment(self, symbol, math):
     if self.document.getLevel() == 2 and self.document.getVersion() == 1:
         raise SystemExit('Error: InitialAssignment does not exist for \
                 this level and version.')
     a = self.model.createInitialAssignment()
     self.check(a,   'create initial assignment a')
     self.check(a.setSymbol(symbol),    'set initial assignment a symbol')
     math_ast = libsbml.parseL3Formula(math)
     self.check(a.setMath(math_ast),    'set initial assignment a math')
     return a
Ejemplo n.º 8
0
    def addEvent(self, trigger, assignments, persistent=True, \
                 initial_value=False, priority=0, delay=0, event_id=''):
        e1 = self.model.createEvent()
        self.check(e1,     'create event')
        if len(event_id) == 0:
            event_id = 'e' + str(self.model.getNumEvents())
        self.check(e1.setId(event_id),    'add id to event')
        if self.document.getLevel()==3 or (self.document.getLevel()==2 \
                    and self.document.getVersion()==4):
            self.check(e1.setUseValuesFromTriggerTime(True), 'set use values from trigger time')

        tri = e1.createTrigger()
        self.check(tri,  'add trigger to event')
        tri_ast = libsbml.parseL3Formula(trigger)
        self.check(tri.setMath(tri_ast),     'add formula to trigger')
        if self.document.getLevel() == 3:
            self.check(tri.setPersistent(persistent),   'set persistence of trigger')
            self.check(tri.setInitialValue(initial_value), 'set initial value of trigger')

        de = e1.createDelay()
        if self.document.getLevel() == 3:
            k = self.addParameter(event_id+'Delay', delay, self.model.getTimeUnits())
        else:
            k = self.addParameter(event_id+'Delay', delay, 'time')
        self.check(de,               'add delay to event')
        delay_ast = libsbml.parseL3Formula(k.getId())
        self.check(de.setMath(delay_ast),     'set formula for delay')

        for a in assignments.keys():
            assign = e1.createEventAssignment()
            self.check(assign,   'add event assignment to event')
            self.check(assign.setVariable(a),  'add variable to event assignment')
            val_ast = libsbml.parseL3Formula(assignments.get(a))
            self.check(assign.setMath(val_ast),    'add value to event assignment')

        if self.document.getLevel() == 3:
            pri = e1.createPriority()
            pri_ast = libsbml.parseL3Formula(str(priority))
            self.check(pri.setMath(pri_ast), 'add priority to event')
        return e1
Ejemplo n.º 9
0
    def addRateRule(self, var, math, rr_id=''):
        r = self.model.createRateRule()
        self.check(r, 'create rate rule r')
        if len(rr_id) == 0:
            rr_id = 'Rate' + str(self.model.getNumRules())
        r.setIdAttribute(rr_id)
        print('rr_id=', r.getIdAttribute())

        #self.check(r.setId(rr_id),           'set rate rule id')
        self.check(r.setVariable(var), 'set rate rule variable')
        math_ast = libsbml.parseL3Formula(math)
        self.check(r.setMath(math_ast), 'set rate rule equation')
        return r
Ejemplo n.º 10
0
    def addInitialAssignment(self, symbol, math):
        """Describe the initial value of the variable in terms of
        other variables or parameters.
        """

        if self.document.getLevel() == 2 and self.document.getVersion() == 1:
            raise SystemExit("Error: InitialAssignment does not exist for \
                    this level and version.")
        a = self.model.createInitialAssignment()
        self.check(a, "create initial assignment a")
        self.check(a.setSymbol(symbol), "set initial assignment a symbol")
        math_ast = libsbml.parseL3Formula(math)
        self.check(a.setMath(math_ast), "set initial assignment a math")
        return a
Ejemplo n.º 11
0
    def addRateRule(self, var, math, rr_id=""):
        """Describe the derivative of the state variable wrt time as an expression."""

        r = self.model.createRateRule()
        self.check(r, "create rate rule r")
        if len(rr_id) == 0:
            rr_id = "Rate" + str(self.model.getNumRules())
        r.setIdAttribute(rr_id)
        # print("rr_id=", r.getIdAttribute())

        # self.check(r.setId(rr_id),           'set rate rule id')
        self.check(r.setVariable(var), "set rate rule variable")
        math_ast = libsbml.parseL3Formula(math)
        self.check(r.setMath(math_ast), "set rate rule equation")
        return r
Ejemplo n.º 12
0
    def addReaction(self,
                    reactants,
                    products,
                    expression,
                    local_params={},
                    rxn_id=''):
        r1 = self.model.createReaction()
        self.check(r1, 'create reaction')
        if len(rxn_id) == 0:
            rxn_id = 'v' + str(self.model.getNumReactions())
        self.check(r1.setId(rxn_id), 'set reaction id')
        self.check(r1.setReversible(False), 'set reaction reversibility flag')
        self.check(r1.setFast(False), 'set reaction "fast" attribute')

        for re in reactants:
            if re is not None and '$' in re:
                re.translate(None, '$')
            re_split = re.split()
            if len(re_split) == 1:
                sto = 1.0
                re_id = re
            elif len(re_split) == 2 and re_split[0].isdigit():
                sto = float(re_split[0])
                re_id = re_split[1]
            else:
                err_msg = 'Error: reactants must be listed in format \'S\' or \'(float)\' S\''
                raise SystemExit(err_msg)
            s1 = self.model.getSpecies(re_id)
            species_ref1 = r1.createReactant()
            self.check(species_ref1, 'create reactant')
            self.check(species_ref1.setSpecies(s1.getId()), \
                    'assign reactant species')
            self.check(species_ref1.setStoichiometry(sto), \
              'assign reactant stoichiometry')
            if self.document.getLevel() == 3:
                self.check(species_ref1.setConstant(True), \
                    'set "constant" on species ref 1')

        for pro in products:
            if pro is not None and '$' in pro:
                pro.translate(None, '$')
            pro_split = pro.split()
            if len(pro_split) == 1:
                sto = 1.0
                pro_id = pro
            elif len(pro_split) == 2:
                sto = float(pro_split[0])
                pro_id = pro_split[1]
            else:
                err_msg = 'Error: products must be listed in format \'S\' or \'(float)\' S\''
                raise SystemExit(err_msg)
            s2 = self.model.getSpecies(pro_id)
            species_ref2 = r1.createProduct()
            self.check(species_ref2, 'create product')
            self.check(species_ref2.setSpecies(s2.getId()), \
                    'assign product species')
            self.check(species_ref2.setStoichiometry(sto), \
              'set product stoichiometry')
            if self.document.getLevel() == 3:
                self.check(species_ref2.setConstant(True), \
                    'set "constant" on species ref 2')

        math_ast = libsbml.parseL3Formula(expression)
        self.check(math_ast, 'create AST for rate expression')

        kinetic_law = r1.createKineticLaw()
        self.check(kinetic_law, 'create kinetic law')
        self.check(kinetic_law.setMath(math_ast), 'set math on kinetic law')
        for param in local_params.keys():
            val = local_params.get(param)
            if self.document.getLevel() == 3:
                p = kinetic_law.createLocalParameter()
            else:
                p = kinetic_law.createParameter()
            self.check(p, 'create local parameter')
            self.check(p.setId(param), 'set id of local parameter')
            self.check(p.setValue(val), 'set value of local parameter')
        return r1
Ejemplo n.º 13
0
    def addReaction(self, reactants, products, expression, local_params={}, rxn_id=''):
        r1 = self.model.createReaction()
        self.check(r1,                         'create reaction')
        if len(rxn_id) == 0:
            rxn_id = 'v' + str(self.model.getNumReactions())
        self.check(r1.setId(rxn_id),           'set reaction id')
        self.check(r1.setReversible(False),    'set reaction reversibility flag')
        self.check(r1.setFast(False),          'set reaction "fast" attribute')

        for re in reactants:
            if re is not None and '$' in re:
                re.translate(None, '$')
            re_split = re.split()
            if len(re_split) == 1:
                sto = 1.0
                re_id = re
            elif len(re_split) == 2 and re_split[0].isdigit():
                sto = float(re_split[0])
                re_id = re_split[1]
            else:
                err_msg = 'Error: reactants must be listed in format \'S\' or \'(float)\' S\''
                raise SystemExit(err_msg)
            s1 = self.model.getSpecies(re_id)
            species_ref1 = r1.createReactant()
            self.check(species_ref1,                       'create reactant')
            self.check(species_ref1.setSpecies(s1.getId()), \
                    'assign reactant species')
            self.check(species_ref1.setStoichiometry(sto), \
            		'assign reactant stoichiometry')
            if self.document.getLevel() == 3:
                self.check(species_ref1.setConstant(True), \
                    'set "constant" on species ref 1')

        for pro in products:
            if pro is not None and '$' in pro:
                pro.translate(None, '$')
            pro_split = pro.split()
            if len(pro_split) == 1:
                sto = 1.0
                pro_id = pro
            elif len(pro_split) == 2:
                sto = float(pro_split[0])
                pro_id = pro_split[1]
            else:
                err_msg = 'Error: products must be listed in format \'S\' or \'(float)\' S\''
                raise SystemExit(err_msg)
            s2 = self.model.getSpecies(pro_id)
            species_ref2 = r1.createProduct()
            self.check(species_ref2, 'create product')
            self.check(species_ref2.setSpecies(s2.getId()), \
                    'assign product species')
            self.check(species_ref2.setStoichiometry(sto), \
            		'set product stoichiometry')
            if self.document.getLevel() == 3:
                self.check(species_ref2.setConstant(True), \
                    'set "constant" on species ref 2')

        math_ast = libsbml.parseL3Formula(expression)
        self.check(math_ast,    'create AST for rate expression')

        kinetic_law = r1.createKineticLaw()
        self.check(kinetic_law,                   'create kinetic law')
        self.check(kinetic_law.setMath(math_ast), 'set math on kinetic law')
        for param in local_params.keys():
            val = local_params.get(param)
            if self.document.getLevel() == 3:
                p = kinetic_law.createLocalParameter()
            else:
                p = kinetic_law.createParameter()
            self.check(p, 'create local parameter')
            self.check(p.setId(param), 'set id of local parameter')
            self.check(p.setValue(val),   'set value of local parameter')
        return r1
Ejemplo n.º 14
0
    def addEvent(
        self,
        trigger,
        assignments,
        persistent=True,
        initial_value=False,
        priority=0,
        delay=0,
        event_id="",
    ):
        """Add event supplied with when an event is triggered and
        what happens using assignments in a dictionary.

        :param trigger: define when an event is triggered (logical expression)
        :type trigger: str
        :param assignments: keys are the variables to be modified and the values are the new values
        :type assignments: dict
        :param persistent: determine if the event will still be executed if trigger turns from
            True to False
        :type persistent: boolean
        :param initial_value: value of trigger before t=0
        :type initial_value: boolean
        :param priority: determine which event is executed, event with larger priority is executed
        :type priority: float
        :param delay: time between when the event is triggered and the assignment is implemented
        :type delay: float
        :param event_id: id of the event
        :type event_id: str
        """

        e1 = self.model.createEvent()
        self.check(e1, "create event")
        if len(event_id) == 0:
            event_id = "e" + str(self.model.getNumEvents())
        self.check(e1.setId(event_id), "add id to event")
        if self.document.getLevel() == 3 or (self.document.getLevel() == 2 and
                                             self.document.getVersion() == 4):
            self.check(e1.setUseValuesFromTriggerTime(True),
                       "set use values from trigger time")

        tri = e1.createTrigger()
        self.check(tri, "add trigger to event")
        tri_ast = libsbml.parseL3Formula(trigger)
        self.check(tri.setMath(tri_ast), "add formula to trigger")
        if self.document.getLevel() == 3:
            self.check(tri.setPersistent(persistent),
                       "set persistence of trigger")
            self.check(tri.setInitialValue(initial_value),
                       "set initial value of trigger")

        de = e1.createDelay()
        if self.document.getLevel() == 3:
            k = self.addParameter(event_id + "Delay", delay,
                                  self.model.getTimeUnits())
        else:
            k = self.addParameter(event_id + "Delay", delay, "time")
        self.check(de, "add delay to event")
        delay_ast = libsbml.parseL3Formula(k.getId())
        self.check(de.setMath(delay_ast), "set formula for delay")

        for a in assignments.keys():
            assign = e1.createEventAssignment()
            self.check(assign, "add event assignment to event")
            self.check(assign.setVariable(a),
                       "add variable to event assignment")
            val_ast = libsbml.parseL3Formula(assignments.get(a))
            self.check(assign.setMath(val_ast),
                       "add value to event assignment")

        if self.document.getLevel() == 3:
            pri = e1.createPriority()
            pri_ast = libsbml.parseL3Formula(str(priority))
            self.check(pri.setMath(pri_ast), "add priority to event")
        return e1
Ejemplo n.º 15
0
    def addReaction(self,
                    reactants,
                    products,
                    expression,
                    local_params=None,
                    rxn_id=""):
        """Create reaction provided with reactants and products in lists

        :param reactants: list of species id for reactants
        :type reactants: list
        :param products: list of species id for products
        :type products: list
        :param expression: reaction rate expression
        :type expression: str
        :param local_params: keys are the param id and values are their respective values
        :type local_params: dict
        :param rxn_id: id for the reaction
        :type rxn_id: str, optional
        """

        r1 = self.model.createReaction()
        self.check(r1, "create reaction")
        if len(rxn_id) == 0:
            rxn_id = "v" + str(self.model.getNumReactions())
        self.check(r1.setId(rxn_id), "set reaction id")
        self.check(r1.setReversible(False), "set reaction reversibility flag")
        self.check(r1.setFast(False), 'set reaction "fast" attribute')

        for re in reactants:
            if re is not None and "$" in re:
                re.translate(None, "$")
            re_split = re.split()
            if len(re_split) == 1:
                sto = 1.0
                re_id = re
            elif len(re_split) == 2 and re_split[0].isdigit():
                sto = float(re_split[0])
                re_id = re_split[1]
            else:
                err_msg = (
                    "Error: reactants must be listed in format 'S' or '(float)' S'"
                )
                raise SystemExit(err_msg)
            s1 = self.model.getSpecies(re_id)
            species_ref1 = r1.createReactant()
            self.check(species_ref1, "create reactant")
            self.check(species_ref1.setSpecies(s1.getId()),
                       "assign reactant species")
            self.check(species_ref1.setStoichiometry(sto),
                       "assign reactant stoichiometry")
            if self.document.getLevel() == 3:
                self.check(species_ref1.setConstant(True),
                           'set "constant" on species ref 1')

        for pro in products:
            if pro is not None and "$" in pro:
                pro.translate(None, "$")
            pro_split = pro.split()
            if len(pro_split) == 1:
                sto = 1.0
                pro_id = pro
            elif len(pro_split) == 2:
                sto = float(pro_split[0])
                pro_id = pro_split[1]
            else:
                err_msg = "Error: products must be listed in format 'S' or '(float)' S'"
                raise SystemExit(err_msg)
            s2 = self.model.getSpecies(pro_id)
            species_ref2 = r1.createProduct()
            self.check(species_ref2, "create product")
            self.check(species_ref2.setSpecies(s2.getId()),
                       "assign product species")
            self.check(species_ref2.setStoichiometry(sto),
                       "set product stoichiometry")
            if self.document.getLevel() == 3:
                self.check(species_ref2.setConstant(True),
                           'set "constant" on species ref 2')

        math_ast = libsbml.parseL3Formula(expression)
        self.check(math_ast, "create AST for rate expression")

        kinetic_law = r1.createKineticLaw()
        self.check(kinetic_law, "create kinetic law")
        self.check(kinetic_law.setMath(math_ast), "set math on kinetic law")
        if local_params is not None:
            for param in local_params.keys():
                val = local_params.get(param)
                if self.document.getLevel() == 3:
                    p = kinetic_law.createLocalParameter()
                else:
                    p = kinetic_law.createParameter()
                self.check(p, "create local parameter")
                self.check(p.setId(param), "set id of local parameter")
                self.check(p.setValue(val), "set value of local parameter")
        return r1