bool CReactionInterface::loadMappingAndValues(const CReaction & rea) { bool success = true; std::vector< std::vector<std::string> >::const_iterator it; std::vector< std::vector<std::string> >::const_iterator iEnd; std::vector<std::string>::const_iterator jt; std::vector<std::string>::const_iterator jEnd; size_t i; std::string metabName; const CModelEntity* pObj; std::vector<std::string> SubList; SubList.resize(1); SubList[0] = "unknown"; mNameMap.resize(size()); for (i = 0; i != size(); ++i) { mNameMap[i] = SubList; } mValues.resize(size(), 0.1); mIsLocal.resize(size(), false); it = rea.getParameterMappings().begin(); iEnd = rea.getParameterMappings().end(); for (i = 0; it != iEnd; ++it, ++i) { if (isVector(i)) { assert((getUsage(i) == CFunctionParameter::SUBSTRATE) || (getUsage(i) == CFunctionParameter::PRODUCT) || (getUsage(i) == CFunctionParameter::MODIFIER)); SubList.clear(); for (jt = it->begin(), jEnd = it->end(); jt != jEnd; ++jt) { metabName = CMetabNameInterface::getDisplayName(mpModel, *jt, true); assert(metabName != ""); SubList.push_back(metabName); } } else { assert(it->size() == 1); SubList.resize(1); SubList[0] = "unknown"; switch (getUsage(i)) { case CFunctionParameter::SUBSTRATE: case CFunctionParameter::PRODUCT: case CFunctionParameter::MODIFIER: metabName = CMetabNameInterface::getDisplayName(mpModel, *(it->begin()), true); // assert(metabName != ""); SubList[0] = metabName; //TODO: check if the metabolite is in the chemical equation with the correct rule break; case CFunctionParameter::VOLUME: pObj = dynamic_cast<const CCompartment*>(CCopasiRootContainer::getKeyFactory()->get(*(it->begin()))); assert(pObj); SubList[0] = pObj->getObjectName(); break; case CFunctionParameter::TIME: pObj = dynamic_cast<const CModel*>(CCopasiRootContainer::getKeyFactory()->get(*(it->begin()))); assert(pObj); SubList[0] = pObj->getObjectName(); break; case CFunctionParameter::PARAMETER: { const CCopasiParameter * pParameter = rea.getParameters().getParameter(getParameterName(i)); if (pParameter != NULL) { mValues[i] = * pParameter->getValue().pDOUBLE; } else { mValues[i] = std::numeric_limits< C_FLOAT64 >::quiet_NaN(); } mIsLocal[i] = rea.isLocalParameter(i); if (!mIsLocal[i]) { pObj = dynamic_cast<const CModelValue*>(CCopasiRootContainer::getKeyFactory()->get(*(it->begin()))); if (pObj) { SubList[0] = pObj->getObjectName(); mValues[i] = pObj->getInitialValue(); } } } break; default: break; } } mNameMap[i] = SubList; } return success; }
void CModelExpansion::duplicateReaction(const CReaction* 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 CReaction* newObj; std::ostringstream infix; do { std::ostringstream name; name << source->getObjectName() << infix.str() << index; newObj = mpModel->createReaction(name.str()); infix << "_"; } while (!newObj); //add duplicated object to the map emap.add(source, newObj); //now copy the chemical equation size_t i; for (i = 0; i < source->getChemEq().getSubstrates().size(); ++i) { const CChemEqElement * sourceElement = source->getChemEq().getSubstrates()[i]; const CMetab* pMetab = NULL; if (sourceSet.contains(sourceElement->getMetabolite())) { if (!emap.exists(sourceElement->getMetabolite())) duplicateMetab(sourceElement->getMetabolite(), index, sourceSet, emap); pMetab = dynamic_cast<const CMetab*>(emap.getDuplicatePtr(sourceElement->getMetabolite())); } else //add the original metab { pMetab = sourceElement->getMetabolite(); } if (pMetab) newObj->addSubstrate(pMetab->getKey(), sourceElement->getMultiplicity()); } for (i = 0; i < source->getChemEq().getProducts().size(); ++i) { const CChemEqElement * sourceElement = source->getChemEq().getProducts()[i]; const CMetab* pMetab = NULL; if (sourceSet.contains(sourceElement->getMetabolite())) { if (!emap.exists(sourceElement->getMetabolite())) duplicateMetab(sourceElement->getMetabolite(), index, sourceSet, emap); pMetab = dynamic_cast<const CMetab*>(emap.getDuplicatePtr(sourceElement->getMetabolite())); } else //add the original metab { pMetab = sourceElement->getMetabolite(); } if (pMetab) newObj->addProduct(pMetab->getKey(), sourceElement->getMultiplicity()); } for (i = 0; i < source->getChemEq().getModifiers().size(); ++i) { const CChemEqElement * sourceElement = source->getChemEq().getModifiers()[i]; const CMetab* pMetab = NULL; if (sourceSet.contains(sourceElement->getMetabolite())) { if (!emap.exists(sourceElement->getMetabolite())) duplicateMetab(sourceElement->getMetabolite(), index, sourceSet, emap); pMetab = dynamic_cast<const CMetab*>(emap.getDuplicatePtr(sourceElement->getMetabolite())); } else //add the original metab { pMetab = sourceElement->getMetabolite(); } if (pMetab) newObj->addModifier(pMetab->getKey()); } newObj->setReversible(source->isReversible()); //set the kinetic function newObj->setFunction(const_cast<CFunction*>(source->getFunction())); //mapping and local parameters for (i = 0; i < newObj->getFunctionParameters().size(); ++i) { switch (newObj->getFunctionParameters()[i]->getUsage()) { case CFunctionParameter::SUBSTRATE: case CFunctionParameter::PRODUCT: case CFunctionParameter::MODIFIER: { bool isVector = (newObj->getFunctionParameters()[i]->getType() == CFunctionParameter::VFLOAT64); if (isVector) newObj->clearParameterMapping(i); size_t k; for (k = 0; k < source->getParameterMappings()[i].size(); ++k) { //we assume that by now the metab was copied if necessary. //therefore we only need to check the map. std::string targetKey; if (emap.exists(source->getParameterMappings()[i][k])) targetKey = emap.getDuplicateKey(source->getParameterMappings()[i][k]); else targetKey = source->getParameterMappings()[i][k]; if (isVector) newObj->addParameterMapping(i, targetKey); else newObj->setParameterMapping(i, targetKey); } } break; case CFunctionParameter::TIME: newObj->setParameterMapping(i, source->getParameterMappings()[i][0]); break; case CFunctionParameter::VOLUME: if (sourceSet.contains(source->getParameterMappings()[i][0])) { if (!emap.exists(source->getParameterMappings()[i][0])) { const CCompartment* pSource = dynamic_cast<const CCompartment*>( (CCopasiRootContainer::getKeyFactory()->get(source->getParameterMappings()[i][0]))); duplicateCompartment(pSource, index, sourceSet, emap); } newObj->setParameterMapping(i, emap.getDuplicateKey(source->getParameterMappings()[i][0])); } else //add the original metab { newObj->setParameterMapping(i, source->getParameterMappings()[i][0]); } break; case CFunctionParameter::PARAMETER: if (newObj->isLocalParameter(i)) { //just copy the value newObj->setParameterValue(newObj->getFunctionParameters()[i]->getObjectName(), source->getParameterValue(newObj->getFunctionParameters()[i]->getObjectName())); } else { if (sourceSet.contains(source->getParameterMappings()[i][0])) { if (!emap.exists(source->getParameterMappings()[i][0])) { const CModelValue* pSource = dynamic_cast<const CModelValue*>( (CCopasiRootContainer::getKeyFactory()->get(source->getParameterMappings()[i][0]))); duplicateGlobalQuantity(pSource, index, sourceSet, emap); } newObj->setParameterMapping(i, emap.getDuplicateKey(source->getParameterMappings()[i][0])); } else //add the original global quantity { newObj->setParameterMapping(i, source->getParameterMappings()[i][0]); } } break; default: break; } } newObj->setNotes(source->getNotes()); newObj->setMiriamAnnotation(source->getMiriamAnnotation(), newObj->getKey(), source->getKey()); }
void CFixLocalReactionParameters::changeModel() { CCopasiParameter * pParameter = NULL; CModelValue * pModelValue = NULL; CReaction * pReaction = NULL; std::stringstream NameStream; std::stringstream Message; std::string OldCN; std::string NewCNBase; std::string NewCN; std::string Infix; std::string::size_type Start; // Loop through all changes. std::multimap< CCopasiParameter *, const CExpression * >::const_iterator itChanges = mChanges.begin(); std::multimap< CCopasiParameter *, const CExpression * >::const_iterator endChanges = mChanges.end(); for (; itChanges != endChanges; ++itChanges) { if (pParameter != itChanges->first) { // We have a new parameter pParameter = itChanges->first; OldCN = "<" + pParameter->getCN() + ",Reference="; // Create a global quantity of type FIXED. std::string Name = pParameter->getObjectName(); pReaction = static_cast< CReaction * >(pParameter->getObjectAncestor("Reaction")); Name += "{" + pReaction->getObjectName() + "}"; pModelValue = mpModel->createModelValue(Name, pParameter->getValue< C_FLOAT64 >()); // In case the created name is not unique we append _n with increasing n // until we succeed; C_INT32 index = 0; while (pModelValue == NULL) { NameStream.str(""); NameStream << Name << "_" << index++; pModelValue = mpModel->createModelValue(NameStream.str(), pParameter->getValue< C_FLOAT64 >()); } NewCNBase = "<" + pModelValue->getCN() + ",Reference="; // If the parameter is actually used in the reaction // it is changed to the global quantity. if (pReaction->isLocalParameter(pParameter->getObjectName())) pReaction->setParameterMapping(pParameter->getObjectName(), pModelValue->getKey()); Message << " " << pParameter->getObjectName() << " in " << pReaction->getObjectName() << " is replaced by " << pModelValue->getObjectName() << std::endl; } // We need to distinguish between initial and other expressions. if (itChanges->second->getObjectName().compare(0, 7, "Initial") == 0) NewCN = NewCNBase + "Initial"; else NewCN = NewCNBase; // Replace the OldCN of the parameter with the NewCN of global quantity in all expressions. Infix = itChanges->second->getInfix(); // There may be more than one occurrence. Start = 0; while ((Start = Infix.find(OldCN), Start) != std::string::npos) Infix.replace(Start, OldCN.length(), NewCN); const_cast< CExpression * >(itChanges->second)->setInfix(Infix); } CCopasiMessage(CCopasiMessage::WARNING, MCXML + 14, Message.str().c_str()); }
bool CODEExporterC::exportTitleData(const CModel* copasiModel, std::ostream & os) { size_t metab_size = copasiModel->getMetabolitesX().size(); size_t indep_size = copasiModel->getNumIndependentReactionMetabs(); size_t ode_size = copasiModel->getNumODEMetabs(); size_t comps_size = copasiModel->getCompartments().size(); size_t modvals_size = copasiModel->getModelValues().size(); size_t reacs_size = copasiModel->getReactions().size(); size_t i, j, num_params, count; const CCopasiVector< CReaction > & reacs = copasiModel->getReactions(); CReaction* reac; count = 0; for (i = 0; i < reacs_size; ++i) { reac = reacs[i]; num_params = reac->getParameters().size(); for (j = 0; j < num_params; ++j) { if (reac->isLocalParameter(j)) ++count; } } size_t numX = 0; size_t numY = 0; size_t numXC = 0; size_t numYC = 0; size_t numPC = 0; size_t numP = 0; size_t numDX = 0; size_t numCT = 0; std::ostringstream p_names; p_names << "const char* p_names[] = {"; std::ostringstream x_names; x_names << "const char* x_names[] = {"; std::ostringstream y_names; y_names << "const char* y_names[] = {"; std::ostringstream xc_names; xc_names << "const char* xc_names[] = {"; std::ostringstream pc_names; pc_names << "const char* pc_names[] = {"; std::ostringstream yc_names; yc_names << "const char* yc_names[] = {"; std::ostringstream dx_names; dx_names << "const char* dx_names[] = {"; std::ostringstream ct_names; ct_names << "const char* ct_names[] = {"; CKeyFactory* kf = CCopasiRootContainer::getKeyFactory(); std::map< std::string, std::string >::const_iterator it = NameMap.begin(); std::map< std::string, std::string > reverse_map; while (it != NameMap.end()) { const std::string& abbrev = it->second; const std::string& key = it->first; if (startsWith(abbrev, "p[")) { reverse_map[abbrev] = key; ++numP; } else if (startsWith(abbrev, "x[")) { reverse_map[abbrev] = key; ++numX; } else if (startsWith(abbrev, "y[")) { reverse_map[abbrev] = key; ++numY; } else if (startsWith(abbrev, "dx[")) { reverse_map[abbrev] = key; ++numDX; } else if (startsWith(abbrev, "ct[")) { reverse_map[abbrev] = key; ++numCT; } else if (startsWith(abbrev, "x_c[")) { CCopasiObject* obj = kf->get(key); if (obj != NULL) { reverse_map[abbrev] = obj->getObjectName(); } else { reverse_map[abbrev] = key; } ++numXC; } else if (startsWith(abbrev, "p_c[")) { CCopasiObject* obj = kf->get(key); if (obj != NULL) { reverse_map[abbrev] = obj->getObjectName(); } else { reverse_map[abbrev] = key; } ++numPC; } else if (startsWith(abbrev, "y_c[")) { CCopasiObject* obj = kf->get(key); if (obj != NULL) { reverse_map[abbrev] = obj->getObjectName(); } else { reverse_map[abbrev] = key; } ++numYC; } ++it; } for (size_t i = 0; i < numP; ++i) { std::stringstream str; str << "p[" << i << "]"; printNameForKey(p_names, kf, reverse_map[str.str()]); } for (size_t i = 0; i < numX; ++i) { std::stringstream str; str << "x[" << i << "]"; printNameForKey(x_names, kf, reverse_map[str.str()]); } for (size_t i = 0; i < numY; ++i) { std::stringstream str; str << "y[" << i << "]"; printNameForKey(y_names, kf, reverse_map[str.str()]); } for (size_t i = 0; i < numDX; ++i) { std::stringstream str; str << "dx[" << i << "]"; printNameForKey(dx_names, kf, reverse_map[str.str()]); } for (size_t i = 0; i < numCT; ++i) { std::stringstream str; str << "ct[" << i << "]"; printNameForKey(ct_names, kf, reverse_map[str.str()]); } for (size_t i = 0; i < numXC; ++i) { std::stringstream str; str << "x_c[" << i << "]"; xc_names << "\"" << reverse_map[str.str()] << "\", "; } for (size_t i = 0; i < numPC; ++i) { std::stringstream str; str << "p_c[" << i << "]"; pc_names << "\"" << reverse_map[str.str()] << "\", "; } for (size_t i = 0; i < numYC; ++i) { std::stringstream str; str << "y_c[" << i << "]"; yc_names << "\"" << reverse_map[str.str()] << "\", "; } os << "#ifdef SIZE_DEFINITIONS" << std::endl; os << "#define N_METABS " << metab_size << std::endl; os << "#define N_ODE_METABS " << ode_size << std::endl; os << "#define N_INDEP_METABS " << indep_size << std::endl; os << "#define N_COMPARTMENTS " << comps_size << std::endl; os << "#define N_GLOBAL_PARAMS " << modvals_size << std::endl; os << "#define N_KIN_PARAMS " << count << std::endl; os << "#define N_REACTIONS " << reacs_size << std::endl << std::endl; os << "#define N_ARRAY_SIZE_P " << numP << "\t// number of parameters" << std::endl; os << "#define N_ARRAY_SIZE_X " << numX << "\t// number of initials" << std::endl; os << "#define N_ARRAY_SIZE_Y " << numY << "\t// number of assigned elements" << std::endl; os << "#define N_ARRAY_SIZE_XC " << numXC << "\t// number of x concentration" << std::endl; os << "#define N_ARRAY_SIZE_PC " << numPC << "\t// number of p concentration" << std::endl; os << "#define N_ARRAY_SIZE_YC " << numYC << "\t// number of y concentration" << std::endl; os << "#define N_ARRAY_SIZE_DX " << numDX << "\t// number of ODEs " << std::endl; os << "#define N_ARRAY_SIZE_CT " << numCT << "\t// number of conserved totals" << std::endl << std::endl; os << "#endif // SIZE_DEFINITIONS" << std::endl; os << std::endl; os << "#ifdef TIME" << std::endl; os << "#define T <set here a user name for the time variable> " << std::endl; os << "#endif // TIME" << std::endl; os << std::endl; os << "#ifdef NAME_ARRAYS" << std::endl; os << p_names.str() << " \"\" };" << std::endl; os << x_names.str() << " \"\" };" << std::endl; os << y_names.str() << " \"\" };" << std::endl; os << xc_names.str() << " \"\" };" << std::endl; os << pc_names.str() << " \"\" };" << std::endl; os << yc_names.str() << " \"\" };" << std::endl; os << dx_names.str() << " \"\" };" << std::endl; os << ct_names.str() << " \"\" };" << std::endl; os << "#endif // NAME_ARRAYS" << std::endl; return true; }
bool CModelAdd::addReactions(std::string name) { bool info = false; //create copies of the relevant reactions size_t i, imax = mmModel->getReactions().size(); size_t ic, icmax = mmModel->getCompartments().size(); for (ic = 0; ic < icmax; ++ic) { const CCompartment* sourceComp = &mmModel->getCompartments()[ic]; if (!sourceComp) return info; for (i = 0; i < imax; ++i) { CReaction * sourceReac = &mmModel->getReactions()[i]; if (reactionInvolvesCompartment(sourceReac, sourceComp)) { std::string newName = sourceReac->getObjectName() + "_" + name; CReaction* newReac = mpModel->createReaction(newName); if (!newReac) return info; //copy the chemical equation. If the involved metabs are among those that //were copied with the compartment, replace them. Otherwise keep the original metab newReac->setReversible(sourceReac->isReversible()); std::map<std::string, std::string>::const_iterator mapIt; std::string targetKey; size_t j, jmax = sourceReac->getChemEq().getSubstrates().size(); for (j = 0; j < jmax; ++j) { const CChemEqElement * sourceElement = &sourceReac->getChemEq().getSubstrates()[j]; //check if the metab is in the map. If yes, translate it, otherwise not. mapIt = keyMap.find(sourceElement->getMetaboliteKey()); if (mapIt == keyMap.end()) { targetKey = sourceElement->getMetaboliteKey(); } else targetKey = mapIt->second; newReac->addSubstrate(targetKey, sourceElement->getMultiplicity()); } jmax = sourceReac->getChemEq().getProducts().size(); for (j = 0; j < jmax; ++j) { const CChemEqElement * sourceElement = &sourceReac->getChemEq().getProducts()[j]; //check if the metab is in the map. If yes, translate it, otherwise not. mapIt = keyMap.find(sourceElement->getMetaboliteKey()); if (mapIt == keyMap.end()) { targetKey = sourceElement->getMetaboliteKey(); } else targetKey = mapIt->second; newReac->addProduct(targetKey, sourceElement->getMultiplicity()); } jmax = sourceReac->getChemEq().getModifiers().size(); for (j = 0; j < jmax; ++j) { const CChemEqElement * sourceElement = &sourceReac->getChemEq().getModifiers()[j]; //check if the metab is in the map. If yes, translate it, otherwise not. mapIt = keyMap.find(sourceElement->getMetaboliteKey()); if (mapIt == keyMap.end()) { targetKey = sourceElement->getMetaboliteKey(); } else targetKey = mapIt->second; newReac->addModifier(targetKey); } //set the kinetic function newReac->setFunction(const_cast<CFunction*>(sourceReac->getFunction())); //mapping and local parameters for (j = 0; j < newReac->getFunctionParameters().size(); ++j) { switch (newReac->getFunctionParameters()[j]->getUsage()) { case CFunctionParameter::SUBSTRATE: case CFunctionParameter::PRODUCT: case CFunctionParameter::MODIFIER: //translate the metab keys { bool isVector = (newReac->getFunctionParameters()[j]->getType() == CFunctionParameter::VFLOAT64); //we assume that only SUBSTRATE, PRODUCT, MODIFIER can be vectors if (isVector) newReac->clearParameterMapping(j); size_t k; for (k = 0; k < sourceReac->getParameterMappings()[j].size(); ++k) { mapIt = keyMap.find(sourceReac->getParameterMappings()[j][k]); if (mapIt == keyMap.end()) { targetKey = sourceReac->getParameterMappings()[j][k]; } else targetKey = mapIt->second; if (isVector) newReac->addParameterMapping(j, targetKey); else newReac->setParameterMapping(j, targetKey); } } break; case CFunctionParameter::TIME: //just copy the key { mapIt = keyMap.find(sourceReac->getParameterMappings()[j][0]); if (mapIt == keyMap.end()) { targetKey = sourceReac->getParameterMappings()[j][0]; } else targetKey = mapIt->second; newReac->setParameterMapping(j, targetKey); } break; case CFunctionParameter::VOLUME: //translate the compartment key if necessary if (sourceReac->getParameterMappings()[j][0] == sourceComp->getKey()) newReac->setParameterMapping(j, keyMap[sourceComp->getKey()]); else { mapIt = keyMap.find(sourceReac->getParameterMappings()[j][0]); if (mapIt == keyMap.end()) { targetKey = sourceReac->getParameterMappings()[j][0]; } else targetKey = mapIt->second; newReac->setParameterMapping(j, targetKey); } //TODO: this needs to be adapted when sets of compartments will be copied break; case CFunctionParameter::PARAMETER: if (sourceReac->isLocalParameter(j)) newReac->setParameterValue(newReac->getFunctionParameters()[j]->getObjectName(), sourceReac->getParameterValue(newReac->getFunctionParameters()[j]->getObjectName())); else { mapIt = keyMap.find(sourceReac->getParameterMappings()[j][0]); if (mapIt == keyMap.end()) { targetKey = sourceReac->getParameterMappings()[j][0]; } else targetKey = mapIt->second; newReac->setParameterMapping(j, targetKey); } break; default: return info; break; } } } } } return true; }