Exemplo n.º 1
0
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;

      CChemEqElement* substr;
      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();
}
Exemplo n.º 2
0
void CModelExpansion::replaceInReaction(CReaction* pX, const ElementsMap & emap)
{
  //replace in the chemical equation
  size_t i;

  for (i = 0; i < pX->getChemEq().getSubstrates().size(); ++i)
    {
      CChemEqElement * sourceElement = pX->getChemEq().getSubstrates()[i];
      const CMetab* pMetab = dynamic_cast<const CMetab*>(emap.getDuplicatePtr(sourceElement->getMetabolite()));

      if (pMetab)
        {
          sourceElement->setMetabolite(pMetab->getKey());
        }
    }

  for (i = 0; i < pX->getChemEq().getProducts().size(); ++i)
    {
      CChemEqElement * sourceElement = pX->getChemEq().getProducts()[i];
      const CMetab* pMetab = dynamic_cast<const CMetab*>(emap.getDuplicatePtr(sourceElement->getMetabolite()));

      if (pMetab)
        {
          sourceElement->setMetabolite(pMetab->getKey());
        }
    }

  for (i = 0; i < pX->getChemEq().getModifiers().size(); ++i)
    {
      CChemEqElement * sourceElement = pX->getChemEq().getModifiers()[i];
      const CMetab* pMetab = dynamic_cast<const CMetab*>(emap.getDuplicatePtr(sourceElement->getMetabolite()));

      if (pMetab)
        {
          sourceElement->setMetabolite(pMetab->getKey());
        }
    }

  //mapping and local parameters
  for (i = 0; i < pX->getFunctionParameters().size(); ++i)
    {
      switch (pX->getFunctionParameters()[i]->getUsage())
        {
          case CFunctionParameter::SUBSTRATE:
          case CFunctionParameter::PRODUCT:
          case CFunctionParameter::MODIFIER:
          case CFunctionParameter::VOLUME:
          case CFunctionParameter::PARAMETER:
          {
            size_t k;

            for (k = 0; k < pX->getParameterMappings()[i].size(); ++k)
              {
                std::string targetKey = emap.getDuplicateKey(pX->getParameterMappings()[i][k]);

                if (targetKey != "")
                  pX->getParameterMappings()[i][k] = targetKey;
              }
          }
          break;

          case CFunctionParameter::TIME:
          default:
            break;
        }
    }
}
Exemplo n.º 3
0
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;
}