COPASI API  4.16.103
CMMLOutput.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/model/CMMLOutput.cpp,v $
3 // $Revision: 1.12 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/05/16 23:14:34 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 #include <sstream>
24 
25 #include "CMMLOutput.h"
27 #include "model/CModel.h"
28 #include "function/CExpression.h"
29 #include "report/CKeyFactory.h"
31 
33 {}
34 
35 #include "utilities/copasimathml.h"
36 
37 void CMMLOutput::writeLHS(std::ostream & out,
38  const std::string & metabName,
39  const std::string & compName, size_t l)
40 {
41  out << SPC(l + 0) << "<mfrac>" << std::endl;
42  out << SPC(l + 1) << "<mrow>" << std::endl;
43  out << SPC(l + 2) << "<mo>d</mo>" << std::endl;
44  out << SPC(l + 2) << "<mfenced>" << std::endl;
45  out << SPC(l + 3) << "<mrow>" << std::endl;
46  //out << SPC(l + 4) << "<mi>" << CMathMl::fixName(metabName) << "</mi>" << std::endl;
47  out << SPC(l + 4) << "<mi>[" << CMathMl::fixName(metabName) << "]</mi>" << std::endl;
48  out << SPC(l + 4) << "<mo>" << "&CenterDot;" << "</mo>" << std::endl;
49  out << SPC(l + 4) << "<msub><mi>V</mi><mi>" << CMathMl::fixName(compName) << "</mi></msub>" << std::endl;
50 
51  out << SPC(l + 3) << "</mrow>" << std::endl;
52 
53  out << SPC(l + 2) << "</mfenced>" << std::endl;
54  out << SPC(l + 1) << "</mrow>" << std::endl;
55  out << SPC(l + 1) << "<mrow>" << std::endl;
56  out << SPC(l + 2) << "<mo>d</mo><mi>t</mi>" << std::endl;
57  out << SPC(l + 1) << "</mrow>" << std::endl;
58  out << SPC(l + 0) << "</mfrac>" << std::endl;
59 }
60 
61 void CMMLOutput::writeRHS(std::ostream & out,
62  const CMetab* pMetab, const CReaction* pReac,
63  bool numbers, bool expand, bool expandFull,
64  size_t l)
65 {
66  if (!pMetab)
67  {
68  out << SPC(l + 0) << "Error: invalid metabolite" << std::endl;
69  return;
70  }
71 
72  if (!pReac)
73  {
74  out << SPC(l + 0) << "Error: invalid reaction" << std::endl;
75  return;
76  }
77 
78  const CCopasiVector < CChemEqElement > & balances = pReac->getChemEq().getBalances();
79 
80  C_FLOAT64 balance = 0;
81 
82  size_t i, imax = balances.size();
83 
84  for (i = 0; i < imax; ++i)
85  {
86  if (balances[i]->getMetaboliteKey() == pMetab->getKey())
87  {
88  balance = balances[i]->getMultiplicity();
89  break;
90  }
91  }
92 
93  if (0 == balance) return;
94 
95  out << SPC(l + 0) << "<mrow>" << std::endl;
96 
97  //balance
98  if (balance == 1.0)
99  {
100  out << SPC(l + 1) << "<mo>" << "+" << "</mo>" << std::endl;
101  }
102  else if (balance == -1.0)
103  {
104  out << SPC(l + 1) << "<mo>" << "-" << "</mo>" << std::endl;
105  }
106  else if (balance < 0.0)
107  {
108  out << SPC(l + 1) << "<mo>" << "-" << "</mo><mn>" << -balance << "</mn>"
109  << "<mo>" << "&CenterDot;" << "</mo>" << std::endl;
110  }
111  else // >0
112  {
113  out << SPC(l + 1) << "<mo>" << "+" << "</mo><mn>" << balance << "</mn>"
114  << "<mo>" << "&CenterDot;" << "</mo>" << std::endl;
115  }
116 
117  //compartment volume?
118  if (pReac->getCompartmentNumber() == 1)
119  {
120  std::string compName = pMetab->getCompartment()->getObjectName();
121  out << SPC(l + 1) << "<msub><mi>V</mi><mi>" << CMathMl::fixName(compName)
122  << "</mi></msub>" << std::endl;
123  out << SPC(l + 1) << "<mo>" << "&CenterDot;" << "</mo>" << std::endl;
124  }
125 
126  //kinetic function
127  if (pReac->getFunction())
128  {
129  std::vector<std::vector<std::string> > params;
130  createParameterMapping(pReac, params, numbers);
131 
132  if (expand) out << SPC(l + 1) << "<mfenced>" << std::endl;
133 
134  out << pReac->getFunction()->writeMathML(params, expand, expandFull);
135 
136  if (expand) out << SPC(l + 1) << "</mfenced>" << std::endl;
137  }
138 
139  out << SPC(l + 0) << "</mrow>" << std::endl;
140 }
141 
143  std::vector<std::vector<std::string> > & params,
144  bool numbers)
145 {
146  assert(pReac);
147  assert(pReac->getFunction());
148 
149  const CFunctionParameters & functionParams = pReac->getFunctionParameters();
150  size_t j, jmax;
151  size_t i, imax = functionParams.size();
152  params.resize(imax);
153 
154  for (i = 0; i < imax; ++i)
155  {
156  params[i].resize(1);
157 
158  std::string name;
159 
160  //std::ostringstream number;
161  switch (functionParams[i]->getUsage())
162  {
166 
167  if (functionParams[i]->getType() == CFunctionParameter::FLOAT64)
168  {
170 
171  if (pObject != NULL)
172  {
173  name = pObject->getObjectDisplayName();
174  }
175  else
176  {
177  name = "unknown";
178  }
179 
180  //params[i][0] = "<mi>"+ CMathMl::fixName(name)+"</mi>";
181  params[i][0] = "<mi>[" + CMathMl::fixName(name) + "]</mi>";
182  }
183  else if (functionParams[i]->getType() == CFunctionParameter::VFLOAT64)
184  {
185  jmax = pReac->getParameterMappings()[i].size();
186  params[i].resize(jmax);
187 
188  for (j = 0; j < jmax; ++j)
189  {
190  name = CCopasiRootContainer::getKeyFactory()->get(pReac->getParameterMappings()[i][j])->getObjectDisplayName();
191  //params[i][j] = "<mi>"+ CMathMl::fixName(name)+"</mi>";
192  params[i][j] = "<mi>[" + CMathMl::fixName(name) + "]</mi>";
193  }
194  }
195  else assert(false);
196 
197  break;
198 
200 
201  if (pReac->isLocalParameter(i))
202  {
203  if (numbers)
204  {
205  std::ostringstream number;
206  number << pReac->getParameterValue(functionParams[i]->getObjectName());
207  params[i][0] = "<mn>" + number.str() + "</mn>";
208  }
209  else
210  {
211  name = CCopasiRootContainer::getKeyFactory()->get(pReac->getParameterMappings()[i][0])->getObjectName();
212  //params[i][0] = "<mi>" + CMathMl::fixName(name) + "</mi>";
213  params[i][0] = "<msub><mi>" + CMathMl::fixName(name) + "</mi><mi>("
214  + CMathMl::fixName(pReac->getObjectName()) + ")</mi></msub>";
215  }
216  }
217  else
218  {
219  name = CCopasiRootContainer::getKeyFactory()->get(pReac->getParameterMappings()[i][0])->getObjectName();
220  params[i][0] = "<mi>" + CMathMl::fixName(name) + "</mi>";
221  //params[i][0] = "<mi>ggg</mi>";
222  }
223 
224  break;
225 
227  name = CCopasiRootContainer::getKeyFactory()->get(pReac->getParameterMappings()[i][0])->getObjectName();
228  params[i][0] = "<msub><mi>V</mi><mi>" + CMathMl::fixName(name)
229  + "</mi></msub>";
230  break;
231 
233  params[i][0] = "<mi>time</mi>";
234  break;
235 
236  default:
237  break;
238  }
239  }
240 }
241 
242 void CMMLOutput::writeLHS_ModelValue(std::ostream & out,
243  const std::string & valueName, size_t l)
244 {
245  out << SPC(l + 0) << "<mfrac>" << std::endl;
246  out << SPC(l + 1) << "<mrow>" << std::endl;
247  out << SPC(l + 2) << "<mo>d</mo>" << std::endl;
248  out << SPC(l + 2) << "<mi>" << CMathMl::fixName(valueName) << "</mi>" << std::endl;
249  out << SPC(l + 1) << "</mrow>" << std::endl;
250  out << SPC(l + 1) << "<mrow>" << std::endl;
251  out << SPC(l + 2) << "<mo>d</mo><mi>t</mi>" << std::endl;
252  out << SPC(l + 1) << "</mrow>" << std::endl;
253  out << SPC(l + 0) << "</mfrac>" << std::endl;
254 }
255 
256 void CMMLOutput::writeRHS_ModelEntity(std::ostream & out,
257  const CModelEntity* pEntity,
258  bool expandFull, size_t l)
259 {
260  if (!pEntity)
261  {
262  out << SPC(l + 0) << "Error: invalid model entity" << std::endl;
263  return;
264  }
265 
266  if (!pEntity->getExpressionPtr())
267  {
268  out << SPC(l + 0) << "Error: no expression" << std::endl;
269  return;
270  }
271 
272  out << SPC(l + 0) << "<mrow>" << std::endl;
273 
274  pEntity->getExpressionPtr()->writeMathML(out, expandFull, l + 1);
275 
276  out << SPC(l + 0) << "</mrow>" << std::endl;
277 }
278 
279 void CMMLOutput::writeDifferentialEquations(std::ostream & mml, CModel * model, bool localParameterNumbers, bool expand, bool expandFull)
280 {
281  bool hasContents = false;
282  //mml.str("");
283 
284  size_t l = 0;
285  mml << SPC(l) << "<mtable>" << std::endl;
286 
287  //write equations for compartments
288  size_t i, imax = model->getCompartments().size();
289 
290  for (i = 0; i < imax; i++)
291  {
292  if (model->getCompartments()[i]->getStatus() == CModelEntity::ODE)
293  {
294  mml << SPC(l + 1) << "<mtr>" << std::endl;
295 
296  //first column (lhs)
297  mml << SPC(l + 2) << "<mtd>" << std::endl;
298 
299  mml << SPC(l + 3) << "<mfrac>" << std::endl;
300  mml << SPC(l + 4) << "<mrow>" << std::endl;
301  mml << SPC(l + 5) << "<mo>d</mo>" << std::endl;
302 
303  mml << SPC(l + 5) << "<msub><mi>V</mi><mi>"
305  << "</mi></msub>" << std::endl;
306 
307  mml << SPC(l + 4) << "</mrow>" << std::endl;
308  mml << SPC(l + 3) << "<mrow>" << std::endl;
309  mml << SPC(l + 2) << "<mo>d</mo><mi>t</mi>" << std::endl;
310  mml << SPC(l + 1) << "</mrow>" << std::endl;
311  mml << SPC(l + 0) << "</mfrac>" << std::endl;
312 
313  mml << SPC(l + 2) << "</mtd>" << std::endl;
314 
315  //second column ("=")
316  mml << SPC(l + 2) << "<mtd>" << std::endl;
317  mml << SPC(l + 3) << "<mo>=</mo>" << std::endl;
318  mml << SPC(l + 2) << "</mtd>" << std::endl;
319 
320  //third column (rhs)
321  mml << SPC(l + 2) << "<mtd columnalign='left'>" << std::endl;
322 
323  writeRHS_ModelEntity(mml, model->getCompartments()[i],
324  expandFull, l + 3);
325  mml << SPC(l + 2) << "</mtd>" << std::endl;
326 
327  mml << SPC(l + 1) << "</mtr>" << std::endl;
328  }
329  else if (model->getCompartments()[i]->getStatus() == CModelEntity::ASSIGNMENT)
330  {
331  mml << SPC(l + 1) << "<mtr>" << std::endl;
332 
333  //first column (lhs)
334  mml << SPC(l + 2) << "<mtd>" << std::endl;
335  mml << SPC(l + 3) << "<msub><mi>V</mi><mi>"
337  << "</mi></msub>" << std::endl;
338  mml << SPC(l + 2) << "</mtd>" << std::endl;
339 
340  //second column ("=")
341  mml << SPC(l + 2) << "<mtd>" << std::endl;
342  mml << SPC(l + 3) << "<mo>=</mo>" << std::endl;
343  mml << SPC(l + 2) << "</mtd>" << std::endl;
344 
345  //third column (rhs)
346  mml << SPC(l + 2) << "<mtd columnalign='left'>" << std::endl;
347  writeRHS_ModelEntity(mml, model->getCompartments()[i],
348  expandFull, l + 3);
349  mml << SPC(l + 2) << "</mtd>" << std::endl;
350 
351  mml << SPC(l + 1) << "</mtr>" << std::endl;
352  }
353  }
354 
355  //write equations for metabs
356  imax = model->getMetabolites().size();
357 
358  for (i = 0; i < imax; i++)
359  {
360  if (model->getMetabolites()[i]->getStatus() == CModelEntity::REACTIONS)
361  {
362 
363  std::set<std::string> reacKeys = listReactionsForMetab(model, model->getMetabolites()[i]->getKey());
364  std::set<std::string>::const_iterator it, itEnd = reacKeys.end();
365 
366  for (it = reacKeys.begin(); it != itEnd; ++it)
367  {
368  hasContents = true;
369 
370  mml << SPC(l + 1) << "<mtr>" << std::endl;
371 
372  //first column (lhs)
373  mml << SPC(l + 2) << "<mtd>" << std::endl;
374 
375  if (it == reacKeys.begin())
376  writeLHS(mml, model->getMetabolites()[i]->getObjectDisplayName(),
377  model->getMetabolites()[i]->getCompartment()->getObjectName(), l + 3);
378 
379  mml << SPC(l + 2) << "</mtd>" << std::endl;
380 
381  //second column ("=")
382  mml << SPC(l + 2) << "<mtd>" << std::endl;
383 
384  if (it == reacKeys.begin())
385  mml << SPC(l + 3) << "<mo>=</mo>" << std::endl;
386 
387  mml << SPC(l + 2) << "</mtd>" << std::endl;
388 
389  //third column (rhs)
390  mml << SPC(l + 2) << "<mtd columnalign='left'>" << std::endl;
391  writeRHS(mml, model->getMetabolites()[i],
392  dynamic_cast<CReaction*>(CCopasiRootContainer::getKeyFactory()->get(*it)) ,
393  localParameterNumbers, expand, expandFull, l + 3);
394  mml << SPC(l + 2) << "</mtd>" << std::endl;
395 
396  mml << SPC(l + 1) << "</mtr>" << std::endl;
397  }
398  }
399  else if (model->getMetabolites()[i]->getStatus() == CModelEntity::ODE)
400  {
401  mml << SPC(l + 1) << "<mtr>" << std::endl;
402 
403  //first column (lhs)
404  mml << SPC(l + 2) << "<mtd>" << std::endl;
405  writeLHS(mml, model->getMetabolites()[i]->getObjectDisplayName(),
406  model->getMetabolites()[i]->getCompartment()->getObjectName(), l + 3);
407  mml << SPC(l + 2) << "</mtd>" << std::endl;
408 
409  //second column ("=")
410  mml << SPC(l + 2) << "<mtd>" << std::endl;
411  mml << SPC(l + 3) << "<mo>=</mo>" << std::endl;
412  mml << SPC(l + 2) << "</mtd>" << std::endl;
413 
414  //third column (rhs)
415  mml << SPC(l + 2) << "<mtd columnalign='left'>" << std::endl;
416 
417  std::string compName = model->getMetabolites()[i]->getCompartment()->getObjectName();
418  mml << SPC(l + 3) << "<msub><mi>V</mi><mi>" << CMathMl::fixName(compName)
419  << "</mi></msub>" << std::endl;
420  mml << SPC(l + 3) << "<mo>" << "&CenterDot;" << "</mo>" << std::endl;
421 
422  writeRHS_ModelEntity(mml, model->getMetabolites()[i],
423  expandFull, l + 3);
424  mml << SPC(l + 2) << "</mtd>" << std::endl;
425 
426  mml << SPC(l + 1) << "</mtr>" << std::endl;
427  }
428  else if (model->getMetabolites()[i]->getStatus() == CModelEntity::ASSIGNMENT)
429  {
430  mml << SPC(l + 1) << "<mtr>" << std::endl;
431 
432  //first column (lhs)
433  mml << SPC(l + 2) << "<mtd>" << std::endl;
434  mml << SPC(l + 3) << "<mi>" << CMathMl::fixName("[" + model->getMetabolites()[i]->getObjectName() + "]") << "</mi>" << std::endl;
435  mml << SPC(l + 2) << "</mtd>" << std::endl;
436 
437  //second column ("=")
438  mml << SPC(l + 2) << "<mtd>" << std::endl;
439  mml << SPC(l + 3) << "<mo>=</mo>" << std::endl;
440  mml << SPC(l + 2) << "</mtd>" << std::endl;
441 
442  //third column (rhs)
443  mml << SPC(l + 2) << "<mtd columnalign='left'>" << std::endl;
444  writeRHS_ModelEntity(mml, model->getMetabolites()[i],
445  expandFull, l + 3);
446  mml << SPC(l + 2) << "</mtd>" << std::endl;
447 
448  mml << SPC(l + 1) << "</mtr>" << std::endl;
449  }
450  }
451 
452  //write differential equations for model values
453  imax = model->getModelValues().size();
454 
455  for (i = 0; i < imax; ++i)
456  if (model->getModelValues()[i]->getStatus() == CModelEntity::ODE)
457  {
458  hasContents = true;
459  mml << SPC(l + 1) << "<mtr>" << std::endl;
460 
461  //first column (lhs)
462  mml << SPC(l + 2) << "<mtd columnalign='right'>" << std::endl;
463  writeLHS_ModelValue(mml, model->getModelValues()[i]->getObjectName(), l + 3);
464  mml << SPC(l + 2) << "</mtd>" << std::endl;
465 
466  //second column ("=")
467  mml << SPC(l + 2) << "<mtd>" << std::endl;
468  mml << SPC(l + 3) << "<mo>=</mo>" << std::endl;
469  mml << SPC(l + 2) << "</mtd>" << std::endl;
470 
471  //third column (rhs)
472  mml << SPC(l + 2) << "<mtd columnalign='left'>" << std::endl;
473  writeRHS_ModelEntity(mml, model->getModelValues()[i],
474  expandFull, l + 3);
475  mml << SPC(l + 2) << "</mtd>" << std::endl;
476 
477  mml << SPC(l + 1) << "</mtr>" << std::endl;
478  }
479 
480  //write assignment rules
481  imax = model->getModelValues().size();
482 
483  for (i = 0; i < imax; ++i)
484  if (model->getModelValues()[i]->getStatus() == CModelEntity::ASSIGNMENT)
485  {
486  hasContents = true;
487  mml << SPC(l + 1) << "<mtr>" << std::endl;
488 
489  //first column (lhs)
490  mml << SPC(l + 2) << "<mtd columnalign='right'>" << std::endl;
491  mml << SPC(l + 3) << "<mi>" << CMathMl::fixName(model->getModelValues()[i]->getObjectName()) << "</mi>" << std::endl;
492  //writeLHS_ModelValue(mml, model->getModelValues()[i]->getObjectName(), l + 3);
493  mml << SPC(l + 2) << "</mtd>" << std::endl;
494 
495  //second column ("=")
496  mml << SPC(l + 2) << "<mtd>" << std::endl;
497  mml << SPC(l + 3) << "<mo>=</mo>" << std::endl;
498  mml << SPC(l + 2) << "</mtd>" << std::endl;
499 
500  //third column (rhs)
501  mml << SPC(l + 2) << "<mtd columnalign='left'>" << std::endl;
502  writeRHS_ModelEntity(mml, model->getModelValues()[i],
503  expandFull, l + 3);
504  mml << SPC(l + 2) << "</mtd>" << std::endl;
505 
506  mml << SPC(l + 1) << "</mtr>" << std::endl;
507  }
508 
509  mml << SPC(l) << "</mtable>" << std::endl;
510 }
511 
512 std::set<std::string> CMMLOutput::listReactionsForMetab(const CModel* model,
513  const std::string & key)
514 {
515  std::set<std::string> Keys;
516  const CCopasiVectorN<CReaction> & Reactions = model->getReactions();
517  size_t j, jmax = Reactions.size();
518 
519  for (j = 0; j < jmax; j++)
520  {
521  const CCopasiVector <CChemEqElement> &Balances = Reactions[j]->getChemEq().getBalances();
522  size_t i, imax = Balances.size();
523 
524  for (i = 0; i < imax; i++)
525  if (key == Balances[i]->getMetaboliteKey() && Balances[i]->getMultiplicity() != 0)
526  {
527  Keys.insert(Reactions[j]->getKey());
528  break;
529  }
530  }
531 
532  return Keys;
533 }
const CExpression * getExpressionPtr() const
Header file of class CExpression.
static void writeRHS_ModelEntity(std::ostream &out, const CModelEntity *pEntity, bool expandFull, size_t l)
Definition: CMMLOutput.cpp:256
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
const CCopasiVector< CMetab > & getMetabolites() const
Definition: CModel.cpp:1051
const std::string & getObjectName() const
const CCopasiVectorN< CModelValue > & getModelValues() const
Definition: CModel.cpp:1060
virtual size_t size() const
CCopasiObject * get(const std::string &key)
static std::string fixName(const std::string &name)
Definition: copasimathml.h:33
bool isLocalParameter(const size_t &index) const
Definition: CReaction.cpp:449
const CCopasiVector< CChemEqElement > & getBalances() const
Definition: CChemEq.cpp:69
Definition: CMetab.h:178
static void writeLHS(std::ostream &out, const std::string &metabName, const std::string &compName, size_t l)
Definition: CMMLOutput.cpp:37
virtual const std::string & getKey() const
const CFunction * getFunction() const
Definition: CReaction.cpp:252
static void createParameterMapping(const CReaction *pReac, std::vector< std::vector< std::string > > &params, bool numbers)
Definition: CMMLOutput.cpp:142
const CFunctionParameters & getFunctionParameters() const
Definition: CReaction.cpp:576
#define SPC(level)
virtual const std::string & getKey() const
virtual void writeMathML(std::ostream &out, size_t l=0) const
Definition: CFunction.cpp:389
static std::set< std::string > listReactionsForMetab(const CModel *model, const std::string &key)
Definition: CMMLOutput.cpp:512
static void writeRHS(std::ostream &out, const CMetab *pMetab, const CReaction *pReac, bool numbers, bool expand, bool expandFull, size_t l)
Definition: CMMLOutput.cpp:61
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
static CKeyFactory * getKeyFactory()
#define C_FLOAT64
Definition: copasi.h:92
size_t getCompartmentNumber() const
Definition: CReaction.cpp:873
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
void writeMathML(std::ostream &out, bool fullExpand, size_t l) const
Definition: CModel.h:50
const std::vector< std::vector< std::string > > & getParameterMappings() const
Definition: CReaction.h:285
static void writeDifferentialEquations(std::ostream &mml, CModel *model, bool localParameterNumbers, bool expand, bool expandFull)
Definition: CMMLOutput.cpp:279
const CCompartment * getCompartment() const
Definition: CMetab.cpp:222
const CChemEq & getChemEq() const
Definition: CReaction.cpp:223
const C_FLOAT64 & getParameterValue(const std::string &parameterName) const
Definition: CReaction.cpp:326
static void writeLHS_ModelValue(std::ostream &out, const std::string &valueName, size_t l)
Definition: CMMLOutput.cpp:242