COPASI API  4.16.103
CChemEq.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) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 // CChemEqElement
16 //
17 // A class describing an element of a chemical equation
18 // (C) Stefan Hoops 2001
19 //
20 
21 #include <cmath>
22 
23 #include "copasi.h"
24 #include "CChemEq.h"
25 #include "utilities/CReadConfig.h"
27 #include "CMetabNameInterface.h"
28 #include "CCompartment.h"
29 
30 CChemEq::CChemEq(const std::string & name,
31  const CCopasiContainer * pParent):
32  CCopasiContainer(name, pParent, "Chemical Equation"),
33  mReversible(false),
34  mSubstrates("Substrates", this),
35  mProducts("Products", this),
36  mModifiers("Modifiers", this),
37  mBalances("Balances", this)
39 
41  const CCopasiContainer * pParent):
42  CCopasiContainer(src, pParent),
43  mReversible(src.mReversible),
44  mSubstrates(src.mSubstrates, this),
45  mProducts(src.mProducts, this),
46  mModifiers(src.mModifiers, this),
47  mBalances(src.mBalances, this)
49 
51 
53 {
58 }
59 
61 {return mSubstrates;}
62 
64 {return mProducts;}
65 
67 {return mModifiers;}
68 
70 {return mBalances;}
71 
72 void CChemEq::setReversibility(const bool & reversible)
73 {
74  mReversible = reversible;
75 }
76 
77 const bool & CChemEq::getReversibility() const
78 {return mReversible;}
79 
80 bool CChemEq::addMetabolite(const std::string & key, const C_FLOAT64 multiplicity, const MetaboliteRole & role)
81 {
82  CChemEqElement element;
83  element.setMetabolite(key);
84  element.setMultiplicity(multiplicity);
85 
86  switch (role)
87  {
88  case CChemEq::SUBSTRATE:
89  addElement(mSubstrates, element);
91  break;
92 
93  case CChemEq::PRODUCT:
94  addElement(mProducts, element);
95  addElement(mBalances, element);
96  break;
97 
98  case CChemEq::MODIFIER:
99  addElement(mModifiers, element);
100  break;
101 
102  default:
103  fatalError();
104  break;
105  }
106 
107  return true;
108 }
109 
111 {
112  return getCompartments().size();
113 }
114 
115 std::set< const CCompartment * > CChemEq::getCompartments() const
116 {
117  const CCompartment * pCompartment = NULL;
118  std::set< const CCompartment * > Compartments;
119 
122 
123  for (; it != end; ++it)
124  {
125  if ((*it)->getMetabolite() == NULL)
126  continue;
127 
128  if ((pCompartment = (*it)->getMetabolite()->getCompartment()) != NULL)
129  {
130  Compartments.insert(pCompartment);
131  }
132  }
133 
134  return Compartments;
135 }
136 
138 {
139  size_t indexSubstrates = C_INVALID_INDEX;
140  size_t indexProducts = C_INVALID_INDEX;
141  size_t i, imax;
142 
143  C_FLOAT64 tmp, maxVol = -1.0;
144 
145  for (i = 0, imax = mSubstrates.size(); i < imax; i++)
146  {
147  if (!mSubstrates[i]->getMetabolite()) continue;
148 
149  tmp = mSubstrates[i]->getMetabolite()->getCompartment()->getValue();
150 
151  if (tmp > maxVol)
152  {
153  maxVol = tmp;
154  indexSubstrates = i;
155  }
156  }
157 
158  for (i = 0, imax = mProducts.size(); i < imax; i++)
159  {
160  if (!mProducts[i]->getMetabolite()) continue;
161 
162  tmp = mProducts[i]->getMetabolite()->getCompartment()->getValue();
163 
164  if (tmp > maxVol)
165  {
166  maxVol = tmp;
167  indexProducts = i;
168  }
169  }
170 
171  if (indexProducts != C_INVALID_INDEX)
172  return mProducts[indexProducts]->getMetabolite()->getCompartment();
173 
174  if (indexSubstrates != C_INVALID_INDEX)
175  return mSubstrates[indexSubstrates]->getMetabolite()->getCompartment();
176 
177  return NULL;
178 }
179 
181  const CChemEqElement & element,
183 {
184  size_t i;
185 
186  std::string key = element.getMetaboliteKey();
187 
188  if (key == "")
189  return; // don�t add empty element
190 
191  for (i = 0; i < structure.size(); i++)
192  if (key == structure[i]->getMetaboliteKey())
193  break;
194 
195  if (i >= structure.size())
196  {
197  CChemEqElement * Element = new CChemEqElement(element);
198 
199  if (role == CChemEq::SUBSTRATE)
200  Element->setMultiplicity(- Element->getMultiplicity());
201 
202  structure.add(Element, true);
203  }
204  else if (role == CChemEq::SUBSTRATE)
205  structure[i]->addToMultiplicity(- element.getMultiplicity());
206  else
207  structure[i]->addToMultiplicity(element.getMultiplicity());
208 }
209 
210 size_t CChemEq::getMolecularity(const MetaboliteRole role) const
211 {
212  const CCopasiVector<CChemEqElement> * tmpVector = NULL;
213 
214  switch (role)
215  {
216  case CChemEq::SUBSTRATE:
217  tmpVector = &mSubstrates;
218  break;
219 
220  case CChemEq::PRODUCT:
221  tmpVector = &mProducts;
222  break;
223 
224  case CChemEq::MODIFIER:
225  tmpVector = &mModifiers;
226  break;
227 
228  default:
229  fatalError();
230  break;
231  }
232 
233  size_t ccc, i, imax = tmpVector->size();
234  ccc = 0;
235 
236  for (i = 0; i < imax; ++i)
237  ccc += (size_t) floor((*tmpVector)[i]->getMultiplicity());
238 
239  return ccc;
240 }
241 
242 std::ostream & operator<<(std::ostream &os, const CChemEq & d)
243 {
244  os << "CChemEq:" << std::endl;
245  //os << " mChemicalEquation: " << d.getChemicalEquation() << std::endl;
246  //os << " mChemicalEquationConverted: " << d.getChemicalEquationConverted() << std::endl;
247 
248  os << " mSubstrates:" << std::endl;
249  os << d.mSubstrates;
250  os << " mProducts:" << std::endl;
251  os << d.mProducts;
252  os << " mBalances:" << std::endl;
253  os << d.mBalances;
254 
255  os << "----CChemEq" << std::endl;
256  return os;
257 }
258 
259 /**
260  * Sets the stoichiometry of the chemical element that corresponds to the
261  * given metabolite and role to the new value.
262  * The role has to be either CChemEqElement::SUBSTRATE or
263  * CChemEqElement::PRODUCT.
264  * If the role is invalid, the multiplicity if negative or if the element for
265  * the metabolite and role can not be found in the chemical equation, false is returned and
266  * nothing is changed.
267  */
268 bool CChemEq::setMultiplicity(const CMetab* pMetab, C_FLOAT64 newMult, MetaboliteRole role)
269 {
270  bool result = true;
271 
272  // check if the multiplicity and the role are valid
273  if (pMetab == NULL || newMult <= 0 || !(role == CChemEq::SUBSTRATE || role == CChemEq::PRODUCT))
274  {
275  result = false;
276  }
277  else
278  {
279  // find the corresponding chemical element
280  std::string key = pMetab->getKey();
282 
283  if (role == CChemEq::SUBSTRATE)
284  {
285  it = this->mSubstrates.begin();
286  endit = this->mSubstrates.end();
287  }
288  else
289  {
290  it = this->mProducts.begin();
291  endit = this->mProducts.end();
292  }
293 
294  while (it != endit)
295  {
296  if ((*it)->getMetaboliteKey() == key)
297  {
298  break;
299  }
300 
301  ++it;
302  }
303 
304  if (it == endit)
305  {
306  result = false;
307  }
308  else
309  {
310  // set the new multiplicity and update the balances
311  C_FLOAT64 diff = newMult - (*it)->getMultiplicity();
312 
313  // we only make changes if there actually is a difference
314  if (fabs(diff) > 1e-9)
315  {
316  // we have to add the difference between the new and the old
317  // multiplicity to the balances
318  (*it)->setMultiplicity(newMult);
319  // copy the element
320  CChemEqElement tmp(**it);
321  // set the difference of the multiplicities
322  // as the multiplicity of the copy
323  tmp.setMultiplicity(diff);
324  // update the balances
325  this->addElement(this->mBalances, tmp, role);
326  }
327  }
328  }
329 
330  return result;
331 }
void setMultiplicity(const C_FLOAT64 multiplicity)
size_t getCompartmentNumber() const
Definition: CChemEq.cpp:110
CCopasiVector< CChemEqElement > mModifiers
Definition: CChemEq.h:209
bool setMultiplicity(const CMetab *pMetab, C_FLOAT64 newMult, MetaboliteRole role)
Definition: CChemEq.cpp:268
virtual size_t size() const
iterator begin()
#define fatalError()
#define C_INVALID_INDEX
Definition: copasi.h:222
CCopasiVector< CChemEqElement > mProducts
Definition: CChemEq.h:204
const CCopasiVector< CChemEqElement > & getBalances() const
Definition: CChemEq.cpp:69
Definition: CMetab.h:178
void addElement(CCopasiVector< CChemEqElement > &structure, const CChemEqElement &element, CChemEq::MetaboliteRole role=CChemEq::PRODUCT)
Definition: CChemEq.cpp:180
CCopasiVector< CChemEqElement > mSubstrates
Definition: CChemEq.h:197
const CCopasiVector< CChemEqElement > & getProducts() const
Definition: CChemEq.cpp:63
std::set< const CCompartment * > getCompartments() const
Definition: CChemEq.cpp:115
#define DESTRUCTOR_TRACE
Definition: copasi.h:206
iterator end()
void setMetabolite(const std::string &key)
CChemEq(const std::string &name="NoName", const CCopasiContainer *pParent=NULL)
Definition: CChemEq.cpp:30
virtual const std::string & getKey() const
virtual void cleanup()
const bool & getReversibility() const
Definition: CChemEq.cpp:77
const C_FLOAT64 & getMultiplicity() const
virtual bool add(const CType &src)
const std::string & getMetaboliteKey() const
void cleanup()
Definition: CChemEq.cpp:52
const CCopasiVector< CChemEqElement > & getSubstrates() const
Definition: CChemEq.cpp:60
bool addMetabolite(const std::string &key, const C_FLOAT64 multiplicity, const MetaboliteRole &role)
Definition: CChemEq.cpp:80
MetaboliteRole
Definition: CChemEq.h:36
virtual ~CChemEq()
Definition: CChemEq.cpp:50
#define C_FLOAT64
Definition: copasi.h:92
CCopasiVector< CChemEqElement > mBalances
Definition: CChemEq.h:216
const CCopasiVector< CChemEqElement > & getModifiers() const
Definition: CChemEq.cpp:66
bool mReversible
Definition: CChemEq.h:190
std::ostream & operator<<(std::ostream &os, const CChemEq &d)
Definition: CChemEq.cpp:242
size_t getMolecularity(const MetaboliteRole role) const
Definition: CChemEq.cpp:210
const CCompartment * getLargestCompartment() const
Definition: CChemEq.cpp:137
void setReversibility(const bool &reversible)
Definition: CChemEq.cpp:72
#define CONSTRUCTOR_TRACE
Definition: copasi.h:202