END_TEST START_TEST ( test_ModifierSpeciesReference ) { ModifierSpeciesReference* msr = new ModifierSpeciesReference(2, 4); fail_unless (msr->hasRequiredElements()); delete msr; }
END_TEST START_TEST ( test_ModifierSpeciesReference ) { ModifierSpeciesReference* msr = new ModifierSpeciesReference(2, 4); fail_unless (!(msr->hasRequiredAttributes())); msr->setSpecies("msr"); fail_unless (msr->hasRequiredAttributes()); delete msr; }
END_TEST START_TEST ( test_SpeciesReference_Modifier_parent_create ) { Reaction *r = new Reaction(2, 4); ModifierSpeciesReference *sr = r->createModifier(); ListOf *lo = r->getListOfModifiers(); fail_unless(lo == sr->getParentSBMLObject()); fail_unless(lo == r->getModifier(0)->getParentSBMLObject()); fail_unless(r == lo->getParentSBMLObject()); }
END_TEST START_TEST ( test_SpeciesReference_Modifier_parent_add ) { ModifierSpeciesReference *sr = new ModifierSpeciesReference(2, 4); sr->setSpecies("s"); Reaction *r = new Reaction(2, 4); r->addModifier(sr); delete sr; ListOf *lo = r->getListOfModifiers(); fail_unless(lo == r->getModifier(0)->getParentSBMLObject()); fail_unless(r == lo->getParentSBMLObject()); }
// ####################################################################### ModifierSpeciesReference* SBML_formatter::species2ModifierSpeciesReference(const Species* species, const Reaction* rxn) { string value; ModifierSpeciesReference* MSR = new ModifierSpeciesReference(species->getLevel(), species->getVersion()); value = species->getId(); MSR->setSpecies(value); MSR->setId((value.append("_ref_")).append(dtostr(unique))); value.clear(); value.append(species->getId()); MSR->setName((value.append("_in_rxn_")).append(rxn->getId())); // unique is a static variable which allows anything that needs a modelwide unique value to get // one; unique++; return MSR; }
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; }
/** * Save the gene network to an SBML file. If the argument is null, use the network id. * @param filename URL to the file describing the network to load * @throws IOException */ void GeneNetwork::writeSBML(const char *filename) { ofstream data_file(filename); if (!data_file.is_open()) { std::cerr << "Failed to open file " << filename << std::endl; exit(1); } data_file.close(); ::logging::log::emit<Info>() << "Writing file " << filename << ::logging::log::endl; SBMLDocument *sbmlDoc = new SBMLDocument(3, 1); Model *model = sbmlDoc->createModel(); model->setId(id_); //model.getNotes ().add (comment_); // save network description int size = getSize(); Compartment *comp = model->createCompartment(); comp->setId("cell"); comp->setSize(1); std::vector<Species*> all_sp; Species *sp; for (int s=0; s < size; s++) { // save gene as species // species[s] = new Species(nodeIds_.get(s), nodeIds_.get(s)); sp = model->createSpecies(); sp->setCompartment("cell"); sp->setId((nodes_.at(s)).getLabel()); all_sp.push_back(sp); //species[s].setInitialAmount(?); // maybe save the wild-type steady state? //model.addSpecies(species[s]); } // create the void species sp = model->createSpecies(); sp->setCompartment("cell"); sp->setId("_void_"); sp->setInitialAmount(0); sp->setBoundaryCondition(true); sp->setConstant(true); all_sp.push_back(sp); //model.addSpecies(species[size]); // SET SYNTHESIS AND DEGRADATION REACTIONS FOR EVERY GENE for (int i=0; i<size; i++) { //::logging::log::emit<Info>() << ::logging::log::dec << i << //::logging::log::endl; // the ID of gene i // String currentGeneID = nodeIds_.get(i); string currentGeneID = (nodes_.at(i)).getLabel(); // The modifiers (regulators) of gene i std::vector<std::string> inputGenes = (nodes_.at(i)).getInputGenes(); // SYNTHESIS REACTION std::string reactionId = currentGeneID + "_synthesis"; Reaction *reaction = model->createReaction(); KineticLaw *kineticLaw = reaction->createKineticLaw(); SpeciesReference *spr; ModifierSpeciesReference *msr; reaction->setId(reactionId); reaction->setReversible (false); spr = reaction->createReactant(); spr->setSpecies(sp->getId()); spr = reaction->createProduct(); spr->setSpecies((all_sp.at(i))->getId()); std::stringstream ss; ss << inputGenes.size(); //::logging::log::emit<Debug>() << "node = " << nodes_.at(i).getLabel().c_str() << " #inputs = " << ss.str().c_str() << ::logging::log::endl; for (unsigned int r=0; r<inputGenes.size(); r++) {// set gene modifiers // reaction.addModifier(species[inputIndexes.get(r)]); //log.log(Level.INFO, "i = " + size); msr = reaction->createModifier(); msr->setSpecies((all_sp.at(getIndexOfNode(inputGenes.at(r))))->getId()); } //std::vector<RegulatoryModule> modules = (nodes_.at(i)).getRegulatoryModules(); //log.log(Level.INFO, "size = " + modules.size()); std::map<std::string, double> *params = new std::map<std::string, double>(); (nodes_.at(i)).compileParameters(*params); //char buf[256]; //sprintf(buf, "%f", nodes_.at(i).getDelta()); //::logging::log::emit<Info>() << buf << ::logging::log::endl; //::logging::log::emit<Info>() << ::logging::log::dec << nodes_.at(i).getAlpha().size() << // ::logging::log::endl; Parameter *para; // save gene parameters (note, the first param is the degradation rate) std::map<std::string, double>::iterator p = params->begin(); //p++; for (; p!=params->end(); p++) { //if (p == params->begin()) { // p++; // continue; //} //::logging::log::emit<Info>() << p->first.c_str() << // ::logging::log::endl; if (p->first != "delta") { para = kineticLaw->createParameter(); para->setId(p->first); para->setValue(p->second); } } reaction->setKineticLaw(kineticLaw); model->addReaction(reaction); // DEGRADATION REACTION reaction = model->createReaction(); kineticLaw = reaction->createKineticLaw(); reactionId = currentGeneID + "_degradation"; reaction->setId(reactionId); reaction->setReversible(false); spr = reaction->createReactant(); spr->setSpecies((all_sp.at(i))->getId()); spr = reaction->createProduct(); spr->setSpecies(sp->getId()); para = kineticLaw->createParameter(); std::map<std::string,double>::iterator it = params->find("delta"); para->setId(it->first); para->setValue(it->second); reaction->setKineticLaw (kineticLaw); model->addReaction (reaction); } // PRINT FILE SBMLWriter sbmlWriter; sbmlWriter.writeSBML(sbmlDoc, filename); delete sbmlDoc; }
/** * Load a gene network from an SBML file. Overrides Structure.load(). Format must * be equal GeneNetwork.SBML. Note, the SBML file must be in the exact same format * as the SBML files produced by writeSBML(). In particular, we assume that reactions are listed * *ordered* as we do in writeSBML(). * @param filename URL to the file describing the network to load * @param format File format (GML, DOT, etc.) * @throws IOException */ void GeneNetwork::load_sbml(const char *filename) { SBMLDocument* document; SBMLReader reader; document = reader.readSBML(filename); unsigned int errors = document->getNumErrors(); if (errors > 0) { std::cerr << "Failed to open file " << filename << std::endl; exit(1); } Model *m = document->getModel(); // ----------------------------------------- // Set the network size and create the genes // do not count the species _void_ int size = m->getNumSpecies() - 1; ListOfSpecies *species = m->getListOfSpecies(); for (int g=0; g < size; g++) { if (species->get(g)->getId() != "_void_") { //HillGene hg = new HillGene(this); //hg.setLabel(species.get(g).getId()); HillGene *n = new HillGene(species->get(g)->getId()); //n.setLabel(species->get(g)->getId()); nodes_.push_back(*n); delete n; } } x_ = Vec_DP(nodes_.size()); x_ = 0; y_ = Vec_DP(nodes_.size()); y_ = 0; //vector<string> parameterNames; // the names of the parameters //vector<double> parameterValues; // the values of the parameters std::map<std::string, double> params; std::vector<std::string> inputNodes; // the indexes of the inputs HillGene src, tgt; Parameter *param; // 2 loops for one gene: both synthesis and degradation reactions // (we assume that reactions are listed *ordered* as we do in writeSBML()) //int counter = 0; for (unsigned int i=0; i < m->getNumReactions(); i++) { Reaction *re = m->getReaction(i); std::string id = re->getId(); std::stringstream ss; ss << i; //::logging::log::emit<Debug>() << id.c_str() << // ::logging::log::endl; tgt = nodes_.at(getIndexOfNode(getGeneReactantId(id))); //tgt->setLabel(getGeneReactantId(*re)); //SpeciesReference *rt = re->getReactant(0); //Node *tgt = new HillGene(); //tgt->setLabel(rt->getSpecies()); //ListOfSpeciesReferences *modifiers = re->getListOfModifiers(); for (unsigned int j=0; j < re->getNumModifiers(); j++) { ModifierSpeciesReference *md = re->getModifier(j); src = nodes_.at(getIndexOfNode(md->getSpecies())); inputNodes.push_back(src.getLabel()); // set output genes std::vector<std::string> outputs = src.getOutputGenes(); outputs.push_back(tgt.getLabel()); src.setOutputGenes(outputs); // The edge type is unknown for now, it is initialized later Edge *e = new Edge(&src, &tgt, "+-"); edges_.push_back(*e); //delete src; delete e; } KineticLaw *kl = re->getKineticLaw(); for(unsigned int j=0; j < kl->getNumParameters(); j++) { param = kl->getParameter(j); params[param->getId()] = param->getValue(); //char buf[256]; //sprintf(buf, "%s\t%f", param->getId().c_str(), param->getValue()); //::logging::log::emit<Info>() << buf << ::logging::log::endl; } //::logging::log::emit<Info>() << ::logging::log::dec << params.size() << // ::logging::log::endl; // in the second iteration for this gene if (i%2 == 1) { // set parameters in gene //tgt.initialization(params, inputNodes); nodes_.at(getIndexOfNode(getGeneReactantId(id))).initialization(params, inputNodes);; //char buf[256]; //sprintf(buf, "%f", params["k_1"]); //::logging::log::emit<Info>() << buf << ::logging::log::endl; inputNodes.clear(); // don't clear because the reference was copied to the gene //parameterNames.clear(); // reset (they were not copied) //parameterValues.clear(); params.clear(); } //counter++; } //setEdgeTypesAccordingToDynamicalModel(); //signed_ = true; //delete document; //delete n; //delete e; }
void Module::CreateSBMLModel() { Model* sbmlmod = m_sbml.createModel(); sbmlmod->setId(m_modulename); sbmlmod->setName(m_modulename); sbmlmod->setNotes("<body xmlns=\"http://www.w3.org/1999/xhtml\"><p> Originally created by libAntimony " VERSION_STRING " (using libSBML " LIBSBML_DOTTED_VERSION ") </p></body>"); char cc = g_registry.GetCC(); //User-defined functions for (size_t uf=0; uf<g_registry.GetNumUserFunctions(); uf++) { const UserFunction* userfunction = g_registry.GetNthUserFunction(uf); assert(userfunction != NULL); FunctionDefinition* fd = sbmlmod->createFunctionDefinition(); fd->setId(userfunction->GetModuleName()); ASTNode* math = parseStringToASTNode(userfunction->ToSBMLString()); fd->setMath(math); delete math; } //Compartments Compartment* defaultCompartment = sbmlmod->createCompartment(); defaultCompartment->setId(DEFAULTCOMP); defaultCompartment->setConstant(true); defaultCompartment->setSize(1); defaultCompartment->setSBOTerm(410); //The 'implicit compartment' size_t numcomps = GetNumVariablesOfType(allCompartments); for (size_t comp=0; comp<numcomps; comp++) { const Variable* compartment = GetNthVariableOfType(allCompartments, comp); Compartment* sbmlcomp = sbmlmod->createCompartment(); sbmlcomp->setId(compartment->GetNameDelimitedBy(cc)); if (compartment->GetDisplayName() != "") { sbmlcomp->setName(compartment->GetDisplayName()); } sbmlcomp->setConstant(compartment->GetIsConst()); formula_type ftype = compartment->GetFormulaType(); assert (ftype == formulaINITIAL || ftype==formulaASSIGNMENT || ftype==formulaRATE); if (ftype != formulaINITIAL) { sbmlcomp->setConstant(false); } const Formula* formula = compartment->GetFormula(); if (formula->IsDouble()) { sbmlcomp->setSize(atof(formula->ToSBMLString().c_str())); } SetAssignmentFor(sbmlmod, compartment); } //Species size_t numspecies = GetNumVariablesOfType(allSpecies); for (size_t spec=0; spec < numspecies; spec++) { const Variable* species = GetNthVariableOfType(allSpecies, spec); Species* sbmlspecies = sbmlmod->createSpecies(); sbmlspecies->setId(species->GetNameDelimitedBy(cc)); if (species->GetDisplayName() != "") { sbmlspecies->setName(species->GetDisplayName()); } sbmlspecies->setConstant(false); //There's no need to try to distinguish between const and var for species. if (species->GetIsConst()) { sbmlspecies->setBoundaryCondition(true); } else { sbmlspecies->setBoundaryCondition(false); } const Variable* compartment = species->GetCompartment(); if (compartment == NULL) { sbmlspecies->setCompartment(defaultCompartment->getId()); } else { sbmlspecies->setCompartment(compartment->GetNameDelimitedBy(cc)); } const Formula* formula = species->GetFormula(); if (formula->IsDouble()) { sbmlspecies->setInitialConcentration(atof(formula->ToSBMLString().c_str())); } else if (formula->IsAmountIn(species->GetCompartment())) { sbmlspecies->setInitialAmount(formula->ToAmount()); } SetAssignmentFor(sbmlmod, species); } //Formulas size_t numforms = GetNumVariablesOfType(allFormulas); for (size_t form=0; form < numforms; form++) { const Variable* formvar = GetNthVariableOfType(allFormulas, form); const Formula* formula = formvar->GetFormula(); Parameter* param = sbmlmod->createParameter(); param->setId(formvar->GetNameDelimitedBy(cc)); if (formvar->GetDisplayName() != "") { param->setName(formvar->GetDisplayName()); } param->setConstant(formvar->GetIsConst()); if (formula->IsDouble()) { param->setValue(atof(formula->ToSBMLString().c_str())); } SetAssignmentFor(sbmlmod, formvar); formula_type ftype = formvar->GetFormulaType(); assert (ftype == formulaINITIAL || ftype==formulaASSIGNMENT || ftype==formulaRATE); if (ftype != formulaINITIAL) { param->setConstant(false); } } //Reactions size_t numrxns = GetNumVariablesOfType(allReactions); for (size_t rxn=0; rxn < numrxns; rxn++) { const Variable* rxnvar = GetNthVariableOfType(allReactions, rxn); const AntimonyReaction* reaction = rxnvar->GetReaction(); if (reaction->IsEmpty()) { continue; //Reactions that involve no species are illegal in SBML. } Reaction* sbmlrxn = sbmlmod->createReaction(); sbmlrxn->setId(rxnvar->GetNameDelimitedBy(cc)); if (rxnvar->GetDisplayName() != "") { sbmlrxn->setName(rxnvar->GetDisplayName()); } if (reaction->GetType() == rdBecomes) { sbmlrxn->setReversible(true); } else { assert(reaction->GetType() == rdBecomesIrreversibly); sbmlrxn->setReversible(false); } const Formula* formula = reaction->GetFormula(); string formstring = formula->ToSBMLString(rxnvar->GetStrandVars()); if (!formula->IsEmpty()) { KineticLaw* kl = sbmlmod->createKineticLaw(); ASTNode* math = parseStringToASTNode(formstring); kl->setMath(math); delete math; } const ReactantList* left = reaction->GetLeft(); for (size_t lnum=0; lnum<left->Size(); lnum++) { const Variable* nthleft = left->GetNthReactant(lnum); double nthstoich = left->GetStoichiometryFor(lnum); SpeciesReference* sr = sbmlmod->createReactant(); sr->setSpecies(nthleft->GetNameDelimitedBy(cc)); sr->setStoichiometry(nthstoich); } const ReactantList* right = reaction->GetRight(); for (size_t rnum=0; rnum<right->Size(); rnum++) { const Variable* nthright = right->GetNthReactant(rnum); double nthstoich = right->GetStoichiometryFor(rnum); SpeciesReference* sr = sbmlmod->createProduct(); sr->setSpecies(nthright->GetNameDelimitedBy(cc)); sr->setStoichiometry(nthstoich); } //Find 'modifiers' and add them. vector<const Variable*> subvars = formula->GetVariablesFrom(formstring, m_modulename); for (size_t v=0; v<subvars.size(); v++) { if (subvars[v] != NULL && subvars[v]->GetType() == varSpeciesUndef) { if (left->GetStoichiometryFor(subvars[v]) == 0 && right->GetStoichiometryFor(subvars[v]) == 0) { ModifierSpeciesReference* msr = sbmlmod->createModifier(); msr->setSpecies(subvars[v]->GetNameDelimitedBy(cc)); } } } } //Events size_t numevents = GetNumVariablesOfType(allEvents); for (size_t ev=0; ev < numevents; ev++) { const Variable* eventvar = GetNthVariableOfType(allEvents, ev); const AntimonyEvent* event = eventvar->GetEvent(); Event* sbmlevent = sbmlmod->createEvent(); sbmlevent->setId(eventvar->GetNameDelimitedBy(cc)); if (eventvar->GetDisplayName() != "") { sbmlevent->setName(eventvar->GetDisplayName()); } Trigger* trig = sbmlevent->createTrigger(); ASTNode* ASTtrig = parseStringToASTNode(event->GetTrigger()->ToSBMLString()); trig->setMath(ASTtrig); delete ASTtrig; const Formula* delay = event->GetDelay(); if (!delay->IsEmpty()) { ASTtrig = parseStringToASTNode(delay->ToSBMLString()); Delay* sbmldelay = sbmlevent->createDelay(); sbmldelay->setMath(ASTtrig); delete ASTtrig; } long numasnts = static_cast<long>(event->GetNumAssignments()); for (long asnt=numasnts-1; asnt>=0; asnt--) { //events are stored in reverse order. Don't ask... EventAssignment* sbmlasnt = sbmlmod->createEventAssignment(); sbmlasnt->setVariable(event->GetNthAssignmentVariableName(asnt, cc)); ASTNode* ASTasnt = parseStringToASTNode(event->GetNthAssignmentFormulaString(asnt, '_', true)); sbmlasnt->setMath(ASTasnt); delete ASTasnt; } } //Interactions size_t numinteractions = GetNumVariablesOfType(allInteractions); for (size_t irxn=0; irxn<numinteractions; irxn++) { const Variable* arxnvar = GetNthVariableOfType(allInteractions, irxn); const AntimonyReaction* arxn = arxnvar->GetReaction(); Reaction* rxn = sbmlmod->getReaction(arxn->GetRight()->GetNthReactant(0)->GetNameDelimitedBy(cc)); if (rxn != NULL) { for (size_t interactor=0; interactor<arxn->GetLeft()->Size(); interactor++) { ModifierSpeciesReference* msr = rxn->createModifier(); msr->setSpecies(arxn->GetLeft()->GetNthReactant(interactor)->GetNameDelimitedBy(cc)); msr->setName(arxnvar->GetNameDelimitedBy(cc)); } } } //Unknown variables (turn into parameters) size_t numunknown = GetNumVariablesOfType(allUnknown); for (size_t form=0; form < numunknown; form++) { const Variable* formvar = GetNthVariableOfType(allUnknown, form); Parameter* param = sbmlmod->createParameter(); param->setId(formvar->GetNameDelimitedBy(cc)); if (formvar->GetDisplayName() != "") { param->setName(formvar->GetDisplayName()); } switch(formvar->GetConstType()) { case constVAR: param->setConstant(true); break; case constCONST: param->setConstant(false); break; case constDEFAULT: break; } } }
void CompIdBase::checkId (const ModifierSpeciesReference& x) { if (x.isSetId()) doCheckId(x.getId(), x); }