/** @cond doxygenLibsbmlInternal */ IdList* SBMLLevelVersionConverter::collectSpeciesReferenceIds() { IdList* srids = new IdList(); for (unsigned int i = 0; i < mDocument->getModel()->getNumReactions(); i++) { Reaction *r = mDocument->getModel()->getReaction(i); for (unsigned int j = 0; j < r->getNumReactants(); j++) { if (r->getReactant(j)->isSetId()) { srids->append(r->getReactant(j)->getId()); } } for (unsigned int j = 0; j < r->getNumProducts(); j++) { if (r->getProduct(j)->isSetId()) { srids->append(r->getProduct(j)->getId()); } } } return srids; }
std::string fixMissingStoich(const std::string sbml) { SBMLDocument *doc = NULL; try { doc = readSBMLFromString (sbml.c_str()); Model *m = doc->getModel(); for (int j = 0; j<m->getNumReactions(); ++j) { Reaction* r = m->getReaction(j); if (!r) throw std::runtime_error("No reaction"); // check stoich defined on reactants / products for (int k = 0; k<r->getNumReactants(); ++k) { SpeciesReference* s = r->getReactant(k); if (!isStoichDefined(s)) if (s->setStoichiometry(1.) != LIBSBML_OPERATION_SUCCESS) throw std::runtime_error("Unable to set stoichiometry"); } for (int k = 0; k<r->getNumProducts(); ++k) { SpeciesReference* s = r->getProduct(k); if (!isStoichDefined(s)) if (s->setStoichiometry(1.) != LIBSBML_OPERATION_SUCCESS) throw std::runtime_error("Unable to set stoichiometry"); } // modifiers have no stoichiometry } } catch(...) { delete doc; throw; } SBMLWriter writer; char* sbml_cstr = writer.writeSBMLToString(doc); delete doc; std::string result(sbml_cstr); free(sbml_cstr); return result; }
void Model::setSpeciesReferenceConstantValueAndStoichiometry() { for (unsigned int i = 0; i < getNumReactions(); i++) { Reaction *r = getReaction(i); unsigned int j; for (j = 0; j < r->getNumReactants(); j++) { if (!(r->getReactant(j)->isSetStoichiometryMath())) { r->getReactant(j)->setConstant(true); if (!(r->getReactant(j)->isSetStoichiometry())) { r->getReactant(j)->setStoichiometry(1); } } else { r->getReactant(j)->setConstant(false); } } for (j = 0; j < r->getNumProducts(); j++) { if (!(r->getProduct(j)->isSetStoichiometryMath())) { r->getProduct(j)->setConstant(true); if (!(r->getProduct(j)->isSetStoichiometry())) { r->getProduct(j)->setStoichiometry(1); } } else { r->getProduct(j)->setConstant(false); } } } }
/* * Checks that all variables referenced in FunctionDefinition bodies are * bound variables (function arguments). */ void KineticLawVars::check_ (const Model& m, const Reaction& r) { unsigned int n; /* create list of all species in the reaction */ for (n = 0; n < r.getNumReactants(); n++) { mSpecies.append(r.getReactant(n)->getSpecies()); } for (n = 0; n < r.getNumProducts(); n++) { mSpecies.append(r.getProduct(n)->getSpecies()); } for (n = 0; n < r.getNumModifiers(); n++) { mSpecies.append(r.getModifier(n)->getSpecies()); } if ( r.isSetKineticLaw() && r.getKineticLaw()->isSetMath() ) { const ASTNode* math = r.getKineticLaw()->getMath(); List* names = math->getListOfNodes( ASTNode_isName ); for (n = 0; n < names->getSize(); ++n) { ASTNode* node = static_cast<ASTNode*>( names->get(n) ); string name = node->getName() ? node->getName() : ""; if (m.getSpecies(name) != NULL && !mSpecies.contains(name) ) logUndefined(r, name); } delete names; } mSpecies.clear(); }
int main (int argc, char* argv[]) { if (argc != 2) { cout << endl << "Usage: printNotes filename" << endl << endl; return 1; } unsigned int i,j; const char* filename = argv[1]; SBMLDocument* document; SBMLReader reader; document = reader.readSBML(filename); unsigned int errors = document->getNumErrors(); cout << endl; cout << "filename: " << filename << endl; cout << endl; if(errors > 0) { document->printErrors(cerr); delete document; return errors; } /* Model */ Model* m = document->getModel(); printNotes(m); for(i=0; i < m->getNumReactions(); i++) { Reaction* re = m->getReaction(i); printNotes(re); /* SpeciesReference (Reacatant) */ for(j=0; j < re->getNumReactants(); j++) { SpeciesReference* rt = re->getReactant(j); if (rt->isSetNotes()) cout << " "; printNotes(rt, (rt->isSetSpecies() ? rt->getSpecies() : std::string("")) ); } /* SpeciesReference (Product) */ for(j=0; j < re->getNumProducts(); j++) { SpeciesReference* rt = re->getProduct(j); if (rt->isSetNotes()) cout << " "; printNotes(rt, (rt->isSetSpecies() ? rt->getSpecies() : std::string("")) ); } /* ModifierSpeciesReference (Modifier) */ for(j=0; j < re->getNumModifiers(); j++) { ModifierSpeciesReference* md = re->getModifier(j); if (md->isSetNotes()) cout << " "; printNotes(md, (md->isSetSpecies() ? md->getSpecies() : std::string("")) ); } /* Kineticlaw */ if(re->isSetKineticLaw()) { KineticLaw* kl = re->getKineticLaw(); if (kl->isSetNotes()) cout << " "; printNotes(kl); /* Parameter */ for(j=0; j < kl->getNumParameters(); j++) { Parameter* pa = kl->getParameter(j); if (pa->isSetNotes()) cout << " "; printNotes(pa); } } } /* Species */ for(i=0; i < m->getNumSpecies(); i++) { Species* sp = m->getSpecies(i); printNotes(sp); } /* Compartment */ for(i=0; i < m->getNumCompartments(); i++) { Compartment* sp = m->getCompartment(i); printNotes(sp); } /* FunctionDefinition */ for(i=0; i < m->getNumFunctionDefinitions(); i++) { FunctionDefinition* sp = m->getFunctionDefinition(i); printNotes(sp); } /* UnitDefinition */ for(i=0; i < m->getNumUnitDefinitions(); i++) { UnitDefinition* sp = m->getUnitDefinition(i); printNotes(sp); } /* Parameter */ for(i=0; i < m->getNumParameters(); i++) { Parameter* sp = m->getParameter(i); printNotes(sp); } /* Rule */ for(i=0; i < m->getNumRules(); i++) { Rule* sp = m->getRule(i); printNotes(sp); } /* InitialAssignment */ for(i=0; i < m->getNumInitialAssignments(); i++) { InitialAssignment* sp = m->getInitialAssignment(i); printNotes(sp); } /* Event */ for(i=0; i < m->getNumEvents(); i++) { Event* sp = m->getEvent(i); printNotes(sp); /* Trigger */ if(sp->isSetTrigger()) { const Trigger* tg = sp->getTrigger(); if (tg->isSetNotes()) cout << " "; printNotes(const_cast<Trigger*>(tg)); } /* Delay */ if(sp->isSetDelay()) { const Delay* dl = sp->getDelay(); if (dl->isSetNotes()) cout << " "; printNotes(const_cast<Delay*>(dl)); } /* EventAssignment */ for(j=0; j < sp->getNumEventAssignments(); j++) { EventAssignment* ea = sp->getEventAssignment(j); if (ea->isSetNotes()) cout << " "; printNotes(ea); } } /* SpeciesType */ for(i=0; i < m->getNumSpeciesTypes(); i++) { SpeciesType* sp = m->getSpeciesType(i); printNotes(sp); } /* Constraint */ for(i=0; i < m->getNumConstraints(); i++) { Constraint* sp = m->getConstraint(i); printNotes(sp); } delete document; return errors; }
void Model::removeDuplicateTopLevelAnnotations() { unsigned int i, n; this->removeDuplicateAnnotations(); if (getNumFunctionDefinitions() > 0) { getListOfFunctionDefinitions()->removeDuplicateAnnotations(); for (i = 0; i < getNumFunctionDefinitions(); i++) { getFunctionDefinition(i)->removeDuplicateAnnotations(); } } if (getNumUnitDefinitions() > 0) { getListOfUnitDefinitions()->removeDuplicateAnnotations(); for (i = 0; i < getNumUnitDefinitions(); i++) { getUnitDefinition(i)->removeDuplicateAnnotations(); getUnitDefinition(i)->getListOfUnits()->removeDuplicateAnnotations(); for (n = 0; n < getUnitDefinition(i)->getNumUnits(); n++) { getUnitDefinition(i)->getUnit(n)->removeDuplicateAnnotations(); } } } if (getNumCompartmentTypes() > 0) { getListOfCompartmentTypes()->removeDuplicateAnnotations(); for (i = 0; i < getNumCompartmentTypes(); i++) { getCompartmentType(i)->removeDuplicateAnnotations(); } } if (getNumSpeciesTypes() > 0) { getListOfSpeciesTypes()->removeDuplicateAnnotations(); for (i = 0; i < getNumSpeciesTypes(); i++) { getSpeciesType(i)->removeDuplicateAnnotations(); } } if (getNumCompartments() > 0) { getListOfCompartments()->removeDuplicateAnnotations(); for (i = 0; i < getNumCompartments(); i++) { getCompartment(i)->removeDuplicateAnnotations(); } } if (getNumSpecies() > 0) { getListOfSpecies()->removeDuplicateAnnotations(); for (i = 0; i < getNumSpecies(); i++) { getSpecies(i)->removeDuplicateAnnotations(); } } if (getNumParameters() > 0) { getListOfParameters()->removeDuplicateAnnotations(); for (i = 0; i < getNumParameters(); i++) { getParameter(i)->removeDuplicateAnnotations(); } } if (getNumInitialAssignments() > 0) { getListOfInitialAssignments()->removeDuplicateAnnotations(); for (i = 0; i < getNumInitialAssignments(); i++) { getInitialAssignment(i)->removeDuplicateAnnotations(); } } if (getNumConstraints() > 0) { getListOfConstraints()->removeDuplicateAnnotations(); for (i = 0; i < getNumConstraints(); i++) { getConstraint(i)->removeDuplicateAnnotations(); } } if (getNumRules() > 0) { getListOfRules()->removeDuplicateAnnotations(); for (i = 0; i < getNumRules(); i++) { getRule(i)->removeDuplicateAnnotations(); } } if (getNumReactions() > 0) { getListOfReactions()->removeDuplicateAnnotations(); for (i = 0; i < getNumReactions(); i++) { Reaction * r = getReaction(i); r->removeDuplicateAnnotations(); if (r->getNumReactants() > 0) { r->getListOfReactants()->removeDuplicateAnnotations(); for (n = 0; n < r->getNumReactants(); n++) { r->getReactant(n)->removeDuplicateAnnotations(); } } if (r->getNumProducts() > 0) { r->getListOfProducts()->removeDuplicateAnnotations(); for (n = 0; n < r->getNumProducts(); n++) { r->getProduct(n)->removeDuplicateAnnotations(); } } if (r->getNumModifiers() > 0) { r->getListOfModifiers()->removeDuplicateAnnotations(); for (n = 0; n < r->getNumModifiers(); n++) { r->getModifier(n)->removeDuplicateAnnotations(); } } if (r->isSetKineticLaw()) { r->getKineticLaw()->removeDuplicateAnnotations(); if (r->getKineticLaw()->getNumParameters() > 0) { r->getKineticLaw()->getListOfParameters() ->removeDuplicateAnnotations(); for (n = 0; n < r->getKineticLaw()->getNumParameters(); n++) { r->getKineticLaw()->getParameter(n)->removeDuplicateAnnotations(); } } } } } if (getNumEvents() > 0) { getListOfEvents()->removeDuplicateAnnotations(); for (i = 0; i < getNumEvents(); i++) { getEvent(i)->removeDuplicateAnnotations(); if (getEvent(i)->getNumEventAssignments() > 0) { getEvent(i)->getListOfEventAssignments()->removeDuplicateAnnotations(); for (n = 0; n < getEvent(i)->getNumEventAssignments(); n++) { getEvent(i)->getEventAssignment(n)->removeDuplicateAnnotations(); } } } } }
void Model::convertStoichiometryMath() { unsigned int n, j; Reaction * r; SpeciesReference *sr; unsigned int idCount = 0; char newid[15]; std::string id; for (n = 0; n < getNumReactions(); n++) { r = getReaction(n); for (j = 0; j < r->getNumReactants(); j++) { sr = r->getReactant(j); if (sr->isSetStoichiometryMath()) { if (!sr->isSetId()) { sprintf(newid, "generatedId_%u", idCount); id.assign(newid); sr->setId(id); idCount++; } else { id = sr->getId(); } sr->setConstant(false); AssignmentRule * ar = createAssignmentRule(); ar->setVariable(id); if (sr->getStoichiometryMath()->isSetMath()) { ar->setMath(sr->getStoichiometryMath()->getMath()); } } } for (j = 0; j < r->getNumProducts(); j++) { sr = r->getProduct(j); if (sr->isSetStoichiometryMath()) { if (!sr->isSetId()) { sprintf(newid, "generatedId_%u", idCount); id.assign(newid); sr->setId(id); idCount++; } else { id = sr->getId(); } sr->setConstant(false); AssignmentRule * ar = createAssignmentRule(); ar->setVariable(id); if (sr->getStoichiometryMath()->isSetMath()) { ar->setMath(sr->getStoichiometryMath()->getMath()); } } } } }
void dealWithAssigningL1Stoichiometry(Model & m, bool l2) { //char newid[15]; std::string id; for (unsigned int i = 0; i < m.getNumReactions(); i++) { Reaction *r = m.getReaction(i); unsigned int j; for (j = 0; j < r->getNumReactants(); j++) { SpeciesReference *sr = r->getReactant(j); // we do not get here unless the stoichiometryMath is an integer // or a rational if (l2 == true && sr->isSetStoichiometryMath() == true) { const ASTNode* ast = sr->getStoichiometryMath()->getMath(); if (ast->isInteger()) { int num = ast->getInteger(); sr->setStoichiometry(num); sr->setDenominator(1); } else { int num = ast->getNumerator(); int denom = ast->getDenominator(); sr->setStoichiometry(num); sr->setDenominator(denom); } sr->unsetStoichiometryMath(); } else { sr->setStoichiometry(sr->getStoichiometry()); sr->setDenominator(1); } } for (j = 0; j < r->getNumProducts(); j++) { SpeciesReference *sr = r->getProduct(j); // we do not get here unless the stoichiometryMath is an integer // or a rational if (l2 == true && sr->isSetStoichiometryMath() == true) { const ASTNode* ast = sr->getStoichiometryMath()->getMath(); if (ast->isInteger()) { int num = ast->getInteger(); sr->setStoichiometry(num); sr->setDenominator(1); } else { int num = ast->getNumerator(); int denom = ast->getDenominator(); sr->setStoichiometry(num); sr->setDenominator(denom); } sr->unsetStoichiometryMath(); } else { sr->setStoichiometry(sr->getStoichiometry()); sr->setDenominator(1); } } } }
void dealWithL1Stoichiometry(Model & m, bool l2) { unsigned int idCount = 0; char newid[15]; std::string id; for (unsigned int i = 0; i < m.getNumReactions(); i++) { Reaction *r = m.getReaction(i); unsigned int j; for (j = 0; j < r->getNumReactants(); j++) { SpeciesReference *sr = r->getReactant(j); if (sr->getDenominator() != 1) { long stoich = static_cast<long>(sr->getStoichiometry()); int denom = sr->getDenominator(); ASTNode *node = new ASTNode(); node->setValue(stoich, denom); if (l2 == true) { StoichiometryMath * sm = sr->createStoichiometryMath(); sm->setMath(node); } else { sprintf(newid, "speciesRefId_%u", idCount); id.assign(newid); idCount++; sr->setId(id); InitialAssignment * ar = m.createInitialAssignment(); ar->setSymbol(id); ar->setMath(node); sr->unsetStoichiometry(); } } } for (j = 0; j < r->getNumProducts(); j++) { SpeciesReference *sr = r->getProduct(j); if (sr->getDenominator() != 1) { long stoich = static_cast<long>(sr->getStoichiometry()); int denom = sr->getDenominator(); ASTNode *node = new ASTNode(); node->setValue(stoich, denom); if (l2 == true) { StoichiometryMath * sm = sr->createStoichiometryMath(); sm->setMath(node); } else { sprintf(newid, "speciesRefId_%u", idCount); id.assign(newid); idCount++; sr->setId(id); InitialAssignment * ar = m.createInitialAssignment(); ar->setSymbol(id); ar->setMath(node); sr->unsetStoichiometry(); } } } } }
void Model::dealWithStoichiometry() { unsigned int idCount = 0; for (unsigned int i = 0; i < getNumReactions(); i++) { Reaction *r = getReaction(i); unsigned int j; for (j = 0; j < r->getNumReactants(); j++) { SpeciesReference *sr = r->getReactant(j); if (sr->isSetStoichiometry() == false) { if (sr->isSetId() == false) { createNoValueStoichMath(*this, *sr, idCount); idCount++; } else { // id is set it could be used by initialAssignment // used by rule // not used if (getInitialAssignment(sr->getId()) != NULL) { useStoichMath(*this, *sr, false); } else if (getRule(sr->getId()) != NULL) { //assignmentRule if (getRule(sr->getId())->getTypeCode() == SBML_ASSIGNMENT_RULE) { useStoichMath(*this, *sr, true); } else if (getRule(sr->getId())->getTypeCode() == SBML_RATE_RULE) { createParameterAsRateRule(*this, *sr, *(getRule(sr->getId())), idCount); idCount++; } } else { createNoValueStoichMath(*this, *sr, idCount); idCount++; } } } else { // stoichiometry is set if (sr->isSetId()) { // id is set it could be used by initialAssignment // used by rule // not used if (getInitialAssignment(sr->getId()) != NULL) { useStoichMath(*this, *sr, false); } else if (getRule(sr->getId()) != NULL) { //assignmentRule if (getRule(sr->getId())->getTypeCode() == SBML_ASSIGNMENT_RULE) { useStoichMath(*this, *sr, true); } else if (getRule(sr->getId())->getTypeCode() == SBML_RATE_RULE) { createParameterAsRateRule(*this, *sr, *(getRule(sr->getId())), idCount); idCount++; } } } // no id set - do not need to do anything } } for (j = 0; j < r->getNumProducts(); j++) { SpeciesReference *sr = r->getProduct(j); if (sr->isSetStoichiometry() == false) { if (sr->isSetId() == false) { createNoValueStoichMath(*this, *sr, idCount); idCount++; } else { // id is set it could be used by initialAssignment // used by rule // not used if (getInitialAssignment(sr->getId()) != NULL) { useStoichMath(*this, *sr, false); } else if (getRule(sr->getId()) != NULL) { //assignmentRule if (getRule(sr->getId())->getTypeCode() == SBML_ASSIGNMENT_RULE) { useStoichMath(*this, *sr, true); } else if (getRule(sr->getId())->getTypeCode() == SBML_RATE_RULE) { createParameterAsRateRule(*this, *sr, *(getRule(sr->getId())), idCount); idCount++; } } else { createNoValueStoichMath(*this, *sr, idCount); idCount++; } } } else { // stoichiometry is set if (sr->isSetId()) { // id is set it could be used by initialAssignment // used by rule // not used if (getInitialAssignment(sr->getId()) != NULL) { useStoichMath(*this, *sr, false); } else if (getRule(sr->getId()) != NULL) { //assignmentRule if (getRule(sr->getId())->getTypeCode() == SBML_ASSIGNMENT_RULE) { useStoichMath(*this, *sr, true); } else if (getRule(sr->getId())->getTypeCode() == SBML_RATE_RULE) { createParameterAsRateRule(*this, *sr, *(getRule(sr->getId())), idCount); idCount++; } } } // no id set - do not need to do anything } } } }
void Model::assignRequiredValues() { // when converting to L3 some attributes which have default values in L1/L2 // but are required in L3 are not present or set unsigned int i, n; if (getNumUnitDefinitions() > 0) { for (i = 0; i < getNumUnitDefinitions(); i++) { for (n = 0; n < getUnitDefinition(i)->getNumUnits(); n++) { Unit *u = getUnitDefinition(i)->getUnit(n); if (!u->isSetExponent()) u->setExponent(1.0); if (!u->isSetScale()) u->setScale(0); if (!u->isSetMultiplier()) u->setMultiplier(1.0); } } } if (getNumCompartments() > 0) { for (i = 0; i < getNumCompartments(); i++) { Compartment *c = getCompartment(i); c->setConstant(c->getConstant()); } } if (getNumSpecies() > 0) { for (i = 0; i < getNumSpecies(); i++) { Species * s = getSpecies(i); s->setBoundaryCondition(s->getBoundaryCondition()); s->setHasOnlySubstanceUnits(s->getHasOnlySubstanceUnits()); s->setConstant(s->getConstant()); } } if (getNumParameters() > 0) { for (i = 0; i < getNumParameters(); i++) { Parameter * p = getParameter(i); p->setConstant(p->getConstant()); } } if (getNumReactions() > 0) { for (i = 0; i < getNumReactions(); i++) { Reaction * r = getReaction(i); r->setFast(r->getFast()); r->setReversible(r->getReversible()); if (r->getNumReactants() > 0) { for (n = 0; n < r->getNumReactants(); n++) { SpeciesReference *sr = r->getReactant(n); if (sr->isSetStoichiometryMath()) { sr->setConstant(false); } else { sr->setConstant(true); } } } if (r->getNumProducts() > 0) { for (n = 0; n < r->getNumProducts(); n++) { SpeciesReference *sr = r->getProduct(n); if (sr->isSetStoichiometryMath()) { sr->setConstant(false); } else { sr->setConstant(true); } } } } } if (getNumEvents() > 0) { for (i = 0; i < getNumEvents(); i++) { Event * e = getEvent(i); e->setUseValuesFromTriggerTime(e->getUseValuesFromTriggerTime()); if (e->isSetTrigger()) { Trigger *t = e->getTrigger(); t->setPersistent(true); t->setInitialValue(true); } } } }
void test000009::test_references_to_species() { // load the CPS file // export to SBML // check the resulting SBML model CCopasiDataModel* pDataModel = pCOPASIDATAMODEL; std::istringstream iss(test000009::MODEL_STRING); CPPUNIT_ASSERT(load_cps_model_from_stream(iss, *pDataModel) == true); CPPUNIT_ASSERT(pDataModel->getModel() != NULL); CPPUNIT_ASSERT(pDataModel->exportSBMLToString(NULL, 2, 3).empty() == false); SBMLDocument* pDocument = pDataModel->getCurrentSBMLDocument(); CPPUNIT_ASSERT(pDocument != NULL); Model* pModel = pDocument->getModel(); CPPUNIT_ASSERT(pModel != NULL); // assert that there is only one compartment and // assert the compartment is constant CPPUNIT_ASSERT(pModel->getNumCompartments() == 1); Compartment* pCompartment = pModel->getCompartment(0); CPPUNIT_ASSERT(pCompartment->getConstant() == false); CPPUNIT_ASSERT(pModel->getNumSpecies() == 2); Species* pSpecies = pModel->getSpecies(1); CPPUNIT_ASSERT(pSpecies->getHasOnlySubstanceUnits() == true); pSpecies = pModel->getSpecies(0); std::string idSpeciesA = pSpecies->getId(); CPPUNIT_ASSERT(pSpecies->getHasOnlySubstanceUnits() == true); CPPUNIT_ASSERT(pModel->getNumRules() == 2); // there are two rules, one is the rule for the compartment AssignmentRule* pRule = dynamic_cast<AssignmentRule*>(pModel->getRule(0)); CPPUNIT_ASSERT(pRule != NULL); CPPUNIT_ASSERT(pModel->getNumParameters() == 1); Parameter* pParameter = pModel->getParameter(0); CPPUNIT_ASSERT(pParameter != NULL); if (pRule->getVariable() != pParameter->getId()) { pRule = dynamic_cast<AssignmentRule*>(pModel->getRule(1)); } CPPUNIT_ASSERT(pRule->getVariable() == pParameter->getId()); const ASTNode* pMath = pRule->getMath(); CPPUNIT_ASSERT(pMath != NULL); // the expression should be the species divided by the volume CPPUNIT_ASSERT(pMath->getType() == AST_DIVIDE); CPPUNIT_ASSERT(pMath->getChild(0) != NULL); CPPUNIT_ASSERT(pMath->getChild(0)->getType() == AST_NAME); CPPUNIT_ASSERT(pMath->getChild(0)->getName() == pSpecies->getId()); CPPUNIT_ASSERT(pMath->getChild(1) != NULL); CPPUNIT_ASSERT(pMath->getChild(1)->getType() == AST_NAME); CPPUNIT_ASSERT(pMath->getChild(1)->getName() == pCompartment->getId()); CPPUNIT_ASSERT(pModel->getNumReactions() == 2); Reaction* pReaction = pModel->getReaction(0); // make sure this is reaction A -> CPPUNIT_ASSERT(pReaction != NULL); CPPUNIT_ASSERT(pReaction->getNumReactants() == 1); CPPUNIT_ASSERT(pReaction->getNumProducts() == 0); // check if all references in the kinetic law are unmodified // math element must be a multiplication of the mass action term by // the compartment volume // the mass action term is a multiplication of the parameter node by // the species node // the code that multiplies the reaction by the compartments volume // recognizes the division of the species by the compartment and cancels // those two CPPUNIT_ASSERT(pReaction->isSetKineticLaw() == true); KineticLaw* pLaw = pReaction->getKineticLaw(); CPPUNIT_ASSERT(pLaw != NULL); CPPUNIT_ASSERT(pLaw->isSetMath() == true); pMath = pLaw->getMath(); CPPUNIT_ASSERT(pMath->getType() == AST_TIMES); CPPUNIT_ASSERT(pMath->getNumChildren() == 2); CPPUNIT_ASSERT(pMath->getChild(0)->getType() == AST_NAME); CPPUNIT_ASSERT(pMath->getChild(0)->getName() == std::string("k1")); CPPUNIT_ASSERT(pMath->getChild(1) != NULL); CPPUNIT_ASSERT(pMath->getChild(1)->getType() == AST_NAME); CPPUNIT_ASSERT(pMath->getChild(1)->getName() == idSpeciesA); pReaction = pModel->getReaction(1); // make sure this is reaction A -> S CPPUNIT_ASSERT(pReaction != NULL); CPPUNIT_ASSERT(pReaction->getNumReactants() == 1); CPPUNIT_ASSERT(pReaction->getNumProducts() == 1); // check if all references in the kinetic law are unmodified // math element must be a multiplication of the compartments volume with // a function call with three arguments // the first argument is the reference to the species CPPUNIT_ASSERT(pReaction->isSetKineticLaw() == true); pLaw = pReaction->getKineticLaw(); CPPUNIT_ASSERT(pLaw != NULL); CPPUNIT_ASSERT(pLaw->isSetMath() == true); pMath = pLaw->getMath(); CPPUNIT_ASSERT(pMath->getType() == AST_TIMES); CPPUNIT_ASSERT(pMath->getNumChildren() == 2); CPPUNIT_ASSERT(pMath->getChild(0)->getType() == AST_NAME); CPPUNIT_ASSERT(pMath->getChild(0)->getName() == pCompartment->getId()); pMath = pMath->getChild(1); CPPUNIT_ASSERT(pMath != NULL); CPPUNIT_ASSERT(pMath->getType() == AST_FUNCTION); CPPUNIT_ASSERT(pMath->getNumChildren() == 3); pMath = pMath->getChild(0); CPPUNIT_ASSERT(pMath != NULL); CPPUNIT_ASSERT(pMath->getType() == AST_DIVIDE); CPPUNIT_ASSERT(pMath->getNumChildren() == 2); CPPUNIT_ASSERT(pMath->getChild(0) != NULL); CPPUNIT_ASSERT(pMath->getChild(0)->getType() == AST_NAME); CPPUNIT_ASSERT(pMath->getChild(0)->getName() == idSpeciesA); CPPUNIT_ASSERT(pMath->getChild(1) != NULL); CPPUNIT_ASSERT(pMath->getChild(1)->getType() == AST_NAME); CPPUNIT_ASSERT(pMath->getChild(1)->getName() == pCompartment->getId()); }
//create REACTION void SbmlReader::createReaction( map< string,Id > &molMap ) { map< string,double > rctMap; map< string,double >::iterator rctMap_iter; map< string,double >pdtMap; map< string,double >::iterator pdtMap_iter; map< string,Eref >::iterator elemt_iter; map< string,EnzymeInfo >enzInfoMap; double rctorder,pdtorder; static const Cinfo* moleculeCinfo = initMoleculeCinfo(); static const Finfo* reacFinfo =moleculeCinfo->findFinfo( "reac" ); static const Cinfo* reactionCinfo = initReactionCinfo(); static const Finfo* subFinfo = reactionCinfo->findFinfo( "sub" ); static const Finfo* prdFinfo = reactionCinfo->findFinfo( "prd" ); static const Finfo* kfFinfo = reactionCinfo->findFinfo( "kf" ); static const Finfo* kbFinfo = reactionCinfo->findFinfo( "kb" ); Reaction* reac; for ( unsigned int r = 0; r < model_->getNumReactions(); r++ ) { reac = model_->getReaction( r ); const string id=reac->getId(); //cout<<"reaction is "<<id<<endl; std::string name; if ( reac->isSetName() ){ name = reac->getName(); } string grpname = getAnnotation( reac,enzInfoMap ); if ( (grpname != "") && (enzInfoMap[grpname].stage == 3) ) setupEnzymaticReaction( enzInfoMap[grpname],grpname ); else if ( grpname == "" ) { if ( reac->getNumModifiers()> 0 ) setupMMEnzymeReaction( reac,id ); else{ bool rev=reac->getReversible(); bool fast=reac->getFast(); if ( fast ){ cout<<"warning: for now fast attribute is not handled"<<endl; errorFlag_ = true; } int numRcts = reac->getNumReactants(); int numPdts = reac->getNumProducts(); if ( numRcts == 0 && numPdts != 0 ){ const SpeciesReference* pdt = reac->getProduct( 0 ); std::string spName = pdt->getSpecies(); Id parent = molMap.find( spName )->second; //gives compartment of spName string parentCompt = parent()->name(); //cout<<"parent of reactant :"<<parentCompt<<endl; ostringstream spId; spId <<id<<"_Src"; molecule_ = Neutral::create( "Molecule",spId.str(),parent,Id::scratchId() );//create Molecule molMap[spId.str()] = parent; elmtMap_[spId.str()] = Eref( molecule_ ); ::set< double >( molecule_,"conc", 1 ); ::set< int >( molecule_,"mode",4 ); reaction_ = Neutral::create( "Reaction",id,parent,Id::scratchId() ); //create Reaction Eref( reaction_ ).add( subFinfo->msg(),elmtMap_[spId.str()],reacFinfo->msg(),ConnTainer::Default ); } else{ const SpeciesReference* rect=reac->getReactant(0); std::string sp=rect->getSpecies(); Id m = molMap.find(sp)->second; //gives compartment of sp reaction_ = Neutral::create( "Reaction",id,m,Id::scratchId() ); //create Reaction double rctcount=0.0; rctMap.clear(); for ( unsigned int rt=0;rt<reac->getNumReactants();rt++ ) { const SpeciesReference* rct=reac->getReactant(rt); sp=rct->getSpecies(); rctMap_iter = rctMap.find(sp); if ( rctMap_iter != rctMap.end() ){ rctcount = rctMap_iter->second; } else { rctcount = 0.0; } rctcount += rct->getStoichiometry(); rctMap[sp] = rctcount; for ( int i=0;(int)i<rct->getStoichiometry();i++ ) { Eref(reaction_).add( subFinfo->msg(),elmtMap_[sp],reacFinfo->msg(),ConnTainer::Default ); } } } double pdtcount = 0.0; pdtMap.clear(); for ( unsigned int pt=0;pt<reac->getNumProducts();pt++ ) { const SpeciesReference* pdt=reac->getProduct(pt); std::string sp=pdt->getSpecies(); pdtMap_iter = pdtMap.find(sp); if ( pdtMap_iter != pdtMap.end() ){ pdtcount = pdtMap_iter->second; } else { pdtcount = 0.0; } pdtcount += pdt->getStoichiometry(); pdtMap[sp] = pdtcount; for ( int i=0;i<pdt->getStoichiometry();i++ ) { Eref(reaction_).add( prdFinfo->msg(),elmtMap_[sp],reacFinfo->msg(),ConnTainer::Default ); } } //order of reactants rctorder = 0.0; string rsp = "",psp = ""; for ( rctMap_iter=rctMap.begin();rctMap_iter!=rctMap.end();rctMap_iter++ ) { rctorder += rctMap_iter->second; rsp=rctMap_iter->first; //species of the reactant } //cout<<"rct order = "<<rctorder<<endl; //order of products pdtorder = 0.0; for ( pdtMap_iter=pdtMap.begin();pdtMap_iter!=pdtMap.end();pdtMap_iter++ ) { pdtorder += pdtMap_iter->second; psp=pdtMap_iter->first; //species of the product } //cout<<"pdt order = "<<pdtorder<<endl; if ( reac->isSetKineticLaw() ) { KineticLaw * klaw=reac->getKineticLaw(); //vector< double > rate = getKLaw( klaw,rev ); vector< double > rate; rate.clear(); getKLaw( klaw,rev,rate ); if ( errorFlag_ ) return; else if ( !errorFlag_ ){ ::set< double >( reaction_, kfFinfo, rate[0] ); ::set< double >( reaction_, kbFinfo, rate[1] ); } } }//else modifier }//else }//reaction }//create reaction
LIBSBML_CPP_NAMESPACE_USE int main (int argc, char *argv[]) { if (argc != 2) { cout << endl << "Usage: printUnits filename" << endl << endl; return 1; } const char* filename = argv[1]; SBMLDocument* document = readSBML(filename); if (document->getNumErrors() > 0) { cerr << "Encountered the following SBML errors:" << endl; document->printErrors(cerr); return 1; } Model* model = document->getModel(); if (model == 0) { cout << "No model present." << endl; return 1; } unsigned int i,j; for (i = 0; i < model->getNumSpecies(); i++) { Species* s = model->getSpecies(i); cout << "Species " << i << ": " << UnitDefinition::printUnits(s->getDerivedUnitDefinition()) << endl; } for (i = 0; i < model->getNumCompartments(); i++) { Compartment *c = model->getCompartment(i); cout << "Compartment " << i << ": " << UnitDefinition::printUnits(c->getDerivedUnitDefinition()) << endl; } for (i = 0; i < model->getNumParameters(); i++) { Parameter *p = model->getParameter(i); cout << "Parameter " << i << ": " << UnitDefinition::printUnits(p->getDerivedUnitDefinition()) << endl; } for (i = 0; i < model->getNumInitialAssignments(); i++) { InitialAssignment *ia = model->getInitialAssignment(i); cout << "InitialAssignment " << i << ": " << UnitDefinition::printUnits(ia->getDerivedUnitDefinition()) << endl; cout << " undeclared units: "; cout << (ia->containsUndeclaredUnits() ? "yes\n" : "no\n"); } for (i = 0; i < model->getNumEvents(); i++) { Event *e = model->getEvent(i); cout << "Event " << i << ": " << endl; if (e->isSetDelay()) { cout << "Delay: " << UnitDefinition::printUnits(e->getDelay()->getDerivedUnitDefinition()) << endl; cout << " undeclared units: "; cout << (e->getDelay()->containsUndeclaredUnits() ? "yes\n" : "no\n"); } for (j = 0; j < e->getNumEventAssignments(); j++) { EventAssignment *ea = e->getEventAssignment(j); cout << "EventAssignment " << j << ": " << UnitDefinition::printUnits(ea->getDerivedUnitDefinition()) << endl; cout << " undeclared units: "; cout << (ea->containsUndeclaredUnits() ? "yes\n" : "no\n"); } } for (i = 0; i < model->getNumReactions(); i++) { Reaction *r = model->getReaction(i); cout << "Reaction " << i << ": " << endl; if (r->isSetKineticLaw()) { cout << "Kinetic Law: " << UnitDefinition::printUnits(r->getKineticLaw()->getDerivedUnitDefinition()) << endl; cout << " undeclared units: "; cout << (r->getKineticLaw()->containsUndeclaredUnits() ? "yes\n" : "no\n"); } for (j = 0; j < r->getNumReactants(); j++) { SpeciesReference *sr = r->getReactant(j); if (sr->isSetStoichiometryMath()) { cout << "Reactant stoichiometryMath" << j << ": " << UnitDefinition::printUnits(sr->getStoichiometryMath()->getDerivedUnitDefinition()) << endl; cout << " undeclared units: "; cout << (sr->getStoichiometryMath()->containsUndeclaredUnits() ? "yes\n" : "no\n"); } } for (j = 0; j < r->getNumProducts(); j++) { SpeciesReference *sr = r->getProduct(j); if (sr->isSetStoichiometryMath()) { cout << "Product stoichiometryMath" << j << ": " << UnitDefinition::printUnits(sr->getStoichiometryMath()->getDerivedUnitDefinition()) << endl; cout << " undeclared units: "; cout << (sr->getStoichiometryMath()->containsUndeclaredUnits() ? "yes\n" : "no\n"); } } } for (i = 0; i < model->getNumRules(); i++) { Rule *r = model->getRule(i); cout << "Rule " << i << ": " << UnitDefinition::printUnits(r->getDerivedUnitDefinition()) << endl; cout << " undeclared units: "; cout << (r->containsUndeclaredUnits() ? "yes\n" : "no\n"); } delete document; return 0; }
void SbmlReader::createReaction(const map< string, Id > &molSidcmptMIdMap ) { Reaction* reac; map< string,double > rctMap; map< string,double >::iterator rctMap_iter; map< string,double >prdMap; map< string,double >::iterator prdMap_iter; map< string,EnzymeInfo >enzInfoMap; for ( unsigned int r = 0; r < model_->getNumReactions(); r++ ) { Id reaction_; reac = model_->getReaction( r ); noOfsub_ = 0; noOfprd_ = 0; std:: string id; //=reac->getId(); if ( reac->isSetId() ) id = reac->getId(); std::string name; if ( reac->isSetName() ) { name = reac->getName(); name = nameString(name); } if (name.empty()) { if (id.empty()) assert("Reaction id and name is empty"); else name = id; } string grpname = getAnnotation( reac,enzInfoMap ); if ( (grpname != "") && (enzInfoMap[grpname].stage == 3) ) { setupEnzymaticReaction( enzInfoMap[grpname],grpname ,molSidcmptMIdMap,name); } //if (grpname != "") // { //cout << "\n enz matic reaction " << enzInfoMap[grpname].stage; //setupEnzymaticReaction( enzInfoMap[grpname],grpname ,molSidcmptMIdMap); //} else if ( grpname == "" ) { if (reac->getNumModifiers() > 0) setupMMEnzymeReaction( reac,id,name ,molSidcmptMIdMap); else { bool rev=reac->getReversible(); bool fast=reac->getFast(); if ( fast ) { cout<<"warning: for now fast attribute is not handled"<<endl; errorFlag_ = true; } int numRcts = reac->getNumReactants(); int numPdts = reac->getNumProducts(); if ( numRcts == 0 && numPdts != 0 ) { cout << "Reaction with zero Substrate is not possible but exist in this model"; const SpeciesReference* pdt = reac->getProduct( 0 ); std::string spName = pdt->getSpecies(); Id parent = molSidcmptMIdMap.find( spName )->second; //gives compartment of spName cout << " \n \t ################################# Sub = 0 and prd != 0 need to the reac ############### "; const SpeciesReference* rect=reac->getReactant(0); std::string sp=rect->getSpecies(); Id comptRef = molSidcmptMIdMap.find(sp)->second; //gives compartment of sp Id meshEntry = Neutral::child( comptRef.eref(), "mesh" ); Shell* shell = reinterpret_cast< Shell* >( Id().eref().data() ); reaction_ = shell->doCreate("Reac", meshEntry, name, 1); //shell->doAddMsg( "Single", meshEntry, "remeshReacs", reaction_, "remesh"); //Get Substrate addSubPrd(reac,reaction_,"prd"); } //if numRcts == 0 else { const SpeciesReference* rect=reac->getReactant(0); std::string sp=rect->getSpecies(); Id comptRef = molSidcmptMIdMap.find(sp)->second; //gives compartment of sp Id meshEntry = Neutral::child( comptRef.eref(), "mesh" ); Shell* shell = reinterpret_cast< Shell* >( Id().eref().data() ); reaction_ = shell->doCreate("Reac", comptRef, name, 1); //shell->doAddMsg( "Single", meshEntry, "remeshReacs", reaction_, "remesh"); //Get Substrate addSubPrd(reac,reaction_,"sub"); //Get Product addSubPrd(reac,reaction_,"prd"); } if ( reac->isSetKineticLaw() ) { KineticLaw * klaw=reac->getKineticLaw(); //vector< double > rate = getKLaw( klaw,rev ); vector< double > rate; rate.clear(); getKLaw( klaw,rev,rate ); if ( errorFlag_ ) return; else if ( !errorFlag_ ) { //cout << " Reaction name " << name << " kf " << rate[0] << " kb " << rate[1]<<endl; Field < double > :: set( reaction_, "Kf", rate[0] ); Field < double > :: set( reaction_, "Kb", rate[1] ); /*if (numRcts > 1) rate[0] = rate[0]*pow(1e3,1.0); cout << "Reaction " << id << " " << name << " " << rate[0] << " " << rate[1]<<endl; Field < double > :: set( reaction_, "Kf", rate[0] ); Field < double > :: set( reaction_, "Kb", rate[1] ); */ } } //issetKineticLaw } //else } // else grpname == "" }//for unsigned } //reaction