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; }
CMetab * CMetabNameInterface::getMetabolite(const CModel* model, const std::string & metabolite, const std::string & compartment) { unsigned C_INT32 Index; if (compartment != "") { Index = model->getCompartments().getIndex(compartment); if (Index != C_INVALID_INDEX) { CCompartment *pCompartment = model->getCompartments()[Index]; Index = pCompartment->getMetabolites().getIndex(metabolite); if (Index != C_INVALID_INDEX) return pCompartment->getMetabolites()[Index]; } return NULL; } Index = model->findMetabByName(metabolite); if (Index != C_INVALID_INDEX) return model->getMetabolites()[Index]; return NULL; }
void InsertCompartmentRowsCommand::redo() { if (firstTime) { mpCompartmentDM->insertNewCompartmentRow(mPosition, mRows, QModelIndex()); assert(CCopasiRootContainer::getDatamodelList()->size() > 0); CCopasiDataModel* pDataModel = (*CCopasiRootContainer::getDatamodelList())[0]; assert(pDataModel != NULL); CModel * pModel = pDataModel->getModel(); assert(pModel != NULL); CCompartment *pCompartment = pModel->getCompartments()[mPosition]; mpCompartmentData->setName(pCompartment->getObjectName()); mpCompartmentData->setStatus(pCompartment->getStatus()); mpCompartmentData->setInitialValue(pCompartment->getInitialValue()); firstTime = false; } else { mpCompartmentDM->addCompartmentRow(mpCompartmentData); } setUndoState(true); setAction("Add to list"); setName(mpCompartmentData->getName()); }
bool CModelAdd::addCompartments(std::string name) { size_t i, imax = mmModel->getCompartments().size(); for (i = 0; i < imax; ++i) { const CCompartment* sourceComp = &mmModel->getCompartments()[i]; if (!sourceComp) return false; //create new compartment std::string newName = sourceComp->getObjectName() + "_" + name; CCompartment* newComp = mpModel->createCompartment(newName, sourceComp->getInitialValue()); if (!newComp) return false; newComp->setStatus(sourceComp->getStatus()); newComp->setDimensionality(sourceComp->getDimensionality()); keyMap[sourceComp->getKey()] = newComp->getKey(); nameMap[sourceComp->getObjectName()] = newName; } return true; }
void CModelExpansion::replaceInMetab(CMetab* pX, const ElementsMap & emap) { replaceInModelEntity(pX, emap); //is the metab in a compartment that needs to be replaced? if (emap.exists(pX->getCompartment())) { //move the metab to the new compartment CCompartment* oldComp = const_cast<CCompartment*>(pX->getCompartment()); CCompartment* newComp = dynamic_cast<CCompartment*>(emap.getDuplicatePtr(pX->getCompartment())); bool success = false; do { success = newComp->addMetabolite(pX); if (success) { oldComp->getMetabolites().remove(pX->getObjectName()); mpModel->setCompileFlag(); mpModel->initializeMetabolites(); } else { //rename the metab so that it can be added to the new compartment pX->setObjectName(pX->getObjectName() + "_"); //TODO: check if the renaming actually worked } } while (!success); } }
void CQSpecieDM::deleteSpecieRow(UndoSpeciesData *pSpecieData) { GET_MODEL_OR_RETURN(pModel); switchToWidget(CCopasiUndoCommand::SPECIES); CMetab * pSpecies = dynamic_cast< CMetab * >(pSpecieData->getObject(pModel)); if (pSpecies == NULL) return; size_t Index = pModel->getMetabolites().getIndex(pSpecies); removeRow((int) Index); if (!pSpecieData->getCreatedCompartment()) return; Index = pModel->getCompartments().getIndex(pSpecieData->getCompartment()); if (Index == C_INVALID_INDEX) return; CCompartment* pComp = &pModel->getCompartments()[Index]; if (pComp == NULL) return; std::string key = pComp->getKey(); pModel->removeCompartment(Index); emit notifyGUI(ListViews::COMPARTMENT, ListViews::DELETE, key); }
void CQCompartmentDM::insertNewRows(int position, int rows, int column, const QVariant & value) { beginInsertRows(QModelIndex(), position, position + rows - 1); for (int row = 0; row < rows; ++row) { QString name = createNewName(column == COL_NAME_COMPARTMENTS ? value.toString() : "compartment", COL_NAME_COMPARTMENTS); CCompartment * pComp = mpDataModel->getModel()->createCompartment(TO_UTF8(name)); if (pComp == NULL) continue; if (column == COL_TYPE_COMPARTMENTS) { pComp->setStatus(CModelEntity::StatusName.toEnum(TO_UTF8(value.toString()))); } if (column == COL_IVOLUME) { pComp->setInitialValue(value.toDouble()); } CUndoData UndoData(CUndoData::Type::INSERT, pComp); ListViews::addUndoMetaData(this, UndoData); emit signalNotifyChanges(mpDataModel->recordData(UndoData)); } endInsertRows(); }
QList <UndoSpeciesData *> CQSpecieDM::insertNewSpecieRow(int position, int rows, const QModelIndex&index, const QVariant& value) { QList <UndoSpeciesData *> result; GET_MODEL_OR(pModel, return result); bool createdCompartment = false; if (pModel->getCompartments().size() == 0) { CCompartment* pComp = pModel->createCompartment("compartment"); createdCompartment = true; if (mNotify) { emit notifyGUI(ListViews::COMPARTMENT, ListViews::ADD, pComp->getKey()); } } beginInsertRows(QModelIndex(), position, position + rows - 1); int column = index.column(); for (int row = 0; row < rows; ++row) { QString name = createNewName(index.isValid() && column == COL_NAME_SPECIES ? value.toString() : "species", COL_NAME_SPECIES); QString compartment = index.isValid() && column == COL_COMPARTMENT ? value.toString() : ""; double initial = index.isValid() && column == COL_ICONCENTRATION ? value.toDouble() : 1.0; CModelEntity::Status status = index.isValid() && column == COL_TYPE_SPECIES ? (CModelEntity::Status) mItemToType[value.toInt()] : CModelEntity::REACTIONS; mpSpecies = pModel->createMetabolite(TO_UTF8(name), TO_UTF8(compartment), initial, status); if (mpSpecies == NULL) continue; if (mNotify) { emit notifyGUI(ListViews::METABOLITE, ListViews::ADD, mpSpecies->getKey()); } UndoSpeciesData* data = new UndoSpeciesData(mpSpecies); data->setCreatedCompartment(row == 0 && createdCompartment); result.append(data); } endInsertRows(); return result; }
void CQCompartment::deleteCompartment(UndoCompartmentData *pCompartmentData) { switchToWidget(CCopasiUndoCommand::COMPARTMENTS); CModel * pModel = mpCompartment->getModel(); assert(pModel != NULL); CCompartment* pComp = &pModel->getCompartments()[pCompartmentData->getName()]; if (pComp == NULL) return; std::string key = pComp->getKey(); pModel->removeCompartment(key); mpCompartment = NULL; #undef DELETE protectedNotify(ListViews::COMPARTMENT, ListViews::DELETE, key); protectedNotify(ListViews::COMPARTMENT, ListViews::DELETE, "");//Refresh all as there may be dependencies. }
void UndoDependentData::createDependentObjects(CModel *pModel, QList<UndoCompartmentData *> *pCompartmentData) { if (pModel == NULL || pCompartmentData == NULL || pCompartmentData->empty()) return; //reaction may further has dependencies, these must be taken care of QList <UndoCompartmentData *>::const_iterator rs; for (rs = pCompartmentData->begin(); rs != pCompartmentData->end(); ++rs) { UndoCompartmentData * data = *rs; CCompartment* pCompartment = data->createObjectIn(pModel); if (pCompartment == NULL) continue; updateGUI(ListViews::COMPARTMENT, ListViews::ADD, pCompartment->getKey()); } }
void CQCompartment::deleteCompartment(UndoCompartmentData *pCompartmentData) { assert(CCopasiRootContainer::getDatamodelList()->size() > 0); CCopasiDataModel* pDataModel = (*CCopasiRootContainer::getDatamodelList())[0]; assert(pDataModel != NULL); CModel * pModel = pDataModel->getModel(); assert(pModel != NULL); CCompartment * pCompartment = pModel->getCompartments()[pCompartmentData->getName()]; std::string key = pCompartment->getKey(); pModel->removeCompartment(key); mpCompartment = NULL; #undef DELETE protectedNotify(ListViews::COMPARTMENT, ListViews::DELETE, key); protectedNotify(ListViews::COMPARTMENT, ListViews::DELETE, "");//Refresh all as there may be dependencies. mpListView->switchToOtherWidget(111, ""); }
void CModelExpansion::duplicateCompartment(const CCompartment* 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; //try creating the object until we find a name that is not yet used CCompartment* newObj; std::ostringstream infix; do { std::ostringstream name; name << source->getObjectName() << infix.str() << index; newObj = mpModel->createCompartment(name.str(), source->getInitialValue()); infix << "_"; } while (!newObj); //add duplicated object to the map emap.add(source, newObj); //now copy the contents of the object newObj->setDimensionality(source->getDimensionality()); //status newObj->setStatus(source->getStatus()); //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 CQCompartmentDM::removeRows(QModelIndexList rows, const QModelIndex& index) { if (rows.isEmpty()) return false; // Build the list of pointers to items to be deleted // before actually deleting any item. QList <CCompartment *> Compartments; QModelIndexList::const_iterator i; for (i = rows.begin(); i != rows.end(); ++i) if (!isDefaultRow(*i) && &mpCompartments->operator[](i->row()) != NULL) { Compartments.append(&mpCompartments->operator[](i->row())); } QList< CCompartment * >::const_iterator j; for (j = Compartments.begin(); j != Compartments.end(); ++j) { CCompartment * pCompartment = *j; QMessageBox::StandardButton choice = CQMessageBox::confirmDelete(NULL, "compartment", FROM_UTF8(pCompartment->getObjectName()), pCompartment); if (choice == QMessageBox::Ok) { removeRows(mpCompartments->getIndex(pCompartment->getObjectName()), 1); } } return true; }
void CQCompartment::addCompartment(UndoCompartmentData *pSData) { assert(CCopasiRootContainer::getDatamodelList()->size() > 0); CCopasiDataModel* pDataModel = (*CCopasiRootContainer::getDatamodelList())[0]; assert(pDataModel != NULL); CModel * pModel = pDataModel->getModel(); assert(pModel != NULL); //reinsert all the Compartments CCompartment *pCompartment = pModel->createCompartment(pSData->getName()); pCompartment->setInitialValue(pSData->getInitialValue()); pCompartment->setStatus(pSData->getStatus()); std::string key = pCompartment->getKey(); protectedNotify(ListViews::COMPARTMENT, ListViews::ADD, key); //restore all the dependencies //reinsert all the species QList <UndoSpecieData *> *pSpecieData = pSData->getSpecieDependencyObjects(); QList <UndoSpecieData *>::const_iterator i; for (i = pSpecieData->begin(); i != pSpecieData->end(); ++i) { UndoSpecieData * data = *i; // beginInsertRows(QModelIndex(), 1, 1); CMetab *pSpecie = pModel->createMetabolite(data->getName(), data->getCompartment(), data->getIConc(), data->getStatus()); protectedNotify(ListViews::METABOLITE, ListViews::ADD, pSpecie->getKey()); //endInsertRows(); } //reinsert the dependency reaction QList <UndoReactionData *> *pReactionData = pSData->getReactionDependencyObjects(); QList <UndoReactionData *>::const_iterator j; for (j = pReactionData->begin(); j != pReactionData->end(); ++j) { //UndoReactionData * rData = dynamic_cast<UndoReactionData*>(*j); UndoReactionData * rData = *j; //TODO check if reaction already exist in the model, better idea may be implemented in the future bool exist = false; for (int ii = 0; ii < (int)pModel->getReactions().size(); ++ii) { if (pModel->getReactions()[ii]->getObjectName() == rData->getName()) { exist = true; ii = ii + pModel->getReactions().size() + 1; //jump out of the loop reaction exist already } else { exist = false; } } if (!exist) { protectedNotify(ListViews::METABOLITE, ListViews::ADD, ""); //Refresh all dependency species. CReaction *pRea = pModel->createReaction(rData->getName()); rData->getRi()->writeBackToReaction(pRea); protectedNotify(ListViews::REACTION, ListViews::ADD, pRea->getKey()); } } mpListView->switchToOtherWidget(C_INVALID_INDEX, key); }
std::string CODEExporterC::KineticFunction2ODEmember(const CReaction *reac) { std::ostringstream equation; if (reac->getFunction()->getType() != CEvaluationTree::MassAction) { const CFunctionParameters & params = reac->getFunctionParameters(); size_t k, params_size = params.size(); const std::vector<std::vector<std::string> > & keyMap = reac->getParameterMappings(); std::string name; equation << NameMap[reac->getFunction()->getKey()] << "("; for (k = 0; k < params_size; ++k) { CFunctionParameter::Role role = params[k]->getUsage(); CCopasiObject * obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[k][0]); if ((role == CFunctionParameter::SUBSTRATE) || (role == CFunctionParameter::PRODUCT) || (role == CFunctionParameter::MODIFIER)) { if (obj) name = NameMap[obj->getKey()]; else name = "unknown"; } if (role == CFunctionParameter::PARAMETER) { if (!(reac->isLocalParameter(k))) { CModelValue* modval; modval = dynamic_cast< CModelValue * >(obj); name = NameMap[modval->getKey()]; } else { CCopasiParameter* param; param = dynamic_cast< CCopasiParameter * >(obj); name = NameMap[param->getKey()]; } } if (role == CFunctionParameter::VOLUME) { CCompartment* comp; comp = dynamic_cast< CCompartment * >(obj); name = NameMap[comp->getKey()]; } if (role == CFunctionParameter::TIME) { name = "T"; } if (name.empty()) { std::string message = "Could not export C code, since one of the arguments could not be resolved. Please consider filing a bug with the COPASI tracker: http://www.copasi.org/tracker"; CCopasiMessage(CCopasiMessage::EXCEPTION, message.c_str()); } equation << name; if (k != params_size - 1) equation << ", "; } equation << ")"; } else { const CCopasiVector<CChemEqElement> & substrs = reac->getChemEq().getSubstrates(); const CCopasiVector<CChemEqElement> & prods = reac->getChemEq().getProducts(); const std::vector<std::vector<std::string> > & keyMap = reac->getParameterMappings(); CCopasiObject * obj; size_t substrs_size = substrs.size(), prods_size = prods.size(); size_t k, m, mult; const CChemEqElement* substr; const CChemEqElement* prod; const CMassAction & cMassAction = *static_cast<const CMassAction*>(reac->getFunction()); equation << "("; obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[0][0]); if (!(reac->isLocalParameter(0))) { CModelValue* modval; modval = dynamic_cast< CModelValue * >(obj); equation << NameMap[modval->getKey()]; } else { CCopasiParameter* param; param = dynamic_cast< CCopasiParameter * >(obj); equation << NameMap[param->getKey()]; } for (k = 0; k < substrs_size; ++k) { substr = &substrs[k]; mult = (size_t) substr->getMultiplicity(); assert(substr->getMetabolite()); equation << " * " << NameMap[substr->getMetabolite()->getKey()]; if (mult > 1) for (m = 1; m < mult; ++m) equation << " * " << NameMap[substr->getMetabolite()->getKey()]; } if (cMassAction.isReversible() == TriTrue) { equation << " - "; obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[2][0]); if (!(reac->isLocalParameter(2))) { CModelValue* modval; modval = dynamic_cast< CModelValue * >(obj); equation << NameMap[modval->getKey()]; } else { CCopasiParameter* param; param = dynamic_cast< CCopasiParameter * >(obj); equation << NameMap[param->getKey()]; } for (k = 0; k < prods_size; ++k) { prod = &prods[k]; mult = (size_t) prod->getMultiplicity(); assert(prod->getMetabolite()); equation << " * " << NameMap[prod->getMetabolite()->getKey()]; if (mult > 1) for (m = 1; m < mult; ++m) equation << " * " << NameMap[prod->getMetabolite()->getKey()]; } } equation << ") "; } return equation.str(); }
CompartmentDataChangeCommand::CompartmentDataChangeCommand( const QModelIndex& index, const QVariant& value, int role, CQCompartmentDM *pCompartmentDM) : CCopasiUndoCommand("Compartment", COMPARTMENT_DATA_CHANGE, "Change") , mNew(value) , mOld(index.data(Qt::DisplayRole)) , mIndex(index) , mpCompartmentDM(pCompartmentDM) , mRole(role) , mPathIndex() , mpCompartmentUndoData(NULL) { //set the data for UNDO history assert(pCompartmentDM->getDataModel() != NULL); CModel * pModel = pCompartmentDM->getDataModel()->getModel(); assert(pModel != NULL); if (pModel->getCompartments().size() <= (size_t)index.row()) { return; } CCompartment *pCompartment = &pModel->getCompartments()[index.row()]; setKey(pCompartment->getKey()); setName(pCompartment->getObjectName()); setOldValue(TO_UTF8(mOld.toString())); setNewValue(TO_UTF8(mNew.toString())); switch (index.column()) { case 0: setProperty(""); break; case 1: setProperty("Name"); break; case 2: setProperty("Type"); switch (mNew.toInt()) { case 0: setNewValue("fixed"); break; case 1: setNewValue("assignment"); break; case 2: setNewValue("ode"); break; } break; case 3: setProperty("Initial Volume"); // need to store additional undo data to be able to restore // species initial concentrations / patricle numbers mpCompartmentUndoData = new UndoCompartmentData(pCompartment); break; } setText(QString(": Changed compartment %1").arg(getProperty().c_str())); }
bool CModelMerging::mergeMetabolites(std::string toKey, std::string key) { bool info = false; //merge in the relevant reactions size_t i, imax = mpModel->getReactions().size(); size_t j, jmax; for (i = 0; i < imax; ++i) { CReaction * reac = &mpModel->getReactions()[i]; jmax = reac->getChemEq().getSubstrates().size(); for (j = 0; j < jmax; ++j) { CChemEqElement * subst = const_cast< CChemEqElement * >(&reac->getChemEq().getSubstrates()[j]); if (subst->getMetabolite()->getKey() == key) subst->setMetabolite(toKey); } jmax = reac->getChemEq().getProducts().size(); for (j = 0; j < jmax; ++j) { CChemEqElement * prod = const_cast< CChemEqElement * >(&reac->getChemEq().getProducts()[j]); if (prod->getMetabolite()->getKey() == key) prod->setMetabolite(toKey); } jmax = reac->getChemEq().getModifiers().size(); for (j = 0; j < jmax; ++j) { CChemEqElement * modif = const_cast< CChemEqElement * >(&reac->getChemEq().getModifiers()[j]); if (modif->getMetabolite()->getKey() == key) modif->setMetabolite(toKey); } //change parameters of the kinetic function for (j = 0; j < reac->getFunctionParameters().size(); ++j) { switch (reac->getFunctionParameters()[j]->getUsage()) { case CFunctionParameter::SUBSTRATE: case CFunctionParameter::PRODUCT: case CFunctionParameter::MODIFIER: //translate the metab keys { //we assume that only SUBSTRATE, PRODUCT, MODIFIER can be vectors size_t k, kmax = reac->getParameterMappings()[j].size(); for (k = 0; k < kmax; ++k) if (reac->getParameterMappings()[j][k] == key) reac->getParameterMappings()[j][k] = toKey; } break; case CFunctionParameter::TIME: break; case CFunctionParameter::VOLUME: // ??? TODO : have to ask break; case CFunctionParameter::PARAMETER: break; default: return info; break; } } } imax = mpModel->getEvents().size(); for (i = 0; i < imax; ++i) { CEvent * event = &mpModel->getEvents()[i]; if (!event) return info; /* merge in trigger expressions */ CExpression* pExpression = event->getTriggerExpressionPtr(); if (pExpression == NULL) return info; if (!mergeInExpression(toKey, key, pExpression)) return info; pExpression = event->getDelayExpressionPtr(); if (pExpression) if (!mergeInExpression(toKey, key, pExpression)) return info; jmax = event->getAssignments().size(); for (j = 0; j < jmax; ++j) { CEventAssignment* assignment = &event->getAssignments()[j]; if (!assignment) return info; std::string assignmentKey = assignment->getTargetKey(); if (assignmentKey == key) assignment->setTargetKey(toKey); pExpression = assignment->getExpressionPtr(); if (pExpression == NULL) return info; if (!mergeInExpression(toKey, key, pExpression)) return info; } } imax = mpModel->getMetabolites().size(); for (i = 0; i < imax; ++i) { CMetab* metab = &mpModel->getMetabolites()[i]; if (!metab) return info; switch (metab->getStatus()) { case CModelEntity::FIXED: case CModelEntity::REACTIONS: break; case CModelEntity::ASSIGNMENT: if (!mergeInExpression(toKey, key, metab->getExpressionPtr())) return info; break; case CModelEntity::ODE: if (!mergeInExpression(toKey, key, metab->getExpressionPtr())) return info; if (metab->getInitialExpression() != "") if (!mergeInExpression(toKey, key, metab->getInitialExpressionPtr())) return info; break; default: return info; break; } } imax = mpModel->getCompartments().size(); for (i = 0; i < imax; ++i) { CCompartment* comp = &mpModel->getCompartments()[i]; if (!comp) return info; switch (comp ->getStatus()) { case CModelEntity::FIXED: break; case CModelEntity::ASSIGNMENT: if (!mergeInExpression(toKey, key, comp->getExpressionPtr())) return info; break; case CModelEntity::ODE: if (!mergeInExpression(toKey, key, comp->getExpressionPtr())) return info; if (comp->getInitialExpression() != "") if (!mergeInExpression(toKey, key, comp->getInitialExpressionPtr())) return info; break; default: return info; break; } } imax = mpModel->getModelValues().size(); for (i = 0; i < imax; ++i) { CModelValue* modval = &mpModel->getModelValues()[i]; if (!modval) return info; switch (modval ->getStatus()) { case CModelEntity::FIXED: break; case CModelEntity::ASSIGNMENT: if (!mergeInExpression(toKey, key, modval->getExpressionPtr())) return info; break; case CModelEntity::ODE: if (!mergeInExpression(toKey, key, modval->getExpressionPtr())) return info; if (modval->getInitialExpression() != "") if (!mergeInExpression(toKey, key, modval->getInitialExpressionPtr())) return info; break; default: return info; break; } } return true; }
void CQEventWidget1::addEvent(UndoEventData *pSData) { assert(CCopasiRootContainer::getDatamodelList()->size() > 0); CCopasiDataModel* pDataModel = (*CCopasiRootContainer::getDatamodelList())[0]; assert(pDataModel != NULL); CModel * pModel = pDataModel->getModel(); assert(pModel != NULL); //reinsert the Event CEvent *pEvent = pModel->createEvent(pSData->getName()); //set the expressions pEvent->setTriggerExpression(pSData->getTriggerExpression()); pEvent->setDelayExpression(pSData->getDelayExpression()); pEvent->setPriorityExpression(pSData->getPriorityExpression()); QList <UndoEventAssignmentData *> *assignmentData = pSData->getEventAssignmentData(); QList <UndoEventAssignmentData *>::const_iterator i; for (i = assignmentData->begin(); i != assignmentData->end(); ++i) { UndoEventAssignmentData * assignData = *i; CCopasiObject * pObject = NULL; bool speciesExist = false; size_t ci; for (ci = 0; ci < pModel->getCompartments().size(); ci++) { CCompartment * pCompartment = pModel->getCompartments()[ci]; if (pCompartment->getMetabolites().getIndex(assignData->getName()) != C_INVALID_INDEX) speciesExist = true; } if (speciesExist) { size_t index = pModel->findMetabByName(assignData->getName()); pObject = pModel->getMetabolites()[index]; } else if (pModel->getModelValues().getIndex(assignData->getName()) != C_INVALID_INDEX) { pObject = pModel->getModelValues()[assignData->getName()]; } else if (pModel->getReactions().getIndex(assignData->getName()) != C_INVALID_INDEX) { pObject = pModel->getReactions()[assignData->getName()]; } const CModelEntity * pEntity = dynamic_cast< const CModelEntity * >(pObject); CEventAssignment *eventAssign = new CEventAssignment(pObject->getKey(), pEvent->getObjectParent()); eventAssign->setExpression(assignData->getExpression()); eventAssign->getExpressionPtr()->compile(); pEvent->getAssignments().add(eventAssign); } std::string key = pEvent->getKey(); protectedNotify(ListViews::EVENT, ListViews::ADD, key); mpListView->switchToOtherWidget(C_INVALID_INDEX, key); }
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(); }
// virtual CXMLHandler * MetaboliteHandler::processStart(const XML_Char * pszName, const XML_Char ** papszAttrs) { CXMLHandler * pHandlerToCall = NULL; CCompartment * pCompartment = NULL; const char * Name; const char * simulationType; CModelEntity::Status SimulationType; const char reactions[] = "reactions"; const char * Compartment; bool AddNoise; switch (mCurrentElement.first) { case Metabolite: mKey = mpParser->getAttributeValue("key", papszAttrs); Name = mpParser->getAttributeValue("name", papszAttrs); simulationType = mpParser->getAttributeValue("simulationType", papszAttrs, false); // We need to handle old files which used the attribute status. if (!simulationType) { simulationType = mpParser->getAttributeValue("status", papszAttrs, false); if (!simulationType) // status and simulationType are both missing simulationType = mpParser->getAttributeValue("simulationType", papszAttrs); else if (!strcmp(simulationType, "variable")) // reactions was named variable simulationType = reactions; } SimulationType = toEnum(simulationType, CModelEntity::XMLStatus, CModelEntity::REACTIONS); Compartment = mpParser->getAttributeValue("compartment", papszAttrs); AddNoise = mpParser->toBool(mpParser->getAttributeValue("addNoise", papszAttrs, "false")); mpMetabolite = new CMetab(); addFix(mKey, mpMetabolite); mpMetabolite->setObjectName(Name); mpMetabolite->setStatus(SimulationType); mpMetabolite->setAddNoise(AddNoise); pCompartment = dynamic_cast< CCompartment* >(mpData->mKeyMap.get(Compartment)); if (!pCompartment) fatalError(); pCompartment->addMetabolite(mpMetabolite); mpData->pModel->getMetabolites().add(mpMetabolite, false); break;; case MiriamAnnotation: case Comment: case Expression: case InitialExpression: case NoiseExpression: pHandlerToCall = getHandler(mCurrentElement.second); break; case ListOfUnsupportedAnnotations: mpData->mUnsupportedAnnotations.clear(); pHandlerToCall = getHandler(mCurrentElement.second); break; default: CCopasiMessage(CCopasiMessage::EXCEPTION, MCXML + 2, mpParser->getCurrentLineNumber(), mpParser->getCurrentColumnNumber(), pszName); break; } return pHandlerToCall; }
//static C_FLOAT64 CMetab::convertToConcentration(const C_FLOAT64 & number, const CCompartment & compartment, const CModel & model) {return number / compartment.getInitialValue() * model.getNumber2QuantityFactor();}
//static C_FLOAT64 CMetab::convertToNumber(const C_FLOAT64 & concentration, const CCompartment & compartment, const CModel & model) {return concentration * compartment.getInitialValue() * model.getQuantity2NumberFactor();}