COPASI API  4.16.103
CEFMTask.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2006 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 /**
16  * CEFMTask class.
17  *
18  * This class implements a optimization task which is comprised of a
19  * of a problem and a method.
20  *
21  */
22 
23 #include <sstream>
24 
25 #include "copasi.h"
26 
27 #include "CEFMTask.h"
28 #include "CEFMMethod.h"
29 #include "CEFMProblem.h"
30 #include "CFluxMode.h"
31 
32 #ifdef COPASI_SSA
33 #include "CSSAMethod.h"
34 #endif
35 
36 #include "model/CModel.h"
37 #include "model/CMetab.h"
39 #include "model/CChemEqInterface.h"
41 
42 const unsigned int CEFMTask::ValidMethods[] =
43 {
45 #ifdef COPASI_DEBUG
48 #endif // COPASI_DEBUG
49 #ifdef COPASI_SSA
50  CCopasiMethod::stoichiometricStabilityAnalysis,
51 #endif // COPASI_SSA
52  0
53 };
54 
56  CCopasiTask(CCopasiTask::fluxMode, pParent)
57 {
58  mpProblem = new CEFMProblem(this);
60 
61  this->add(mpMethod, true);
62 }
63 
65  const CCopasiContainer * pParent):
66  CCopasiTask(src, pParent)
67 {
68  mpProblem = new CCopasiProblem(*static_cast< CEFMProblem * >(src.mpProblem), this);
70 
71  this->add(mpMethod, true);
72 }
73 
75 {}
76 
78 {
79  bool success = CCopasiTask::setCallBack(pCallBack);
80 
81  if (!mpProblem->setCallBack(pCallBack)) success = false;
82 
83  if (!mpMethod->setCallBack(pCallBack)) success = false;
84 
85  return success;
86 }
87 
89  COutputHandler * pOutputHandler,
90  std::ostream * pOstream)
91 {
92  CEFMMethod * pMethod = dynamic_cast<CEFMMethod *>(mpMethod);
93 
94  if (pMethod == NULL) return false;
95 
97  {
98  return false;
99  }
100 
101  return CCopasiTask::initialize(of, pOutputHandler, pOstream);
102 }
103 
104 bool CEFMTask::process(const bool & /* useInitialValues */)
105 {
106  return static_cast<CEFMMethod *>(mpMethod)->calculate();
107 }
108 
109 bool CEFMTask::setMethodType(const int & type)
110 {
112 
113  if (mpMethod->getSubType() == Type) return true;
114 
115  pdelete(mpMethod);
116 
117  mpMethod = createMethod(Type);
118 
119  this->add(mpMethod, true);
120 
121  return true;
122 }
123 
124 // virtual
125 CCopasiMethod * CEFMTask::createMethod(const int & type) const
126 {
128 
129  return CEFMMethod::createMethod(Type);
130 }
131 
132 std::string CEFMTask::getFluxModeDescription(const CFluxMode & fluxMode) const
133 {
134  std::stringstream tmp;
135  tmp.flags(std::ios::fixed);
136  tmp.precision(0);
137 
138  const std::vector< const CReaction * > & ReorderedReactions =
139  static_cast<CEFMProblem *>(mpProblem)->getReorderedReactions();
140 
141  unsigned C_INT32 j;
142  CFluxMode::const_iterator itMode = fluxMode.begin();
143  CFluxMode::const_iterator endMode = fluxMode.end();
144 
145 #ifdef COPASI_SSA
146 
147  if (mpMethod->getSubType() == CCopasiMethod::stoichiometricStabilityAnalysis)
148  {
149  CSSAMethod * method = dynamic_cast<CSSAMethod *>(mpMethod);
150 
151  for (j = 0; j < jmax; j++)
152  {
153  if (j) tmp << "\n";
154 
155  std::string addstrng = "";
156 
157  if (ReorderedReactions[FluxModes[index].getReactionIndex(j)]->isReversible())
158  {
159  addstrng = " (Forward)";
160 
161  if (method->isReactionReversed(FluxModes[index].getReactionIndex(j)))
162  addstrng = " (Backward)";
163  }
164 
165  tmp << FluxModes[index].getMultiplier(j) << " * "
166  << ReorderedReactions[FluxModes[index].getReactionIndex(j)]->getObjectName()
167  << addstrng;
168  }
169  }
170  else
171 #endif
172  for (j = 0; itMode != endMode; ++itMode, j++)
173  {
174  if (j) tmp << "\n";
175 
176  tmp << itMode->second << " * "
177  << ReorderedReactions[itMode->first]->getObjectName();
178  }
179 
180  return tmp.str();
181 }
182 
183 const std::vector< CFluxMode > & CEFMTask::getFluxModes() const
184 {
185  return static_cast<const CEFMProblem *>(mpProblem)->getFluxModes();
186 }
187 
188 std::string CEFMTask::getReactionEquation(const std::map< size_t, C_FLOAT64 >::const_iterator & itReaction) const
189 {
190  CEFMProblem * pProblem = static_cast<CEFMProblem *>(mpProblem);
191 
192  const CReaction * pReaction =
193  pProblem->getReorderedReactions()[itReaction->first];
194 
195 #ifdef COPASI_SSA
196 
197  if (mpMethod->getSubType() == CCopasiMethod::stoichiometricStabilityAnalysis)
198  {
199  CSSAMethod * method = static_cast<CSSAMethod *>(mpMethod);
200 
201  std::string retstring = getReactionEquationForward(index1, index2);
202 
203  if (pReaction->isReversible())
204  {
205  if (method->isReactionReversed(pMethod->getFluxModes()[index1].getReactionIndex(index2)))
206  retstring = getReactionEquationBackward(index1, index2);
207  }
208 
209  return retstring;
210  }
211  else
212 #endif //COPASI_SSA
213  return CChemEqInterface::getChemEqString(mpProblem->getModel(), *pReaction, false);
214 }
215 
216 std::pair< C_FLOAT64, C_FLOAT64 > CEFMTask::getSpeciesChanges(const CFluxMode & fluxMode,
217  const CMetab & metab) const
218 {
219  C_FLOAT64 In = 0.0;
220  C_FLOAT64 Out = 0.0;
221  C_FLOAT64 *pIn, *pOut;
222 
223  CFluxMode::const_iterator itReaction = fluxMode.begin();
224  CFluxMode::const_iterator endReaction = fluxMode.end();
225 
226  std::string Key = metab.getKey();
227 
228  const std::vector< const CReaction * > & ReorderedReactions =
229  static_cast<CEFMProblem *>(mpProblem)->getReorderedReactions();
230 
231  for (; itReaction != endReaction; ++itReaction)
232  {
233  const CReaction * pReaction = ReorderedReactions[itReaction->first];
234 
235  if (itReaction->second < 0.0)
236  {
237  pIn = &Out;
238  pOut = &In;
239  }
240  else
241  {
242  pIn = &In;
243  pOut = &Out;
244  }
245 
248 
249  for (; it != end; ++it)
250  {
251  if ((*it)->getMetaboliteKey() == Key)
252  {
253  *pIn += fabs(itReaction->second) * (*it)->getMultiplicity();
254  break;
255  }
256  }
257 
258  it = pReaction->getChemEq().getProducts().begin();
259  end = pReaction->getChemEq().getProducts().end();
260 
261  for (; it != end; ++it)
262  {
263  if ((*it)->getMetaboliteKey() == Key)
264  {
265  *pOut += fabs(itReaction->second) * (*it)->getMultiplicity();
266  break;
267  }
268  }
269  }
270 
271  return std::make_pair(In, Out);
272 }
273 
274 std::string CEFMTask::getNetReaction(const CFluxMode & fluxMode) const
275 {
276  const CModel* pModel = getProblem()->getModel();
277 
278  if (pModel == NULL)
279  return "";
280 
281  std::map< const CMetab *, C_FLOAT64 > Data = getNetReactionData(fluxMode);
282 
283  std::stringstream Substrates;
284  Substrates.flags(std::ios::fixed);
285  Substrates.precision(0);
286  std::string SubstratesSeparator = "";
287 
288  std::stringstream Products;
289  Products.flags(std::ios::fixed);
290  Products.precision(0);
291  std::string ProductsSeparator = "";
292 
293  std::map< const CMetab *, C_FLOAT64 >::const_iterator it = Data.begin();
294  std::map< const CMetab *, C_FLOAT64 >::const_iterator end = Data.end();
295 
296  for (; it != end; ++it)
297  {
298  if (it->second > 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
299  {
300  Products << ProductsSeparator;
301 
302  if (it->second > 1.0 + 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
303  {
304  Products << it->second << " * ";
305  }
306 
307  Products << CMetabNameInterface::getDisplayName(pModel, *it->first, true);
308  ProductsSeparator = " + ";
309  }
310  else if (it->second < -100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
311  {
312  Substrates << SubstratesSeparator;
313 
314  if (it->second < -1.0 - 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
315  {
316  Substrates << -it->second << " * ";
317  }
318 
319  Substrates << CMetabNameInterface::getDisplayName(pModel, *it->first, true);
320  SubstratesSeparator = " + ";
321  }
322  }
323 
324  if (fluxMode.isReversible())
325  {
326  return Substrates.str() + " = " + Products.str();
327  }
328  else
329  {
330  return Substrates.str() + " -> " + Products.str();
331  }
332 }
333 
334 std::string CEFMTask::getInternalSpecies(const CFluxMode & fluxMode) const
335 {
336  const CModel* pModel = getProblem()->getModel();
337 
338  if (pModel == NULL)
339  return "";
340 
341  std::map< const CMetab *, C_FLOAT64 > Data = getNetReactionData(fluxMode);
342 
343  std::stringstream Modifiers;
344  Modifiers.flags(std::ios::fixed);
345  Modifiers.precision(0);
346  std::string ModifiersSeparator = "";
347 
348  std::map< const CMetab *, C_FLOAT64 >::const_iterator it = Data.begin();
349  std::map< const CMetab *, C_FLOAT64 >::const_iterator end = Data.end();
350 
351  for (; it != end; ++it)
352  {
353  if (fabs(it->second) < 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
354  {
355  Modifiers << ModifiersSeparator;
356  Modifiers << CMetabNameInterface::getDisplayName(pModel, *it->first, true);
357  ModifiersSeparator = ", ";
358  }
359  }
360 
361  return Modifiers.str();
362 }
363 
364 std::map< const CMetab *, C_FLOAT64 > CEFMTask::getNetReactionData(const CFluxMode & fluxMode) const
365 {
366  std::map< const CMetab *, C_FLOAT64 > NetReaction;
367  std::pair< C_FLOAT64, C_FLOAT64 > Changes;
368 
369  CFluxMode::const_iterator itReaction = fluxMode.begin();
370  CFluxMode::const_iterator endReaction = fluxMode.end();
371 
372  const std::vector< const CReaction * > & ReorderedReactions =
373  static_cast<CEFMProblem *>(mpProblem)->getReorderedReactions();
374 
375  const CMetab * pSpecies;
376 
377  for (; itReaction != endReaction; ++itReaction)
378  {
379  const CReaction * pReaction = ReorderedReactions[itReaction->first];
380 
383 
384  for (; it != end; ++it)
385  {
386  pSpecies = (*it)->getMetabolite();
387 
388  if (NetReaction.count(pSpecies) == 0)
389  {
390  Changes = getSpeciesChanges(fluxMode, *pSpecies);
391  NetReaction[pSpecies] = Changes.second - Changes.first;
392  }
393  }
394 
395  it = pReaction->getChemEq().getProducts().begin();
396  end = pReaction->getChemEq().getProducts().end();
397 
398  for (; it != end; ++it)
399  {
400  pSpecies = (*it)->getMetabolite();
401 
402  if (NetReaction.count(pSpecies) == 0)
403  {
404  Changes = getSpeciesChanges(fluxMode, *pSpecies);
405  NetReaction[pSpecies] = Changes.second - Changes.first;
406  }
407  }
408  }
409 
410  return NetReaction;
411 }
412 
413 #ifdef COPASI_SSA
414 std::string CEFMTask::getReactionEquationForward(unsigned C_INT32 index1, unsigned C_INT32 index2) const
415 {
416  const CReaction *pReaction =
417  static_cast<CEFMMethod *>(mpMethod)->getReorderedReactions()[static_cast<CEFMMethod *>(mpMethod)->getFluxModes()[index1].getReactionIndex(index2)];
418 
419  std::string equation = CChemEqInterface::getChemEqString(mpProblem->getModel(),
420  *pReaction,
421  false);
422 
423  if (pReaction->isReversible())
424  {
425  int i = equation.find("=", 0);
426  equation.replace(i, 1, "->");
427  }
428 
429  return equation;
430 }
431 
432 std::string CEFMTask::getReactionEquationBackward(unsigned C_INT32 index1, unsigned C_INT32 index2) const
433 {
434  const CReaction *pReaction =
435  static_cast<CEFMMethod *>(mpMethod)->getReorderedReactions()[static_cast<CEFMMethod *>(mpMethod)->getFluxModes()[index1].getReactionIndex(index2)];
436 
437  std::string equation = CChemEqInterface::getChemEqString(mpProblem->getModel(),
438  *pReaction,
439  false);
440 
441  if (pReaction->isReversible())
442  {
443  int i = equation.find("=", 0);
444  std::string lhs = equation.substr(0, i - 1);
445  std::string rhs = equation.substr(i + 2 , equation.size() - i - 1);
446  equation = rhs + " -> " + lhs;
447  }
448 
449  return equation;
450 }
451 #endif // COPASI_SSA
virtual bool process(const bool &useInitialValues)
Definition: CEFMTask.cpp:104
std::string getNetReaction(const CFluxMode &fluxMode) const
Definition: CEFMTask.cpp:274
virtual bool setCallBack(CProcessReport *pCallBack)
CCopasiMethod * mpMethod
Definition: CCopasiTask.h:239
#define pdelete(p)
Definition: copasi.h:215
static CEFMMethod * createMethod(CCopasiMethod::SubType subType=CCopasiMethod::EFMAlgorithm)
Definition: CEFMMethod.cpp:47
CCopasiProblem * getProblem()
std::string getReactionEquation(const std::map< size_t, C_FLOAT64 >::const_iterator &itReaction) const
Definition: CEFMTask.cpp:188
std::pair< C_FLOAT64, C_FLOAT64 > getSpeciesChanges(const CFluxMode &fluxMode, const CMetab &metab) const
Definition: CEFMTask.cpp:216
std::string getFluxModeDescription(const CFluxMode &fluxMode) const
Definition: CEFMTask.cpp:132
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
bool isReactionReversed(int index) const
Definition: CSSAMethod.cpp:199
std::map< const CMetab *, C_FLOAT64 > getNetReactionData(const CFluxMode &fluxMode) const
Definition: CEFMTask.cpp:364
iterator begin()
virtual bool setCallBack(CProcessReport *pCallBack)
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
Definition: CEFMTask.cpp:88
const std::vector< CFluxMode > & getFluxModes() const
Definition: CEFMTask.cpp:183
const CCopasiMethod::SubType & getSubType() const
#define C_INT32
Definition: copasi.h:90
static std::string getDisplayName(const CModel *model, const std::string &key, const bool &quoted)
Definition: CMetab.h:178
virtual bool isValidProblem(const CCopasiProblem *pProblem)
CCopasiProblem * mpProblem
Definition: CCopasiTask.h:234
const CCopasiVector< CChemEqElement > & getProducts() const
Definition: CChemEq.cpp:63
iterator end()
std::string getInternalSpecies(const CFluxMode &fluxMode) const
Definition: CEFMTask.cpp:334
virtual const std::string & getKey() const
std::string getChemEqString(bool expanded) const
const_iterator begin() const
Definition: CFluxMode.cpp:69
virtual ~CEFMTask()
Definition: CEFMTask.cpp:74
const std::vector< const CReaction * > & getReorderedReactions() const
Definition: CEFMProblem.cpp:86
const CCopasiVector< CChemEqElement > & getSubstrates() const
Definition: CChemEq.cpp:60
static const unsigned int ValidMethods[]
Definition: CEFMTask.h:47
bool isReversible() const
Definition: CReaction.cpp:229
CEFMTask(const CCopasiContainer *pParent=NULL)
Definition: CEFMTask.cpp:55
#define C_FLOAT64
Definition: copasi.h:92
std::map< size_t, C_FLOAT64 >::const_iterator const_iterator
Definition: CFluxMode.h:43
virtual bool setCallBack(CProcessReport *pCallBack)
Definition: CEFMTask.cpp:77
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
Definition: CModel.h:50
virtual bool setCallBack(CProcessReport *pCallBack)
virtual bool setMethodType(const int &type)
Definition: CEFMTask.cpp:109
bool isReversible() const
Definition: CFluxMode.cpp:79
size_t getReactionIndex(const CCopasiVector< CReaction > &reacs, const CReaction *react)
const CChemEq & getChemEq() const
Definition: CReaction.cpp:223
CModel * getModel() const
const_iterator end() const
Definition: CFluxMode.cpp:74
virtual CCopasiMethod * createMethod(const int &type) const
Definition: CEFMTask.cpp:125