void SBML_sim::loadSBML(SBMLDocument * doc) { if (!doc || doc->getNumErrors() > 0) { } else { Model * model = doc->getModel(); ListOfParameters * params = model->getListOfParameters(); ListOfReactions * reacs = model->getListOfReactions(); ListOfSpecies * species = model->getListOfSpecies(); ListOfSpeciesTypes * types = model->getListOfSpeciesTypes(); ListOfEvents * events = model->getListOfEvents(); ListOfRules * rules = model->getListOfRules(); vector<string> assignmentEquations, rateEquations, eventTriggers; vector< vector<string> > eventResponses; if (events) for (int i=0; i < events->size(); ++i) { Event * e = events->get(i); eventTriggers.push_back( SBML_formulaToString( e->getTrigger()->getMath() ) ); ListOfEventAssignments * eventAssn = e->getListOfEventAssignments(); vector<string> responses; string s; for (int j=0; j < eventAssn->size(); ++j) { s = eventAssn->get(j)->getVariable(); s.append("="); s.append( SBML_formulaToString( eventAssn->get(j)->getMath() ) ); responses.push_back(s); } eventResponses.push_back( responses ); } if (rules) for (int i=0; i < rules->size(); ++i) { Rule * r = rules->get(i); if (r->isAssignment()) { AssignmentRule * ar = (AssignmentRule*)r; assignmentVariables.push_back(ar->getVariable()); assignmentValues.push_back(1.0); assignmentEquations.push_back(ar->getFormula()); } } if (species) for (int i=0; i < species->size(); ++i) if (!species->get(i)->getConstant() && !species->get(i)->getBoundaryCondition()) { variableNames.push_back(species->get(i)->getId()); if (species->get(i)->isSetInitialAmount()) variableValues.push_back(species->get(i)->getInitialAmount()); else if (species->get(i)->isSetInitialConcentration()) variableValues.push_back(species->get(i)->getInitialConcentration()); else variableValues.push_back(0.0); } else { parameterNames.push_back(species->get(i)->getId()); if (species->get(i)->isSetInitialAmount()) parameterValues.push_back(species->get(i)->getInitialAmount()); else if (species->get(i)->isSetInitialConcentration()) parameterValues.push_back(species->get(i)->getInitialConcentration()); else parameterValues.push_back(0.0); } if (params) for (int i=0; i < params->size(); ++i) { parameterNames.push_back(params->get(i)->getId()); parameterValues.push_back(params->get(i)->getValue()); } int numReacs = 0; if (reacs) numReacs = reacs->size(); stoichiometryMatrix = new double[ numReacs * variableNames.size() ]; for (int i=0; i < numReacs; ++i) { Reaction * r = reacs->get(i); reactionNames.push_back(r->getId()); rateEquations.push_back(r->getKineticLaw()->getFormula()); ListOfSpeciesReferences * reactants = r->getListOfReactants(), * products = r->getListOfProducts(); for (int j=0; j < variableNames.size(); ++j) { stoichiometryMatrix[ j*numReacs + i ] = 0.0; for (int k=0; k < reactants->size(); ++k) if (reactants->get(k) && reactants->get(k)->getSpecies() == variableNames[j]) stoichiometryMatrix[ j*numReacs + i ] -= 1.0; //stoichiometryMatrix[ j*numReacs + i ] -= SpeciesReference_getStoichiometry(reactants->get(k)); for (int k=0; k < products->size(); ++k) if (products->get(k) && products->get(k)->getSpecies() == variableNames[j]) stoichiometryMatrix[ j*numReacs + i ] += 1.0; //stoichiometryMatrix[ j*numReacs + i ] += SpeciesReference_getStoichiometry(reactants->get(k)); } } for (int i=0; i < rateEquations.size(); ++i) { mu::Parser p; addSBMLFunctions(p); p.SetExpr(rateEquations[i]); for (int j=0; j < variableNames.size(); ++j) p.DefineVar("time",&(this->time)); for (int j=0; j < variableNames.size(); ++j) p.DefineVar("Time",&(this->time)); for (int j=0; j < variableNames.size(); ++j) p.DefineVar(variableNames[j],&variableValues[j]); for (int j=0; j < parameterNames.size(); ++j) p.DefineVar(parameterNames[j],¶meterValues[j]); for (int j=0; j < assignmentVariables.size(); ++j) p.DefineVar(assignmentVariables[j],&assignmentValues[j]); p.SetVarFactory(muparser_add_variable, (void*)this); try { p.Eval(); rateEqns.push_back(p); } catch(...) { //reactionNames.clear(); //rateEqns.clear(); break; } } for (int i=0; i < assignmentEquations.size(); ++i) { mu::Parser p; addSBMLFunctions(p); p.SetExpr(assignmentEquations[i]); for (int j=0; j < variableNames.size(); ++j) p.DefineVar(variableNames[j],&variableValues[j]); for (int j=0; j < parameterNames.size(); ++j) p.DefineVar(parameterNames[j],¶meterValues[j]); for (int j=0; j < assignmentVariables.size(); ++j) p.DefineVar(assignmentVariables[j],&assignmentValues[j]); //p.SetVarFactory(muparser_add_variable, (void*)this); try { p.Eval(); assignmentEqns.push_back(p); } catch(...) { std::cout << assignmentEquations[i] << std::endl; //assignmentVariables.clear(); //assignmentEqns.clear(); break; } } for (int i=0; i < eventTriggers.size(); ++i) { mu::Parser p; addSBMLFunctions(p); p.SetExpr(eventTriggers[i]); for (int j=0; j < variableNames.size(); ++j) p.DefineVar("time",&(this->time)); for (int j=0; j < variableNames.size(); ++j) p.DefineVar("Time",&(this->time)); for (int j=0; j < variableNames.size(); ++j) p.DefineVar(variableNames[j],&variableValues[j]); for (int j=0; j < parameterNames.size(); ++j) p.DefineVar(parameterNames[j],¶meterValues[j]); for (int j=0; j < assignmentVariables.size(); ++j) p.DefineVar(assignmentVariables[j],&assignmentValues[j]); try { p.Eval(); //resposes for the trigger vector<mu::Parser> responses; for (int j=0; j < eventResponses[i].size(); ++j) { mu::Parser p; addSBMLFunctions(p); p.SetExpr(eventResponses[i][j]); try { p.Eval(); responses.push_back(p); } catch(...) {} } if (responses.size() > 0) { triggerEqns.push_back(p); responseEqns.push_back(responses); } } catch(...) { //assignmentVariables.clear(); //assignmentEqns.clear(); break; } } //delete params; //delete reacs; } }
/* Give this reaction links to all species involved in it, and give those species links to this reaction. */ void DSBReaction::doublyLink(QMap<QString, DSBSpecies*> map) { ListOfSpeciesReferences *lsr; SimpleSpeciesReference *ssr; unsigned int N; QString specId; // "reactants", or inputs lsr = m_sbmlReaction->getListOfReactants(); N = lsr->size(); for (unsigned int i = 0; i < N; i++) { ssr = lsr->get(i); specId = QString(ssr->getSpecies().c_str()); if (!map.contains(specId)) { // TODO: Report error. Reaction is referring to a species that // was not declared in the SBML list of species. qDebug() << "map does not contain species id " << specId; } else { DSBSpecies *dsbspec = map.value(specId); m_inputs.append(dsbspec); dsbspec->addReactionEntered(this); } } // "products", or outputs lsr = m_sbmlReaction->getListOfProducts(); N = lsr->size(); for (unsigned int i = 0; i < N; i++) { ssr = lsr->get(i); specId = QString(ssr->getSpecies().c_str()); if (!map.contains(specId)) { // TODO: Report error. Reaction is referring to a species that // was not declared in the SBML list of species. qDebug() << "map does not contain species id " << specId; } else { DSBSpecies *dsbspec = map.value(specId); m_outputs.append(dsbspec); dsbspec->addReactionExited(this); } } // modifiers (e.g. catalysts) lsr = m_sbmlReaction->getListOfModifiers(); N = lsr->size(); for (unsigned int i = 0; i < N; i++) { ssr = lsr->get(i); specId = QString(ssr->getSpecies().c_str()); if (!map.contains(specId)) { // TODO: Report error. Reaction is referring to a species that // was not declared in the SBML list of species. qDebug() << "map does not contain species id " << specId; } else { DSBSpecies *dsbspec = map.value(specId); m_modifiers.append(dsbspec); dsbspec->addReactionModified(this); } } }