END_TEST START_TEST(test_FbcExtension_convert_and_write) { string file(TestDataDirectory); file += "/fbc_ga_example.xml"; SBMLDocument* document = readSBMLFromFile(file.c_str()); document->printErrors(); fail_unless(document->getNumErrors(LIBSBML_SEV_ERROR) == 0); fail_unless(document->getModel() != NULL); // convert to v2 { ConversionProperties props; props.addOption("convert fbc v1 to fbc v2", true); props.addOption("strict", true); int result = document->convert(props); // ensure that all is well with the model fail_unless(result == LIBSBML_OPERATION_SUCCESS); fail_unless(document->getLevel() == 3); fail_unless(document->getVersion() == 1); fail_unless(document->getPlugin("fbc") != NULL); fail_unless(document->getPlugin("fbc")->getPackageVersion() == 2); } // ensure that all the v1 stuff is no longer there FbcModelPlugin* mplug = dynamic_cast<FbcModelPlugin*>( document->getModel()->getPlugin("fbc")); fail_unless(mplug != NULL); fail_unless(mplug->isSetStrict()); fail_unless(mplug->getNumGeneAssociations() == 0); fail_unless(mplug->getNumFluxBounds() == 0); delete document; }
END_TEST START_TEST (test_FbcExtension_read_L3V1V1_defaultNS) { char *filename = safe_strcat(TestDataDirectory, "fbc_example1_defaultNS.xml"); SBMLDocument *document = readSBMLFromFile(filename); fail_unless(document->getPackageName() == "core"); Model *model = document->getModel(); fail_unless(model != NULL); fail_unless(model->getPackageName() == "core"); fail_unless(document->getNumErrors() == 0); // get the fbc plugin FbcModelPlugin* mplugin = static_cast<FbcModelPlugin*>(model->getPlugin("fbc")); fail_unless(mplugin != NULL); fail_unless(mplugin->getNumObjectives() == 1); fail_unless(mplugin->getListOfObjectives()->getPackageName() == "fbc"); Objective* objective = mplugin->getObjective(0); fail_unless(objective->getId() == "obj1"); fail_unless(objective->getType() == "maximize"); fail_unless(objective->getNumFluxObjectives() == 1); fail_unless(objective->getPackageName() == "fbc"); fail_unless(objective->getListOfFluxObjectives()->getPackageName() == "fbc"); FluxObjective* fluxObjective = objective->getFluxObjective(0); fail_unless(fluxObjective->getReaction() == "J8"); fail_unless(fluxObjective->getPackageName() == "fbc"); fail_unless(fluxObjective->getCoefficient() == 1); fail_unless(mplugin->getNumFluxBounds() == 1); fail_unless(mplugin->getListOfFluxBounds()->getPackageName() == "fbc"); FluxBound* bound = mplugin->getFluxBound(0); fail_unless(bound->getId() == "bound1"); fail_unless(bound->getPackageName() == "fbc"); fail_unless(bound->getReaction() == "J0"); fail_unless(bound->getOperation() == "equal"); fail_unless(bound->getValue() == 10); delete document; }
int FbcToCobraConverter::convert() { int result = LIBSBML_OPERATION_FAILED; if (mDocument == NULL) { return LIBSBML_INVALID_OBJECT; } Model* mModel = mDocument->getModel(); if (mModel == NULL) { return LIBSBML_INVALID_OBJECT; } FbcModelPlugin *plugin = static_cast<FbcModelPlugin*>(mDocument->getModel()->getPlugin("fbc")); // if we have don't have a fbc model we cannot do the conversion if (plugin == NULL || mDocument->getLevel() != 3) { return LIBSBML_OPERATION_FAILED; } // collect information Model* model = mDocument->getModel(); map<const string, int> chargeMap; map<const string, string> formulaMap; for (unsigned int i = 0; i < model->getNumSpecies(); ++i) { Species* current = model->getSpecies(i); const string& currentId = current->getId(); FbcSpeciesPlugin *splugin = static_cast<FbcSpeciesPlugin*>(current->getPlugin("fbc")); if (splugin == NULL) continue; if (splugin->isSetCharge()) { chargeMap[currentId] = splugin->getCharge(); } if (splugin->isSetChemicalFormula()) { formulaMap[currentId] = splugin->getChemicalFormula(); } } // create KineticLaw for (unsigned int i = 0; i < model->getNumReactions(); ++i) { Reaction* reaction = model->getReaction(i); if (reaction == NULL) continue; createKineticLawForReaction(reaction); } // update kinetic law from bounds for (unsigned int i = 0; i < plugin->getNumFluxBounds(); ++i) { FluxBound *current = plugin->getFluxBound(i); if (current == NULL) continue; Reaction* reaction = model->getReaction(current->getReaction()); if (reaction == NULL) continue; updateKineticLawFromBound(reaction, current); } setObjectiveCoefficient(plugin, model); // disable package mDocument->enablePackage("http://www.sbml.org/sbml/level3/version1/fbc/version1", "fbc",false); // convert model to L2V1 (as L2V2 is the last model that had charge) mDocument->setConversionValidators(AllChecksON & UnitsCheckOFF); ConversionProperties prop(new SBMLNamespaces(2,1)); prop.addOption("strict", false, "should validity be preserved"); prop.addOption("ignorePackages", true, "convert even if packages are used"); prop.addOption("setLevelAndVersion", true, "convert the document to the given level and version"); int conversionResult = mDocument->convert(prop); if (conversionResult != LIBSBML_OPERATION_SUCCESS) return conversionResult; // set charge on species for (unsigned int i = 0; i < model->getNumSpecies(); ++i) { Species* current = model->getSpecies(i); const string currentId = current->getId(); int charge = chargeMap[currentId]; if (charge != 0) current->setCharge(charge); const string formula = formulaMap[currentId]; if (!formula.empty()) { current->setNotes( getNotesForFormula(formula) ); } } result = LIBSBML_OPERATION_SUCCESS; return result; }
LIBSBML_CPP_NAMESPACE_USE int main(int argc,char** argv) { SBMLNamespaces sbmlns(3,1,"fbc",1); // create the document SBMLDocument *document = new SBMLDocument(&sbmlns); document->setPackageRequired("fbc", false); // create the Model Model* model=document->createModel(); // create the Compartment Compartment* compartment = model->createCompartment(); compartment->setId("compartment"); compartment->setConstant(true); compartment->setSize(1); // create the Species Species* species = model->createSpecies(); species->setId("Node1"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node2"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node3"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node4"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node5"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node6"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node7"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node8"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node0"); species->setCompartment("compartment"); species->setBoundaryCondition(true); species->setConstant(false); species->setHasOnlySubstanceUnits(false); species = model->createSpecies(); species->setId("Node9"); species->setCompartment("compartment"); species->setBoundaryCondition(true); species->setConstant(false); species->setHasOnlySubstanceUnits(false); Reaction* reaction = model->createReaction(); reaction->setId("J0"); reaction->setReversible(false); reaction->setFast(false); SpeciesReference* reactant = reaction->createReactant(); reactant->setSpecies("Node0"); reactant->setStoichiometry(1); reactant->setConstant(true); SpeciesReference* product = reaction->createProduct(); product->setSpecies("Node1"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J1"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node1"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node2"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J2"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node2"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node3"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J3"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node1"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node4"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J4"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node4"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node3"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J5"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node3"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node5"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J6"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node5"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node6"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J7"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node6"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node7"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J8"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node5"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node8"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J9"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node8"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node7"); product->setStoichiometry(1); product->setConstant(true); reaction = model->createReaction(); reaction->setId("J10"); reaction->setReversible(false); reaction->setFast(false); reactant = reaction->createReactant(); reactant->setSpecies("Node7"); reactant->setStoichiometry(1); reactant->setConstant(true); product = reaction->createProduct(); product->setSpecies("Node9"); product->setStoichiometry(1); product->setConstant(true); // // Get a FbcModelPlugin object plugged in the model 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. // FbcModelPlugin* mplugin = static_cast<FbcModelPlugin*>(model->getPlugin("fbc")); FluxBound* bound= mplugin->createFluxBound(); bound->setId("bound1"); bound->setReaction("J0"); bound->setOperation("equal"); bound->setValue(10); Objective* objective = mplugin->createObjective(); objective->setId("obj1"); objective->setType("maximize"); // mark obj1 as active objective mplugin->setActiveObjectiveId("obj1"); FluxObjective* fluxObjective = objective->createFluxObjective(); fluxObjective->setReaction("J8"); fluxObjective->setCoefficient(1); writeSBML(document,"fbc_example1.xml"); delete document; }
END_TEST START_TEST(test_FbcExtension_create_and_write_new_geneassociation ) { FbcPkgNamespaces *sbmlns = new FbcPkgNamespaces(3, 1, 2); // create the document SBMLDocument document(sbmlns); document.setConsistencyChecks(LIBSBML_CAT_UNITS_CONSISTENCY, false); document.setConsistencyChecks(LIBSBML_CAT_MODELING_PRACTICE, false); // create the Model Model* model = document.createModel(); // create the Compartment Compartment* compartment = model->createCompartment(); compartment->setId("compartment"); compartment->setConstant(true); compartment->setSize(1); // create the Species Species* species = model->createSpecies(); species->setId("Node1"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species = model->createSpecies(); species->setId("Node2"); species->setCompartment("compartment"); species->setBoundaryCondition(false); Reaction* reaction = model->createReaction(); reaction->setId("J0"); reaction->setReversible(false); SpeciesReference* reactant = reaction->createReactant(); reactant->setSpecies("Node0"); reactant->setStoichiometry(1); SpeciesReference* product = reaction->createProduct(); product->setSpecies("Node1"); product->setStoichiometry(1); // use fbc FbcModelPlugin* mplugin = static_cast<FbcModelPlugin*>(model->getPlugin("fbc")); fail_unless(mplugin != NULL); FluxBound* bound = mplugin->createFluxBound(); bound->setId("bound1"); bound->setReaction("J0"); bound->setOperation("equal"); bound->setValue(10); Objective* objective = mplugin->createObjective(); objective->setId("obj1"); objective->setType("maximize"); FluxObjective* fluxObjective = objective->createFluxObjective(); fluxObjective->setReaction("J0"); fluxObjective->setCoefficient(1); FbcReactionPlugin* rplug = dynamic_cast<FbcReactionPlugin*>(reaction->getPlugin("fbc")); fail_unless(rplug != NULL); GeneProductAssociation * ga = rplug->createGeneProductAssociation(); ga->setId("ga1"); ga->setAssociation("MG_077 AND MG_321 AND MG_080 AND MG_078 AND MG_079"); fail_unless(ga->getAssociation() != NULL); fail_unless(mplugin->getNumGeneProducts() == 5); ga->setAssociation("MG_077 AND MG_321 AND MG_080 AND MG_078 AND MG_079"); fail_unless(ga->getAssociation() != NULL); fail_unless(mplugin->getNumGeneProducts() == 5); delete sbmlns; }
END_TEST START_TEST(test_FbcExtension_create_and_write_L3V1V1) { FbcPkgNamespaces *sbmlns = new FbcPkgNamespaces(3, 1, 1); // create the document SBMLDocument *document = new SBMLDocument(sbmlns); delete sbmlns; // create the Model Model* model = document->createModel(); // create the Compartment Compartment* compartment = model->createCompartment(); compartment->setId("compartment"); compartment->setConstant(true); compartment->setSize(1); // create the Species Species* species = model->createSpecies(); species->setId("Node1"); species->setCompartment("compartment"); species->setBoundaryCondition(false); species = model->createSpecies(); species->setId("Node2"); species->setCompartment("compartment"); species->setBoundaryCondition(false); Reaction* reaction = model->createReaction(); reaction->setId("J0"); reaction->setReversible(false); SpeciesReference* reactant = reaction->createReactant(); reactant->setSpecies("Node0"); reactant->setStoichiometry(1); SpeciesReference* product = reaction->createProduct(); product->setSpecies("Node1"); product->setStoichiometry(1); // use fbc FbcModelPlugin* mplugin = static_cast<FbcModelPlugin*>(model->getPlugin("fbc")); fail_unless(mplugin != NULL); FluxBound* bound = mplugin->createFluxBound(); bound->setId("bound1"); bound->setReaction("J0"); bound->setOperation("equal"); bound->setValue(10); Objective* objective = mplugin->createObjective(); objective->setId("obj1"); objective->setType("maximize"); FluxObjective* fluxObjective = objective->createFluxObjective(); fluxObjective->setReaction("J0"); fluxObjective->setCoefficient(1); string s1 = writeSBMLToStdString(document); // check clone() SBMLDocument* document2 = document->clone(); string s2 = writeSBMLToStdString(document2); fail_unless(s1 == s2); // check operator= Model m = *(document->getModel()); document2->setModel(&m); s2 = writeSBMLToStdString(document2); fail_unless(s1 == s2); delete document2; delete document; }
END_TEST START_TEST(test_FbcAssociation_parseFbcInfixAssociation_strange_labels) { const char* model1 = "<?xml version='1.0' encoding='UTF-8'?>" "<sbml xmlns:html='http://www.w3.org/1999/xhtml' xmlns='http://www.sbml.org/sbml/level3/version1/core' xmlns:fbc='http://www.sbml.org/sbml/level3/version1/fbc/version2' level='3' version='1' fbc:required='false'>" " <model id='M' name='E' timeUnits='dimensionless' fbc:strict='false'>" " <listOfCompartments>" " <compartment id=\"comp1\" spatialDimensions=\"3\" size=\"1\" constant=\"true\"/>" " </listOfCompartments>" " <listOfSpecies>" " <species id=\"S\" compartment=\"comp1\" initialAmount=\"1\" hasOnlySubstanceUnits=\"false\" boundaryCondition=\"false\" constant=\"false\" fbc:charge=\"2\" fbc:chemicalFormula=\"S20\"/>" " </listOfSpecies>" " <listOfReactions>" " <reaction id=\"R1\" reversible=\"false\" fast=\"false\" fbc:lowerFluxBound=\"low\" fbc:upperFluxBound=\"up\">" " <listOfReactants>" " <speciesReference species=\"S1\" stoichiometry=\"1\" constant=\"true\"/>" " </listOfReactants>" " </reaction>" " </listOfReactions>" " </model>" "</sbml>" ; const char* expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "<sbml xmlns:html=\"http://www.w3.org/1999/xhtml\" xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" xmlns:fbc=\"http://www.sbml.org/sbml/level3/version1/fbc/version2\" level=\"3\" version=\"1\" fbc:required=\"false\">\n" " <model id=\"M\" name=\"E\" timeUnits=\"dimensionless\" fbc:strict=\"false\">\n" " <listOfCompartments>\n" " <compartment id=\"comp1\" spatialDimensions=\"3\" size=\"1\" constant=\"true\"/>\n" " </listOfCompartments>\n" " <listOfSpecies>\n" " <species id=\"S\" compartment=\"comp1\" initialAmount=\"1\" hasOnlySubstanceUnits=\"false\" boundaryCondition=\"false\" constant=\"false\" fbc:charge=\"2\" fbc:chemicalFormula=\"S20\"/>\n" " </listOfSpecies>\n" " <listOfReactions>\n" " <reaction id=\"R1\" reversible=\"false\" fast=\"false\" fbc:lowerFluxBound=\"low\" fbc:upperFluxBound=\"up\">\n" " <listOfReactants>\n" " <speciesReference species=\"S1\" stoichiometry=\"1\" constant=\"true\"/>\n" " </listOfReactants>\n" " <fbc:geneProductAssociation fbc:id=\"gg1\">\n" " <fbc:and>\n" " <fbc:geneProductRef fbc:geneProduct=\"gp_g__DOT__p\"/>\n" " <fbc:geneProductRef fbc:geneProduct=\"gp___ONE__sd\"/>\n" " </fbc:and>\n" " </fbc:geneProductAssociation>\n" " </reaction>\n" " </listOfReactions>\n" " <fbc:listOfGeneProducts>\n" " <fbc:geneProduct fbc:id=\"gp_g__DOT__p\" fbc:label=\"g.p\"/>\n" " <fbc:geneProduct fbc:id=\"gp___ONE__sd\" fbc:label=\"1sd\"/>\n" " </fbc:listOfGeneProducts>\n" " </model>\n" "</sbml>\n" ; std::string infix = "(g.p and 1sd)"; SBMLDocument* doc = readSBMLFromString(model1); fail_unless(doc->getModel() != NULL); FbcModelPlugin* fbc = dynamic_cast<FbcModelPlugin*>(doc->getModel()->getPlugin("fbc")); fail_unless(fbc != NULL); fail_unless(fbc->getNumGeneProducts() == 0); Reaction * r = doc->getModel()->getReaction(0); fail_unless(r != NULL); FbcReactionPlugin * rplug = dynamic_cast<FbcReactionPlugin*>(r->getPlugin("fbc")); fail_unless (rplug != NULL); fail_unless(rplug->isSetGeneProductAssociation() == false); GeneProductAssociation* gpa = rplug->createGeneProductAssociation(); gpa->setId("gg1"); FbcAssociation * fa = FbcAssociation::parseFbcInfixAssociation(infix, fbc); fail_unless(fa->isFbcAnd() == true); gpa->setAssociation(fa); fail_unless(rplug->isSetGeneProductAssociation() == true); fail_unless(fbc->getNumGeneProducts() == 2); GeneProduct* gp = fbc->getGeneProduct(0); fail_unless (gp->isSetId() == true); fail_unless (gp->isSetLabel() == true); FbcAssociation * fa_retrieved = gpa->getAssociation(); fail_unless(fa_retrieved->isFbcAnd() == true); std::string out = fa_retrieved->toInfix(); // if it goes back to infix surely it should be the same infix that it started as fail_unless(fa_retrieved->toInfix() == infix); //GeneProductRef * gpref = dynamic_cast<GeneProductRef *>(gpa->getAssociation()); //fail_unless(gpref->isSetGeneProduct() == true); //fail_unless(gpref->getGeneProduct() == gp->getId()); char * char_doc = writeSBMLToString(doc); fail_unless(equals(expected, char_doc)); safe_free((void*)(char_doc)); delete fa; delete doc; }
END_TEST START_TEST(test_FbcExtension_parseGeneAssociation) { const char* model1 = "<?xml version='1.0' encoding='UTF-8'?>" "<sbml xmlns:html='http://www.w3.org/1999/xhtml' xmlns='http://www.sbml.org/sbml/level3/version1/core' xmlns:fbc='http://www.sbml.org/sbml/level3/version1/fbc/version1' level='3' version='1' fbc:required='false'>" " <model id='M' name='E' timeUnits='dimensionless'>" " <annotation>" " <listOfGeneAssociations xmlns='http://www.sbml.org/sbml/level3/version1/fbc/version1'>" " <geneAssociation id='ga_1' reaction='R_2DGLCNRx'>" " <gene reference='B3553'/>" " </geneAssociation>" " </listOfGeneAssociations>" " </annotation>" " </model>" "</sbml>" ; const char* model2 = "<?xml version='1.0' encoding='UTF-8'?>" "<sbml xmlns='http://www.sbml.org/sbml/level3/version1/core' xmlns:fbc='http://www.sbml.org/sbml/level3/version1/fbc/version1' metaid='_1f29713d_1639_4193_9935_d6c7ec5255c6' level='3' version='1' fbc:required='false'>" " <model metaid='meta' id='id' name='name' timeUnits='dimensionless'>" " <notes>" " <p xmlns='http://www.w3.org/1999/xhtml'>" " I prevent you from reading annotations ... " " </p>" " </notes>" " <annotation>" " <listOfGeneAssociations xmlns='http://www.sbml.org/sbml/level3/version1/fbc/version1'>" " <geneAssociation id='ga_1' reaction='MNXR2184_i'>" " <or>" " <gene reference='MCON_1478_I'/>" " <gene reference='MCON_2528_I'/>" " <gene reference='MCON_3321_I'/>" " <gene reference='METACYC_GHHN_1245_MONOMER_I'/>" " <gene reference='METACYC_GHHN_2657_MONOMER_I'/>" " <gene reference='METACYC_GHHN_2722_MONOMER_I'/>" " </or>" " </geneAssociation>" " </listOfGeneAssociations>" " </annotation>" " </model>" "</sbml>" ; const char* model3 = "<?xml version='1.0' encoding='UTF-8'?>" "<sbml xmlns:html='http://www.w3.org/1999/xhtml' xmlns='http://www.sbml.org/sbml/level3/version1/core' xmlns:fbc='http://www.sbml.org/sbml/level3/version1/fbc/version1' level='3' version='1' fbc:required='false'>" " <model id='M' name='E' timeUnits='dimensionless'>" " <annotation>" " <listOfGeneAssociations xmlns='http://www.sbml.org/sbml/level3/version1/fbc/version1'>" " <geneAssociation id='ga_1' reaction='R_2DGLCNRx'>" " <gene reference='B3553.FR1.F121312'/>" " </geneAssociation>" " </listOfGeneAssociations>" " </annotation>" " </model>" "</sbml>" ; const char* model4 = "<?xml version='1.0' encoding='UTF-8'?>" "<sbml xmlns:html='http://www.w3.org/1999/xhtml' xmlns='http://www.sbml.org/sbml/level3/version1/core' xmlns:fbc='http://www.sbml.org/sbml/level3/version1/fbc/version1' level='3' version='1' fbc:required='false'>" " <model id='M' name='E' timeUnits='dimensionless'>" " <annotation>" " <listOfGeneAssociations xmlns='http://www.sbml.org/sbml/level3/version1/fbc/version1'>" " <geneAssociation id='ga_1' reaction='R_2DGLCNRx'>" " <gene reference='3.14'/>" " </geneAssociation>" " </listOfGeneAssociations>" " </annotation>" " </model>" "</sbml>" ; SBMLDocument* doc = readSBMLFromString(model1); fail_unless(doc->getModel() != NULL); FbcModelPlugin* fbc = dynamic_cast<FbcModelPlugin*>(doc->getModel()->getPlugin("fbc")); fail_unless(fbc != NULL); fail_unless(fbc->getNumGeneAssociations() == 1); SBMLDocument_free(doc); doc = readSBMLFromString(model2); fail_unless(doc->getModel() != NULL); fbc = dynamic_cast<FbcModelPlugin*>(doc->getModel()->getPlugin("fbc")); fail_unless(fbc != NULL); fail_unless(fbc->getNumGeneAssociations() == 1); SBMLDocument_free(doc); doc = readSBMLFromString(model3); fail_unless(doc->getModel() != NULL); fbc = dynamic_cast<FbcModelPlugin*>(doc->getModel()->getPlugin("fbc")); fail_unless(fbc != NULL); fail_unless(fbc->getNumGeneAssociations() == 1); SBMLDocument_free(doc); doc = readSBMLFromString(model4); fail_unless(doc->getModel() != NULL); fbc = dynamic_cast<FbcModelPlugin*>(doc->getModel()->getPlugin("fbc")); fail_unless(fbc != NULL); fail_unless(fbc->getNumGeneAssociations() == 1); SBMLDocument_free(doc); Association* test = Association::parseInfixAssociation("F1.F2.F3 OR F2.f3.f4"); fail_unless(test != NULL); fail_unless(test->toInfix() == "(F1.F2.F3 or F2.f3.f4)"); test = Association::parseInfixAssociation("3.1 or 3.2"); fail_unless(test != NULL); fail_unless(test->toInfix() == "(3.1 or 3.2)"); }
Model* CompModelPlugin::flattenModel() const { //First make a copy of our parent (the model to be flattened): const Model* parent = static_cast<const Model*>(getParentSBMLObject()); if (parent==NULL) { return NULL; } //doc needs to be non-const so that the error messages can be updated. Otherwise, nothing changes. SBMLDocument* doc = const_cast<SBMLDocument*>(getSBMLDocument()); if (doc==NULL) { return NULL; } //Set the original document so that it can find the model definitions //and external model definitions while we flatten. Model* flat = parent->clone(); flat->setSBMLDocument(doc); CompModelPlugin* flatplug = static_cast<CompModelPlugin*>(flat->getPlugin(getPrefix())); // Now instantiate its submodels and // follow all renaming/deletion/replacement rules. vector<const Model*> submods; int success = flatplug->instantiateSubmodels(); if (success != LIBSBML_OPERATION_SUCCESS) { //instantiateSubmodels sets its own error messages. delete flat; return NULL; } //Now start the aggregation process. //This goes from the bottom up, calling 'appendFrom' iteratively //(from the plugin). for (unsigned int sm=0; sm<flatplug->getNumSubmodels(); sm++) { Model* submodel = flatplug->getSubmodel(sm)->getInstantiation(); if (submodel==NULL) { //getInstantiation should be calling a cached value by now, but if not, it will set its own error messages. delete flat; return NULL; } CompModelPlugin* submodplug = static_cast<CompModelPlugin*>(submodel->getPlugin(getPrefix())); if (submodplug != NULL) { //Strip the ports from the submodel, as we no longer need them. while (submodplug->getNumPorts() > 0) { delete submodplug->removePort(0); } } success = flat->appendFrom(submodel); if (success != LIBSBML_OPERATION_SUCCESS) { string error = "Unable to flatten model in CompModelPlugin::flattenModel: appending elements from the submodel '" + submodel->getId() + "' to the elements of the parent model failed."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error); delete flat; return NULL; } #ifdef LIBSBML_HAS_PACKAGE_FBC // for an fbc v2 model we need to check that the model element // in the flat document has the fbc:strict attribute // note this can happen if the parent document did not have fbc but // included a submodel from an external document that did if (SBMLExtensionRegistry::isPackageEnabled("fbc")) { FbcModelPlugin *mplugin = static_cast<FbcModelPlugin*> (flat->getPlugin("fbc")); if (mplugin != NULL && mplugin->getPackageVersion() == 2 && mplugin->isSetStrict() == false) { mplugin->setStrict(false); } } #endif // LIBSBML_HAS_PACKAGE_FBC } // Now we clear the saved referenced elements in the local Port objects, // but point them to the new object if necessary. flatplug->resetPorts(); // Next, strip the package info from 'flat'. // We're going to remove everything but the Ports: flatplug->mListOfSubmodels.clear(); flatplug->clearReplacedElements(); flatplug->unsetReplacedBy(); List* allelements = flat->getAllElements(); vector<SBase*> nonReplacedElements; for (unsigned int el=0; el<allelements->getSize(); el++) { SBase* element = static_cast<SBase*>(allelements->get(el)); int type = element->getTypeCode(); if (!(type==SBML_COMP_REPLACEDBY || type==SBML_COMP_REPLACEDELEMENT || type==SBML_COMP_SBASEREF)) { nonReplacedElements.push_back(element); } } // delete the list delete allelements; for (unsigned int el=0; el<nonReplacedElements.size(); el++) { SBase* element = nonReplacedElements[el]; CompSBasePlugin* elplug = static_cast<CompSBasePlugin*>(element->getPlugin(getPrefix())); if (elplug != NULL) { elplug->clearReplacedElements(); elplug->unsetReplacedBy(); } } //Finally, unset the document again. flat->setSBMLDocument(NULL); return flat; }