COPASI API  4.16.103
CODEExporterBM.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) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <sstream>
16 #include <locale>
17 #include <cmath>
18 
19 #include "copasi.h"
20 
23 
24 #include "CODEExporterBM.h"
25 
26 #include "model/CModel.h"
27 #include "model/CMetab.h"
30 #include "model/CReaction.h"
31 #include "model/CMoiety.h"
32 #include "model/CChemEqElement.h"
33 #include "function/CExpression.h"
34 #include "function/CFunction.h"
35 #include "function/CFunctionDB.h"
36 #include "report/CKeyFactory.h"
42 #include "utilities/CCopasiTree.h"
43 
46 
47 #include <iostream>
48 #include <fstream>
49 #include <ctype.h>
50 
51 /**
52  ** Constructor for the exporter.
53  */
55 {}
56 bool CODEExporterBM::exportTitleData(const CCopasiDataModel* pDataModel, std::ostream & os)
57 {
58  os << "METHOD stiff" << std::endl;
59  os << std::endl;
60  os << "STARTTIME = 0" << std::endl;
61 
62  const CTrajectoryTask * pTrajectory =
63  dynamic_cast<const CTrajectoryTask *>((*const_cast<CCopasiDataModel*>(pDataModel)->getTaskList())["Time-Course"]);
64  const CTrajectoryProblem * pTrajectoryProblem =
65  dynamic_cast<const CTrajectoryProblem *>(pTrajectory->getProblem());
66 
67  os << "STOPTIME = " << pTrajectoryProblem->getDuration() << std::endl;
68  os << "DT = " << pTrajectoryProblem->getStepSize() << std::endl;
69  os << std::endl;
70 
71  return true;
72 }
73 
75 {return "TIME";}
76 
77 /**
78  ** This method adapt a Copasi name for Berkeley Madonna syntax:
79  ** Names can not start with a number.
80  ** Any other combination of letters and numbers is valid as is the underscore.
81  **/
82 std::string CODEExporterBM::translateObjectName(const std::string & realName)
83 {
84  std::locale C("C");
85  char ch;
86 
87  std::ostringstream tmpName;
88 
89  size_t realName_size = realName.size();
90  size_t i;
91 
92  ch = realName[0];
93 
94  if (!std::isalpha(ch, C))
95  {
96  tmpName << "_";
97 
98  if (std::isdigit(ch, C)) tmpName << ch;
99  }
100  else tmpName << ch;
101 
102  for (i = 1; i < realName_size; i++)
103  {
104  ch = realName[i];
105 
106  if (std::isalpha(ch, C))
107  {
108  if (std::isspace(realName[i - 1], C) && std::islower(ch, C))
109  tmpName << (char) toupper(ch);
110  else
111  tmpName << ch;
112  }
113 
114  if (std::isdigit(ch, C)) tmpName << ch;
115 
116  if (std::ispunct(ch, C))
117  switch (ch)
118  {
119  case '_':
120  tmpName << ch;
121  break;
122 
123  case '-':
124  tmpName << "_";
125  break;
126 
127  case '{':
128  tmpName << "_";
129  break;
130 
131  case '}':
132  tmpName << "_";
133  break;
134 
135  case '(':
136  tmpName << "_";
137  break;
138 
139  case ')':
140  tmpName << "_";
141  break;
142 
143  default:
144  break;
145  }
146  }
147 
148  return testName(tmpName.str());
149 }
150 /**
151  ** This method tests whether the given Berkeley Madonna name already assigned,
152  ** put the new name (in cappital letters:
153  ** all names can be upper or lower case)
154  ** in the set of assigned names
155  ** or modify the name
156  **/
157 
158 std::string CODEExporterBM::testName(const std::string & name)
159 {
160  std::locale C("C");
161  char ch;
162 
163  std::ostringstream newname, tmp;
164 
165  size_t name_size = name.size();
166  size_t i;
167 
168  for (i = 0; i < name_size; i++)
169  {
170  ch = name[i];
171 
172  if (std::isalpha(ch, C) && std::islower(ch, C))
173  tmp << (char) toupper(ch);
174  else
175  tmp << ch;
176  }
177 
178  if (NameSet.find(tmp.str()) == NameSet.end())
179  {
180  NameSet.insert(tmp.str());
181  Frequancy[tmp.str()] = 0;
182 
183  return name;
184  }
185  else
186  {
187  Frequancy[tmp.str()]++;
188  newname << name << "_" << Frequancy[tmp.str()];
189 
190  return testName(newname.str());
191  }
192 }
193 
195 {return;} // TODO
196 
197 std::string CODEExporterBM::setConcentrationName(const std::string & objName)
198 {
199  return objName + "_c";
200 }
201 
202 std::string CODEExporterBM::setODEName(const std::string & objName)
203 {
204  return "d/dt(" + objName + ")";
205 }
206 
207 bool CODEExporterBM::exportSingleObject(std::ostringstream & which,
208  const std::string & name,
209  const std::string & expression,
210  const std::string & comments)
211 {
212  which << name << " = " << expression
213  << '\t' << '\t' << "; " << comments << std::endl;
214 
215  return true;
216 }
217 
218 bool CODEExporterBM::exportSingleMetabolite(const CMetab* metab, std::string & expression, std::string & comments)
219 {
220  std::string name;
221 
222  std::ostringstream smKey;
223  smKey << "sm_" << metab->getKey();
224  name = NameMap[smKey.str()];
225 
226  switch (metab->getStatus())
227  {
228  case CModelEntity::FIXED:
229 
230  if (!exportSingleObject(fixed, name, expression, comments))
231  return false;
232 
233  break;
234 
236  {
237  if (metab->isDependent())
238  {
239  if (!exportSingleObject(assignment, name, expression, comments))
240  return false;
241  }
242  else
243  {
244  initial << "init ";
245 
246  if (!exportSingleObject(initial, name, expression, comments))
247  return false;
248  }
249 
250  break;
251  }
252 
253  case CModelEntity::ODE:
254  {
255  initial << "init ";
256 
257  if (!exportSingleObject(initial, name, expression, comments))
258  return false;
259 
260  break;
261  }
262 
264  {
265  if (!exportSingleObject(assignment, name, expression, comments))
266  return false;
267 
268  break;
269  }
270 
271  default:
272  return false;
273  break;
274  }
275 
276  return true;
277 }
278 
279 bool CODEExporterBM::exportSingleCompartment(const CCompartment* comp, std::string & expression, std::string & comments)
280 {
281  switch (comp->getStatus())
282  {
283  case CModelEntity::FIXED:
284  {
285  if (!exportSingleObject(fixed, NameMap[comp->getKey()], expression, comments))
286  return false;
287 
288  break;
289  }
290 
291  case CModelEntity::ODE:
292  {
293  initial << "init ";
294 
295  if (!exportSingleObject(initial, NameMap[comp->getKey()], expression, comments))
296  return false;
297 
298  break;
299  }
300 
302  {
303  if (!exportSingleObject(assignment, NameMap[comp->getKey()], expression, comments))
304  return false;
305 
306  break;
307  }
308 
309  default:
310  return false;
311  break;
312  }
313 
314  return true;
315 }
316 
317 bool CODEExporterBM::exportSingleModVal(const CModelValue* modval, std::string & expression, std::string & comments)
318 {
319  switch (modval->getStatus())
320  {
321  case CModelEntity::FIXED:
322  {
323  if (!exportSingleObject(fixed, NameMap[modval->getKey()], expression, comments))
324  return false;
325 
326  break;
327  }
328 
329  case CModelEntity::ODE:
330  {
331  initial << "init ";
332 
333  if (!exportSingleObject(initial, NameMap[modval->getKey()], expression, comments))
334  return false;
335 
336  break;
337  }
338 
340  {
341  if (!exportSingleObject(assignment, NameMap[modval->getKey()], expression, comments))
342  return false;
343 
344  break;
345  }
346 
347  default:
348  return false;
349  break;
350  }
351 
352  return true;
353 }
354 
355 bool CODEExporterBM::exportSingleModelEntity(const CModelEntity* tmp, std::string & expression, std::string & comments)
356 {
357 
358  std::string name;
359 
360  const CMetab* metab;
361  metab = dynamic_cast< const CMetab * >(tmp);
362 
363  if (metab)
364  {
365  std::ostringstream smKey;
366  smKey << "sm_" << metab->getKey();
367  name = NameMap[smKey.str()];
368  }
369  else
370  name = NameMap[tmp->getKey()];
371 
372  switch (tmp->getStatus())
373  {
374  case CModelEntity::FIXED:
375  {
376  if (!exportSingleObject(fixed, name, expression, comments))
377  return false;
378 
379  break;
380  }
381 
382  case CModelEntity::ODE:
383  {
384  if (!exportSingleObject(initial, name, expression, comments))
385  return false;
386 
387  break;
388  }
389 
391  {
392  if (!exportSingleObject(assignment, name, expression, comments))
393  return false;
394 
395  break;
396  }
397 
398  default:
399  return false;
400  break;
401  }
402 
403  return true;
404 }
405 
406 bool CODEExporterBM::exportSingleParameter(const CCopasiParameter* param, std::string & expression, std::string & comments)
407 {
408  if (!exportSingleObject(fixed, NameMap[param->getKey()], expression, comments)) return false;
409 
410  return true;
411 }
412 
413 bool CODEExporterBM::exportSingleODE(const CModelEntity* mentity, std::string & equation, std::string & comments)
414 {
415  std::ostringstream odeKey;
416 
417  odeKey << "ode_" << mentity->getKey();
418 
419  if (!exportSingleObject(ode, NameMap[odeKey.str()], equation, comments)) return false;
420 
421  return true;
422 }
423 
425 {
426  std::string str1;
427  str1 = func->getRoot()->buildBerkeleyMadonnaString();
428  return str1;
429 }
430 
432 {
433  std::string str1;
434  str1 = tmp->getRoot()->buildBerkeleyMadonnaString();
435  return str1;
436 }
437 
439 {
440  std::ostringstream localKey;
441  localKey << reac->getKey() << "_root_func";
442  return NameMap[localKey.str()];
443 }
444 
445 std::string CODEExporterBM::exportTitleString(const size_t tmp)
446 {
447  switch (tmp)
448  {
449  case INITIAL:
450  return "{Initial values:}";
451 
452  case FIXED:
453  return "{Fixed Model Entities: }";
454 
455  case ASSIGNMENT:
456  return "{Assignment Model Entities: }";
457 
458  case FUNCTIONS:
459  return "{Kinetics: }";
460 
461  case HEADERS:
462  return " ";
463 
464  case ODEs:
465  return "{Equations:}";
466 
467  default:
468  return " ";
469  }
470 }
Header file of class CExpression.
virtual std::string getDisplayFunctionString(CFunction *func)
std::string testName(const std::string &name)
virtual bool exportSingleODE(const CModelEntity *mentity, std::string &equation, std::string &comments)
virtual bool exportSingleModelEntity(const CModelEntity *tmp, std::string &expression, std::string &comments)
CCopasiProblem * getProblem()
virtual std::string translateTimeVariableName()
virtual const std::string & getKey() const
Definition: CReaction.cpp:190
virtual std::string exportTitleString(const size_t tmp)
virtual void setReservedNames()
std::ostringstream initial
Definition: CODEExporter.h:155
Definition: CMetab.h:178
std::string setODEName(const std::string &objName)
std::map< std::string, size_t > Frequancy
const C_FLOAT64 & getStepSize() const
std::ostringstream ode
Definition: CODEExporter.h:160
virtual bool exportSingleModVal(const CModelValue *modval, std::string &expression, std::string &comments)
virtual std::string KineticFunction2ODEmember(const CReaction *reac)
const C_FLOAT64 & getDuration() const
virtual const std::string & getKey() const
virtual const std::string & getKey() const
virtual bool exportSingleCompartment(const CCompartment *comp, std::string &expression, std::string &comments)
virtual std::string getDisplayExpressionString(CExpression *tmp)
virtual bool exportTitleData(const CCopasiDataModel *pDataModel, std::ostream &os)
bool isDependent() const
Definition: CMetab.cpp:989
std::set< std::string > NameSet
std::string setConcentrationName(const std::string &objName)
std::map< std::string, std::string > NameMap
Definition: CODEExporter.h:151
virtual std::string translateObjectName(const std::string &realName)
std::ostringstream assignment
Definition: CODEExporter.h:157
The class for handling a chemical kinetic function.
Definition: CFunction.h:29
std::string buildBerkeleyMadonnaString() const
std::ostringstream fixed
Definition: CODEExporter.h:156
const CModelEntity::Status & getStatus() const
virtual bool exportSingleParameter(const CCopasiParameter *param, std::string &expression, std::string &comments)
virtual bool exportSingleObject(std::ostringstream &which, const std::string &name, const std::string &expression, const std::string &comments)
virtual bool exportSingleMetabolite(const CMetab *metab, std::string &expression, std::string &comments)
CEvaluationNode * getRoot()