COPASI API  4.16.103
CMetabNameInterface.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) 2003 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 //
16 //
17 // C++ Implementation: $MODULE$
18 //
19 // Description:
20 //
21 //
22 // Author: sven <sven@ares>, (C) 2003
23 //
24 // Copyright: See COPYING file that comes with this distribution
25 //
26 //
27 
28 #include "copasi.h"
29 
30 #include "CMetabNameInterface.h"
31 #include "CModel.h"
32 #include "CChemEqParser.h"
33 
35 #include "report/CKeyFactory.h"
37 
39 {}
40 
41 std::string CMetabNameInterface::getDisplayName(const CModel* model, const std::string & key, const bool & quoted)
42 {
43  CMetab * metab = dynamic_cast< CMetab * >(CCopasiRootContainer::getKeyFactory()->get(key));
44 
45  if (metab)
46  return getDisplayName(model, *metab, quoted);
47  else
48  return "";
49 }
50 
51 std::string CMetabNameInterface::getDisplayName(const CModel* model, const CMetab & metab, const bool & quoted)
52 {
53  return getDisplayName(model, metab.getObjectName(), metab.getCompartment()->getObjectName(), quoted);
54 }
55 
57  const std::string & metabolite,
58  const std::string & compartment,
59  const bool & quoted)
60 {
61  std::string DefaultCompartment;
62 
63  if (model->getCompartments().size() == 0)
64  DefaultCompartment = "compartment";
65  else
66  DefaultCompartment = model->getCompartments()[0]->getObjectName();
67 
68  std::string Metabolite = quoted ? quote(metabolite, "{}") : metabolite;
69 
70  if (quoted && isNumber(Metabolite))
71  Metabolite = "\"" + Metabolite + "\"";
72 
73  if ((CMetabNameInterface::doesExist(model, metabolite, compartment) &&
74  CMetabNameInterface::isUnique(model, metabolite)) ||
75  (!CMetabNameInterface::doesExist(model, metabolite, "") &&
76  compartment == DefaultCompartment))
77  return Metabolite;
78 
79  std::string Compartment = quoted ? quote(compartment, "{}") : compartment;
80 
81  if ((quoted && isNumber(Compartment)) ||
82  (Compartment.find(' ') != std::string::npos && Compartment.find('"') == std::string::npos))
83  Compartment = "\"" + Compartment + "\"";
84 
85  return Metabolite + '{' + Compartment + '}';
86 }
87 
89  const std::string & metabolite,
90  const std::string & compartment)
91 {
92  CMetab * metab = getMetabolite(model, metabolite, compartment);
93 
94  if (metab)
95  return metab->getKey();
96  else
97  return "";
98 }
99 
101  const std::string & metabolite,
102  const std::string & compartment)
103 {
104  size_t Index;
105 
106  if (compartment != "")
107  {
108  Index = model->getCompartments().getIndex(compartment);
109 
110  if (Index != C_INVALID_INDEX)
111  {
112  CCompartment *pCompartment = model->getCompartments()[Index];
113 
114  Index = pCompartment->getMetabolites().getIndex(metabolite);
115 
116  if (Index != C_INVALID_INDEX)
117  return pCompartment->getMetabolites()[Index];
118  }
119 
120  return NULL;
121  }
122 
123  Index = model->findMetabByName(metabolite);
124 
125  if (Index != C_INVALID_INDEX)
126  return model->getMetabolites()[Index];
127 
128  return NULL;
129 }
130 
131 bool CMetabNameInterface::isUnique(const CModel* model, const std::string & name)
132 {
133  bool unique = true;
134  size_t i;
135  const CCopasiVector< CMetab > & metabs = model->getMetabolites();
136  std::string metabName;
137 
138  for (i = 0; i < metabs.size(); i++)
139  {
140  metabName = metabs[i]->getObjectName();
141 
142  if (metabName == name)
143  {
144  if (unique)
145  unique = false;
146  else
147  return false; //return true
148  }
149  }
150 
151  return true; //return unique;
152 }
153 
155  const std::string & metabolite,
156  const std::string & compartment)
157 {
158  if (compartment != "")
159  {
160  size_t Index = model->getCompartments().getIndex(compartment);
161 
162  if (Index == C_INVALID_INDEX) return false;
163 
164  Index = model->getCompartments()[Index]->getMetabolites().getIndex(metabolite);
165 
166  return (Index != C_INVALID_INDEX);
167  }
168  else
169  return (model->findMetabByName(metabolite) != C_INVALID_INDEX);
170 }
171 
172 // static
173 std::pair< std::string, std::string > CMetabNameInterface::splitDisplayName(const std::string & name)
174 {
175  // parse the description into a linked node tree
176  std::stringstream buffer((name.find('"') != std::string::npos ? name : quote(name)) + " ->");
177 
178  CChemEqParser Parser(&buffer);
179 
180  std::pair< std::string, std::string > Result;
181 
182  if (Parser.yyparse() != 0)
183  {
184  Result.first = "";
185  Result.second = "";
186  }
187  else
188  {
189  Result.first = Parser.getSubstrateNames()[0];
190  Result.second = Parser.getSubstrateCompartments()[0];
191  }
192 
193  return Result;
194 }
195 
196 // static
197 std::string CMetabNameInterface::unQuote(const std::string & displayName)
198 {
199  // parse the description into a linked node tree
200  std::stringstream buffer(displayName + " ->");
201 
202  CChemEqParser Parser(&buffer);
203 
204  std::pair< std::string, std::string > Names;
205 
206  if (Parser.yyparse() != 0)
207  {
208  Names.first = displayName;
209  Names.second = "";
210  }
211  else
212  {
213  Names.first = Parser.getSubstrateNames()[0];
214  Names.second = Parser.getSubstrateCompartments()[0];
215  }
216 
217  std::string Name = Names.first;
218 
219  if (Names.second != "")
220  {
221  std::string compartment = Names.second;
222 
223  if (compartment.find(' '))
224  compartment = "\"" + compartment + "\"";
225 
226  Name += "{" + compartment + "}";
227  }
228 
229  return Name;
230 }
231 
232 #ifdef XXXX
233 std::string CMetabNameInterface::extractCompartmentName(const std::string & name)
234 {
235  // parse the description into a linked node tree
236  std::stringstream buffer(name);
237  buffer << " ->";
238 
239  CChemEqParser Parser(&buffer);
240 
241  if (Parser.yyparse() != 0) return "";
242 
243  return Parser.getSubstrateCompartments()[0];
244 }
245 
246 std::string CMetabNameInterface::extractMetabName(const std::string & name)
247 {
248  // parse the description into a linked node tree
249  std::stringstream buffer(name);
250  buffer << " ->";
251 
252  CChemEqParser Parser(&buffer);
253 
254  if (Parser.yyparse() != 0) return "";
255 
256  return Parser.getSubstrateNames()[0];
257 }
258 
259 bool CMetabNameInterface::isValidMetabDisplayName(const std::string & name)
260 {
261  // parse the description into a linked node tree
262  std::stringstream buffer(name);
263  buffer << " ->";
264 
265  CChemEqParser Parser(&buffer);
266 
267  return (Parser.yyparse() == 0);
268 }
269 #endif // XXXX
static std::string getMetaboliteKey(const CModel *model, const std::string &metabolite, const std::string &compartment)
bool isNumber(const std::string &str)
Definition: utility.cpp:75
CCopasiVectorNS< CMetab > & getMetabolites()
const CCopasiVector< CMetab > & getMetabolites() const
Definition: CModel.cpp:1051
static std::string unQuote(const std::string &displayName)
const std::string & getObjectName() const
static CMetab * getMetabolite(const CModel *model, const std::string &metabolite, const std::string &compartment)
virtual size_t size() const
const std::vector< std::string > & getSubstrateCompartments() const
CCopasiObject * get(const std::string &key)
#define C_INVALID_INDEX
Definition: copasi.h:222
virtual size_t getIndex(const std::string &name) const
static std::string getDisplayName(const CModel *model, const std::string &key, const bool &quoted)
Definition: CMetab.h:178
size_t findMetabByName(const std::string &Target) const
Definition: CModel.cpp:1198
virtual const std::string & getKey() const
Header file of class CCopasiContainer.
std::string quote(const std::string &name, const std::string &additionalEscapes)
Definition: utility.cpp:144
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
static std::pair< std::string, std::string > splitDisplayName(const std::string &name)
static bool isUnique(const CModel *model, const std::string &name)
static CKeyFactory * getKeyFactory()
const std::vector< std::string > & getSubstrateNames() const
static bool doesExist(const CModel *model, const std::string &metabolite, const std::string &compartment)
Definition: CModel.h:50
const CCompartment * getCompartment() const
Definition: CMetab.cpp:222