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; }