COPASI API  4.16.103
SBMLDocumentLoader.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 <assert.h>
16 
17 #define USE_LAYOUT 1
18 #define USE_RENDER 1
19 
20 #include <sbml/ListOf.h>
21 #include <sbml/packages/layout/sbml/Layout.h>
22 #include <sbml/packages/layout/sbml/SpeciesGlyph.h>
23 #include <sbml/packages/layout/sbml/ReactionGlyph.h>
24 
25 #include <sbml/packages/layout/sbml/SpeciesReferenceGlyph.h>
26 #include <sbml/packages/layout/sbml/TextGlyph.h>
27 #include <sbml/packages/render/sbml/Text.h>
28 #include <sbml/packages/render/sbml/LocalStyle.h>
29 
30 #include "copasi.h"
31 
32 #include "SBMLDocumentLoader.h"
33 #include "CListOfLayouts.h"
34 #include "CLayout.h"
35 #include "CLReactionGlyph.h"
36 #include "CLGlyphs.h"
37 
38 #include "CLRenderCurve.h"
39 #include <sbml/packages/render/extension/RenderListOfLayoutsPlugin.h>
40 #include <sbml/packages/render/extension/RenderLayoutPlugin.h>
41 
42 #include "report/CKeyFactory.h"
44 #include "sbml/SBMLUtils.h" //from the copasi sbml dir
45 
46 //static
48  const ListOf & sbmlList,
49  const std::map<CCopasiObject*, SBase*> & copasimodelmap)
50 {
51  unsigned C_INT32 i, iMax;
52 
53  // read the global render information
54  const ListOfLayouts* pLoL = dynamic_cast<const ListOfLayouts*>(&sbmlList);
55  assert(pLoL != NULL);
56  RenderListOfLayoutsPlugin* rlolPlugin = (RenderListOfLayoutsPlugin*) pLoL ->getPlugin("render");
57 
58  if (rlolPlugin == NULL)
59  {
60  const_cast<SBMLDocument *>(pLoL->getSBMLDocument())->enablePackage(RenderExtension::getXmlnsL3V1V1(), "render", true);
61  rlolPlugin = (RenderListOfLayoutsPlugin*) pLoL ->getPlugin("render");
62  }
63 
64  iMax = rlolPlugin->getNumGlobalRenderInformationObjects();
65  std::map<std::string, std::string> idToKeyMap;
66  CLGlobalRenderInformation* pGRI = NULL;
67 
68  /*
69  std::map<std::string,std::string> colorIdToKeyMap;
70  std::map<std::string,std::string> gradientIdToKeyMap;
71  std::map<std::string,std::string> lineEndingIdToKeyMap;
72  std::map<std::string,std::map<std::string,std::string> > colorIdToKeyMapMap;
73  std::map<std::string,std::map<std::string,std::string> > gradientIdToKeyMapMap;
74  std::map<std::string,std::map<std::string,std::string> > lineEndingIdToKeyMapMap;
75  */
76  for (i = 0; i < iMax; ++i)
77  {
78  //colorIdToKeyMap.clear();
79  //gradientIdToKeyMap.clear();
80  //lineEndingIdToKeyMap.clear();
81  //pGRI=new CLGlobalRenderInformation(*pLoL->getRenderInformation(i),colorIdToKeyMap,gradientIdToKeyMap,lineEndingIdToKeyMap,&lol);
82  pGRI = new CLGlobalRenderInformation(*rlolPlugin->getRenderInformation(i), &lol);
83 
84  if (rlolPlugin->getRenderInformation(i)->isSetId())
85  idToKeyMap.insert(std::pair<std::string, std::string>(rlolPlugin->getRenderInformation(i)->getId(), pGRI->getKey()));
86  else
87  idToKeyMap.insert(std::pair<std::string, std::string>(pGRI->getKey(), pGRI->getKey()));
88 
89  //colorIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pGRI->getKey(),colorIdToKeyMap));
90  //gradientIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pGRI->getKey(),gradientIdToKeyMap));
91  //lineEndingIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pGRI->getKey(),lineEndingIdToKeyMap));
93  }
94 
95  // fix the references
96  SBMLDocumentLoader::convertRenderInformationReferencesIds<CLGlobalRenderInformation>(lol.getListOfGlobalRenderInformationObjects(), idToKeyMap);
97  // fix the color ids, gradient ids and line ending ids.
98 
99  //convert the map as used by the CLxxx constructors
100  std::map<std::string, std::string> modelmap;
101 
102  std::string s1, s2;
103  std::map<CCopasiObject*, SBase*>::const_iterator it;
104  std::map<CCopasiObject*, SBase*>::const_iterator itEnd = copasimodelmap.end();
105 
106  for (it = copasimodelmap.begin(); it != itEnd; ++it)
107  {
108  s1 = SBMLUtils::getIdFromSBase(it->second);
109 
110  if (it->first)
111  s2 = it->first->getKey();
112  else
113  s2 = "";
114 
115  if ((s1 != "") && (s2 != ""))
116  modelmap[s1] = s2;
117  }
118 
119  //iterate through list of layouts
120  iMax = sbmlList.size();
121 
122  for (i = 0; i < iMax; ++i)
123  {
124  std::map<std::string, std::string> layoutmap;
125  const Layout* tmp
126  = dynamic_cast<const Layout*>(sbmlList.get(i));
127 
128  if (tmp)
129  {
130  //CLayout * pLayout = createLayout(*tmp, modelmap, layoutmap,idToKeyMap,expandedColorIdToKeyMapMap,expandedGradientIdToKeyMapMap,expandedLineEndingIdToKeyMapMap);
131  CLayout * pLayout = createLayout(*tmp, modelmap, layoutmap, idToKeyMap);
132  lol.addLayout(pLayout, layoutmap);
133  }
134  }
135 
136  //TODO: the layout object should be added to the copasimodelmap. However,
137  //if this is done, the object also need to be removed if necessary in the
138  //sbml exporter (see comment in CListOfLayouts::exportToSBML()).
139 }
140 
141 //static
142 CLayout * SBMLDocumentLoader::createLayout(const Layout & sbmlLayout,
143  const std::map<std::string, std::string> & modelmap,
144  std::map<std::string, std::string> & layoutmap
145  , const std::map<std::string, std::string>& globalIdToKeyMap
146  , const CCopasiContainer * pParent
147  )
148 {
149  CLayout* layout = new CLayout(sbmlLayout, layoutmap, pParent);
150 
151  //compartments
152  unsigned C_INT32 i, iMax = sbmlLayout.getListOfCompartmentGlyphs()->size();
153 
154  for (i = 0; i < iMax; ++i)
155  {
156  const CompartmentGlyph* tmp
157  = dynamic_cast<const CompartmentGlyph*>(sbmlLayout.getListOfCompartmentGlyphs()->get(i));
158 
159  if (tmp)
160  layout->addCompartmentGlyph(new CLCompartmentGlyph(*tmp, modelmap, layoutmap));
161  }
162 
163  //species
164  iMax = sbmlLayout.getListOfSpeciesGlyphs()->size();
165 
166  for (i = 0; i < iMax; ++i)
167  {
168  const SpeciesGlyph* tmp
169  = dynamic_cast<const SpeciesGlyph*>(sbmlLayout.getListOfSpeciesGlyphs()->get(i));
170 
171  if (tmp)
172  layout->addMetaboliteGlyph(new CLMetabGlyph(*tmp, modelmap, layoutmap));
173  }
174 
175  //reactions
176  iMax = sbmlLayout.getListOfReactionGlyphs()->size();
177 
178  for (i = 0; i < iMax; ++i)
179  {
180  const ReactionGlyph* tmp
181  = dynamic_cast<const ReactionGlyph*>(sbmlLayout.getListOfReactionGlyphs()->get(i));
182 
183  if (tmp)
184  layout->addReactionGlyph(new CLReactionGlyph(*tmp, modelmap, layoutmap));
185  }
186 
187  //text
188  iMax = sbmlLayout.getListOfTextGlyphs()->size();
189 
190  for (i = 0; i < iMax; ++i)
191  {
192  const TextGlyph* tmp
193  = dynamic_cast<const TextGlyph*>(sbmlLayout.getListOfTextGlyphs()->get(i));
194 
195  if (tmp)
196  layout->addTextGlyph(new CLTextGlyph(*tmp, modelmap, layoutmap));
197  }
198 
199  //additional
200  iMax = sbmlLayout.getListOfAdditionalGraphicalObjects()->size();
201 
202  for (i = 0; i < iMax; ++i)
203  {
204  const GraphicalObject* graphical
205  = dynamic_cast<const GraphicalObject*>(sbmlLayout.getListOfAdditionalGraphicalObjects()->get(i));
206 
207  if (graphical)
208  layout->addGeneralGlyph(new CLGeneralGlyph(*graphical, modelmap, layoutmap));
209  }
210 
211  //second pass text (the text glyph can refer to other glyphs. These references can)
212  //only be resolved after all glyphs are created).
213  iMax = sbmlLayout.getListOfTextGlyphs()->size();
214 
215  for (i = 0; i < iMax; ++i)
216  {
217  const TextGlyph* tmp
218  = dynamic_cast<const TextGlyph*>(sbmlLayout.getListOfTextGlyphs()->get(i));
219 
220  if (tmp)
221  postprocessTextGlyph(*tmp, layoutmap);
222  }
223 
224  RenderLayoutPlugin* rlPlugin = (RenderLayoutPlugin*) sbmlLayout.getPlugin("render");
225  assert(rlPlugin != NULL);
226 
227  // import the local render information
228  iMax = rlPlugin->getNumLocalRenderInformationObjects();
229  std::map<std::string, std::string> idToKeyMap;
230  CLLocalRenderInformation* pLRI = NULL;
231 
232  for (i = 0; i < iMax; ++i)
233  {
234  //colorIdToKeyMap.clear();
235  //gradientIdToKeyMap.clear();
236  //lineEndingIdToKeyMap.clear();
237  //pLRI=new CLLocalRenderInformation(*sbmlLayout.getRenderInformation(i),colorIdToKeyMap,gradientIdToKeyMap,lineEndingIdToKeyMap,layout);
238  pLRI = new CLLocalRenderInformation(*rlPlugin->getRenderInformation(i), layout);
239 
240  if (rlPlugin->getRenderInformation(i)->isSetId())
241  idToKeyMap.insert(std::pair<std::string, std::string>(rlPlugin->getRenderInformation(i)->getId(), pLRI->getKey()));
242  else
243  idToKeyMap.insert(std::pair<std::string, std::string>(pLRI->getKey(), pLRI->getKey()));
244 
245  //colorIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pLRI->getKey(),colorIdToKeyMap));
246  //gradientIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pLRI->getKey(),gradientIdToKeyMap));
247  //lineEndingIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pLRI->getKey(),lineEndingIdToKeyMap));
248  // fix the references to layout objects in id lists
249  size_t j, jMax = pLRI->getNumStyles();
250 
251  for (j = 0; j < jMax; j++)
252  {
254  }
255 
256  layout->addLocalRenderInformation(pLRI);
257  }
258 
259  // fix the references
260  // we have to consider the global ids as well
261  // since all ids in these two map should be unique, we can just combine them
262  size_t count = idToKeyMap.size() + globalIdToKeyMap.size();
263  idToKeyMap.insert(globalIdToKeyMap.begin(), globalIdToKeyMap.end());
264  // make sure the ids were really unique
265  assert(idToKeyMap.size() == count);
266  SBMLDocumentLoader::convertRenderInformationReferencesIds<CLLocalRenderInformation>(layout->getListOfLocalRenderInformationObjects(), idToKeyMap);
267  // fix the color ids, gradient ids and line ending ids.
268 
269  return layout;
270 }
271 
272 //static
273 void SBMLDocumentLoader::postprocessTextGlyph(const TextGlyph & sbml,
274  const std::map<std::string, std::string> & layoutmap)
275 {
276  //the corresponding CLTextGlyph should already exist. Let's find it...
277  CLTextGlyph * pTg = NULL;
278 
279  if (sbml.getId() != "")
280  {
281  std::map<std::string, std::string>::const_iterator it = layoutmap.find(sbml.getId());
282 
283  if (it != layoutmap.end())
284  pTg = dynamic_cast<CLTextGlyph *>(CCopasiRootContainer::getKeyFactory()->get(it->second));
285 
286  if (!pTg)
287  {
288  //error?
289  return;
290  }
291  }
292  else
293  {
294  //error?
295  return;
296  }
297 
298  //resolve the graphical object reference
299  assert(pTg);
300 
301  if (sbml.getGraphicalObjectId() != "")
302  {
303  std::map<std::string, std::string>::const_iterator it = layoutmap.find(sbml.getGraphicalObjectId());
304 
305  if (it != layoutmap.end())
306  pTg->setGraphicalObjectKey(it->second);
307  }
308 }
309 
310 /**
311  * Converts references to ids of layout objects to the corresponding key.
312  */
313 void SBMLDocumentLoader::convertLayoutObjectIds(CLLocalStyle& style, const std::map<std::string, std::string>& idToKeyMap)
314 {
315  std::set<std::string> keys;
316  std::map<std::string, std::string>::const_iterator pos;
317  std::set<std::string>::const_iterator it = style.getKeyList().begin(), endit = style.getKeyList().end();
318 
319  while (it != endit)
320  {
321  pos = idToKeyMap.find(*it);
322  assert(pos != idToKeyMap.end());
323  keys.insert(pos->second);
324  ++it;
325  }
326 
327  style.setKeyList(keys);
328 }
329 
330 /**
331  * converts references to color ids, gradient ids and/or line ending ids in graphical primitives
332  * to references to the corresponding keys.
333 void SBMLDocumentLoader::convertPropertyIds(CLTransformation2D* pObject,
334  const std::map<std::string,std::string>& colorIdToKeyMap,
335  const std::map<std::string,std::string>& gradientIdToKeyMap,
336  const std::map<std::string,std::string>& lineEndingIdToKeyMap)
337 {
338  CLGraphicalPrimitive1D* pGP1=dynamic_cast<CLGraphicalPrimitive1D*>(pObject);
339  if(pGP1 != NULL)
340  {
341  std::map<std::string,std::string>::const_iterator pos;
342  // fix the stroke color
343  std::string s=pGP1->getStroke();
344  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none" && s[0] != '#')
345  {
346  pos=colorIdToKeyMap.find(s);
347  // check if we found the color, else it must be a gradient
348  if(pos==colorIdToKeyMap.end())
349  {
350  pos=gradientIdToKeyMap.find(s);
351  assert(pos != gradientIdToKeyMap.end());
352  pGP1->setStroke(pos->second);
353  }
354  else
355  {
356  pGP1->setStroke(pos->second);
357  }
358  }
359  CLGraphicalPrimitive2D* pGP2=dynamic_cast<CLGraphicalPrimitive2D*>(pObject);
360  if(pGP2 != NULL)
361  {
362  // fix the fill color
363  std::string s=pGP2->getFillColor();
364  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none" && s[0] != '#')
365  {
366  pos=colorIdToKeyMap.find(s);
367  // check if we found the color, else it must be a gradient
368  if(pos==colorIdToKeyMap.end())
369  {
370  pos=gradientIdToKeyMap.find(s);
371  assert(pos != gradientIdToKeyMap.end());
372  pGP2->setFillColor(pos->second);
373  }
374  else
375  {
376  pGP2->setFillColor(pos->second);
377  }
378  }
379  CLGroup* pG=dynamic_cast<CLGroup*>(pObject);
380  if(pG != NULL)
381  {
382  // fix the line endings
383  s=pG->getStartHead();
384  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none")
385  {
386  pos=lineEndingIdToKeyMap.find(s);
387  assert(pos != lineEndingIdToKeyMap.end());
388  pG->setStartHead(pos->second);
389  }
390  s=pG->getEndHead();
391  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none")
392  {
393  pos=lineEndingIdToKeyMap.find(s);
394  assert(pos != lineEndingIdToKeyMap.end());
395  pG->setEndHead(pos->second);
396  }
397  // call this method for all children
398  size_t i,iMax=pG->getNumElements();
399  for(i=0; i < iMax; ++i)
400  {
401  SBMLDocumentLoader::convertPropertyIds(dynamic_cast<CLTransformation2D*>(pG->getElement(i)),colorIdToKeyMap,gradientIdToKeyMap,lineEndingIdToKeyMap);
402  }
403  }
404  }
405  else
406  {
407  CLRenderCurve* pC=dynamic_cast<CLRenderCurve*>(pObject);
408  if(pC != NULL)
409  {
410  // fix the line endings
411  s=pC->getStartHead();
412  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none")
413  {
414  pos=lineEndingIdToKeyMap.find(s);
415  assert(pos != lineEndingIdToKeyMap.end());
416  pC->setStartHead(pos->second);
417  }
418  s=pC->getEndHead();
419  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none")
420  {
421  pos=lineEndingIdToKeyMap.find(s);
422  assert(pos != lineEndingIdToKeyMap.end());
423  pC->setEndHead(pos->second);
424  }
425  }
426  }
427  }
428 }
429  */
430 
431 /**
432  * converts references to color ids in gradient stops
433  * to references to the corresponding keys.
434 void SBMLDocumentLoader::convertColorIds(CLGradientBase& gradient,
435  const std::map<std::string,std::string>& colorIdToKeyMap)
436 {
437  size_t i,iMax=gradient.getNumGradientStops();
438  CLGradientStop* pStop=NULL;
439  std::map<std::string,std::string>::const_iterator pos;
440  for(i=0; i < iMax; ++i)
441  {
442  pStop=gradient.getGradientStop(i);
443  std::string color=pStop->getStopColor();
444  if(color.find_first_not_of(" \t\n\r") != std::string::npos && color != "none" && color[0] != '#')
445  {
446  pos=colorIdToKeyMap.find(color);
447  assert(pos!=colorIdToKeyMap.end());
448  pStop->setStopColor(pos->second);
449  }
450  }
451 }
452  */
453 
454 /**
455  * Converts references to keys of layout objects to the corresponding id.
456  */
457 void SBMLDocumentLoader::convertLayoutObjectKeys(LocalStyle& style, const std::map<std::string, std::string>& keyToIdMap)
458 {
459  std::set<std::string> ids;
460  std::map<std::string, std::string>::const_iterator pos;
461  std::set<std::string>::const_iterator it = style.getIdList().begin(), endit = style.getIdList().end();
462 
463  while (it != endit)
464  {
465  pos = keyToIdMap.find(*it);
466  assert(pos != keyToIdMap.end());
467  ids.insert(pos->second);
468  ++it;
469  }
470 
471  style.setIdList(ids);
472 }
473 
474 /**
475  * converts references to color keys, gradient keys and/or line ending keys in graphical primitives
476  * to references to the corresponding Ids.
477 void SBMLDocumentLoader::convertPropertyKeys(Transformation2D* pObject,
478  const std::map<std::string,std::string>& colorKeyToIdMap,
479  const std::map<std::string,std::string>& gradientKeyToIdMap,
480  const std::map<std::string,std::string>& lineEndingKeyToIdMap)
481 {
482  GraphicalPrimitive1D* pGP1=dynamic_cast<GraphicalPrimitive1D*>(pObject);
483  if(pGP1 != NULL)
484  {
485  std::map<std::string,std::string>::const_iterator pos;
486  // fix the stroke color
487  std::string s=pGP1->getStroke();
488  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none" && s[0] != '#')
489  {
490  pos=colorKeyToIdMap.find(s);
491  // check if we found the color, else it must be a gradient
492  if(pos==colorKeyToIdMap.end())
493  {
494  pos=gradientKeyToIdMap.find(s);
495  assert(pos != gradientKeyToIdMap.end());
496  pGP1->setStroke(pos->second);
497  }
498  else
499  {
500  pGP1->setStroke(pos->second);
501  }
502  }
503  GraphicalPrimitive2D* pGP2=dynamic_cast<GraphicalPrimitive2D*>(pObject);
504  if(pGP2 != NULL)
505  {
506  // fix the fill color
507  std::string s=pGP2->getFillColor();
508  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none" && s[0] != '#')
509  {
510  pos=colorKeyToIdMap.find(s);
511  // check if we found the color, else it must be a gradient
512  if(pos==colorKeyToIdMap.end())
513  {
514  pos=gradientKeyToIdMap.find(s);
515  assert(pos != gradientKeyToIdMap.end());
516  pGP2->setFillColor(pos->second);
517  }
518  else
519  {
520  pGP2->setFillColor(pos->second);
521  }
522  }
523  Group* pG=dynamic_cast<Group*>(pObject);
524  if(pG != NULL)
525  {
526  // fix the line endings
527  s=pG->getStartHead();
528  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none")
529  {
530  pos=lineEndingKeyToIdMap.find(s);
531  assert(pos != lineEndingKeyToIdMap.end());
532  pG->setStartHead(pos->second);
533  }
534  s=pG->getEndHead();
535  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none")
536  {
537  pos=lineEndingKeyToIdMap.find(s);
538  assert(pos != lineEndingKeyToIdMap.end());
539  pG->setEndHead(pos->second);
540  }
541  // call this method for all children
542  size_t i,iMax=pG->getNumElements();
543  for(i=0; i < iMax; ++i)
544  {
545  SBMLDocumentLoader::convertPropertyKeys(dynamic_cast<Transformation2D*>(pG->getElement(i)),colorKeyToIdMap,gradientKeyToIdMap,lineEndingKeyToIdMap);
546  }
547  }
548  }
549  else
550  {
551  RenderCurve* pC=dynamic_cast<RenderCurve*>(pObject);
552  if(pC != NULL)
553  {
554  // fix the line endings
555  s=pC->getStartHead();
556  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none")
557  {
558  pos=lineEndingKeyToIdMap.find(s);
559  assert(pos != lineEndingKeyToIdMap.end());
560  pC->setStartHead(pos->second);
561  }
562  s=pC->getEndHead();
563  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none")
564  {
565  pos=lineEndingKeyToIdMap.find(s);
566  assert(pos != lineEndingKeyToIdMap.end());
567  pC->setEndHead(pos->second);
568  }
569  }
570  }
571  }
572 }
573  */
574 
575 /**
576  * converts references to color ids in gradient stops
577  * to references to the corresponding keys.
578 void SBMLDocumentLoader::convertColorKeys(GradientBase& gradient,
579  const std::map<std::string,std::string>& colorKeyToIdMap)
580 {
581  size_t i,iMax=gradient.getNumGradientStops();
582  GradientStop* pStop=NULL;
583  std::map<std::string,std::string>::const_iterator pos;
584  for(i=0; i < iMax; ++i)
585  {
586  pStop=gradient.getGradientStop(i);
587  std::string color=pStop->getStopColor();
588  if(color.find_first_not_of(" \t\n\r") != std::string::npos && color != "none" && color[0] != '#')
589  {
590  pos=colorKeyToIdMap.find(color);
591  assert(pos!=colorKeyToIdMap.end());
592  pStop->setStopColor(pos->second);
593  }
594  }
595 }
596  */
597 
598 /**
599  * Adds all elements of the source map into the dest map if an element with the same key does not already exist in the dest map.
600  */
602  const std::map<std::string, std::string>& sourceMap,
603  std::map<std::string, std::string>& destMap
604 )
605 {
606  std::map<std::string, std::string>::const_iterator it = sourceMap.begin(), endit = sourceMap.end();
607 
608  while (it != endit)
609  {
610  if (destMap.find(it->first) == destMap.end())
611  {
612  destMap.insert(*it);
613  }
614 
615  ++it;
616  }
617 }
618 
619 /**
620  * converts references to color keys, gradient keys and/or line ending keys in graphical primitives
621  * to references to the corresponding Ids.
622 template<typename RENDER_INFORMATION>
623 void SBMLDocumentLoader::convertPropertyKeys(RENDER_INFORMATION* pObject,
624  const std::map<std::string,std::string>& colorKeyToIdMap,
625  const std::map<std::string,std::string>& gradientKeyToIdMap,
626  const std::map<std::string,std::string>& lineEndingKeyToIdMap)
627 {
628  std::string s=pObject->getBackgroundColor();
629  std::map<std::string,std::string>::const_iterator pos;
630  if(s.find_first_not_of(" \t\n\r") != std::string::npos && s != "none" && s[0] != '#')
631  {
632  pos=colorKeyToIdMap.find(s);
633  assert(pos!=colorKeyToIdMap.end());
634  pObject->setBackgroundColor(pos->second);
635  }
636  size_t i,iMax=pObject->getNumLineEndings();
637  for(i=0;i < iMax; ++i)
638  {
639  SBMLDocumentLoader::convertPropertyIds(dynamic_cast<CLTransformation2D*>(pObject->getLineEnding(i)->getGroup()),colorKeyToIdMap,gradientKeyToIdMap,lineEndingKeyToIdMap);
640  }
641  iMax=pObject->getNumStyles();
642  for(i=0; i < iMax; ++i)
643  {
644  SBMLDocumentLoader::convertPropertyIds(dynamic_cast<CLTransformation2D*>(pObject->getStyle(i)->getGroup()),colorKeyToIdMap,gradientKeyToIdMap,lineEndingKeyToIdMap);
645  }
646 }
647  */
648 
649 /**
650  * Creates maps of maps for the colors, gradients and line endings of the given of render information
651  * objects.
652  * Those maps can than be used to convert the id of a color, gradient or lineEnding from the SBML model
653  * to the key used in the COPASI model.
654 template<typename RENDER_INFORMATION>
655 void SBMLDocumentLoader::expandIdToKeyMaps(const CLRenderInformationBase* pRenderInfo,
656  CCopasiVector<RENDER_INFORMATION>& renderInformationVector,
657  std::map<std::string,std::map<std::string,std::string> >& colorIdToKeyMapMap,
658  std::map<std::string,std::map<std::string,std::string> >& gradientIdToKeyMapMap,
659  std::map<std::string,std::map<std::string,std::string> >& lineEndingIdToKeyMapMap,
660  const std::map<std::string,std::map<std::string,std::string> >& unexpandedColorIdToKeyMapMap,
661  const std::map<std::string,std::map<std::string,std::string> >& unexpandedGradientIdToKeyMapMap,
662  const std::map<std::string,std::map<std::string,std::string> >& unexpandedLineEndingIdToKeyMapMap,
663  std::set<std::string>& chain,
664  const std::map<std::string,std::map<std::string,std::string> >& expandedGlobalColorIdToKeyMapMap,
665  const std::map<std::string,std::map<std::string,std::string> >& expandedGlobalGradientIdToKeyMapMap,
666  const std::map<std::string,std::map<std::string,std::string> >& expandedGlobalLineEndingIdToKeyMapMap
667  )
668 {
669  // first we check if there already is an entry in the map for this render info object
670  if(chain.find(pRenderInfo->getId()) != chain.end())
671  {
672  // we have an endless loop
673  fatalError();
674  }
675  else
676  {
677  chain.insert(pRenderInfo->getKey());
678  }
679  if(colorIdToKeyMapMap.find(pRenderInfo->getKey()) == colorIdToKeyMapMap.end())
680  {
681  assert(gradientIdToKeyMapMap.find(pRenderInfo->getKey()) == gradientIdToKeyMapMap.end());
682  assert(lineEndingIdToKeyMapMap.find(pRenderInfo->getKey()) == lineEndingIdToKeyMapMap.end());
683  std::string s=pRenderInfo->getReferenceRenderInformationKey();
684  assert(unexpandedColorIdToKeyMapMap.find(pRenderInfo->getKey()) != unexpandedColorIdToKeyMapMap.end());
685  assert(unexpandedGradientIdToKeyMapMap.find(pRenderInfo->getKey()) != unexpandedGradientIdToKeyMapMap.end());
686  assert(unexpandedLineEndingIdToKeyMapMap.find(pRenderInfo->getKey()) != unexpandedLineEndingIdToKeyMapMap.end());
687  // fill the id map with the ids from this render info only
688  std::map<std::string,std::map<std::string,std::string> >::const_iterator mapPos=unexpandedColorIdToKeyMapMap.find(pRenderInfo->getKey());
689  assert(mapPos!=unexpandedColorIdToKeyMapMap.end());
690  colorIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pRenderInfo->getKey(),mapPos->second));
691  mapPos=unexpandedGradientIdToKeyMapMap.find(pRenderInfo->getKey());
692  assert(mapPos!=unexpandedGradientIdToKeyMapMap.end());
693  gradientIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pRenderInfo->getKey(),mapPos->second));
694  mapPos=unexpandedLineEndingIdToKeyMapMap.find(pRenderInfo->getKey());
695  assert(mapPos!=unexpandedLineEndingIdToKeyMapMap.end());
696  lineEndingIdToKeyMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pRenderInfo->getKey(),mapPos->second));
697  if(s.find_first_not_of(" \t\n\r") != std::string::npos)
698  {
699  // we have to create the entry for the referenced render info
700  size_t i,iMax=renderInformationVector.size();
701  for(i=0; i < iMax; ++i)
702  {
703  if(renderInformationVector[i]->getKey() == "s")
704  {
705  break;
706  }
707  }
708  if(i != iMax)
709  {
710  // we found the render information that goes with the id
711  SBMLDocumentLoader::expandIdToKeyMaps<RENDER_INFORMATION>(renderInformationVector[i],
712  renderInformationVector,
713  colorIdToKeyMapMap,
714  gradientIdToKeyMapMap,
715  lineEndingIdToKeyMapMap,
716  unexpandedColorIdToKeyMapMap,
717  unexpandedGradientIdToKeyMapMap,
718  unexpandedLineEndingIdToKeyMapMap,
719  chain,
720  expandedGlobalColorIdToKeyMapMap,
721  expandedGlobalGradientIdToKeyMapMap,
722  expandedGlobalLineEndingIdToKeyMapMap
723  );
724  }
725  // add the nonexistent ids from the references render info to this one
726  SBMLDocumentLoader::combineMaps(colorIdToKeyMapMap[renderInformationVector[i]->getKey()],colorIdToKeyMapMap[pRenderInfo->getKey()]);
727  SBMLDocumentLoader::combineMaps(gradientIdToKeyMapMap[renderInformationVector[i]->getKey()],gradientIdToKeyMapMap[pRenderInfo->getKey()]);
728  SBMLDocumentLoader::combineMaps(lineEndingIdToKeyMapMap[renderInformationVector[i]->getKey()],lineEndingIdToKeyMapMap[pRenderInfo->getKey()]);
729  }
730  }
731 }
732  */
733 
734 /**
735  * Creates maps of maps for the colors, gradients and line endings of the given of render information
736  * objects.
737  * Those maps can than be used to convert the id of a color, gradient or lineEnding from the SBML model
738  * to the key used in the COPASI model.
739 void SBMLDocumentLoader::expandKeyToIdMaps(const RenderInformationBase* pRenderInfo,
740  ListOf& renderInformationVector,
741  std::map<std::string,std::map<std::string,std::string> >& colorKeyToIdMapMap,
742  std::map<std::string,std::map<std::string,std::string> >& gradientKeyToIdMapMap,
743  std::map<std::string,std::map<std::string,std::string> >& lineEndingKeyToIdMapMap,
744  const std::map<std::string,std::map<std::string,std::string> >& unexpandedColorKeyToIdMapMap,
745  const std::map<std::string,std::map<std::string,std::string> >& unexpandedGradientKeyToIdMapMap,
746  const std::map<std::string,std::map<std::string,std::string> >& unexpandedLineEndingKeyToIdMapMap,
747  std::set<std::string>& chain,
748  const std::map<std::string,std::map<std::string,std::string> >& expandedGlobalColorKeyToIdMapMap,
749  const std::map<std::string,std::map<std::string,std::string> >& expandedGlobalGradientKeyToIdMapMap,
750  const std::map<std::string,std::map<std::string,std::string> >& expandedGlobalLineEndingKeyToIdMapMap
751  )
752 {
753  // first we check if there already is an entry in the map for this render info object
754  if(chain.find(pRenderInfo->getId()) != chain.end())
755  {
756  // we have an endless loop
757  fatalError();
758  }
759  else
760  {
761  chain.insert(pRenderInfo->getId());
762  }
763  if(colorKeyToIdMapMap.find(pRenderInfo->getId()) == colorKeyToIdMapMap.end())
764  {
765  std::map<std::string,std::map<std::string,std::string> >::const_iterator mapPos;
766  assert(gradientKeyToIdMapMap.find(pRenderInfo->getId()) == gradientKeyToIdMapMap.end());
767  assert(lineEndingKeyToIdMapMap.find(pRenderInfo->getId()) == lineEndingKeyToIdMapMap.end());
768  std::string s=pRenderInfo->getReferenceRenderInformationId();
769  assert(unexpandedColorKeyToIdMapMap.find(pRenderInfo->getId()) != unexpandedColorKeyToIdMapMap.end());
770  assert(unexpandedGradientKeyToIdMapMap.find(pRenderInfo->getId()) != unexpandedGradientKeyToIdMapMap.end());
771  assert(unexpandedLineEndingKeyToIdMapMap.find(pRenderInfo->getId()) != unexpandedLineEndingKeyToIdMapMap.end());
772  // fill the id map with the ids from this render info only
773  mapPos=unexpandedColorKeyToIdMapMap.find(pRenderInfo->getId());
774  assert(mapPos!=unexpandedColorKeyToIdMapMap.end());
775  colorKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pRenderInfo->getId(),mapPos->second));
776  mapPos=unexpandedGradientKeyToIdMapMap.find(pRenderInfo->getId());
777  assert(mapPos!=unexpandedGradientKeyToIdMapMap.end());
778  gradientKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pRenderInfo->getId(),mapPos->second));
779  mapPos=unexpandedLineEndingKeyToIdMapMap.find(pRenderInfo->getId());
780  assert(mapPos!=unexpandedLineEndingKeyToIdMapMap.end());
781  lineEndingKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pRenderInfo->getId(),mapPos->second));
782  if(s.find_first_not_of(" \t\n\r") != std::string::npos)
783  {
784  // we have to create the entry for the referenced render info
785  size_t i,iMax=renderInformationVector.size();
786  for(i=0; i < iMax; ++i)
787  {
788  if(renderInformationVector.get(i)->getId() == "s")
789  {
790  break;
791  }
792  }
793  if(i != iMax)
794  {
795  // we found the render information that goes with the id
796 
797  SBMLDocumentLoader::expandKeyToIdMaps(dynamic_cast<RenderInformationBase*>(renderInformationVector.get(i)),
798  renderInformationVector,
799  colorKeyToIdMapMap,
800  gradientKeyToIdMapMap,
801  lineEndingKeyToIdMapMap,
802  unexpandedColorKeyToIdMapMap,
803  unexpandedGradientKeyToIdMapMap,
804  unexpandedLineEndingKeyToIdMapMap,
805  chain,
806  expandedGlobalColorKeyToIdMapMap,
807  expandedGlobalGradientKeyToIdMapMap,
808  expandedGlobalLineEndingKeyToIdMapMap
809  );
810  }
811  // add the nonexistent ids from the references render info to this one
812  SBMLDocumentLoader::combineMaps(colorKeyToIdMapMap[renderInformationVector.get(i)->getId()],colorKeyToIdMapMap[pRenderInfo->getId()]);
813  SBMLDocumentLoader::combineMaps(gradientKeyToIdMapMap[renderInformationVector.get(i)->getId()],gradientKeyToIdMapMap[pRenderInfo->getId()]);
814  SBMLDocumentLoader::combineMaps(lineEndingKeyToIdMapMap[renderInformationVector.get(i)->getId()],lineEndingKeyToIdMapMap[pRenderInfo->getId()]);
815  }
816  }
817 }
818  */
void addReactionGlyph(CLReactionGlyph *glyph)
Definition: CLayout.cpp:251
void addGeneralGlyph(CLGeneralGlyph *glyph)
Definition: CLayout.cpp:263
void addLocalRenderInformation(CLLocalRenderInformation *pRenderInfo)
Definition: CLayout.cpp:646
const CCopasiVector< CLLocalRenderInformation > & getListOfLocalRenderInformationObjects() const
Definition: CLayout.h:149
CCopasiObject * get(const std::string &key)
static void convertLayoutObjectKeys(LocalStyle &style, const std::map< std::string, std::string > &keyToIdMap)
const std::set< std::string > & getKeyList() const
CLLocalStyle * getStyle(size_t i)
void addGlobalRenderInformation(CLGlobalRenderInformation *pRenderInfo)
#define C_INT32
Definition: copasi.h:90
void addMetaboliteGlyph(CLMetabGlyph *glyph)
Definition: CLayout.cpp:245
static CLayout * createLayout(const Layout &sbmlLayout, const std::map< std::string, std::string > &modelmap, std::map< std::string, std::string > &layoutmap, const std::map< std::string, std::string > &globalIdToKeyMap, const CCopasiContainer *pParent=NULL)
void addTextGlyph(CLTextGlyph *glyph)
Definition: CLayout.cpp:257
static void convertLayoutObjectIds(CLLocalStyle &style, const std::map< std::string, std::string > &idToKeyMap)
void setGraphicalObjectKey(const std::string &k)
Definition: CLGlyphs.cpp:338
void addLayout(CLayout *layout, const std::map< std::string, std::string > &m)
void addCompartmentGlyph(CLCompartmentGlyph *glyph)
Definition: CLayout.cpp:239
static void combineMaps(const std::map< std::string, std::string > &sourceMap, std::map< std::string, std::string > &destMap)
static CKeyFactory * getKeyFactory()
static std::string getIdFromSBase(const SBase *s)
Definition: SBMLUtils.cpp:32
const std::string & getKey() const
static void postprocessTextGlyph(const TextGlyph &sbml, const std::map< std::string, std::string > &layoutmap)
static void readListOfLayouts(CListOfLayouts &lol, const ListOf &sbmlList, const std::map< CCopasiObject *, SBase * > &copasimodelmap)
void setKeyList(const std::set< std::string > &keyList)
const CCopasiVector< CLGlobalRenderInformation > & getListOfGlobalRenderInformationObjects() const