def test_AlgebraicRule_L1(self): ar = libsbml.AlgebraicRule(1, 2) self.assertEqual(False, (ar.hasRequiredAttributes())) ar.setFormula("ar") self.assertEqual(True, ar.hasRequiredAttributes()) ar = None pass
def test_AlgebraicRule(self): ar = libsbml.AlgebraicRule(2,4) self.assertEqual( False, (ar.hasRequiredElements()) ) ar.setMath(libsbml.parseFormula("ar")) self.assertEqual( True, ar.hasRequiredElements() ) ar = None pass
def test_Model_getRules(self): ar = libsbml.AlgebraicRule(2, 4) scr = libsbml.AssignmentRule(2, 4) cvr = libsbml.AssignmentRule(2, 4) pr = libsbml.AssignmentRule(2, 4) scr.setVariable("r2") cvr.setVariable("r3") pr.setVariable("r4") ar.setFormula("x + 1") scr.setFormula("k * t/(1 + k)") cvr.setFormula("0.10 * t") pr.setFormula("k3/k2") self.M.addRule(ar) self.M.addRule(scr) self.M.addRule(cvr) self.M.addRule(pr) self.assert_(self.M.getNumRules() == 4) ar = self.M.getRule(0) scr = self.M.getRule(1) cvr = self.M.getRule(2) pr = self.M.getRule(3) self.assert_(("x + 1" == ar.getFormula())) self.assert_(("k * t/(1 + k)" == scr.getFormula())) self.assert_(("0.10 * t" == cvr.getFormula())) self.assert_(("k3/k2" == pr.getFormula())) pass
def test_Rule_setVariable3(self): R1 = libsbml.AlgebraicRule(1, 2) i = R1.setVariable("r") self.assert_(i == libsbml.LIBSBML_UNEXPECTED_ATTRIBUTE) self.assertEqual(False, R1.isSetVariable()) _dummyList = [R1] _dummyList[:] = [] del _dummyList pass
def test_internal_consistency_check_99915_alg(self): d = libsbml.SBMLDocument(2,4) m = d.createModel() r = libsbml.AlgebraicRule(2,4) d.setLevelAndVersion(2,1,False) r.setUnits("kk") m.addRule(r) errors = d.checkInternalConsistency() self.assert_( errors == 0 ) d = None pass
def test_AlgebraicRule_createWithMath(self): math = libsbml.parseFormula("1 + 1") ar = libsbml.AlgebraicRule(2, 4) ar.setMath(math) self.assert_(ar.getTypeCode() == libsbml.SBML_ALGEBRAIC_RULE) self.assert_(ar.getMetaId() == "") self.assert_(("1 + 1" == ar.getFormula())) self.assert_(ar.getMath() != math) _dummyList = [ar] _dummyList[:] = [] del _dummyList pass
def test_internal_consistency_check_99904_rule_alg(self): d = libsbml.SBMLDocument(2,4) r = libsbml.AlgebraicRule(2,4) d.setLevelAndVersion(1,2,False) m = d.createModel() c = m.createCompartment() c.setId("cc") r.setMetaId("mmm") r.setFormula("2") m.addRule(r) errors = d.checkInternalConsistency() self.assert_( errors == 0 ) d = 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_AlgebraicRule_createWithFormula(self): ar = libsbml.AlgebraicRule(2, 4) ar.setFormula("1 + 1") self.assert_(ar.getTypeCode() == libsbml.SBML_ALGEBRAIC_RULE) self.assert_(ar.getMetaId() == "") math = ar.getMath() self.assert_(math != None) formula = libsbml.formulaToString(math) self.assert_(formula != None) self.assert_(("1 + 1" == formula)) self.assert_((formula == ar.getFormula())) _dummyList = [ar] _dummyList[:] = [] del _dummyList pass
def test_AlgebraicRule_createWithNS(self): xmlns = libsbml.XMLNamespaces() xmlns.add("http://www.sbml.org", "testsbml") sbmlns = libsbml.SBMLNamespaces(2, 3) sbmlns.addNamespaces(xmlns) r = libsbml.AlgebraicRule(sbmlns) self.assert_(r.getTypeCode() == libsbml.SBML_ALGEBRAIC_RULE) self.assert_(r.getMetaId() == "") self.assert_(r.getNotes() == None) self.assert_(r.getAnnotation() == None) self.assert_(r.getLevel() == 2) self.assert_(r.getVersion() == 3) self.assert_(r.getNamespaces() != None) self.assert_(r.getNamespaces().getLength() == 2) _dummyList = [r] _dummyList[:] = [] del _dummyList pass
def setUp(self): self.R = libsbml.AlgebraicRule(2, 4) if (self.R == None): pass pass
def test_AlgebraicRule(self): ar = libsbml.AlgebraicRule(2, 4) self.assertEqual(True, ar.hasRequiredAttributes()) ar = None 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 test_AlgebraicRule_constructor(self): s = None try: s = libsbml.AlgebraicRule(1, 1) s = libsbml.AlgebraicRule(1, 2) s = libsbml.AlgebraicRule(2, 1) s = libsbml.AlgebraicRule(2, 2) s = libsbml.AlgebraicRule(2, 3) s = libsbml.AlgebraicRule(2, 4) s = libsbml.AlgebraicRule(3, 1) s = libsbml.AlgebraicRule(self.SN11) s = libsbml.AlgebraicRule(self.SN12) s = libsbml.AlgebraicRule(self.SN21) s = libsbml.AlgebraicRule(self.SN22) s = libsbml.AlgebraicRule(self.SN23) s = libsbml.AlgebraicRule(self.SN24) s = libsbml.AlgebraicRule(self.SN31) except ValueError: inst = sys.exc_info()[1] s = None pass self.assert_(s != None) msg = "" try: s = libsbml.AlgebraicRule(9, 9) except ValueError: inst = sys.exc_info()[1] msg = inst.args[0] pass self.assertEqual(msg, self.ERR_MSG) msg = "" try: s = libsbml.AlgebraicRule(self.SN99) except ValueError: inst = sys.exc_info()[1] msg = inst.args[0] pass self.assertEqual(msg, self.ERR_MSG)