Ejemplo n.º 1
0
 def test_Model_getFunctionDefinitionById(self):
     fd1 = libsbml.FunctionDefinition(2, 4)
     fd2 = libsbml.FunctionDefinition(2, 4)
     fd1.setId("sin")
     fd2.setId("cos")
     fd1.setMath(libsbml.parseFormula("2"))
     fd2.setMath(libsbml.parseFormula("2"))
     self.M.addFunctionDefinition(fd1)
     self.M.addFunctionDefinition(fd2)
     self.assert_(self.M.getNumFunctionDefinitions() == 2)
     self.assert_(self.M.getFunctionDefinition("sin") != fd1)
     self.assert_(self.M.getFunctionDefinition("cos") != fd2)
     self.assert_(self.M.getFunctionDefinition("tan") == None)
     pass
Ejemplo n.º 2
0
 def test_Model_add_get_FunctionDefinitions(self):
     fd1 = libsbml.FunctionDefinition(2, 4)
     fd2 = libsbml.FunctionDefinition(2, 4)
     fd1.setId("fd1")
     fd2.setId("fd2")
     fd1.setMath(libsbml.parseFormula("2"))
     fd2.setMath(libsbml.parseFormula("2"))
     self.M.addFunctionDefinition(fd1)
     self.M.addFunctionDefinition(fd2)
     self.assert_(self.M.getNumFunctionDefinitions() == 2)
     self.assert_(self.M.getFunctionDefinition(0) != fd1)
     self.assert_(self.M.getFunctionDefinition(1) != fd2)
     self.assert_(self.M.getFunctionDefinition(2) == None)
     pass
Ejemplo n.º 3
0
 def test_FunctionDefinition_copyConstructor(self):
   o1 = libsbml.FunctionDefinition(2,4)
   o1.setId("c")
   self.assert_( o1.getId() ==  "c" )
   node = libsbml.ASTNode(libsbml.AST_CONSTANT_PI)
   o1.setMath(node)
   node = None
   self.assert_( o1.getMath() != None )
   o2 = libsbml.FunctionDefinition(o1)
   self.assert_( o2.getId() ==  "c" )
   self.assert_( o2.getMath() != None )
   self.assert_( o2.getParentSBMLObject() == o1.getParentSBMLObject() )
   o2 = None
   o1 = None
   pass  
Ejemplo n.º 4
0
 def test_FunctionDefinition(self):
     fd = libsbml.FunctionDefinition(2, 4)
     self.assertEqual(False, (fd.hasRequiredAttributes()))
     fd.setId("fd")
     self.assertEqual(True, fd.hasRequiredAttributes())
     fd = None
     pass
Ejemplo n.º 5
0
 def test_FunctionDefinition_createWith(self):
     math = libsbml.parseFormula("lambda(x, x^3)")
     fd = libsbml.FunctionDefinition(2, 4)
     fd.setId("pow3")
     fd.setMath(math)
     self.assert_(fd.getTypeCode() == libsbml.SBML_FUNCTION_DEFINITION)
     self.assert_(fd.getMetaId() == "")
     self.assert_(fd.getNotes() == None)
     self.assert_(fd.getAnnotation() == None)
     self.assert_(fd.getName() == "")
     math1 = fd.getMath()
     self.assert_(math1 != None)
     formula = libsbml.formulaToString(math1)
     self.assert_(formula != None)
     self.assert_(("lambda(x, x^3)" == formula))
     self.assert_(fd.getMath() != math)
     self.assertEqual(True, fd.isSetMath())
     self.assert_(("pow3" == fd.getId()))
     self.assertEqual(True, fd.isSetId())
     _dummyList = [math]
     _dummyList[:] = []
     del _dummyList
     _dummyList = [fd]
     _dummyList[:] = []
     del _dummyList
     pass
Ejemplo n.º 6
0
 def test_FunctionDefinition(self):
   fd = libsbml.FunctionDefinition(2,4)
   self.assertEqual( False, (fd.hasRequiredElements()) )
   fd.setMath(libsbml.parseFormula("fd"))
   self.assertEqual( True, fd.hasRequiredElements() )
   fd = None
   pass  
Ejemplo n.º 7
0
 def test_FunctionDefinition_parent_add(self):
     fd = libsbml.FunctionDefinition(2, 4)
     m = libsbml.Model(2, 4)
     fd.setId("fd")
     fd.setMath(libsbml.parseFormula("l"))
     m.addFunctionDefinition(fd)
     fd = None
     lo = m.getListOfFunctionDefinitions()
     self.assert_(lo == m.getFunctionDefinition(0).getParentSBMLObject())
     self.assert_(m == lo.getParentSBMLObject())
     pass
 def test_internal_consistency_check_99911_fd(self):
   d = libsbml.SBMLDocument(2,4)
   m = d.createModel()
   fd = libsbml.FunctionDefinition(2,4)
   d.setLevelAndVersion(2,1,False)
   fd.setId("fd")
   fd.setSBOTerm(2)
   m.addFunctionDefinition(fd)
   errors = d.checkInternalConsistency()
   self.assert_( errors == 0 )
   d = None
   pass  
 def test_internal_consistency_check_99912(self):
   d = libsbml.SBMLDocument(2,4)
   fd = libsbml.FunctionDefinition(2,4)
   m = d.createModel()
   d.setLevelAndVersion(1,2,False)
   c = m.createCompartment()
   c.setId("cc")
   c.setConstant(False)
   m.addFunctionDefinition(fd)
   errors = d.checkInternalConsistency()
   self.assert_( errors == 0 )
   d = None
   pass  
Ejemplo n.º 10
0
 def test_FunctionDefinition_ancestor_add(self):
   fd = libsbml.FunctionDefinition(2,4)
   m = libsbml.Model(2,4)
   fd.setId("fd")
   fd.setMath(libsbml.parseFormula("l"))
   m.addFunctionDefinition(fd)
   fd = None
   lo = m.getListOfFunctionDefinitions()
   obj = m.getFunctionDefinition(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  
Ejemplo n.º 11
0
 def test_Model_addFunctionDefinition4(self):
     m = libsbml.Model(2, 2)
     fd = libsbml.FunctionDefinition(2, 2)
     fd.setId("fd")
     fd.setMath(libsbml.parseFormula("fd"))
     fd1 = libsbml.FunctionDefinition(2, 2)
     fd1.setId("fd")
     fd1.setMath(libsbml.parseFormula("fd"))
     i = m.addFunctionDefinition(fd)
     self.assert_(i == libsbml.LIBSBML_OPERATION_SUCCESS)
     self.assert_(m.getNumFunctionDefinitions() == 1)
     i = m.addFunctionDefinition(fd1)
     self.assert_(i == libsbml.LIBSBML_DUPLICATE_OBJECT_ID)
     self.assert_(m.getNumFunctionDefinitions() == 1)
     _dummyList = [fd]
     _dummyList[:] = []
     del _dummyList
     _dummyList = [fd1]
     _dummyList[:] = []
     del _dummyList
     _dummyList = [m]
     _dummyList[:] = []
     del _dummyList
     pass
Ejemplo n.º 12
0
 def test_Model_addFunctionDefinition2(self):
     m = libsbml.Model(2, 2)
     fd = libsbml.FunctionDefinition(2, 1)
     fd.setId("fd")
     fd.setMath(libsbml.parseFormula("fd"))
     i = m.addFunctionDefinition(fd)
     self.assert_(i == libsbml.LIBSBML_VERSION_MISMATCH)
     self.assert_(m.getNumFunctionDefinitions() == 0)
     _dummyList = [fd]
     _dummyList[:] = []
     del _dummyList
     _dummyList = [m]
     _dummyList[:] = []
     del _dummyList
     pass
 def test_FunctionDefinition_createWithNS(self):
   xmlns = libsbml.XMLNamespaces()
   xmlns.add( "http://www.sbml.org", "testsbml")
   sbmlns = libsbml.SBMLNamespaces(2,1)
   sbmlns.addNamespaces(xmlns)
   object = libsbml.FunctionDefinition(sbmlns)
   self.assert_( object.getTypeCode() == libsbml.SBML_FUNCTION_DEFINITION )
   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  
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
 def setUp(self):
     self.FD = libsbml.FunctionDefinition(2, 4)
     if (self.FD == None):
         pass
     pass
    def test_FunctionDefinition_constructor(self):
        s = None
        try:
            s = libsbml.FunctionDefinition(2, 1)
            s = libsbml.FunctionDefinition(2, 2)
            s = libsbml.FunctionDefinition(2, 3)
            s = libsbml.FunctionDefinition(2, 4)
            s = libsbml.FunctionDefinition(3, 1)
            s = libsbml.FunctionDefinition(self.SN21)
            s = libsbml.FunctionDefinition(self.SN22)
            s = libsbml.FunctionDefinition(self.SN23)
            s = libsbml.FunctionDefinition(self.SN24)
            s = libsbml.FunctionDefinition(self.SN31)
        except ValueError:
            s = None
        pass
        self.assert_(s != None)

        msg = ""
        try:
            s = libsbml.FunctionDefinition(1, 1)
        except ValueError:
            inst = sys.exc_info()[1]
            msg = inst.args[0]
        pass
        self.assertEqual(msg, self.ERR_MSG)

        msg = ""
        try:
            s = libsbml.FunctionDefinition(1, 2)
        except ValueError:
            inst = sys.exc_info()[1]
            msg = inst.args[0]
        pass
        self.assertEqual(msg, self.ERR_MSG)

        msg = ""
        try:
            s = libsbml.FunctionDefinition(9, 9)
        except ValueError:
            inst = sys.exc_info()[1]
            msg = inst.args[0]
        pass
        self.assertEqual(msg, self.ERR_MSG)

        msg = ""
        try:
            s = libsbml.FunctionDefinition(self.SN11)
        except ValueError:
            inst = sys.exc_info()[1]
            msg = inst.args[0]
        pass
        self.assertEqual(msg, self.ERR_MSG)

        msg = ""
        try:
            s = libsbml.FunctionDefinition(self.SN12)
        except ValueError:
            inst = sys.exc_info()[1]
            msg = inst.args[0]
        pass
        self.assertEqual(msg, self.ERR_MSG)

        try:
            s = libsbml.FunctionDefinition(self.SN99)
        except ValueError:
            inst = sys.exc_info()[1]
            msg = inst.args[0]
        pass
        self.assertEqual(msg, self.ERR_MSG)