def test_RateRule(self): r = libsbml.RateRule(2,4) self.assertEqual( False, (r.hasRequiredElements()) ) r.setMath(libsbml.parseFormula("ar")) self.assertEqual( True, r.hasRequiredElements() ) r = None pass
def test_RateRule(self): r = libsbml.RateRule(2, 4) self.assertEqual(False, (r.hasRequiredAttributes())) r.setVariable("r") self.assertEqual(True, r.hasRequiredAttributes()) r = None pass
def test_Rule_assignmentOperator(self): o1 = libsbml.RateRule(2,4) o1.setVariable("a") self.assert_( o1.getVariable() == "a" ) node = libsbml.ASTNode(libsbml.AST_CONSTANT_PI) o1.setMath(node) node = None self.assert_( o1.isSetMath() == True ) o2 = libsbml.RateRule(2,4) o2 = o1 self.assert_( o2.getVariable() == "a" ) self.assert_( o2.isSetMath() == True ) self.assert_( o2.getParentSBMLObject() == o1.getParentSBMLObject() ) o2 = None o1 = None pass
def test_Rule_parent_add(self): ia = libsbml.RateRule(2, 4) ia.setVariable("a") ia.setMath(libsbml.parseFormula("9")) m = libsbml.Model(2, 4) m.addRule(ia) ia = None lo = m.getListOfRules() self.assert_(lo == m.getRule(0).getParentSBMLObject()) self.assert_(m == lo.getParentSBMLObject()) pass
def test_Rule_ancestor_add(self): ia = libsbml.RateRule(2,4) ia.setVariable("a") ia.setMath(libsbml.parseFormula("9")) m = libsbml.Model(2,4) m.addRule(ia) ia = None lo = m.getListOfRules() obj = m.getRule(0) self.assert_( obj.getAncestorOfType(libsbml.SBML_MODEL) == m ) self.assert_( obj.getAncestorOfType(libsbml.SBML_LIST_OF) == lo ) self.assert_( obj.getAncestorOfType(libsbml.SBML_DOCUMENT) == None ) self.assert_( obj.getAncestorOfType(libsbml.SBML_EVENT) == None ) pass
def test_Model_addRules(self): r1 = libsbml.AlgebraicRule(2, 4) r2 = libsbml.AssignmentRule(2, 4) r3 = libsbml.RateRule(2, 4) r2.setVariable("r2") r3.setVariable("r3") r1.setMath(libsbml.parseFormula("2")) r2.setMath(libsbml.parseFormula("2")) r3.setMath(libsbml.parseFormula("2")) self.M.addRule(r1) self.M.addRule(r2) self.M.addRule(r3) self.assert_(self.M.getNumRules() == 3) pass
def test_internal_consistency_check_99911_rule_rate(self): d = libsbml.SBMLDocument(2,4) m = d.createModel() p = m.createParameter() p.setId("p") p.setConstant(False) r = libsbml.RateRule(2,4) d.setLevelAndVersion(2,1,False) r.setVariable("p") r.setSBOTerm(2) m.addRule(r) errors = d.checkInternalConsistency() self.assert_( errors == 0 ) d = None pass
def test_internal_consistency_check_99904_rule_rate(self): d = libsbml.SBMLDocument(2,4) r = libsbml.RateRule(2,4) d.setLevelAndVersion(1,2,False) m = d.createModel() c = m.createCompartment() c.setId("cc") c.setConstant(False) r.setVariable("cc") r.setFormula("2") r.setMetaId("mmm") m.addRule(r) errors = d.checkInternalConsistency() self.assert_( errors == 0 ) d = None pass
def test_RateRule_createWithNS(self): xmlns = libsbml.XMLNamespaces() xmlns.add("http://www.sbml.org", "testsbml") sbmlns = libsbml.SBMLNamespaces(2, 1) sbmlns.addNamespaces(xmlns) object = libsbml.RateRule(sbmlns) self.assert_(object.getTypeCode() == libsbml.SBML_RATE_RULE) self.assert_(object.getMetaId() == "") self.assert_(object.getNotes() == None) self.assert_(object.getAnnotation() == None) self.assert_(object.getLevel() == 2) self.assert_(object.getVersion() == 1) self.assert_(object.getNamespaces() != None) self.assert_(object.getNamespaces().getLength() == 2) _dummyList = [object] _dummyList[:] = [] del _dummyList pass
def toSBMLString(net): metaId = 0 try: m = libsbml.Model(net.id) except NotImplementedError: m = libsbml.Model(sbml_level, sbml_version) m.setId(net.id) m.setName(net.name) m.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 for id, fd in list(net.functionDefinitions.items()): try: sfd = libsbml.FunctionDefinition(id) except: sfd = libsbml.FunctionDefinition(sbml_level, sbml_version) sfd.setId(id) sfd.setName(fd.name) formula = fd.math formula = formula.replace('**', '^') formula = 'lambda(%s, %s)' % (','.join(fd.variables), formula) sfd.setMath(libsbml.parseFormula(formula)) sfd.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addFunctionDefinition(sfd) for id, c in list(net.compartments.items()): try: sc = libsbml.Compartment(id) except NotImplementedError: sc = libsbml.Compartment(sbml_level, sbml_version) sc.setId(id) sc.setName(c.name) sc.setConstant(c.is_constant) sc.setSize(c.initialValue) sc.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addCompartment(sc) for id, s in list(net.species.items()): try: ss = libsbml.Species(id) except NotImplementedError: ss = libsbml.Species(sbml_level, sbml_version) ss.setId(id) ss.setName(s.name) ss.setCompartment(s.compartment) if s.initialValue is not None and not isinstance(s.initialValue, str): ss.setInitialConcentration(s.initialValue) ss.setBoundaryCondition(s.is_boundary_condition) ss.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addSpecies(ss) for id, p in list(net.parameters.items()): try: sp = libsbml.Parameter(id) except NotImplementedError: sp = libsbml.Parameter(sbml_level, sbml_version) sp.setId(id) sp.setName(p.name) if p.initialValue is not None: sp.setValue(p.initialValue) sp.setConstant(p.is_constant) sp.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addParameter(sp) for id, r in list(net.rateRules.items()): try: sr = libsbml.RateRule() except NotImplementedError: sr = libsbml.RateRule(sbml_level, sbml_version) sr.setVariable(id) formula = r.replace('**', '^') sr.setMath(libsbml.parseFormula(formula)) sr.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addRule(sr) for id, r in list(net.assignmentRules.items()): try: sr = libsbml.AssignmentRule() except NotImplementedError: sr = libsbml.AssignmentRule(sbml_level, sbml_version) sr.setVariable(id) formula = r.replace('**', '^') sr.setMath(libsbml.parseFormula(formula)) sr.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addRule(sr) for r, r in list(net.algebraicRules.items()): try: sr = libsbml.AlgebraicRule() except NotImplementedError: sr = libsbml.AlgebraicRule(sbml_level, sbml_version) formula = r.replace('**', '^') sr.setMath(libsbml.parseFormula(formula)) sr.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addRule(sr) for id, rxn in list(net.reactions.items()): # Need to identify modifiers in kinetic law and add them to # stoichiometry kl_vars = ExprManip.extract_vars(rxn.kineticLaw) species_in_kl = kl_vars.intersection(list(net.species.keys())) for s in species_in_kl: if s not in rxn.stoichiometry: rxn.stoichiometry[s] = 0 try: srxn = libsbml.Reaction(id) except NotImplementedError: srxn = libsbml.Reaction(sbml_level, sbml_version) srxn.setId(id) srxn.setName(rxn.name) # Handle the case where the model was originally read in from an # SBML file, so that the reactants and products of the Reaction # object are explicitly set. if rxn.reactant_stoichiometry != None and \ rxn.product_stoichiometry != None: for rid, stoich_list in list(rxn.reactant_stoichiometry.items()): for stoich in stoich_list: rxn_add_stoich(srxn, rid, -float(stoich), is_product=False) for rid, stoich_list in list(rxn.product_stoichiometry.items()): for stoich in stoich_list: rxn_add_stoich(srxn, rid, stoich, is_product=True) # Handle the case where the model was created using the SloppyCell # API, in which case reactants and products are inferred from their # stoichiometries else: for rid, stoich in list(rxn.stoichiometry.items()): rxn_add_stoich(srxn, rid, stoich) formula = rxn.kineticLaw.replace('**', '^') try: kl = libsbml.KineticLaw(formula) except NotImplementedError: kl = libsbml.KineticLaw(sbml_level, sbml_version) kl.setFormula(formula) srxn.setKineticLaw(kl) srxn.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addReaction(srxn) for id, e in list(net.events.items()): try: se = libsbml.Event(id) except NotImplementedError: se = libsbml.Event(sbml_level, sbml_version) se.setId(id) se.setName(e.name) formula = e.trigger.replace('**', '^') formula = formula.replace('and_func(', 'and(') formula = formula.replace('or_func(', 'or(') ast = libsbml.parseFormula(formula) if ast is None: raise ValueError('Problem parsing event trigger: %s. Problem may ' 'be use of relational operators (< and >) rather ' 'than libsbml-friendly functions lt and gt.' % formula) try: se.setTrigger(ast) except TypeError: try: trigger = libsbml.Trigger(ast) except NotImplementedError: trigger = libsbml.Trigger(sbml_level, sbml_version) trigger.setMath(ast) se.setTrigger(trigger) formula = str(e.delay).replace('**', '^') try: se.setDelay(libsbml.parseFormula(formula)) except TypeError: try: se.setDelay(libsbml.Delay(libsbml.parseFormula(formula))) except NotImplementedError: delay = libsbml.Delay(sbml_level, sbml_version) delay.setMath(libsbml.parseFormula(formula)) se.setDelay(delay) for varId, formula in list(e.event_assignments.items()): try: sea = libsbml.EventAssignment() except NotImplementedError: sea = libsbml.EventAssignment(sbml_level, sbml_version) sea.setVariable(varId) formula = str(formula).replace('**', '^') formula = formula.replace('and_func(', 'and(') formula = formula.replace('or_func(', 'or(') ast = libsbml.parseFormula(formula) replaceTime(ast) sea.setMath(ast) se.addEventAssignment(sea) se.setMetaId('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addEvent(se) for id, con in list(net.constraints.items()): try: scon = libsbml.Constraint() except NotImplementedError: scon = libsbml.Constraint(sbml_level, sbml_version) scon.setId(con.id) scon.setName(con.name) formula = con.trigger.replace('**', '^') ast = libsbml.parseFormula(formula) if ast is None: raise ValueError( 'Problem parsing constraint math: %s. Problem may ' 'be use of relational operators (< and >) rather ' 'than libsbml-friendly functions lt and gt.' % formula) scon.setMath(ast) se.setcon('SloppyCell_{0:05d}'.format(metaId)) metaId += 1 m.addConstraint(scon) d = libsbml.SBMLDocument(sbml_level, sbml_version) d.setModel(m) sbmlStr = libsbml.writeSBMLToString(d) return sbmlStr
def toSBMLString(net): try: m = libsbml.Model(net.id) except NotImplementedError: m = libsbml.Model(sbml_level, sbml_version) m.setId(net.id) m.setName(net.name) for id, c in net.compartments.items(): try: sc = libsbml.Compartment(id) except NotImplementedError: sc = libsbml.Compartment(sbml_level, sbml_version) sc.setId(id) sc.setName(c.name) sc.setConstant(c.is_constant) sc.setSize(c.initialValue) save_notes(c, sc) m.addCompartment(sc) for id, s in net.species.items(): try: ss = libsbml.Species(id) except NotImplementedError: ss = libsbml.Species(sbml_level, sbml_version) ss.setId(id) ss.setName(s.name) ss.setCompartment(s.compartment) if s.initialValue is not None and not isinstance(s.initialValue, str): ss.setInitialConcentration(s.initialValue) ss.setBoundaryCondition(s.is_boundary_condition) save_notes(s, ss) m.addSpecies(ss) for id, p in net.parameters.items(): try: sp = libsbml.Parameter(id) except NotImplementedError: sp = libsbml.Parameter(sbml_level, sbml_version) sp.setId(id) sp.setName(p.name) if p.initialValue is not None: sp.setValue(p.initialValue) sp.setConstant(p.is_constant) save_notes(p, sp) m.addParameter(sp) for id, r in net.rateRules.items(): try: sr = libsbml.RateRule() except NotImplementedError: sr = libsbml.RateRule(sbml_level, sbml_version) sr.setVariable(id) formula = r.replace('**', '^') sr.setMath(libsbml.parseFormula(formula)) m.addRule(sr) for id, r in net.assignmentRules.items(): try: sr = libsbml.AssignmentRule() except NotImplementedError: sr = libsbml.AssignmentRule(sbml_level, sbml_version) sr.setVariable(id) formula = r.replace('**', '^') sr.setMath(libsbml.parseFormula(formula)) m.addRule(sr) for r, r in net.algebraicRules.items(): try: sr = libsbml.AlgebraicRule() except NotImplementedError: sr = libsbml.AlgebraicRule(sbml_level, sbml_version) formula = r.replace('**', '^') sr.setMath(libsbml.parseFormula(formula)) m.addRule(sr) for id, rxn in net.reactions.items(): try: srxn = libsbml.Reaction(id) except NotImplementedError: srxn = libsbml.Reaction(sbml_level, sbml_version) srxn.setId(id) srxn.setName(rxn.name) save_notes(rxn, srxn) # Handle the case where the model was originally read in from an # SBML file, so that the reactants and products of the Reaction # object are explicitly set. if rxn.reactant_stoichiometry != None and \ rxn.product_stoichiometry != None: for rid, stoich_list in rxn.reactant_stoichiometry.items(): for stoich in stoich_list: # 'stoich' is a string. If it may be easily cast # to a float, we expect it to be a positive float; # however, if we pass either a float or a string # which may be converted to a float to # rxn_add_stoich, the sign of that float will # determine whether rxn_add_stoich adds a reactant # or a product, so we may need to multiply by -1.0 # here. It is also possible that 'stoich' is a # math expression, in which case the 'is_product' # argument controls whether a reactant or product # is added. try: stoich = float(stoich) rxn_add_stoich(srxn, rid, -stoich, is_product=False) except ValueError: rxn_add_stoich(srxn, rid, stoich, is_product=False) for rid, stoich_list in rxn.product_stoichiometry.items(): for stoich in stoich_list: rxn_add_stoich(srxn, rid, stoich, is_product=True) # Handle the case where the model was created using the SloppyCell # API, in which case reactants and products are inferred from their # stoichiometries else: for rid, stoich in rxn.stoichiometry.items(): rxn_add_stoich(srxn, rid, stoich) # Ensure kinetic laws exist (as they may not for FBA-type models) # -- not clear what a good default kinetic law is; use '0', # which should at least lead to unsubtle problems if not rxn.kineticLaw: formula = '0' else: formula = rxn.kineticLaw formula = formula.replace('**', '^') try: kl = libsbml.KineticLaw(formula) except NotImplementedError: kl = libsbml.KineticLaw(sbml_level, sbml_version) kl.setFormula(formula) srxn.setKineticLaw(kl) # Set the optional reversibility attribute, if one is present. if hasattr(rxn,'reversible'): srxn.setReversible(rxn.reversible) m.addReaction(srxn) # Set the kinetic law's parameters # Note that, properly, we should be handling units and other # attributes here; we are most concerned about COBRA SBML # models, where these appear to be of secondary importance # (Yeastnet 7, for example, defines a lot of these # parameters as 'dimensionless'.) for parameter, value in getattr(rxn, 'parameters', {}).iteritems(): # The createKineticLawParameter method creates a parameter # in the most kinetic law of the most recently created # reaction in the model. sparameter = m.createKineticLawParameter() print sparameter sparameter.setId(parameter) sparameter.setValue(value) d = libsbml.SBMLDocument(sbml_level, sbml_version) d.setModel(m) save_notes(net, d) sbmlStr = libsbml.writeSBMLToString(d) return sbmlStr
def setUp(self): self.RR = libsbml.RateRule(1, 2) if (self.RR == None): pass pass
def test_RateRule_constructor(self): s = None try: s = libsbml.RateRule(1, 1) s = libsbml.RateRule(1, 2) s = libsbml.RateRule(2, 1) s = libsbml.RateRule(2, 2) s = libsbml.RateRule(2, 3) s = libsbml.RateRule(2, 4) s = libsbml.RateRule(3, 1) s = libsbml.RateRule(self.SN11) s = libsbml.RateRule(self.SN12) s = libsbml.RateRule(self.SN21) s = libsbml.RateRule(self.SN22) s = libsbml.RateRule(self.SN23) s = libsbml.RateRule(self.SN24) s = libsbml.RateRule(self.SN31) except ValueError: inst = sys.exc_info()[1] s = None pass self.assert_(s != None) msg = "" try: s = libsbml.RateRule(9, 9) except ValueError: inst = sys.exc_info()[1] msg = inst.args[0] pass self.assertEqual(msg, self.ERR_MSG) msg = "" try: s = libsbml.RateRule(self.SN99) except ValueError: inst = sys.exc_info()[1] msg = inst.args[0] pass self.assertEqual(msg, self.ERR_MSG)