bool CCopasiSpringLayout::initFromLayout(CLayout* layout, Parameters* ppp) { mpLayout = layout; mpPar = ppp; if (!mpLayout) return false; if (!ppp) return false; mCompartmentMap.clear(); //store the compartment glyph for each species glyph (if it exists) unsigned int i; for (i = 0; i < mpLayout->getListOfMetaboliteGlyphs().size() ; ++i) { CMetab* metab = dynamic_cast<CMetab*>(mpLayout->getListOfMetaboliteGlyphs()[i].getModelObject()); CLCompartmentGlyph* tmp = NULL; if (metab) { unsigned int j; for (j = 0; j < mpLayout->getListOfCompartmentGlyphs().size(); ++j) if (mpLayout->getListOfCompartmentGlyphs()[j].getModelObjectKey() == metab->getCompartment()->getKey()) { tmp = &mpLayout->getListOfCompartmentGlyphs()[j]; break; } } mCompartmentMap[&mpLayout->getListOfMetaboliteGlyphs()[i]] = tmp; } //store the compartment glyph for each reaction glyph (if it exists) for (i = 0; i < mpLayout->getListOfReactionGlyphs().size() ; ++i) { mCompartmentMap[&mpLayout->getListOfReactionGlyphs()[i]] = findCompartmentForReactionNode(mpLayout->getListOfReactionGlyphs()[i]); } //create the list of constant positional relations //we assume that text glyphs are always moved with the glyph they refer to. //(i.e. the are not layed out independently) mFixedRelations.clear(); for (i = 0; i < mpLayout->getListOfTextGlyphs().size(); ++i) { CoordinateRelation tmp; tmp.target = &mpLayout->getListOfTextGlyphs()[i]; tmp.source = mpLayout->getListOfTextGlyphs()[i].getGraphicalObject(); if (tmp.source) { tmp.diff = tmp.target->getPosition() - tmp.source->getPosition(); mFixedRelations.push_back(tmp); } } return true; }
// virtual CXMLHandler * SubstrateHandler::processStart(const XML_Char * pszName, const XML_Char ** papszAttrs) { const char * Metabolite; CMetab * pMetabolite; const char * Stoichiometry; switch (mCurrentElement.first) { case Substrate: Metabolite = mpParser->getAttributeValue("metabolite", papszAttrs); Stoichiometry = mpParser->getAttributeValue("stoichiometry", papszAttrs); pMetabolite = dynamic_cast< CMetab * >(mpData->mKeyMap.get(Metabolite)); if (!pMetabolite) fatalError(); mpData->pReaction->addSubstrate(pMetabolite->getKey(), CCopasiXMLInterface::DBL(Stoichiometry)); break; default: CCopasiMessage(CCopasiMessage::EXCEPTION, MCXML + 2, mpParser->getCurrentLineNumber(), mpParser->getCurrentColumnNumber(), pszName); break; } return NULL; }
CMetab * UndoSpeciesData::createObjectIn(CModel *pModel) { if (pModel == NULL) return NULL; createDependentObjects(pModel); if (pModel->getCompartments().getIndex(mCompartment) == C_INVALID_INDEX) return NULL; CCompartment * pCompartment = &pModel->getCompartments()[getCompartment()]; if (pCompartment == NULL) return NULL; if (pCompartment->getMetabolites().getIndex(getName()) != C_INVALID_INDEX) return NULL; CMetab *pSpecies = pModel->createMetabolite(getName(), getCompartment(), getIConc(), getStatus()); if (pSpecies == NULL) return NULL; mKey = pSpecies->getKey(); return pSpecies; }
void UndoSpeciesData::fillObject(CModel *) { CMetab * pSpecies = dynamic_cast<CMetab*>(CCopasiRootContainer::getKeyFactory()->get(mKey)); if (pSpecies == NULL) return; if (getStatus() != CModelEntity::ASSIGNMENT) { pSpecies->setInitialConcentration(getIConc()); pSpecies->setInitialValue(getINumber()); } if (getStatus() == CModelEntity::ODE || getStatus() == CModelEntity::ASSIGNMENT) { pSpecies->setExpression(getExpression()); } // set initial expression if (getStatus() != CModelEntity::ASSIGNMENT) { pSpecies->setInitialExpression(getInitialExpression()); } }
bool CModelAdd::addMetabolites(std::string name) { bool info = false; size_t i, imax = mmModel->getMetabolites().size(); for (i = 0; i < imax; ++i) { const CMetab* sourceMetab = &mmModel->getMetabolites()[i]; const CCompartment* sourceComp = sourceMetab->getCompartment(); if (!sourceMetab) return info; if (!sourceComp) return info; //create new metabolite std::string newName = sourceMetab->getObjectName() + "_" + name; CMetab* newMetab = mpModel->createMetabolite(sourceMetab->getObjectName(), nameMap[sourceComp->getObjectName()], sourceMetab->getInitialConcentration()); if (!newMetab) return info; newMetab->setStatus(sourceMetab->getStatus()); keyMap[sourceMetab->getKey()] = newMetab->getKey(); nameMap[sourceMetab->getObjectName()] = newName; } return true; }
void CQSpeciesDetail::speciesInitialValueLostFocus(UndoSpeciesData *pSData) { GET_MODEL_OR_RETURN(pModel); //find the species of interest and switch to its widget CMetab *pSpecie = pModel->findMetabByName(pSData->getName()); if (pSpecie != NULL) { std::string key = pSpecie->getKey(); mpListView->switchToOtherWidget(C_INVALID_INDEX, key); } switch (mFramework) { case 0: mInitialConcentration = pSData->getIConc(); pSData->setIConc(mpEditInitialValue->text().toDouble()); mpEditInitialValue->setText(QString::number(mInitialConcentration, 'g', 10)); break; case 1: mInitialNumber = pSData->getINumber(); pSData->setINumber(mpEditInitialValue->text().toDouble()); mpEditInitialValue->setText(QString::number(mInitialNumber, 'g', 10)); break; } }
void test000046::test_stoichiometricExpression() { CCopasiDataModel* pDataModel = pCOPASIDATAMODEL; CPPUNIT_ASSERT(pDataModel->importSBMLFromString(MODEL_STRING)); CModel* pModel = pDataModel->getModel(); CPPUNIT_ASSERT(pModel != NULL); CPPUNIT_ASSERT(pModel->getQuantityUnitEnum() == CModel::mMol); CPPUNIT_ASSERT(pModel->getVolumeUnitEnum() == CModel::ml); CPPUNIT_ASSERT(pModel->getTimeUnitEnum() == CModel::s); CPPUNIT_ASSERT(pModel->getCompartments().size() == 1); const CCompartment* pCompartment = pModel->getCompartments()[0]; CPPUNIT_ASSERT(pCompartment != NULL); CPPUNIT_ASSERT(pCompartment->getStatus() == CModelEntity::FIXED); CPPUNIT_ASSERT(pModel->getMetabolites().size() == 2); CMetab* pA = pModel->getMetabolites()[0]; CPPUNIT_ASSERT(pA != NULL); CPPUNIT_ASSERT(pA->getStatus() == CModelEntity::REACTIONS); const CMetab* pB = pModel->getMetabolites()[1]; CPPUNIT_ASSERT(pB != NULL); CPPUNIT_ASSERT(pB->getStatus() == CModelEntity::REACTIONS); CPPUNIT_ASSERT(pModel->getModelValues().size() == 1); const CModelValue* pModelValue = pModel->getModelValues()[0]; CPPUNIT_ASSERT(pModelValue != NULL); CPPUNIT_ASSERT(pModelValue->getStatus() == CModelEntity::FIXED); CPPUNIT_ASSERT(fabs((pModelValue->getInitialValue() - 3.7) / 3.7) < 1e-3); CPPUNIT_ASSERT(pModel->getReactions().size() == 1); const CReaction* pReaction1 = pModel->getReactions()[0]; CPPUNIT_ASSERT(pReaction1 != NULL); CPPUNIT_ASSERT(pReaction1->isReversible() == true); // check the kinetic law const CFunction* pKineticFunction = pReaction1->getFunction(); CPPUNIT_ASSERT(pKineticFunction != NULL); const CMassAction* pMassAction = dynamic_cast<const CMassAction*>(pKineticFunction); CPPUNIT_ASSERT(pMassAction != NULL); const CChemEq* pChemEq = &pReaction1->getChemEq(); CPPUNIT_ASSERT(pChemEq != NULL); CPPUNIT_ASSERT(pChemEq->getCompartmentNumber() == 1); CPPUNIT_ASSERT(pChemEq->getSubstrates().size() == 1); const CChemEqElement* pElement = pChemEq->getSubstrates()[0]; CPPUNIT_ASSERT(pElement != NULL); CPPUNIT_ASSERT(fabs((pElement->getMultiplicity() - 1.85) / 1.85) < 1e-3); CPPUNIT_ASSERT(pElement->getMetabolite() == pA); CPPUNIT_ASSERT(pChemEq->getProducts().size() == 1); pElement = pChemEq->getProducts()[0]; CPPUNIT_ASSERT(pElement != NULL); CPPUNIT_ASSERT(fabs((pElement->getMultiplicity() - 3.35) / 3.35) / 3.35 < 1e-3); CPPUNIT_ASSERT(pElement->getMetabolite() == pB); CPPUNIT_ASSERT(pChemEq->getModifiers().size() == 0); //CPPUNIT_ASSERT(CCopasiMessage::size() == 5); CCopasiMessage message = CCopasiMessage::getLastMessage(); CPPUNIT_ASSERT(message.getType() == CCopasiMessage::WARNING); std::string s = message.getText(); CPPUNIT_ASSERT(!s.empty()); CPPUNIT_ASSERT(s.find(std::string("One or more stoichiometric expressions were evaluated and converted to constants values.")) != std::string::npos); // the other four messages are libSBML unit warnings I don't care about right // now. }
void test000047::test_delay() { CCopasiDataModel* pDataModel = pCOPASIDATAMODEL; CPPUNIT_ASSERT(pDataModel->importSBMLFromString(MODEL_STRING)); CModel* pModel = pDataModel->getModel(); CPPUNIT_ASSERT(pModel != NULL); CPPUNIT_ASSERT(pModel->getQuantityUnitEnum() == CUnit::mMol); CPPUNIT_ASSERT(pModel->getVolumeUnitEnum() == CUnit::ml); CPPUNIT_ASSERT(pModel->getTimeUnitEnum() == CUnit::s); CPPUNIT_ASSERT(pModel->getCompartments().size() == 1); const CCompartment* pCompartment = pModel->getCompartments()[0]; CPPUNIT_ASSERT(pCompartment != NULL); CPPUNIT_ASSERT(pCompartment->getStatus() == CModelEntity::FIXED); CPPUNIT_ASSERT(pModel->getMetabolites().size() == 2); const CMetab* pB = pModel->getMetabolites()[1]; CPPUNIT_ASSERT(pB != NULL); CPPUNIT_ASSERT(pB->getStatus() == CModelEntity::FIXED); CMetab* pA = pModel->getMetabolites()[0]; CPPUNIT_ASSERT(pA != NULL); CPPUNIT_ASSERT(pA->getStatus() == CModelEntity::ASSIGNMENT); const CExpression* pExpr = pA->getExpressionPtr(); CPPUNIT_ASSERT(pExpr != NULL); const CEvaluationNode* pNode = pExpr->getRoot(); CPPUNIT_ASSERT(pNode != NULL); const CEvaluationNodeDelay* pDelayNode = dynamic_cast<const CEvaluationNodeDelay*>(pNode); CPPUNIT_ASSERT(pDelayNode != NULL); const CEvaluationNodeObject* pObjectNode = dynamic_cast<const CEvaluationNodeObject*>(pDelayNode->getChild()); CPPUNIT_ASSERT(pObjectNode != NULL); CCopasiObjectName objectCN = pObjectNode->getObjectCN(); CPPUNIT_ASSERT(!objectCN.empty()); std::vector<CCopasiContainer*> listOfContainers; listOfContainers.push_back(pModel); const CCopasiObject* pObject = pCOPASIDATAMODEL->ObjectFromName(listOfContainers, objectCN); CPPUNIT_ASSERT(pObject != NULL); CPPUNIT_ASSERT(pObject->isReference() == true); CPPUNIT_ASSERT(pObject->getObjectName() == std::string("Concentration")); CPPUNIT_ASSERT(pObject->getObjectParent() == pB); const CEvaluationNodeNumber* pNumberNode = dynamic_cast<const CEvaluationNodeNumber*>(pObjectNode->getSibling()); CPPUNIT_ASSERT(pNumberNode != NULL); CPPUNIT_ASSERT(((CEvaluationNodeNumber::SubType)CEvaluationNode::subType(pNumberNode->getType())) == CEvaluationNodeNumber::DOUBLE); CPPUNIT_ASSERT(fabs((pNumberNode->getValue() - 0.5) / 0.5) < 1e-3); CPPUNIT_ASSERT(pNumberNode->getSibling() == NULL); CPPUNIT_ASSERT(pModel->getModelValues().size() == 1); const CModelValue* pModelValue = pModel->getModelValues()[0]; CPPUNIT_ASSERT(pModelValue != NULL); CPPUNIT_ASSERT(pModelValue->getStatus() == CModelEntity::FIXED); CPPUNIT_ASSERT(pModel->getReactions().size() == 0); //CPPUNIT_ASSERT(CCopasiMessage::size() == 2); CCopasiMessage message = CCopasiMessage::getLastMessage(); CPPUNIT_ASSERT(message.getType() == CCopasiMessage::WARNING); std::string s = message.getText(); CPPUNIT_ASSERT(!s.empty()); CPPUNIT_ASSERT(s.find(std::string("COPASI does not support time delays. Calculations on this model will most likely lead to unusable results.")) != std::string::npos); // right now, we don't care about the last message since it is a units // warning from libSBML }
void CChemEqElement::setMetabolite(const std::string & key) { mMetaboliteKey = key; CMetab* tmp = dynamic_cast< CMetab * >(CCopasiRootContainer::getKeyFactory()->get(mMetaboliteKey)); if (tmp) this->setObjectName("ChEqEl_" + tmp->getObjectName()); else this->setObjectName("ChemEqElement"); }
std::string CMetabNameInterface::getMetaboliteKey(const CModel* model, const std::string & metabolite, const std::string & compartment) { CMetab * metab = getMetabolite(model, metabolite, compartment); if (metab) return metab->getKey(); else return ""; }
void test000052::test_bug988() { CDataModel* pDataModel = pCOPASIDATAMODEL; CPPUNIT_ASSERT(pDataModel->importSBMLFromString(MODEL_STRING)); CModel* pModel = pDataModel->getModel(); CPPUNIT_ASSERT(pModel != NULL); CPPUNIT_ASSERT(pModel->getQuantityUnitEnum() == CUnit::mMol); CPPUNIT_ASSERT(pModel->getVolumeUnitEnum() == CUnit::ml); CPPUNIT_ASSERT(pModel->getTimeUnitEnum() == CUnit::s); CPPUNIT_ASSERT(pModel->getCompartments().size() == 1); const CCompartment* pCompartment = pModel->getCompartments()[0]; CPPUNIT_ASSERT(pCompartment != NULL); CPPUNIT_ASSERT(pCompartment->getStatus() == CModelEntity::Status::FIXED); CPPUNIT_ASSERT(pModel->getMetabolites().size() == 1); CMetab* pA = pModel->getMetabolites()[0]; CPPUNIT_ASSERT(pA != NULL); CPPUNIT_ASSERT(pA->getStatus() == CModelEntity::Status::ASSIGNMENT); const CExpression* pExpr = pA->getExpressionPtr(); // check the expression const CEvaluationNode* pNode = pExpr->getRoot(); CPPUNIT_ASSERT(pNode != NULL); const CEvaluationNodeChoice* pChoiceNode = dynamic_cast<const CEvaluationNodeChoice*>(pNode); CPPUNIT_ASSERT(pChoiceNode != NULL); pNode = dynamic_cast<const CEvaluationNode*>(pChoiceNode->getChild()); CPPUNIT_ASSERT(pNode != NULL); const CEvaluationNodeLogical* pLogicalNode = dynamic_cast<const CEvaluationNodeLogical*>(pNode); CPPUNIT_ASSERT(pLogicalNode != NULL); CPPUNIT_ASSERT((pLogicalNode->subType()) == CEvaluationNode::SubType::LT); const CEvaluationNodeObject* pObjectNode = dynamic_cast<const CEvaluationNodeObject*>(pLogicalNode->getChild()); CPPUNIT_ASSERT(pObjectNode != NULL); CCommonName objectCN = pObjectNode->getObjectCN(); CPPUNIT_ASSERT(!objectCN.empty()); CObjectInterface::ContainerList listOfContainers; listOfContainers.push_back(pModel); const CDataObject* pObject = CObjectInterface::DataModel(pCOPASIDATAMODEL->getObjectFromCN(listOfContainers, objectCN)); CPPUNIT_ASSERT(pObject != NULL); CPPUNIT_ASSERT(pObject->hasFlag(CDataObject::Reference) == true); CPPUNIT_ASSERT(pObject->getObjectName() == std::string("Time")); CPPUNIT_ASSERT(pObject->getObjectParent() == pModel); const CEvaluationNodeNumber* pNumberNode = dynamic_cast<const CEvaluationNodeNumber*>(pObjectNode->getSibling()); CPPUNIT_ASSERT(pNumberNode != NULL); CPPUNIT_ASSERT((pNumberNode->subType()) == CEvaluationNode::SubType::DOUBLE); CPPUNIT_ASSERT(fabs((pNumberNode->getValue() - 5.0) / 5.0) < 1e-3); pNumberNode = dynamic_cast<const CEvaluationNodeNumber*>(pLogicalNode->getSibling()); CPPUNIT_ASSERT(pNumberNode != NULL); CPPUNIT_ASSERT((pNumberNode->subType()) == CEvaluationNode::SubType::DOUBLE); CPPUNIT_ASSERT(pNumberNode->getValue() < 1e-3); pNumberNode = dynamic_cast<const CEvaluationNodeNumber*>(pNumberNode->getSibling()); CPPUNIT_ASSERT(pNumberNode != NULL); CPPUNIT_ASSERT((pNumberNode->subType()) == CEvaluationNode::SubType::DOUBLE); CPPUNIT_ASSERT(fabs((pNumberNode->getValue() - 10.0) / 10.0) < 1e-3); CPPUNIT_ASSERT(pModel->getModelValues().size() == 0); CPPUNIT_ASSERT(pModel->getReactions().size() == 0); }
void CQSpecieDM::addSpecieRow(UndoSpeciesData *pSpecieData) { GET_MODEL_OR_RETURN(pModel); switchToWidget(CCopasiUndoCommand::SPECIES); CMetab *species = pSpecieData->restoreObjectIn(pModel); if (species == NULL) return; beginInsertRows(QModelIndex(), 1, 1); emit notifyGUI(ListViews::METABOLITE, ListViews::ADD, species->getKey()); endInsertRows(); }
void CQSpeciesDetail::addSpecies(UndoSpeciesData *pSData) { GET_MODEL_OR_RETURN(pModel); //reinsert the species CMetab *pSpecies = pSData->restoreObjectIn(pModel); if (pSpecies == NULL) return; std::string key = pSpecies->getKey(); protectedNotify(ListViews::METABOLITE, ListViews::ADD, key); mpListView->switchToOtherWidget(C_INVALID_INDEX, key); }
bool CQSpecieDM::removeRows(QModelIndexList rows, const QModelIndex&) { if (rows.isEmpty()) return false; assert(CCopasiRootContainer::getDatamodelList()->size() > 0); CCopasiDataModel* pDataModel = (*CCopasiRootContainer::getDatamodelList())[0]; assert(pDataModel != NULL); CModel * pModel = pDataModel->getModel(); if (pModel == NULL) return false; //Build the list of pointers to items to be deleted //before actually deleting any item. QList <CMetab *> pSpecies; QModelIndexList::const_iterator i; for (i = rows.begin(); i != rows.end(); ++i) { if (!isDefaultRow(*i) && pModel->getMetabolites()[(*i).row()]) pSpecies.append(pModel->getMetabolites()[(*i).row()]); } QList <CMetab *>::const_iterator j; for (j = pSpecies.begin(); j != pSpecies.end(); ++j) { CMetab * pSpecie = *j; size_t delRow = pModel->getMetabolites().CCopasiVector< CMetab >::getIndex(pSpecie); if (delRow != C_INVALID_INDEX) { QMessageBox::StandardButton choice = CQMessageBox::confirmDelete(NULL, "species", FROM_UTF8(pSpecie->getObjectName()), pSpecie->getDeletedObjects()); if (choice == QMessageBox::Ok) removeRow((int) delRow); } } return true; }
void CModelMerging::simpleCall(std::vector< std::string > & /* toKey */, std::vector< std::string > & objectKey) { if (!mpModel) { fatalError(); } size_t i, j, imax = mpModel->getMetabolites().size(); CMetab * metab; CMetab * metab1; CMetab* tmp; std::string empty = ""; for (i = 0; i < imax; ++i) { metab = &mpModel->getMetabolites()[i]; for (j = 0; j < imax; ++j) { if (objectKey[i] != "") { tmp = &mpModel->getMetabolites()[j]; if (tmp->getKey() == objectKey[i]) { metab1 = tmp; } } } if (! mergeMetabolites(metab->getKey(), objectKey[i])) { CCopasiMessage(CCopasiMessage::ERROR, MCModelMerging + 2, metab1->getObjectName().c_str(), metab->getObjectName().c_str()); return; } } for (i = 0; i < imax; ++i) { if (objectKey[i] != empty) mpModel->removeMetabolite(objectKey[i] , false); } mpModel->compileIfNecessary(NULL); }
void CQSpeciesDetail::deleteSpecies(UndoSpeciesData *pSData) { GET_MODEL_OR_RETURN(pModel); switchToWidget(CCopasiUndoCommand::SPECIES); CMetab * pSpecies = dynamic_cast< CMetab * >(pSData->getObject(pModel)); if (pSpecies == NULL) return; std::string key = pSpecies->getKey(); pModel->removeMetabolite(key); #undef DELETE protectedNotify(ListViews::METABOLITE, ListViews::DELETE, key); //mKey); protectedNotify(ListViews::METABOLITE, ListViews::DELETE, "");//Refresh all as there may be dependencies. }
void CQSpeciesDetail::speciesTypeChanged(UndoSpeciesData *pSData, int type) { GET_MODEL_OR_RETURN(pModel); //find the species of interest and switch to its widget CMetab *pSpecie = pModel->findMetabByName(pSData->getName()); if (pSpecie != NULL) { std::string key = pSpecie->getKey(); mpListView->switchToOtherWidget(C_INVALID_INDEX, key); } //set the species index mpComboBoxType->setCurrentIndex(mpComboBoxType->findText(FROM_UTF8(CModelEntity::StatusName[type]))); mpMetab->setStatus((CModelEntity::Status)mItemToType[mpComboBoxType->currentIndex()]); speciesTypeChanged(mpComboBoxType->currentIndex()); }
std::pair< C_FLOAT64, C_FLOAT64 > CEFMTask::getSpeciesChanges(const CFluxMode & fluxMode, const CMetab & metab) const { C_FLOAT64 In = 0.0; C_FLOAT64 Out = 0.0; C_FLOAT64 *pIn, *pOut; CFluxMode::const_iterator itReaction = fluxMode.begin(); CFluxMode::const_iterator endReaction = fluxMode.end(); std::string Key = metab.getKey(); const std::vector< const CReaction * > & ReorderedReactions = static_cast<CEFMProblem *>(mpProblem)->getReorderedReactions(); for (; itReaction != endReaction; ++itReaction) { const CReaction * pReaction = ReorderedReactions[itReaction->first]; if (itReaction->second < 0.0) { pIn = &Out; pOut = &In; } else { pIn = &In; pOut = &Out; } CCopasiVector < CChemEqElement >::const_iterator it = pReaction->getChemEq().getSubstrates().begin(); CCopasiVector < CChemEqElement >::const_iterator end = pReaction->getChemEq().getSubstrates().end(); for (; it != end; ++it) { if ((*it)->getMetaboliteKey() == Key) { *pIn += fabs(itReaction->second) * (*it)->getMultiplicity(); break; } } it = pReaction->getChemEq().getProducts().begin(); end = pReaction->getChemEq().getProducts().end(); for (; it != end; ++it) { if ((*it)->getMetaboliteKey() == Key) { *pOut += fabs(itReaction->second) * (*it)->getMultiplicity(); break; } } } return std::make_pair(In, Out); }
bool CQSpecieDM::removeSpecieRows(QModelIndexList rows, const QModelIndex&) { if (rows.isEmpty()) return false; GET_MODEL_OR(pModel, return false); switchToWidget(CCopasiUndoCommand::SPECIES); //Build the list of pointers to items to be deleted //before actually deleting any item. QList <CMetab *> pSpecies; QModelIndexList::const_iterator i; for (i = rows.begin(); i != rows.end(); ++i) { if (!isDefaultRow(*i) && &pModel->getMetabolites()[i->row()]) pSpecies.append(&pModel->getMetabolites()[i->row()]); } QList <CMetab *>::const_iterator j; for (j = pSpecies.begin(); j != pSpecies.end(); ++j) { CMetab * pSpecie = *j; size_t delRow = pModel->getMetabolites().CCopasiVector< CMetab >::getIndex(pSpecie); if (delRow == C_INVALID_INDEX) continue; QMessageBox::StandardButton choice = CQMessageBox::confirmDelete(NULL, "species", FROM_UTF8(pSpecie->getObjectName()), pSpecie->getDeletedObjects()); if (choice == QMessageBox::Ok) removeRow((int) delRow); } return true; }
bool CMetabNameInterface::doesExist(const CModel* model, const std::string & metabolite, const std::string & compartment) { if (model == NULL) return false; CDataContainer::objectMap::range Range = model->getMetabolites().getObjects().equal_range(metabolite); CMetab * pSpecies = NULL; for (; Range.first != Range.second; ++Range.first) if ((pSpecies = dynamic_cast< CMetab * >(*Range.first)) != NULL) { if (compartment.empty() || pSpecies->getCompartment()->getObjectName() == compartment) return true; } return false; }
void UndoDependentData::createDependentObjects(CModel *pModel, QList<UndoSpeciesData *> *pSpeciesData) { if (pModel == NULL || pSpeciesData == NULL || pSpeciesData->empty()) return; //reaction may further has dependencies, these must be taken care of QList <UndoSpeciesData *>::const_iterator rs; for (rs = pSpeciesData->begin(); rs != pSpeciesData->end(); ++rs) { UndoSpeciesData * data = *rs; CMetab* pSpecies = data->createObjectIn(pModel); if (pSpecies == NULL) continue; updateGUI(ListViews::METABOLITE, ListViews::ADD, pSpecies->getKey()); } }
void CQUpdatesWidget::loadObjectsTable(CModel* pModel) { if (!pModel) return; mpTableObj->setColumnCount(5); mpTableObj->setRowCount(0); mpTableObj->setHorizontalHeaderItem(0, new QTableWidgetItem("User order")); mpTableObj->setHorizontalHeaderItem(1, new QTableWidgetItem("status")); mpTableObj->setHorizontalHeaderItem(2, new QTableWidgetItem("")); mpTableObj->setHorizontalHeaderItem(3, new QTableWidgetItem("Reduced system")); mpTableObj->setHorizontalHeaderItem(4, new QTableWidgetItem("status")); size_t i, imax; //metabolites imax = pModel->getMetabolites().size(); if ((int) imax > mpTableObj->rowCount()) mpTableObj->setRowCount((int) imax); for (i = 0; i < imax; ++i) { mpTableObj->setVerticalHeaderItem((int) i, new QTableWidgetItem(QString::number(i))); CMetab* pM = pModel->getMetabolites()[i]; QColor c(100, 100, 100); if (pM->getStatus() == CModelEntity::FIXED) c = QColor(150, 150, 150); if (pM->getStatus() == CModelEntity::ASSIGNMENT) c = QColor(250, 150, 250); if (pM->getStatus() == CModelEntity::REACTIONS) c = QColor(250, 250, 200); if (pM->getStatus() == CModelEntity::ODE) c = QColor(150, 250, 250); if (pM->getStatus() == CModelEntity::TIME) c = QColor(250, 150, 150); mpTableObj->setItem((int) i, 0, new QTableWidgetItem(FROM_UTF8(pM->getObjectName()))); //mpTableObj->setText(i, 0, FROM_UTF8(pM->getObjectName())); std::string tmpString = CModelEntity::StatusName[pM->getStatus()]; if (pM->isUsed()) tmpString += " (Used = true, "; else tmpString += " (Used = false, "; mpTableObj->setItem((int) i, 1, new QTableWidgetItem(FROM_UTF8(tmpString))); //mpTableObj->setText(i, 1, FROM_UTF8(tmpString)); } mpTableObj->resizeColumnToContents(0); mpTableObj->resizeColumnToContents(1); mpTableObj->setColumnWidth(2, 10); //metabolitesX imax = pModel->getMetabolitesX().size(); if ((int) imax > mpTableObj->rowCount()) mpTableObj->setRowCount((int) imax); for (i = 0; i < imax; ++i) { CMetab* pM = pModel->getMetabolitesX()[i]; QColor c(100, 100, 100); if (pM->getStatus() == CModelEntity::FIXED) c = QColor(150, 150, 150); if (pM->getStatus() == CModelEntity::ASSIGNMENT) c = QColor(250, 150, 250); if (pM->getStatus() == CModelEntity::REACTIONS) c = QColor(250, 250, 200); if (pM->getStatus() == CModelEntity::ODE) c = QColor(150, 250, 250); if (pM->getStatus() == CModelEntity::TIME) c = QColor(250, 150, 150); mpTableObj->setItem((int) i, 3, new QTableWidgetItem(FROM_UTF8(pM->getObjectName()))); //mpTableObj->setText(i, 3, FROM_UTF8(pM->getObjectName())); std::string tmpString = CModelEntity::StatusName[pM->getStatus()]; if (pM->isUsed()) tmpString += " (Used = true, "; else tmpString += " (Used = false, "; mpTableObj->setItem((int) i, 4, new QTableWidgetItem(FROM_UTF8(tmpString))); } mpTableObj->resizeColumnToContents(3); mpTableObj->resizeColumnToContents(4); //state const CStateTemplate & st = pModel->getStateTemplate(); imax = st.size(); mpTableState->setRowCount(0); mpTableState->setRowCount((int)(imax + 1)); mpTableState->setColumnCount(5); mpTableState->setHorizontalHeaderItem(0, new QTableWidgetItem("Name")); mpTableState->setHorizontalHeaderItem(1, new QTableWidgetItem("status")); mpTableState->setHorizontalHeaderItem(2, new QTableWidgetItem("")); mpTableState->setHorizontalHeaderItem(3, new QTableWidgetItem("ATol(1)")); mpTableState->setHorizontalHeaderItem(4, new QTableWidgetItem("SS criterion(1)")); for (i = 0; i < imax; ++i) { mpTableState->setVerticalHeaderItem((int) i, new QTableWidgetItem(QString::number(i))); CModelEntity* pME = *(st.getEntities() + i); //first column QColor c(100, 100, 100); if (dynamic_cast<CModel*>(pME)) c = QColor(250, 100, 100); if (dynamic_cast<CMetab*>(pME)) c = QColor(250, 250, 150); if (dynamic_cast<CCompartment*>(pME)) c = QColor(100, 250, 100); if (dynamic_cast<CModelValue*>(pME)) c = QColor(100, 100, 250); mpTableState->setItem((int) i, 0, new QTableWidgetItem(FROM_UTF8(pME->getObjectDisplayName()))); // mpTableState->setText(i, 0,FROM_UTF8(pME->getObjectDisplayName())); //second column std::string tmpString = CModelEntity::StatusName[pME->getStatus()]; if (pME->isUsed()) tmpString += " (Used = true, "; else tmpString += " (Used = false, "; c = QColor(100, 100, 100); if (pME->getStatus() == CModelEntity::FIXED) c = QColor(150, 150, 150); if (pME->getStatus() == CModelEntity::ASSIGNMENT) c = QColor(250, 150, 250); if (pME->getStatus() == CModelEntity::REACTIONS) c = QColor(250, 250, 200); if (pME->getStatus() == CModelEntity::ODE) c = QColor(150, 250, 250); if (pME->getStatus() == CModelEntity::TIME) c = QColor(250, 150, 150); mpTableState->setItem((int) i, 1, new QTableWidgetItem(FROM_UTF8(tmpString))); //mpTableState->setText(i, 1, FROM_UTF8(tmpString)); } QColor c(200, 250, 250); for (i = st.beginIndependent() - st.getEntities(); i < (size_t)(st.endIndependent() - st.getEntities()); ++i) mpTableState->setItem((int) i, 2, new QTableWidgetItem("")); c = QColor(250, 200, 250); for (i = st.beginDependent() - st.getEntities(); i < (size_t)(st.endDependent() - st.getEntities()); ++i) mpTableState->setItem((int) i, 2, new QTableWidgetItem("")); c = QColor(200, 200, 200); for (i = st.beginFixed() - st.getEntities(); i < (size_t)(st.endFixed() - st.getEntities()); ++i) mpTableState->setItem((int) i, 2, new QTableWidgetItem("")); int tmpint = st.beginIndependent() - st.getEntities(); mpTableState->setItem(tmpint, 2, new QTableWidgetItem("beginIndependent ")); tmpint = st.endIndependent() - st.getEntities(); mpTableState->setItem(tmpint, 2, new QTableWidgetItem(mpTableState->item(tmpint, 2)->text() + "endIndependent ")); tmpint = st.beginDependent() - st.getEntities(); mpTableState->setItem(tmpint, 2, new QTableWidgetItem(mpTableState->item(tmpint, 2)->text() + "beginDependent ")); tmpint = st.endDependent() - st.getEntities(); mpTableState->setItem(tmpint, 2, new QTableWidgetItem(mpTableState->item(tmpint, 2)->text() + "endDependent ")); tmpint = st.beginFixed() - st.getEntities(); mpTableState->setItem(tmpint, 2, new QTableWidgetItem(mpTableState->item(tmpint, 2)->text() + "beginFixed ")); tmpint = st.endFixed() - st.getEntities(); mpTableState->setItem(tmpint, 2, new QTableWidgetItem("endFixed ")); //add absolute Tolerances to table CVector< C_FLOAT64 > atolv = pModel->initializeAtolVector(1, false); tmpint = st.beginIndependent() - st.getEntities(); for (i = 0; i < atolv.size(); ++i) { mpTableState->setItem((int) i + tmpint, 3, new QTableWidgetItem(QString::number(atolv[i]))); CModelEntity* pME = *(st.getEntities() + i + 1); C_FLOAT64 tmp = std::min(atolv[i], std::max(100.0 * std::numeric_limits< C_FLOAT64 >::min(), fabs(pME->getInitialValue()))); mpTableState->setItem((int) i + tmpint, 4, new QTableWidgetItem(QString::number(tmp))); } mpTableState->resizeColumnToContents(0); mpTableState->resizeColumnToContents(1); mpTableState->resizeColumnToContents(2); mpTableState->resizeColumnToContents(3); mpTableState->resizeColumnToContents(4); }
std::string CMetabNameInterface::createUniqueDisplayName(const CMetab & metab, const bool & quoted) { return createUniqueDisplayName(metab.getObjectName(), metab.getCompartment()->getObjectName(), quoted); }
std::string CMetabNameInterface::getDisplayName(const CModel* model, const CMetab & metab, const bool & quoted) { return getDisplayName(model, metab.getObjectName(), metab.getCompartment()->getObjectName(), quoted); }
void DataModelGUI::buildChangedObjects() { assert(CCopasiRootContainer::getDatamodelList()->size() > 0); CModel * pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel(); pModel->compileIfNecessary(NULL); mChangedObjects.clear(); CModelEntity ** ppEntity = pModel->getStateTemplate().getEntities(); CModelEntity ** ppEntityEnd = pModel->getStateTemplate().endFixed(); CMetab * pMetab; std::set< const CCopasiObject * > Objects; // The objects which are changed are all initial values of of all model entities including // fixed and unused once. Additionally, all kinetic parameters are possibly changed. // This is basically all the parameters in the parameter overview whose value is editable. // :TODO: Theoretically, it is possible that also task parameters influence the initial // state of a model but that is currently not handled. for (; ppEntity != ppEntityEnd; ++ppEntity) { // If we have an initial expression we have no initial values if ((*ppEntity)->getInitialExpression() != "" || (*ppEntity)->getStatus() == CModelEntity::ASSIGNMENT) continue; // Metabolites have two initial values if (mFramework == 0 && (pMetab = dynamic_cast< CMetab * >(*ppEntity)) != NULL) { // The concentration is assumed to be fix accept when this would lead to circular dependencies, // for the parent's compartment's initial volume. if (pMetab->isInitialConcentrationChangeAllowed() && !isnan(pMetab->getInitialConcentration())) mChangedObjects.insert(pMetab->getInitialConcentrationReference()); else mChangedObjects.insert(pMetab->getInitialValueReference()); } else mChangedObjects.insert((*ppEntity)->getInitialValueReference()); } // The reaction parameters CCopasiVector< CReaction >::const_iterator itReaction = pModel->getReactions().begin(); CCopasiVector< CReaction >::const_iterator endReaction = pModel->getReactions().end(); size_t i, imax; for (; itReaction != endReaction; ++itReaction) { const CCopasiParameterGroup & Group = (*itReaction)->getParameters(); for (i = 0, imax = Group.size(); i < imax; i++) mChangedObjects.insert(static_cast< const CCopasiObject * >(Group.getParameter(i)->getObject(CCopasiObjectName("Reference=Value")))); } // Fix for Issue 1170: We need to add elements of the stoichiometry, reduced stoichiometry, // and link matrices. const CArrayAnnotation * pMatrix = NULL; pMatrix = dynamic_cast<const CArrayAnnotation *>(pModel->getObject(std::string("Array=Stoichiometry(ann)"))); if (pMatrix != NULL) pMatrix->appendElementReferences(mChangedObjects); pMatrix = dynamic_cast<const CArrayAnnotation *>(pModel->getObject(std::string("Array=Reduced stoichiometry(ann)"))); if (pMatrix != NULL) pMatrix->appendElementReferences(mChangedObjects); pMatrix = dynamic_cast<const CArrayAnnotation *>(pModel->getObject(std::string("Array=Link matrix(ann)"))); if (pMatrix != NULL) pMatrix->appendElementReferences(mChangedObjects); try { mUpdateVector = pModel->buildInitialRefreshSequence(mChangedObjects); } catch (...) { QString Message = "Error while updating the initial values!\n\n"; Message += FROM_UTF8(CCopasiMessage::getLastMessage().getText()); CQMessageBox::critical(NULL, QString("COPASI Error"), Message, QMessageBox::Ok, QMessageBox::Ok); CCopasiMessage::clearDeque(); mUpdateVector.clear(); return; } }
void CModelExpansion::duplicateMetab(const CMetab* source, const std::string & index, const SetOfModelElements & sourceSet, ElementsMap & emap) { //if the source object has already been duplicated: do nothing if (emap.exists(source)) return; //is the containing compartment also duplicated? const CCompartment* sourceParent = source->getCompartment(); const CCompartment * parent = NULL; bool nameflag; if (!sourceSet.contains(sourceParent)) { parent = sourceParent; //use the original parent compartment nameflag = true; //metab needs renaming } else //use a copy of the parent compartment { nameflag = false; //no renaming necessary (since the copy is in a different compartment) //create copy if it does not exist if (!emap.exists(sourceParent)) { duplicateCompartment(sourceParent, index, sourceSet, emap); } parent = dynamic_cast<CCompartment *>(emap.getDuplicatePtr(sourceParent)); } //try creating the object until we find a name that is not yet used CMetab* newObj; std::ostringstream infix; do { std::ostringstream name; name << source->getObjectName() << infix.str(); if (nameflag) name << index; newObj = mpModel->createMetabolite(name.str(), parent->getObjectName(), source->getInitialConcentration(), source->getStatus()); infix << "_"; } while (!newObj); //add duplicated object to the map emap.add(source, newObj); //expression (for assignment or ODE) newObj->setExpression(source->getExpression()); updateExpression(newObj->getExpressionPtr(), index, sourceSet, emap); //initial expression newObj->setInitialExpression(source->getInitialExpression()); updateExpression(newObj->getInitialExpressionPtr(), index, sourceSet, emap); newObj->setNotes(source->getNotes()); newObj->setMiriamAnnotation(source->getMiriamAnnotation(), newObj->getKey(), source->getKey()); }
bool CODEExporterC::preprocess(const CModel* copasiModel) { size_t n[3] = {0, 0, 0}; size_t n_c[3] = {0, 0, 0}; size_t i, j; size_t dependent; setReservedNames(); NameMap[timeKey] = translateTimeVariableName(); const CCopasiVector< CMetab > & metabs = copasiModel->getMetabolitesX(); size_t metabs_size = metabs.size(); for (i = 0; i < metabs_size; i++) { CMetab * metab = metabs[i]; //if (metab->isUsed()) { std::string smname; std::string name; dependent = metab->isDependent(); smname = setExportName(metab->getStatus(), n, dependent); name = setConcentrationName(metab->getStatus(), n_c, dependent); NameMap[metab->getKey()] = name; std::ostringstream smKey; smKey << "sm_" << metab->getKey(); NameMap[smKey.str()] = smname; if ((metab->getStatus() == CModelEntity::REACTIONS && !metab->isDependent()) || metab->getStatus() == CModelEntity::ODE) { std::ostringstream odeKey; odeKey << "ode_" << metab->getKey(); NameMap[odeKey.str()] = setODEName(smname); } } } size_t comps_size = copasiModel->getCompartments().size(); const CCopasiVector< CCompartment > & comps = copasiModel->getCompartments(); for (i = 0; i < comps_size; i++) { CCompartment * comp = comps[i]; std::string name; dependent = 0; name = setExportName(comp->getStatus(), n, dependent); NameMap[comp->getKey()] = name; if (comp->getStatus() == CModelEntity::ODE) { std::ostringstream odeKey; odeKey << "ode_" << comp->getKey(); NameMap[odeKey.str()] = setODEName(name); } } size_t modvals_size = copasiModel->getModelValues().size(); const CCopasiVector< CModelValue > & modvals = copasiModel->getModelValues(); for (i = 0; i < modvals_size; i++) { CModelValue* modval = modvals[i]; std::string name = setExportName(modval->getStatus(), n, 0); NameMap[modval->getKey()] = name; if (modval->getStatus() == CModelEntity::ODE) { std::ostringstream odeKey; odeKey << "ode_" << modval->getKey(); NameMap[odeKey.str()] = setODEName(name); } } size_t reacs_size = copasiModel->getReactions().size(); const CCopasiVector< CReaction > & reacs = copasiModel->getReactions(); std::set<std::string> tmpset; for (i = 0; i < reacs_size; ++i) { size_t params_size; params_size = reacs[i]->getParameters().size(); for (j = 0; j < params_size; ++j) { if (!reacs[i]->isLocalParameter(reacs[i]->getParameters().getParameter(j)->getObjectName())) continue; std::ostringstream name; name << "p[" << n[0] << "]"; n[0] ++; NameMap[reacs[i]->getParameters().getParameter(j)->getKey()] = name.str(); } const CFunction* func = reacs[i]->getFunction(); std::string name = func->getObjectName(); if (func->getRoot()) setExportNameOfFunction(func->getRoot(), tmpset); if (func->getType() != CEvaluationTree::MassAction) if (tmpset.find(name) == tmpset.end()) { NameMap[func->getKey()] = translateObjectName(name); tmpset.insert(name); } } return true; }
int main() { // initialize the backend library // since we are not interested in the arguments // that are passed to main, we pass 0 and NULL to // init CCopasiRootContainer::init(0, NULL); assert(CCopasiRootContainer::getRoot() != NULL); // create a new datamodel CCopasiDataModel* pDataModel = CCopasiRootContainer::addDatamodel(); assert(CCopasiRootContainer::getDatamodelList()->size() == 1); // get the model from the datamodel CModel* pModel = pDataModel->getModel(); assert(pModel != NULL); // set the units for the model // we want seconds as the time unit // microliter as the volume units // and nanomole as the substance units pModel->setTimeUnit(CModel::s); pModel->setVolumeUnit(CModel::microl); pModel->setQuantityUnit(CModel::nMol); // we have to keep a set of all the initial values that are changed during // the model building process // They are needed after the model has been built to make sure all initial // values are set to the correct initial value std::set<const CCopasiObject*> changedObjects; // create a compartment with the name cell and an initial volume of 5.0 // microliter CCompartment* pCompartment = pModel->createCompartment("cell", 5.0); const CCopasiObject* pObject = pCompartment->getValueReference(); assert(pObject != NULL); changedObjects.insert(pObject); assert(pCompartment != NULL); assert(pModel->getCompartments().size() == 1); // create a new metabolite with the name S and an inital // concentration of 10 nanomol // the metabolite belongs to the compartment we created and is is to be // fixed CMetab* pS = pModel->createMetabolite("S", pCompartment->getObjectName(), 10.0, CMetab::FIXED); pObject = pS->getInitialConcentrationReference(); assert(pObject != NULL); changedObjects.insert(pObject); assert(pCompartment != NULL); assert(pS != NULL); assert(pModel->getMetabolites().size() == 1); // create a second metabolite called P with an initial // concentration of 0. This metabolite is to be changed by reactions CMetab* pP = pModel->createMetabolite("P", pCompartment->getObjectName(), 0.0, CMetab::REACTIONS); assert(pP != NULL); pObject = pP->getInitialConcentrationReference(); assert(pObject != NULL); changedObjects.insert(pObject); assert(pModel->getMetabolites().size() == 2); // now we create a reaction CReaction* pReaction = pModel->createReaction("reaction"); assert(pReaction != NULL); assert(pModel->getReactions().size() == 1); // reaction converts S to P // we can set these on the chemical equation of the reaction CChemEq* pChemEq = &pReaction->getChemEq(); // S is a substrate with stoichiometry 1 pChemEq->addMetabolite(pS->getKey(), 1.0, CChemEq::SUBSTRATE); // P is a product with stoichiometry 1 pChemEq->addMetabolite(pP->getKey(), 1.0, CChemEq::PRODUCT); assert(pChemEq->getSubstrates().size() == 1); assert(pChemEq->getProducts().size() == 1); // this reaction is to be irreversible pReaction->setReversible(false); assert(pReaction->isReversible() == false); CModelValue* pMV = pModel->createModelValue("K", 42.0); // set the status to FIXED pMV->setStatus(CModelValue::FIXED); assert(pMV != NULL); pObject = pMV->getInitialValueReference(); assert(pObject != NULL); changedObjects.insert(pObject); assert(pModel->getModelValues().size() == 1); // now we ned to set a kinetic law on the reaction // for this we create a user defined function CFunctionDB* pFunDB = CCopasiRootContainer::getFunctionList(); assert(pFunDB != NULL); CKinFunction* pFunction = new CKinFunction("My Rate Law"); pFunDB->add(pFunction, true); CFunction* pRateLaw = dynamic_cast<CFunction*>(pFunDB->findFunction("My Rate Law")); assert(pRateLaw != NULL); // now we create the formula for the function and set it on the function std::string formula = "(1-0.4/(EXPONENTIALE^(temp-37)))*0.00001448471257*1.4^(temp-37)*substrate"; bool result = pFunction->setInfix(formula); assert(result == true); // make the function irreversible pFunction->setReversible(TriFalse); // the formula string should have been parsed now // and COPASI should have determined that the formula string contained 2 parameters (temp and substrate) CFunctionParameters& variables = pFunction->getVariables(); // per default the usage of those parameters will be set to VARIABLE size_t index = pFunction->getVariableIndex("temp"); assert(index != C_INVALID_INDEX); CFunctionParameter* pParam = variables[index]; assert(pParam->getUsage() == CFunctionParameter::VARIABLE); // This is correct for temp, but substrate should get the usage SUBSTRATE in order // for us to use the function with the reaction created above // So we need to set the usage for "substrate" manually index = pFunction->getVariableIndex("substrate"); assert(index != C_INVALID_INDEX); pParam = variables[index]; pParam->setUsage(CFunctionParameter::SUBSTRATE); // set the rate law for the reaction pReaction->setFunction(pFunction); assert(pReaction->getFunction() != NULL); // COPASI also needs to know what object it has to assocuiate with the individual function parameters // In our case we need to tell COPASI that substrate is to be replaced by the substrate of the reaction // and temp is to be replaced by the global parameter K pReaction->setParameterMapping("substrate", pS->getKey()); pReaction->setParameterMapping("temp", pMV->getKey()); // finally compile the model // compile needs to be done before updating all initial values for // the model with the refresh sequence pModel->compileIfNecessary(NULL); // now that we are done building the model, we have to make sure all // initial values are updated according to their dependencies std::vector<Refresh*> refreshes = pModel->buildInitialRefreshSequence(changedObjects); std::vector<Refresh*>::iterator it2 = refreshes.begin(), endit2 = refreshes.end(); while (it2 != endit2) { // call each refresh (**it2)(); ++it2; } // save the model to a COPASI file // we save to a file named example1.cps, we don't want a progress report // and we want to overwrite any existing file with the same name // Default tasks are automatically generated and will always appear in cps // file unless they are explicitley deleted before saving. pDataModel->saveModel("example7.cps", NULL, true); // export the model to an SBML file // we save to a file named example1.xml, we want to overwrite any // existing file with the same name and we want SBML L2V3 pDataModel->exportSBML("example7.xml", true, 2, 3); // destroy the root container once we are done CCopasiRootContainer::destroy(); }
void test000027::test_hasOnlySubstanceUnits() { CCopasiDataModel* pDataModel = pCOPASIDATAMODEL; CPPUNIT_ASSERT(pDataModel->importSBMLFromString(MODEL_STRING)); CModel* pModel = pDataModel->getModel(); CPPUNIT_ASSERT(pModel != NULL); CPPUNIT_ASSERT(pModel->getQuantityUnitEnum() == CUnit::mMol); CPPUNIT_ASSERT(pModel->getVolumeUnitEnum() == CUnit::ml); CPPUNIT_ASSERT(pModel->getTimeUnitEnum() == CUnit::s); CPPUNIT_ASSERT(pModel->getCompartments().size() == 1); const CCompartment* pCompartment = pModel->getCompartments()[0]; CPPUNIT_ASSERT(pCompartment != NULL); CPPUNIT_ASSERT(pCompartment->getStatus() == CModelEntity::FIXED); CPPUNIT_ASSERT(pModel->getMetabolites().size() == 2); CMetab* pA = pModel->getMetabolites()[0]; CPPUNIT_ASSERT(pA != NULL); CPPUNIT_ASSERT(pA->getStatus() == CModelEntity::REACTIONS); const CMetab* pB = pModel->getMetabolites()[1]; CPPUNIT_ASSERT(pB != NULL); CPPUNIT_ASSERT(pB->getStatus() == CModelEntity::REACTIONS); CPPUNIT_ASSERT(pModel->getModelValues().size() == 2); // check the kinetic law const CModelValue* pFactor = pModel->getModelValues()[1]; CPPUNIT_ASSERT(pFactor != NULL); CPPUNIT_ASSERT(pFactor->getStatus() == CModelEntity::FIXED); CPPUNIT_ASSERT(fabs((pFactor->getInitialValue() - pModel->getQuantity2NumberFactor()) / pModel->getQuantity2NumberFactor()) < 1e-3); const CModelValue* pModelValue = pModel->getModelValues()[0]; CPPUNIT_ASSERT(pModelValue != NULL); CPPUNIT_ASSERT(pModelValue->getStatus() == CModelEntity::ASSIGNMENT); const CExpression* pExpr = pModelValue->getExpressionPtr(); // check the expression const CEvaluationNode* pNode = pExpr->getRoot(); CPPUNIT_ASSERT(pNode != NULL); const CEvaluationNodeObject* pObjectNode = dynamic_cast<const CEvaluationNodeObject*>(pNode); CPPUNIT_ASSERT(pObjectNode != NULL); CCopasiObjectName objectCN = pObjectNode->getObjectCN(); CPPUNIT_ASSERT(!objectCN.empty()); CObjectInterface::ContainerList listOfContainers; listOfContainers.push_back(pModel); const CCopasiObject* pObject = CObjectInterface::DataModel(pCOPASIDATAMODEL->getObjectFromCN(listOfContainers, objectCN)); CPPUNIT_ASSERT(pObject != NULL); CPPUNIT_ASSERT(pObject->isReference() == true); CPPUNIT_ASSERT(pObject->getObjectName() == std::string("ParticleNumber")); CPPUNIT_ASSERT(pObject->getObjectParent() == pA); CPPUNIT_ASSERT(pModel->getReactions().size() == 2); const CReaction* pReaction1 = pModel->getReactions()[0]; CPPUNIT_ASSERT(pReaction1 != NULL); CPPUNIT_ASSERT(pReaction1->isReversible() == false); const CFunction* pKineticFunction = pReaction1->getFunction(); CPPUNIT_ASSERT(pKineticFunction != NULL); const CMassAction* pMassAction = dynamic_cast<const CMassAction*>(pKineticFunction); //FTB: this no longer is recognized as mass action reaction because of the // special case of a species with hOSU CPPUNIT_ASSERT(pMassAction == NULL); const CChemEq* pChemEq = &pReaction1->getChemEq(); CPPUNIT_ASSERT(pChemEq != NULL); CPPUNIT_ASSERT(pChemEq->getCompartmentNumber() == 1); CPPUNIT_ASSERT(pChemEq->getSubstrates().size() == 1); const CChemEqElement* pElement = pChemEq->getSubstrates()[0]; CPPUNIT_ASSERT(pElement != NULL); CPPUNIT_ASSERT(fabs(pElement->getMultiplicity() - 1.0) < 1e-3); CPPUNIT_ASSERT(pElement->getMetabolite() == pA); CPPUNIT_ASSERT(pChemEq->getProducts().size() == 0); CPPUNIT_ASSERT(pChemEq->getModifiers().size() == 0); const CReaction* pReaction2 = pModel->getReactions()[1]; CPPUNIT_ASSERT(pReaction2 != NULL); CPPUNIT_ASSERT(pReaction2->isReversible() == false); // check the kinetic law pKineticFunction = pReaction2->getFunction(); CPPUNIT_ASSERT(pKineticFunction != NULL); CPPUNIT_ASSERT(pKineticFunction->getObjectName() == std::string("Henri-Michaelis-Menten (irreversible)")); // check the function parameters one should be the reference to the substrate pChemEq = &pReaction2->getChemEq(); CPPUNIT_ASSERT(pChemEq != NULL); CPPUNIT_ASSERT(pChemEq->getCompartmentNumber() == 1); CPPUNIT_ASSERT(pChemEq->getSubstrates().size() == 1); pElement = pChemEq->getSubstrates()[0]; CPPUNIT_ASSERT(pElement != NULL); CPPUNIT_ASSERT(fabs(pElement->getMultiplicity() - 1.0) < 1e-3); CPPUNIT_ASSERT(pElement->getMetabolite() == pA); CPPUNIT_ASSERT(pChemEq->getProducts().size() == 1); pElement = pChemEq->getProducts()[0]; CPPUNIT_ASSERT(pElement != NULL); CPPUNIT_ASSERT(fabs(pElement->getMultiplicity() - 1.0) < 1e-3); CPPUNIT_ASSERT(pElement->getMetabolite() == pB); CPPUNIT_ASSERT(pChemEq->getModifiers().size() == 0); const std::vector<std::vector<std::string> > parameterMappings = pReaction2->getParameterMappings(); CPPUNIT_ASSERT(parameterMappings.size() == 3); CPPUNIT_ASSERT(parameterMappings[0].size() == 1); const std::string parameterKey = parameterMappings[0][0]; CPPUNIT_ASSERT(parameterKey == pA->getKey()); }
int main(int argc, char** argv) { // initialize the backend library CCopasiRootContainer::init(argc, argv); assert(CCopasiRootContainer::getRoot() != NULL); // create a new datamodel CCopasiDataModel* pDataModel = CCopasiRootContainer::addDatamodel(); assert(CCopasiRootContainer::getDatamodelList()->size() == 1); // the only argument to the main routine should be the name of an SBML file if (argc == 2) { std::string filename = argv[1]; try { // load the model without progress report pDataModel->importSBML(filename, NULL); } catch (...) { std::cerr << "Error while importing the model from file named \"" << filename << "\"." << std::endl; CCopasiRootContainer::destroy(); return 1; } CModel* pModel = pDataModel->getModel(); assert(pModel != NULL); // create a report with the correct filename and all the species against // time. CReportDefinitionVector* pReports = pDataModel->getReportDefinitionList(); // create a new report definition object CReportDefinition* pReport = pReports->createReportDefinition("Report", "Output for timecourse"); // set the task type for the report definition to timecourse pReport->setTaskType(CTaskEnum::timeCourse); // we don't want a table pReport->setIsTable(false); // the entries in the output should be seperated by a ", " pReport->setSeparator(", "); // we need a handle to the header and the body // the header will display the ids of the metabolites and "time" for // the first column // the body will contain the actual timecourse data std::vector<CRegisteredObjectName>* pHeader = pReport->getHeaderAddr(); std::vector<CRegisteredObjectName>* pBody = pReport->getBodyAddr(); pBody->push_back(CCopasiObjectName(pDataModel->getModel()->getCN() + ",Reference=Time")); pBody->push_back(CRegisteredObjectName(pReport->getSeparator().getCN())); pHeader->push_back(CCopasiStaticString("time").getCN()); pHeader->push_back(pReport->getSeparator().getCN()); size_t i, iMax = pModel->getMetabolites().size(); for (i = 0; i < iMax; ++i) { CMetab* pMetab = &pModel->getMetabolites()[i]; assert(pMetab != NULL); // we don't want output for FIXED metabolites right now if (pMetab->getStatus() != CModelEntity::FIXED) { // we want the concentration oin the output // alternatively, we could use "Reference=Amount" to get the // particle number pBody->push_back(pMetab->getObject(CCopasiObjectName("Reference=Concentration"))->getCN()); // after each entry, we need a seperator pBody->push_back(pReport->getSeparator().getCN()); // add the corresponding id to the header pHeader->push_back(CCopasiStaticString(pMetab->getSBMLId()).getCN()); // and a seperator pHeader->push_back(pReport->getSeparator().getCN()); } } if (iMax > 0) { // delete the last separator // since we don't need one after the last element on each line if ((*pBody->rbegin()) == pReport->getSeparator().getCN()) { pBody->erase(--pBody->end()); } if ((*pHeader->rbegin()) == pReport->getSeparator().getCN()) { pHeader->erase(--pHeader->end()); } } // get the task list CCopasiVectorN< CCopasiTask > & TaskList = * pDataModel->getTaskList(); // get the trajectory task object CTrajectoryTask* pTrajectoryTask = dynamic_cast<CTrajectoryTask*>(&TaskList["Time-Course"]); // if there isn't one if (pTrajectoryTask == NULL) { // remove any existing trajectory task just to be sure since in // theory only the cast might have failed above TaskList.remove("Time-Course"); // create a new one pTrajectoryTask = new CTrajectoryTask(& TaskList); // add the new time course task to the task list TaskList.add(pTrajectoryTask, true); } // run a deterministic time course pTrajectoryTask->setMethodType(CTaskEnum::deterministic); // Activate the task so that it will be run when the model is saved // and passed to CopasiSE pTrajectoryTask->setScheduled(true); // set the report for the task pTrajectoryTask->getReport().setReportDefinition(pReport); // set the output filename pTrajectoryTask->getReport().setTarget("example3.txt"); // don't append output if the file exists, but overwrite the file pTrajectoryTask->getReport().setAppend(false); // get the problem for the task to set some parameters CTrajectoryProblem* pProblem = dynamic_cast<CTrajectoryProblem*>(pTrajectoryTask->getProblem()); // simulate 100 steps pProblem->setStepNumber(100); // start at time 0 pDataModel->getModel()->setInitialTime(0.0); // simulate a duration of 10 time units pProblem->setDuration(10); // tell the problem to actually generate time series data pProblem->setTimeSeriesRequested(true); // set some parameters for the LSODA method through the method CTrajectoryMethod* pMethod = dynamic_cast<CTrajectoryMethod*>(pTrajectoryTask->getMethod()); CCopasiParameter* pParameter = pMethod->getParameter("Absolute Tolerance"); assert(pParameter != NULL); pParameter->setValue(1.0e-12); try { // initialize the trajectory task // we want complete output (HEADER, BODY and FOOTER) // // The output has to be set to OUTPUT_UI, otherwise the time series will not be // kept in memory and some of the assert further down will fail // If it is OK that the output is only written to file, the output type can // be set to OUTPUT_SE pTrajectoryTask->initialize(CCopasiTask::OUTPUT_UI, pDataModel, NULL); // now we run the actual trajectory pTrajectoryTask->process(true); } catch (...) { std::cerr << "Error. Running the time course simulation failed." << std::endl; // check if there are additional error messages if (CCopasiMessage::size() > 0) { // print the messages in chronological order std::cerr << CCopasiMessage::getAllMessageText(true); } CCopasiRootContainer::destroy(); return 1; } // restore the state of the trajectory pTrajectoryTask->restore(); // look at the timeseries const CTimeSeries* pTimeSeries = &pTrajectoryTask->getTimeSeries(); // we simulated 100 steps, including the initial state, this should be // 101 step in the timeseries assert(pTimeSeries->getRecordedSteps() == 101); std::cout << "The time series consists of " << pTimeSeries->getRecordedSteps() << "." << std::endl; std::cout << "Each step contains " << pTimeSeries->getNumVariables() << " variables." << std::endl; std::cout << "The final state is: " << std::endl; iMax = pTimeSeries->getNumVariables(); size_t lastIndex = pTimeSeries->getRecordedSteps() - 1; for (i = 0; i < iMax; ++i) { // here we get the particle number (at least for the species) // the unit of the other variables may not be particle numbers // the concentration data can be acquired with getConcentrationData std::cout << pTimeSeries->getTitle(i) << ": " << pTimeSeries->getData(lastIndex, i) << std::endl; } } else { std::cerr << "Usage: example3 SBMLFILE" << std::endl; CCopasiRootContainer::destroy(); return 1; } // clean up the library CCopasiRootContainer::destroy(); }