COPASI API  4.16.103
SEDMLUtils.cpp
Go to the documentation of this file.
1 // Copyright (C) 2013 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 /*
7  * SEDMLUtils.cpp
8  *
9  * Created on: 15 Jul 2013
10  * Author: dada
11  */
12 
13 //#include <zip.h>
14 #include <sstream>
15 #include <iostream>
16 #include <vector>
17 #include "SEDMLUtils.h"
18 
19 #include <sedml/SedTypes.h>
20 #include <sbml/SBMLTypes.h>
21 
22 #include <copasi/model/CModel.h>
25 
27  const std::map<CCopasiObject*, SBase*>& map,
28  int typeCode,
29  const std::string& name)
30 {
31  std::map<CCopasiObject*, SBase*>::const_iterator it = map.begin();
32 
33  std::string::size_type compartmentStart = name.find("{");
34  std::string nameOnly = name.substr(0, compartmentStart);
35 
36  while (it != map.end())
37  {
38  SBase* current = it->second;
39 
40  if (((current->getTypeCode() & typeCode) == typeCode) &&
41  current->getName() == name)
42  return current->getId();
43 
44  if (typeCode == SBML_SPECIES && compartmentStart != std::string::npos)
45  {
46  if (((current->getTypeCode() & typeCode) == typeCode) &&
47  current->getName() == nameOnly)
48  {
49  std::string compName = name.substr(compartmentStart + 1, name.size() - compartmentStart - 2);
50  std::string compId = findIdByNameAndType(map, SBML_COMPARTMENT, compName);
51  Species* species = (Species*) current;
52 
53  if (species->getCompartment() == compId)
54  return species->getId();
55  }
56  }
57 
58  ++it;
59  }
60 
61  return "";
62 }
63 
64 std::string
65 SEDMLUtils::getXPathAndName(std::string& sbmlId,
66  const std::string &type,
67  const CModel *pModel,
68  const CCopasiDataModel& dataModel)
69 {
70  std::vector<std::string> stringsContainer;
71  std::string targetXPathString;
72 
73  const std::map<CCopasiObject*, SBase*>& copasi2sbmlmap =
74  const_cast<CCopasiDataModel&>(dataModel).getCopasi2SBMLMap();
75  std::string displayName = sbmlId;
76 
77  if (copasi2sbmlmap.size() == 0)
78  {
79  // this should not be happening, as this is used to verify the element.
80  return "";
81  }
82 
83  std::map<CCopasiObject*, SBase*>::const_iterator pos;
84 
85  if (type == "Concentration" || type == "InitialConcentration")
86  {
87  targetXPathString = "/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id=\'";
88  //remove unwanted characters from the plot item object name
89  removeCharactersFromString(displayName, "[]");
90 
91  if (type == "InitialConcentration")
92  {
93  displayName = displayName.substr(0, displayName.length() - 2);
94  }
95 
96  sbmlId = findIdByNameAndType(copasi2sbmlmap, SBML_SPECIES, displayName);
97 
98  if (!sbmlId.empty())
99  {
100  return targetXPathString + sbmlId + "\']";
101  }
102  else
103  return "";
104  }
105 
106  else if (type == "Flux")
107  {
108  targetXPathString = "/sbml:sbml/sbml:model/sbml:listOfReactions/sbml:reaction[@id=\'";
109  splitStrings(displayName, ')', stringsContainer);
110  displayName = stringsContainer[0];
111 
112  removeCharactersFromString(displayName, "(");
113 
114  sbmlId = findIdByNameAndType(copasi2sbmlmap, SBML_REACTION, displayName);
115 
116  if (!sbmlId.empty())
117  {
118  return targetXPathString + sbmlId + "\']";
119  }
120  else
121  return "";
122  }
123  else if (type == "Value" || type == "InitialValue")
124  {
125  if (type == "InitialValue")
126  {
127  displayName = displayName.substr(0, displayName.find(".InitialValue"));
128  }
129 
130  targetXPathString = "/sbml:sbml/sbml:model/sbml:listOfParameters/sbml:parameter[@id=\'";
131  splitStrings(displayName, '[', stringsContainer);
132 
133  if (stringsContainer.size() == 1)
134  {
135  // not found ... might be a local parameter
136  removeCharactersFromString(displayName, "()");
137  splitStrings(displayName, '.', stringsContainer);
138 
139  if (stringsContainer.size() == 2)
140  {
141  sbmlId = stringsContainer[0] + "_" + stringsContainer[1];
142  std::stringstream xpath;
143  xpath << "/sbml:sbml/sbml:model/sbml:listOfReactions/sbml:reaction[@id=\'";
144  xpath << stringsContainer[0];
145  xpath << "\']/sbml:kineticLaw/sbml:listOfParameters/sbml:parameter[@id=\'";
146  xpath << stringsContainer[1];
147  xpath << "\']";
148  return xpath.str();
149  }
150  }
151 
152  displayName = stringsContainer[1];
153 
154  removeCharactersFromString(displayName, "]");
155 
156  sbmlId = findIdByNameAndType(copasi2sbmlmap, SBML_PARAMETER , displayName);
157 
158  if (sbmlId.empty())
159  sbmlId = findIdByNameAndType(copasi2sbmlmap, SBML_LOCAL_PARAMETER , displayName);
160 
161  if (!sbmlId.empty())
162  {
163  return targetXPathString + sbmlId + "\']";
164  }
165  else
166  return "";
167  }
168  else if (type == "Volume" || type == "InitialVolume")
169  {
170  targetXPathString = "/sbml:sbml/sbml:model/sbml:listOfCompartments/sbml:compartment[@id=\'";
171  splitStrings(displayName, '[', stringsContainer);
172  displayName = stringsContainer[1];
173 
174  if (type == "InitialVolume")
175  {
176  displayName = displayName.substr(0, displayName.find(".InitialVolume"));
177  }
178 
179  if (type == "Volume")
180  {
181  displayName = displayName.substr(0, displayName.find(".Volume"));
182  }
183 
184  removeCharactersFromString(displayName, "]");
185 
186  sbmlId = findIdByNameAndType(copasi2sbmlmap, SBML_COMPARTMENT, displayName);
187 
188  if (!sbmlId.empty())
189  {
190  return targetXPathString + sbmlId + "\']";
191  }
192  else
193  return "";
194  }
195  else if (type == "Time" || type == "Initial Time")
196  return SEDML_TIME_URN;
197 
198  sbmlId = "";
199  return targetXPathString;
200 }
201 
202 const CCopasiObject*
203 SEDMLUtils::resolveDatagenerator(const CModel *model, const SedDataGenerator* dataReference)
204 {
205  // for now one variable only
206  if (dataReference == NULL || dataReference->getNumVariables() < 1) return NULL;
207 
208  const SedVariable* var = dataReference->getVariable(0);
209 
210  if (var->isSetSymbol() && var->getSymbol() == SEDML_TIME_URN)
211  {
212  return static_cast<const CCopasiObject *>(model->getObject(CCopasiObjectName("Reference=Time")));
213  }
214 
215  return resolveXPath(model, var->getTarget());
216 }
217 
218 std::string
219 SEDMLUtils::translateTargetXpathInSBMLId(const std::string &xpath, std::string& SBMLType)
220 {
221  std::vector<std::string> xpathStrings;
222  std::string id, nextString;
223 
224  splitStrings(xpath, ':', xpathStrings);
225  nextString = xpathStrings[xpathStrings.size() - 1];
226 
227  splitStrings(nextString, '[', xpathStrings);
228  SBMLType = xpathStrings[0];
229  nextString = xpathStrings[xpathStrings.size() - 1];
230 
231  splitStrings(nextString, '=', xpathStrings);
232  nextString = xpathStrings[xpathStrings.size() - 1];
233  splitStrings(nextString, ']', xpathStrings);
234  id = xpathStrings[0];
235 
236  //remove the remaining unwanted characters
237  removeCharactersFromString(id, "\"']");
238  return id;
239 }
240 
241 const CCopasiObject*
242 SEDMLUtils::resolveXPath(const CModel *model, const std::string& xpath,
243  bool initial /* = false*/)
244 {
245  std::string SBMLType;
246  std::string id = translateTargetXpathInSBMLId(xpath, SBMLType);
247  const CCopasiObject* result = getObjectForSbmlId(model, id, SBMLType, initial);
248 
249  if (result == NULL)
250  {
251  // old method fails here, possibly a local parameter
252  size_t pos = xpath.find("/sbml:kineticLaw/sbml:listOfParameters/");
253 
254  if (pos != std::string::npos)
255  {
256  std::string reactionType;
257  std::string reactionId = translateTargetXpathInSBMLId(xpath.substr(0, pos), reactionType);
258  const CCopasiObject* flux = getObjectForSbmlId(model, reactionId, reactionType);
259 
260  if (flux != NULL)
261  {
262  const CCopasiObject* reactionObj = flux->getObjectParent();
263  std::string cn = "ParameterGroup=Parameters,Parameter=" + id + ",Reference=Value";
264  return dynamic_cast<const CCopasiObject*>(reactionObj->getObject(cn));
265  }
266  }
267  }
268 
269  return result;
270 }
271 
272 std::string&
273 SEDMLUtils::removeCharactersFromString(std::string& str, const std::string& characters)
274 {
275  for (unsigned int ii = 0; ii < characters.length(); ++ii)
276  {
277  str.erase(std::remove(str.begin(), str.end(), characters[ii]), str.end());
278  }
279 
280  return str;
281 }
282 
283 std::string
285 {
286  const std::string& type = object.getObjectName();
287  const CCopasiDataModel* dm = object.getObjectDataModel();
288  std::string yAxis = object.getObjectDisplayName();
289  std::string targetXPathString = getXPathAndName(yAxis, type,
290  dm->getModel(), *dm);
291  return targetXPathString;
292 }
293 
294 std::string SEDMLUtils::getNextId(const std::string& base, int count)
295 {
296  std::stringstream str; str << base << count;
297  return str.str();
298 }
299 
300 int SEDMLUtils::processArchive(const std::string & archiveFile,
301  std::string &fileName, std::string &fileContent)
302 {
303 
304  int err = 0;
305  /*
306  const char * cArchive = archiveFile.c_str();
307 
308  //Open the ZIP archive
309  zip *zip = zip_open(cArchive, 0, &err);
310 
311  //Search for file using its given name
312  const char *nameOfFile = fileName.c_str();
313  struct zip_stat st;
314  zip_stat_init(&st);
315  zip_stat(zip, nameOfFile, 0, &st);
316 
317  //Alloc memory for its uncompressed contents
318  char *fileCont = new char[st.size];
319 
320  //Read the compressed file
321  zip_file *file = zip_fopen(zip, nameOfFile, 0);
322  zip_fread(file, fileCont, st.size);
323 
324  std::ostringstream fileContentStream;
325  size_t i, iMax = st.size;
326  for(i = 0; i<iMax; ++i){
327  fileContentStream << fileCont[i];
328  }
329  fileContent = fileContentStream.str();
330 
331  //close the file and archive
332  zip_fclose(file);
333  zip_close(zip);
334  */
335 
336  return err;
337 }
338 
339 //split: receives a char delimiter and string and a vector of strings that will contain the splited strings
340 //this is presently a hack to parse the XPath based target attribute in SEDML. A better solution may be
341 //necessary in the future.
342 
343 void
344 SEDMLUtils::splitStrings(const std::string &xpath, char delim,
345  std::vector<std::string> &xpathStrings)
346 {
347  std::string myPath = xpath;
348  xpathStrings.clear();
349  std::string next;
350 
351  // For each character in the string
352  for (std::string::const_iterator it = xpath.begin(); it != xpath.end(); it++)
353  {
354  // check delimeter character
355  if (*it == delim)
356  {
357  if (!next.empty())
358  {
359  // Add them to the xpathStrings vector
360  xpathStrings.push_back(next);
361  next.clear();
362  }
363  }
364  else
365  {
366  next += *it;
367  }
368  }
369 
370  if (!next.empty())
371  xpathStrings.push_back(next);
372 }
373 
374 const CCopasiObject *
375 SEDMLUtils::getObjectForSbmlId(const CModel* pModel, const std::string& id, const std::string& SBMLType, bool initial/* = false*/)
376 {
377  if (SBMLType == "Time")
378  return static_cast<const CCopasiObject *>(pModel->getObject(CCopasiObjectName("Reference=Time")));
379 
380  if (SBMLType == "species")
381  {
382  size_t iMet, imax = pModel->getMetabolites().size();
383 
384  for (iMet = 0; iMet < imax; ++iMet)
385  {
386  // the importer should not need to change the initial concentration
387  // pModel->getMetabolites()[iMet]->setInitialConcentration(0.896901);
388 
389  if (pModel->getMetabolites()[iMet]->getSBMLId() == id)
390  {
391  if (initial)
392  return pModel->getMetabolites()[iMet]->getInitialConcentrationReference();
393 
394  return pModel->getMetabolites()[iMet]->getConcentrationReference();
395  }
396  }
397  }
398  else if (SBMLType == "reaction")
399  {
400  size_t iMet, imax = pModel->getReactions().size();
401 
402  for (iMet = 0; iMet < imax; ++iMet)
403  {
404  if (pModel->getReactions()[iMet]->getSBMLId() == id)
405  {
406  if (initial)
407  return NULL;
408 
409  return pModel->getReactions()[iMet]->getFluxReference();
410  }
411  }
412  }
413  else if (SBMLType == "parameter")
414  {
415  size_t iMet, imax = pModel->getModelValues().size();
416 
417  for (iMet = 0; iMet < imax; ++iMet)
418  {
419  if (pModel->getModelValues()[iMet]->getSBMLId() == id)
420  {
421  if (initial)
422  return pModel->getModelValues()[iMet]->getInitialValueReference();
423 
424  return pModel->getModelValues()[iMet]->getValueReference();
425  }
426  }
427  }
428 
429  else if (SBMLType == "compartment")
430  {
431  size_t iComp, imax = pModel->getCompartments().size();
432 
433  for (iComp = 0; iComp < imax; ++iComp)
434  {
435  if (pModel->getCompartments()[iComp]->getSBMLId() == id)
436  {
437  if (initial)
438  return pModel->getCompartments()[iComp]->getInitialValueReference();
439 
440  return pModel->getCompartments()[iComp]->getValueReference();
441  }
442  }
443  }
444 
445  return NULL;
446 }
447 
448 /*void SEDMLUtils::resmoveUnwantedChars(std::string & str, char chars[]) {
449  for (unsigned int i = 0; i < strlen(chars); ++i) {
450 
451  str.erase(std::remove(str.begin(), str.end(), chars[i]), str.end());
452  }
453 }
454  */
456 {
457  // TODO Auto-generated constructor stub
458 }
459 
461 {
462  // TODO Auto-generated destructor stub
463 }
CCopasiDataModel * getObjectDataModel()
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
const CCopasiVector< CMetab > & getMetabolites() const
Definition: CModel.cpp:1051
const CCopasiVectorN< CModelValue > & getModelValues() const
Definition: CModel.cpp:1060
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
static std::string translateTargetXpathInSBMLId(const std::string &xpath, std::string &SBMLType)
Definition: SEDMLUtils.cpp:219
virtual size_t size() const
static const CCopasiObject * resolveDatagenerator(const CModel *model, const SedDataGenerator *dataReference)
Definition: SEDMLUtils.cpp:203
static std::string getXPathAndName(std::string &sbmlId, const std::string &type, const CModel *pModel, const CCopasiDataModel &dataModel)
Definition: SEDMLUtils.cpp:65
static const CCopasiObject * getObjectForSbmlId(const CModel *pModel, const std::string &id, const std::string &SBMLType, bool initial=false)
Definition: SEDMLUtils.cpp:375
virtual ~SEDMLUtils()
Definition: SEDMLUtils.cpp:460
static std::string findIdByNameAndType(const std::map< CCopasiObject *, SBase * > &map, int typeCode, const std::string &name)
Definition: SEDMLUtils.cpp:26
static void splitStrings(const std::string &xpath, char delim, std::vector< std::string > &stringsContainer)
Definition: SEDMLUtils.cpp:344
static std::string getXPathForObject(const CCopasiObject &object)
Definition: SEDMLUtils.cpp:284
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
static const CCopasiObject * resolveXPath(const CModel *model, const std::string &xpath, bool initial=false)
Definition: SEDMLUtils.cpp:242
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
static std::string getNextId(const std::string &base, int count)
Definition: SEDMLUtils.cpp:294
Definition: CModel.h:50
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
int processArchive(const std::string &archiveFile, std::string &fileName, std::string &fileContent)
Definition: SEDMLUtils.cpp:300
static std::string & removeCharactersFromString(std::string &str, const std::string &characters)
Definition: SEDMLUtils.cpp:273
#define SEDML_TIME_URN
Definition: SEDMLUtils.h:21
CCopasiContainer * getObjectParent() const