COPASI API  4.16.103
Macros | Functions
CSBMLExporter.cpp File Reference
#include <cmath>
#include "CSBMLExporter.h"
#include "SBMLUtils.h"
#include "SBMLImporter.h"
#include "utilities/CCopasiException.h"
#include "sbml/SBMLDocument.h"
#include "sbml/Compartment.h"
#include "sbml/Model.h"
#include "sbml/Species.h"
#include "sbml/Parameter.h"
#include "sbml/Reaction.h"
#include "sbml/KineticLaw.h"
#include "sbml/SBMLWriter.h"
#include "sbml/SpeciesReference.h"
#include "sbml/math/ASTNode.h"
#include "sbml/annotation/ModelHistory.h"
#include "sbml/annotation/CVTerm.h"
#include "sbml/SBMLErrorLog.h"
#include "sbml/SBMLError.h"
#include "CopasiDataModel/CCopasiDataModel.h"
#include "SBMLIncompatibility.h"
#include "model/CCompartment.h"
#include "model/CModel.h"
#include "model/CEvent.h"
#include "model/CMetab.h"
#include "function/CExpression.h"
#include "function/CEvaluationNode.h"
#include "model/CAnnotation.h"
#include "model/CReaction.h"
#include "utilities/CCopasiParameter.h"
#include "model/CModelValue.h"
#include "function/CFunction.h"
#include "report/CKeyFactory.h"
#include "ConverterASTNode.h"
#include "utilities/CCopasiTree.h"
#include "model/CChemEqElement.h"
#include "utilities/CVersion.h"
#include "sbml/Trigger.h"
#include "sbml/Event.h"
#include "sbml/EventAssignment.h"
#include <sbml/xml/XMLInputStream.h>
#include "compareExpressions/compare_utilities.h"
#include "MIRIAM/CRDFUtilities.h"
#include "MIRIAM/CModelMIRIAMInfo.h"
#include "MIRIAM/CReference.h"
#include "MIRIAM/CBiologicalDescription.h"
#include "MIRIAM/CConstants.h"
#include "MIRIAM/CCreator.h"
#include "MIRIAM/CModified.h"
#include "MIRIAM/CRDFPredicate.h"
#include "layout/CListOfLayouts.h"
#include "copasi/report/CCopasiRootContainer.h"
#include "commandline/CLocaleString.h"

Go to the source code of this file.

Macros

#define INIT_DEFAULTS(element)
 
#define USE_LAYOUT   1
 

Functions

void addInitialAssignmentsToModel (SBMLDocument *doc, std::map< const std::string, Parameter * > &initialValueMap, const CCopasiDataModel &dataModel)
 
std::string addRateOfIfItDoesNotExist (SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap, const char *id)
 
void addToInitialValueMap (std::map< const std::string, Parameter * > *initialMap, const CCopasiObject *pObject, const CCopasiObject *pObjectParent, int sbmlLevel, int sbmlVersion)
 
void adjustNames (ASTNode *node, SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap)
 
std::string convertExpression (const std::string &expression, const std::map< const std::string, Parameter * > &initialValueMap)
 
std::string createFunctionDefinitonForURI (SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap, const char *id, const std::string &sNamespace, const std::string &elementName, const std::string &definition, const std::string &lambda)
 
std::string getAnnotationStringFor (const CCopasiObject *pObjectParent)
 
std::string getUserDefinedFuctionForName (SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap, const char *id)
 
std::string hasFunctionDefinitionForURI (SBMLDocument *pSBMLDocument, const std::string &sNamespace, const std::string &elementName, const std::string &definition)
 
void removeStickyTagFromElements (SBMLDocument *pSBMLDocument)
 

Macro Definition Documentation

#define INIT_DEFAULTS (   element)
#define USE_LAYOUT   1

Definition at line 17 of file CSBMLExporter.cpp.

Function Documentation

void addInitialAssignmentsToModel ( SBMLDocument *  doc,
std::map< const std::string, Parameter * > &  initialValueMap,
const CCopasiDataModel dataModel 
)

Definition at line 3369 of file CSBMLExporter.cpp.

References CCopasiContainer::getObject(), and CCopasiObject::getObjectParent().

Referenced by CSBMLExporter::createSBMLDocument().

3372 {
3373  if (doc == NULL || doc->getModel() == NULL || initialValueMap.size() == 0)
3374  return;
3375 
3376  std::map<const std::string, Parameter*>::const_iterator it;
3377 
3378  for (it = initialValueMap.begin(); it != initialValueMap.end(); ++it)
3379  {
3380  Parameter* param = it->second;
3381  // add parameter to model
3382  int result = doc->getModel()->addParameter(param);
3383  doc->getModel()->getParameter(param->getId())->setUserData((void*)"1");
3384 
3385  const CCopasiObject *obj = static_cast<const CCopasiObject *>(dataModel.getObject(it->first));
3386  const std::string &sbmlId = (static_cast<const CModelEntity*>(obj->getObjectParent()))->getSBMLId();
3387 
3388  // create an initial assignment for the newly created initial quantity
3389  // to synchronize it with the original element.
3390  InitialAssignment* ia = doc->getModel()->createInitialAssignment();
3391  ia->setSymbol(it->second->getId());
3392  ia->setMath(SBML_parseFormula(sbmlId.c_str()));
3393  ia->setUserData((void*)"1");
3394 
3395  delete param;
3396  }
3397 }
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
CCopasiContainer * getObjectParent() const
std::string addRateOfIfItDoesNotExist ( SBMLDocument *  pSBMLDocument,
std::map< std::string, const SBase * > &  idMap,
const char *  id 
)

Definition at line 167 of file CSBMLExporter.cpp.

References createFunctionDefinitonForURI(), and hasFunctionDefinitionForURI().

Referenced by getUserDefinedFuctionForName(), and CSBMLExporter::replaceSpeciesReferences().

170 {
171  std::string newId = hasFunctionDefinitionForURI(pSBMLDocument,
172  "http://sbml.org/annotations/symbols",
173  "symbols",
174  "http://en.wikipedia.org/wiki/Derivative");
175 
176  if (!newId.empty()) return newId;
177 
179  pSBMLDocument,
180  idMap,
181  id,
182  "http://sbml.org/annotations/symbols",
183  "symbols",
184  "http://en.wikipedia.org/wiki/Derivative",
185  "lambda(a,NaN)"
186  );
187  return newId;
188 }
std::string createFunctionDefinitonForURI(SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap, const char *id, const std::string &sNamespace, const std::string &elementName, const std::string &definition, const std::string &lambda)
std::string hasFunctionDefinitionForURI(SBMLDocument *pSBMLDocument, const std::string &sNamespace, const std::string &elementName, const std::string &definition)
void addToInitialValueMap ( std::map< const std::string, Parameter * > *  initialMap,
const CCopasiObject pObject,
const CCopasiObject pObjectParent,
int  sbmlLevel,
int  sbmlVersion 
)

Definition at line 2347 of file CSBMLExporter.cpp.

References C_FLOAT64, getAnnotationStringFor(), CCopasiObject::getCN(), CCopasiObject::getKey(), CCopasiObject::getObjectName(), CCopasiObject::getValuePointer(), and CCopasiObject::isValueDbl().

Referenced by CSBMLExporter::checkForUnsupportedObjectReferences().

2352 {
2353  if (initialMap == NULL || pObject == NULL || pObjectParent == NULL)
2354  return;
2355 
2356  const std::string& cn = pObject->getCN();
2357 
2358  if ((*initialMap)[cn] != NULL)
2359  {
2360  // already have the initial assignment no need to add another one
2361  return;
2362  }
2363 
2364  Parameter* initial = new Parameter(sbmlLevel, sbmlVersion);
2365  initial->setAnnotation(getAnnotationStringFor(pObjectParent));
2366  initial->initDefaults();
2367  initial->setId(pObjectParent->getKey());
2368  initial->setName("Initial for " + pObjectParent->getObjectName());
2369 
2370  if (pObject->isValueDbl())
2371  initial->setValue(*((const C_FLOAT64*) pObject->getValuePointer()));
2372 
2373  (*initialMap) [cn] = initial;
2374 }
virtual CCopasiObjectName getCN() const
const std::string & getObjectName() const
std::string getAnnotationStringFor(const CCopasiObject *pObjectParent)
virtual const std::string & getKey() const
#define C_FLOAT64
Definition: copasi.h:92
bool isValueDbl() const
virtual void * getValuePointer() const
void adjustNames ( ASTNode *  node,
SBMLDocument *  pSBMLDocument,
std::map< std::string, const SBase * > &  idMap 
)

Definition at line 7054 of file CSBMLExporter.cpp.

References getUserDefinedFuctionForName().

Referenced by CSBMLExporter::convertToASTNode().

7055 {
7056  if (node == NULL)
7057  return;
7058 
7059  if (node->isNumber() || node->isConstant() || node->isName())
7060  return;
7061 
7062  if (node->getType() == AST_FUNCTION)
7063  {
7064  std::string adjustedName = getUserDefinedFuctionForName(
7065  pSBMLDocument,
7066  idMap,
7067  node->getName()
7068  );
7069  node->setName(adjustedName .c_str());
7070  }
7071 
7072  for (unsigned int i = 0; i < node->getNumChildren(); ++i)
7073  {
7074  adjustNames(node->getChild(i), pSBMLDocument, idMap);
7075  }
7076 }
std::string getUserDefinedFuctionForName(SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap, const char *id)
void adjustNames(ASTNode *node, SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap)
std::string convertExpression ( const std::string &  expression,
const std::map< const std::string, Parameter * > &  initialValueMap 
)

Definition at line 1810 of file CSBMLExporter.cpp.

Referenced by CSBMLExporter::createEvent(), CSBMLExporter::createRule(), and CSBMLExporter::exportEventAssignments().

1811 {
1812  if (initialValueMap.empty())
1813  return expression;
1814 
1815  std::string result = expression;
1816  std::map<const std::string, Parameter*>::const_iterator it;
1817 
1818  for (it = initialValueMap.begin(); it != initialValueMap.end(); ++it)
1819  {
1820  size_t length = it->first.length();
1821  size_t start;
1822 
1823  while ((start = result.find(it->first)) != std::string::npos)
1824  {
1825  result.replace(start, length, it->second->getId());
1826  }
1827  }
1828 
1829  return result;
1830 }
std::string createFunctionDefinitonForURI ( SBMLDocument *  pSBMLDocument,
std::map< std::string, const SBase * > &  idMap,
const char *  id,
const std::string &  sNamespace,
const std::string &  elementName,
const std::string &  definition,
const std::string &  lambda 
)

Definition at line 139 of file CSBMLExporter.cpp.

References CSBMLExporter::createUniqueId().

Referenced by addRateOfIfItDoesNotExist(), and getUserDefinedFuctionForName().

146 {
147  if (pSBMLDocument == NULL || pSBMLDocument->getModel() == NULL) return id;
148 
149  std::string newId = CSBMLExporter::createUniqueId(idMap, id, false);
150 
151  FunctionDefinition *def = pSBMLDocument->getModel()->createFunctionDefinition();
152  def -> setId(newId);
153  def -> setMath(SBML_parseFormula(lambda.c_str()));
154 
155  std::stringstream annotation;
156  std::string annotElement = pSBMLDocument->getLevel() == 1 ? "annotations" : "annotation";
157  annotation << "<" << annotElement << "> <" << elementName
158  << " xmlns='" << sNamespace
159  << "' definition='" << definition
160  << "' /> </" << annotElement << ">";
161 
162  def->setAnnotation(annotation.str());
163 
164  return newId;
165 }
static const std::string createUniqueId(const std::map< std::string, const SBase * > &idMap, const std::string &prefix, bool addIndexForFirst, const std::string &separator="_")
std::string getAnnotationStringFor ( const CCopasiObject pObjectParent)

Definition at line 2335 of file CSBMLExporter.cpp.

Referenced by addToInitialValueMap().

2336 {
2337  std::stringstream str;
2338  str << "<initialValue xmlns='http://copasi.org/initialValue' ";
2339  str << "parent='" << ((CModelEntity*)pObjectParent)->getSBMLId() << "' />";
2340  return str.str();
2341 }
std::string getUserDefinedFuctionForName ( SBMLDocument *  pSBMLDocument,
std::map< std::string, const SBase * > &  idMap,
const char *  id 
)

Definition at line 190 of file CSBMLExporter.cpp.

References addRateOfIfItDoesNotExist(), createFunctionDefinitonForURI(), and hasFunctionDefinitionForURI().

Referenced by adjustNames().

193 {
194  std::string newId;
195 
196  if (id == std::string("RNORMAL"))
197  {
198  newId = hasFunctionDefinitionForURI(pSBMLDocument,
199  "http://sbml.org/annotations/distribution",
200  "distribution",
201  "http://www.uncertml.org/distributions/normal");
202 
203  if (!newId.empty()) return newId;
204 
206  pSBMLDocument,
207  idMap,
208  id,
209  "http://sbml.org/annotations/distribution",
210  "distribution",
211  "http://www.uncertml.org/distributions/normal",
212  "lambda(m,s,m)"
213  );
214  return newId;
215  }
216  else if (id == std::string("RUNIFORM"))
217  {
218  newId = hasFunctionDefinitionForURI(pSBMLDocument,
219  "http://sbml.org/annotations/distribution",
220  "distribution",
221  "http://www.uncertml.org/distributions/uniform");
222 
223  if (!newId.empty()) return newId;
224 
226  pSBMLDocument,
227  idMap,
228  id,
229  "http://sbml.org/annotations/distribution",
230  "distribution",
231  "http://www.uncertml.org/distributions/uniform",
232  "lambda(a,b,(a+b)/2)"
233  );
234  return newId;
235  }
236  else if (id == std::string("RGAMMA"))
237  {
238  newId = hasFunctionDefinitionForURI(pSBMLDocument,
239  "http://sbml.org/annotations/distribution",
240  "distribution",
241  "http://www.uncertml.org/distributions/gamma");
242 
243  if (!newId.empty()) return newId;
244 
246  pSBMLDocument,
247  idMap,
248  id,
249  "http://sbml.org/annotations/distribution",
250  "distribution",
251  "http://www.uncertml.org/distributions/gamma",
252  "lambda(a,b,a*b)"
253  );
254  return newId;
255  }
256  else if (id == std::string("RPOISSON"))
257  {
258  newId = hasFunctionDefinitionForURI(pSBMLDocument,
259  "http://sbml.org/annotations/distribution",
260  "distribution",
261  "http://www.uncertml.org/distributions/poisson");
262 
263  if (!newId.empty()) return newId;
264 
266  pSBMLDocument,
267  idMap,
268  id,
269  "http://sbml.org/annotations/distribution",
270  "distribution",
271  "http://www.uncertml.org/distributions/poisson",
272  "lambda(mu,mu)"
273  );
274  return newId;
275  }
276  else if (id == std::string("MAX"))
277  {
278  newId = hasFunctionDefinitionForURI(pSBMLDocument,
279  "http://sbml.org/annotations/function",
280  "function",
281  "http://sbml.org/annotations/function/max");
282 
283  if (!newId.empty()) return newId;
284 
286  pSBMLDocument,
287  idMap,
288  id,
289  "http://sbml.org/annotations/function",
290  "function",
291  "http://sbml.org/annotations/function/max",
292  "lambda(a,b,piecewise(a,geq(a,b),b))"
293  );
294  return newId;
295  }
296  else if (id == std::string("MIN"))
297  {
298  newId = hasFunctionDefinitionForURI(pSBMLDocument,
299  "http://sbml.org/annotations/function",
300  "function",
301  "http://sbml.org/annotations/function/min");
302 
303  if (!newId.empty()) return newId;
304 
306  pSBMLDocument,
307  idMap,
308  id,
309  "http://sbml.org/annotations/function",
310  "function",
311  "http://sbml.org/annotations/function/min",
312  "lambda(a,b,piecewise(a,leq(a,b),b))"
313  );
314  return newId;
315  }
316  else if (id == std::string("rateOf"))
317  {
318  return addRateOfIfItDoesNotExist(pSBMLDocument, idMap, id);
319  }
320 
321  return id;
322 }
std::string createFunctionDefinitonForURI(SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap, const char *id, const std::string &sNamespace, const std::string &elementName, const std::string &definition, const std::string &lambda)
std::string hasFunctionDefinitionForURI(SBMLDocument *pSBMLDocument, const std::string &sNamespace, const std::string &elementName, const std::string &definition)
std::string addRateOfIfItDoesNotExist(SBMLDocument *pSBMLDocument, std::map< std::string, const SBase * > &idMap, const char *id)
std::string hasFunctionDefinitionForURI ( SBMLDocument *  pSBMLDocument,
const std::string &  sNamespace,
const std::string &  elementName,
const std::string &  definition 
)

Definition at line 104 of file CSBMLExporter.cpp.

Referenced by addRateOfIfItDoesNotExist(), and getUserDefinedFuctionForName().

108 {
109  if (pSBMLDocument == NULL || pSBMLDocument->getModel() == NULL) return "";
110 
111  for (unsigned int i = 0; i < pSBMLDocument->getModel()->getNumFunctionDefinitions(); ++i)
112  {
113  FunctionDefinition* current = pSBMLDocument->getModel()->getFunctionDefinition(i);
114 
115  if (current == NULL) continue;
116 
117  if (!current->isSetAnnotation()) continue;
118 
119  const XMLNode* element = current->getAnnotation();
120 
121  if (element == NULL) continue;
122 
123  for (unsigned int i = 0 ; i < element->getNumChildren(); ++i)
124  {
125  const XMLNode& annot = element->getChild(i);
126 
127  if (annot.getURI() == sNamespace &&
128  annot.getName() == elementName &&
129  annot.getAttrValue("definition") == definition)
130  {
131  return current->getId();
132  }
133  }
134  }
135 
136  return "";
137 }
void removeStickyTagFromElements ( SBMLDocument *  pSBMLDocument)

Definition at line 3222 of file CSBMLExporter.cpp.

Referenced by CSBMLExporter::exportModelToString().

3223 {
3224  if (pSBMLDocument == NULL || pSBMLDocument->getModel() == NULL)
3225  return;
3226 
3227  // reset sticky parameters
3228  for (unsigned int i = 0; i < pSBMLDocument->getModel()->getNumParameters(); ++i)
3229  pSBMLDocument->getModel()->getParameter(i)->setUserData(NULL);
3230 
3231  // reset sticky initial assignments
3232  for (unsigned int i = 0; i < pSBMLDocument->getModel()->getNumInitialAssignments(); ++i)
3233  pSBMLDocument->getModel()->getInitialAssignment(i)->setUserData(NULL);
3234 }