END_TEST START_TEST (test_WriteL3SBML_Reaction_full) { const char* expected = "<reaction id=\"v1\" reversible=\"true\" fast=\"false\">\n" " <listOfReactants>\n" " <speciesReference species=\"x0\"/>\n" " </listOfReactants>\n" " <listOfProducts>\n" " <speciesReference species=\"s1\"/>\n" " </listOfProducts>\n" " <listOfModifiers>\n" " <modifierSpeciesReference species=\"m1\"/>\n" " </listOfModifiers>\n" " <kineticLaw>\n" " <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n" " <apply>\n" " <divide/>\n" " <apply>\n" " <times/>\n" " <ci> vm </ci>\n" " <ci> s1 </ci>\n" " </apply>\n" " <apply>\n" " <plus/>\n" " <ci> km </ci>\n" " <ci> s1 </ci>\n" " </apply>\n" " </apply>\n" " </math>\n" " </kineticLaw>\n" "</reaction>"; D->createModel(); Reaction* r = D->getModel()->createReaction(); r->setId("v1"); r->setReversible(true); r->setFast(false); r->createReactant()->setSpecies("x0"); r->createProduct ()->setSpecies("s1"); r->createModifier()->setSpecies("m1"); r->createKineticLaw()->setFormula("(vm * s1)/(km + s1)"); char* sbml = r->toSBML(); fail_unless( equals(expected, sbml) ); safe_free(sbml); }
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()); }
LIBSBML_CPP_NAMESPACE_USE int main(int argc,char** argv) { DynPkgNamespaces sbmlns; // create the document SBMLDocument *document = new SBMLDocument(&sbmlns); document->setPackageRequired("dyn", true); // create the Model Model* model=document->createModel(); model->setId("singleCell"); // create the Compartment Compartment* compartment = model->createCompartment(); compartment->setId("Extracellular"); compartment->setConstant(true); compartment->setSize(8000000); compartment->setSpatialDimensions(3.0); compartment = model->createCompartment(); compartment->setId("PlasmaMembrane"); compartment->setConstant(true); compartment->setSize(314); compartment->setSpatialDimensions(2.0); compartment = model->createCompartment(); compartment->setId("Cytosol"); compartment->setConstant(true); compartment->setSize(523); compartment->setSpatialDimensions(3.0); // create the Species Species* species = model->createSpecies(); species->setId("C_EC"); species->setCompartment("Extracellular"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("RTR_M"); species->setCompartment("PlasmaMembrane"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("RCC_M"); species->setCompartment("PlasmaMembrane"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("A_C"); species->setCompartment("Cytosol"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("AA_C"); species->setCompartment("Cytosol"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("T"); species->setCompartment("Cytosol"); species->setBoundaryCondition(false); species->setConstant(false); species->setInitialConcentration(10); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("S"); species->setCompartment("Cytosol"); species->setBoundaryCondition(false); species->setConstant(false); species->setInitialConcentration(5); species->setHasOnlySubstanceUnits(false); // create the Reactions Reaction* reaction = model->createReaction(); reaction->setId("r1"); reaction->setReversible(true); reaction->setFast(false); reaction->setCompartment("Extracellular"); SpeciesReference* reactant = reaction->createReactant(); reactant->setSpecies("RTR_M"); reactant->setStoichiometry(1); reactant->setConstant(true); reactant = reaction->createReactant(); reactant->setSpecies("C_EC"); reactant->setStoichiometry(1); reactant->setConstant(true); SpeciesReference* product = reaction->createProduct(); product->setSpecies("RCC_M"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("r2"); reaction->setReversible(true); reaction->setFast(false); reaction->setCompartment("Cytosol"); reactant = reaction->createReactant(); reactant->setSpecies("A_C"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("AA_C"); product->setStoichiometry(1); product->setConstant(true); SimpleSpeciesReference* modifier = reaction->createModifier(); modifier->setSpecies("RCC_M"); // Create Event Event* event = model->createEvent(); event->setUseValuesFromTriggerTime(true); Trigger* trigger = event->createTrigger(); trigger->setInitialValue(false); trigger->setPersistent(true); trigger->setMath(SBML_parseFormula("lt(AA_C, T)")); // // Get a DynEventPlugin object plugged in the event object. // // The type of the returned value of SBase::getPlugin() function is // SBasePlugin*, and thus the value needs to be casted for the // corresponding derived class. // DynEventPlugin* eplugin = static_cast<DynEventPlugin*>(event->getPlugin("dyn")); eplugin->setApplyToAll(true); eplugin->setCboTerm("http://cbo.biocomplexity.indiana.edu/svn/cbo/trunk/CBO_1_0.owl#CellDeath"); event = model->createEvent(); event->setUseValuesFromTriggerTime(true); trigger = event->createTrigger(); trigger->setInitialValue(false); trigger->setPersistent(true); trigger->setMath(SBML_parseFormula("lt(AA_C, S)")); eplugin = static_cast<DynEventPlugin*>(event->getPlugin("dyn")); eplugin->setApplyToAll(true); eplugin->setCboTerm("http://cbo.biocomplexity.indiana.edu/svn/cbo/trunk/CBO_1_0.owl#CellDevision"); document->checkConsistency(); if (document->getNumErrors(LIBSBML_SEV_ERROR) > 0) document->printErrors(); writeSBML(document,"dyn_example1.xml"); delete document; }
/** * 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; }
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; } } }