COPASI API  4.16.103
CQCompartment.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 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 "CQCompartment.h"
16 
17 #include "CQExpressionWidget.h"
18 #include "CQMessageBox.h"
20 #include "qtUtilities.h"
21 
23 #include "model/CModel.h"
24 #include "model/CMetab.h"
25 #include "model/CCompartment.h"
26 #include "model/CChemEqInterface.h"
27 #include "model/CModelExpansion.h" //for Copy button and options
28 #include "model/CReactionInterface.h" //for Copy button internal reactions only
29 #include "function/CExpression.h"
30 #include "report/CKeyFactory.h"
33 
34 /*
35  * Constructs a CQCompartment which is a child of 'parent', with the
36  * name 'name'.'
37  */
38 CQCompartment::CQCompartment(QWidget* parent, const char* name):
39  CopasiWidget(parent, name),
40  mItemToType(),
41  mpCompartment(NULL),
42  mChanged(false),
43  mExpressionValid(true),
44  mInitialExpressionValid(true)
45 {
46  setupUi(this);
47 
48  mpComboBoxType->insertItem(mpComboBoxType->count(), FROM_UTF8(CModelEntity::StatusName[CModelEntity::FIXED]));
49  mpComboBoxType->insertItem(mpComboBoxType->count(), FROM_UTF8(CModelEntity::StatusName[CModelEntity::ASSIGNMENT]));
50  mpComboBoxType->insertItem(mpComboBoxType->count(), FROM_UTF8(CModelEntity::StatusName[CModelEntity::ODE]));
51 
52  mItemToType.push_back(CModelEntity::FIXED);
53  mItemToType.push_back(CModelEntity::ASSIGNMENT);
54  mItemToType.push_back(CModelEntity::ODE);
55 
56  mpMetaboliteTable->horizontalHeader()->hide();
57 
58  mExpressionValid = false;
59  mpExpressionEMW->mpExpressionWidget->setExpressionType(CQExpressionWidget::TransientExpression);
60 
62  mpInitialExpressionEMW->mpExpressionWidget->setExpressionType(CQExpressionWidget::InitialExpression);
63 
64 #ifdef COPASI_EXTUNIT
65  mpLblDim->show();
66  mpComboBoxDim->show();
67 #else
68  mpLblDim->hide();
69  mpComboBoxDim->hide();
70 #endif
71 }
72 
73 /*
74  * Destroys the object and frees any allocated resources
75  */
77 {
78  // no need to delete child widgets, Qt does it all for us
79 }
80 
82 {
83  leave();
84 
85  std::string name = "compartment_1";
86  int i = 1;
87 
88  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
89 
90  while (!(mpCompartment = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->createCompartment(name)))
91  {
92  i++;
93  name = "compartment_";
94  name += TO_UTF8(QString::number(i));
95  }
96 
97  std::string key = mpCompartment->getKey();
98 
101 }
102 
104 {
105  CModel * pModel = mpDataModel->getModel();
106  CModelExpansion cModelExpObj = CModelExpansion(pModel);
107  CModelExpansion::SetOfModelElements compartmentObjectsToCopy;
108  CModelExpansion::ElementsMap origToCopyMappings;
109 
111  pDialog->exec();
112 
113  bool success = false;
114 
115  switch (pDialog->result())
116  {
117  case QDialog::Rejected:
118  break;
119 
120  case CQCompartmentCopyOptions::COMP: //compartment only
121 
122  compartmentObjectsToCopy.addObject(mpObject);
123  success = true;
124  break;
125 
126  case CQCompartmentCopyOptions::SPECIES: // include the species
127  {
128  compartmentObjectsToCopy.addObject(mpObject);
131 
132  for (itMetab = Metabolites.begin(); itMetab != Metabolites.end(); ++itMetab)
133  {
134  compartmentObjectsToCopy.addMetab(*itMetab);
135  }
136  }
137 
138  success = true;
139  break;
140 
141  case CQCompartmentCopyOptions::INTREAC: //also include the internal reactions
142  {
143  compartmentObjectsToCopy.addObject(mpObject);
144 
145  // Get all the compartment's species first
148 
149  for (itMetab = Metabolites.begin(); itMetab != Metabolites.end(); ++itMetab)
150  {
151  compartmentObjectsToCopy.addMetab(*itMetab);
152  }
153 
154  // Now get the reactions which are not multi-compartment
157  CReactionInterface * pRi = new CReactionInterface(pModel);
158 
159  for (; it != end; ++it)
160  {
161  pRi->initFromReaction((*it)->getKey());
162 
163  if (!pRi->isMulticompartment())
164  {
165  if (pRi->getChemEqInterface().getCompartment()->getKey() == mKey)
166  compartmentObjectsToCopy.addReaction(*it);
167  }
168  }
169 
170  pdelete(pRi);
171  success = true;
172  break;
173  }
174 
175  case CQCompartmentCopyOptions::ALLREAC: //get everything in compartment
176 
177  compartmentObjectsToCopy.addObject(mpObject);
178  compartmentObjectsToCopy.fillDependencies(pModel);
179  success = true;
180  break;
181  }
182 
183  pdelete(pDialog);
184 
185  if (success)
186  {
187  cModelExpObj.duplicate(compartmentObjectsToCopy, "_copy", origToCopyMappings);
188 
193  }
194 }
195 
197 {
198  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
200  assert(pDataModel != NULL);
201  CModel * pModel = pDataModel->getModel();
202 
203  if (pModel == NULL) return;
204 
205  if (mpCompartment == NULL) return;
206 
207  QMessageBox::StandardButton choice =
208  CQMessageBox::confirmDelete(this, "compartment",
211 
212  switch (choice)
213  {
214  case QMessageBox::Ok:
215  {
216  pDataModel->getModel()->removeCompartment(mKey);
217 
219  protectedNotify(ListViews::COMPARTMENT, ListViews::DELETE, ""); //Refresh all as there may be dependencies.
220  break;
221  }
222 
223  default:
224  break;
225  }
226 }
227 
228 /*!
229  If the simulation type is changed then COPASI will automatically adjust its appearance,
230  especially correlating to the Expression Widget and its buttons.
231  */
233 {
234  QString Units;
235 
236  const CModel * pModel = NULL;
237 
238  if (mpCompartment != NULL)
239  pModel = dynamic_cast<const CModel *>(mpCompartment->getObjectAncestor("Model"));
240 
241  switch ((CModelEntity::Status) mItemToType[type])
242  {
243  case CModelEntity::FIXED:
244  mpLblExpression->hide();
245  mpExpressionEMW->hide();
246 
247  mpBoxUseInitialExpression->setEnabled(true);
248  slotInitialTypeChanged(mpBoxUseInitialExpression->isChecked());
249  break;
250 
252 
253  if (pModel)
254  Units = FROM_UTF8(pModel->getVolumeUnitsDisplayString());
255 
256  if (!Units.isEmpty())
257  Units = " (" + Units + ")";
258 
259  mpLblExpression->setText("Expression" + Units);
260 
261  mpLblExpression->show();
262  mpExpressionEMW->show();
263 
264  mpBoxUseInitialExpression->setEnabled(false);
265  slotInitialTypeChanged(false);
266 
267  mpExpressionEMW->updateWidget();
268  break;
269 
270  case CModelEntity::ODE:
271 
272  if (pModel)
273  Units = FROM_UTF8(pModel->getVolumeRateUnitsDisplayString());
274 
275  if (!Units.isEmpty())
276  Units = " (" + Units + ")";
277 
278  mpLblExpression->setText("Expression" + Units);
279 
280  mpLblExpression->show();
281  mpExpressionEMW->show();
282 
283  mpBoxUseInitialExpression->setEnabled(true);
284  slotInitialTypeChanged(mpBoxUseInitialExpression->isChecked());
285 
286  mpExpressionEMW->updateWidget();
287 
288  break;
289 
290  default:
291  break;
292  }
293 }
294 
295 /*!
296  This function is used in case of not FIXED type
297  */
298 void CQCompartment::slotInitialTypeChanged(bool useInitialAssignment)
299 {
300  if (useInitialAssignment)
301  {
302  mpLblInitialExpression->show();
303  mpInitialExpressionEMW->show();
304 
305  mpEditInitialVolume->setEnabled(false);
306  mpInitialExpressionEMW->updateWidget();
307  }
308  else
309  {
310  mpLblInitialExpression->hide();
311  mpInitialExpressionEMW->hide();
312 
313  mpEditInitialVolume->setEnabled((CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()] != CModelEntity::ASSIGNMENT);
314  }
315 }
316 
317 /*!
318  */
319 
321 {
322  mExpressionValid = valid;
323 }
324 
326 {
327  mInitialExpressionValid = valid;
328 }
329 
331 {
332  mpCompartment = dynamic_cast< CCompartment * >(mpObject);
333 
334  load();
335 
336  return true;
337 }
338 
340 {
341  if ((CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()] != CModelEntity::FIXED)
342  {
343  // -- Expression --
344  mpExpressionEMW->updateWidget();
345  }
346 
347  if (mpBoxUseInitialExpression->isChecked())
348  {
349  // -- Initial Expression --
350  mpInitialExpressionEMW->updateWidget();
351  }
352 
353  save();
354 
355  return true;
356 }
357 
359  ListViews::Action /* action */,
360  const std::string & /* key */)
361 {
362  if (!isVisible() || mIgnoreUpdates) return true;
363 
364  switch (objectType)
365  {
366  case ListViews::STATE:
367  case ListViews::MODEL:
370  load();
371  break;
372 
373  default:
374  break;
375  }
376 
377  return true;
378 }
379 
381 {
382  if (mpCompartment == NULL) return;
383 
384  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
385 
386  const CModel * pModel = NULL;
387 
388  if (mpCompartment != NULL)
389  pModel = dynamic_cast<const CModel *>(mpCompartment->getObjectAncestor("Model"));
390 
391  // Update the labels to reflect the model units
392  QString ValueUnits;
393 
394  if (pModel)
395  ValueUnits = FROM_UTF8(pModel->getVolumeUnitsDisplayString());
396 
397  if (!ValueUnits.isEmpty())
398  ValueUnits = " (" + ValueUnits + ")";
399 
400  QString RateUnits;
401 
402  if (pModel)
403  RateUnits = FROM_UTF8(pModel->getVolumeRateUnitsDisplayString());
404 
405  if (!RateUnits.isEmpty())
406  RateUnits = " (" + RateUnits + ")";
407 
408  mpLblInitialValue->setText("Initial Volume" + ValueUnits);
409  mpLblInitialExpression->setText("Initial Expression" + ValueUnits);
410  mpLblVolume->setText("Volume" + ValueUnits);
411  mpLblRate->setText("Rate" + RateUnits);
412 
413  //Dimensionality
414  mpComboBoxDim->setCurrentIndex(mpCompartment->getDimensionality());
415  //this assumes the indices of the entries in the combobox correspond 1to1 to the values of dimensionality
416 
417  // Simulation Type
418  mpComboBoxType->setCurrentIndex(mpComboBoxType->findText(FROM_UTF8(CModelEntity::StatusName[mpCompartment->getStatus()])));
419 
420  // Initial Volume
421  mpEditInitialVolume->setText(QString::number(mpCompartment->getInitialValue(), 'g', 10));
422 
423  // Transient Volume
424  mpEditCurrentVolume->setText(QString::number(mpCompartment->getValue(), 'g', 10));
425 
426  // Concentration Rate
427  mpEditRate->setText(QString::number(mpCompartment->getRate(), 'g', 10));
428 
429  // Expression
430  mpExpressionEMW->mpExpressionWidget->setExpression(mpCompartment->getExpression());
431  mpExpressionEMW->updateWidget();
432 
433  // Initial Expression
434  mpInitialExpressionEMW->mpExpressionWidget->setExpression(mpCompartment->getInitialExpression());
435  mpInitialExpressionEMW->updateWidget();
436 
437  // Type dependent display of values
438  slotTypeChanged(mpComboBoxType->currentIndex());
439 
440  // Use Initial Expression
443  {
444  mpBoxUseInitialExpression->setChecked(false);
445  }
446  else
447  {
448  mpBoxUseInitialExpression->setChecked(true);
449  }
450 
452 
453  mChanged = false;
454  return;
455 }
456 
458 {
459 
460  if (mpCompartment == NULL) return;
461 
462 #ifdef COPASI_EXTUNIT
463 
464  //Dimensionality
465  if ((C_INT32)mpCompartment->getDimensionality() != mpComboBoxDim->currentIndex()) //this makes assumptions about the order of entries in the combo box!
466  {
467  mpCompartment->setDimensionality(mpComboBoxDim->currentIndex());
468  mChanged = true;
469  }
470 
471 #endif
472 
473  // Type
474  if (mpCompartment->getStatus() != (CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()])
475  {
476  mpCompartment->setStatus((CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()]);
477  mChanged = true;
478  }
479 
480  // Initial Volume
481  if (QString::number(mpCompartment->getInitialValue(), 'g', 10) != mpEditInitialVolume->text())
482  {
483  mpCompartment->setInitialValue(mpEditInitialVolume->text().toDouble());
484  mChanged = true;
485  }
486 
487  // Expression
488  if (mpCompartment->getExpression() != mpExpressionEMW->mpExpressionWidget->getExpression())
489  {
490  mpCompartment->setExpression(mpExpressionEMW->mpExpressionWidget->getExpression());
491  mChanged = true;
492  }
493 
494  // Initial Expression
495  if ((CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()] != CModelEntity::ASSIGNMENT)
496  {
497  if (mpBoxUseInitialExpression->isChecked() &&
498  mpCompartment->getInitialExpression() != mpInitialExpressionEMW->mpExpressionWidget->getExpression())
499  {
500  mpCompartment->setInitialExpression(mpInitialExpressionEMW->mpExpressionWidget->getExpression());
501  mChanged = true;
502  }
503  else if (!mpBoxUseInitialExpression->isChecked() &&
505  {
507  mChanged = true;
508  }
509  }
510 
511  if (mChanged)
512  {
513  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
514  (*CCopasiRootContainer::getDatamodelList())[0]->changed();
516  }
517 
518  mChanged = false;
519 }
520 
522 {}
523 
525 {
526  if (mpCompartment == NULL) return;
527 
528  QTableWidgetItem * pItem = mpMetaboliteTable->item(row, col);
529 
530  std::string s1, s2;
531  s1 = TO_UTF8(pItem->text());
532 
533  CCopasiContainer::objectMap::const_iterator it =
535  CCopasiContainer::objectMap::const_iterator end =
537 
538  for (; it != end; ++it)
539  if (dynamic_cast< CMetab * >(it->second) != NULL)
540  {
541  s2 = it->second->getObjectName();
542 
543  if (s1 == s2)
544  mpListView->switchToOtherWidget(C_INVALID_INDEX, it->second->getKey());
545  }
546 }
547 
549 {
550  if (mpCompartment == NULL) return;
551 
552  mpMetaboliteTable->setRowCount(mpCompartment->getMetabolites().size());
553 
554  CCopasiContainer::objectMap::const_iterator it =
556  CCopasiContainer::objectMap::const_iterator end =
558 
559  for (int i = 0; it != end; ++it)
560  {
561  if (dynamic_cast< CMetab * >(it->second) != NULL)
562  {
563  mpMetaboliteTable->setItem(i++, 0, new QTableWidgetItem(FROM_UTF8(it->second->getObjectName())));
564  }
565  }
566 
567  mpMetaboliteTable->resizeRowsToContents();
568 
569  return;
570 }
Header file of class CExpression.
CCopasiContainer * getObjectAncestor(const std::string &type) const
#define pdelete(p)
Definition: copasi.h:215
objectType
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
CCopasiVectorNS< CMetab > & getMetabolites()
static const std::string StatusName[]
Definition: CModelValue.h:67
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
const C_FLOAT64 & getRate() const
void addReaction(const CReaction *x)
const std::string & getObjectName() const
bool setDimensionality(unsigned C_INT32 dim)
const CChemEqInterface & getChemEqInterface() const
unsigned C_INT32 getDimensionality() const
virtual size_t size() const
void slotInitialExpressionValid(bool valid)
iterator begin()
virtual const objectMap & getObjects() const
bool removeCompartment(const size_t index, const bool &recursive=true)
Definition: CModel.cpp:2720
void initFromReaction(const std::string &key)
CCopasiObject * mpObject
Definition: copasiWidget.h:64
std::string getVolumeRateUnitsDisplayString() const
Definition: CModel.cpp:4571
const CCompartment * getCompartment() const
bool setInitialExpression(const std::string &expression)
#define C_INVALID_INDEX
Definition: copasi.h:222
std::string getExpression() const
virtual bool leave()
#define C_INT32
Definition: copasi.h:90
virtual bool enterProtected()
virtual std::set< const CCopasiObject * > getDeletedObjects() const
bool duplicate(const SetOfModelElements &source, const std::string &index, ElementsMap &emap)
bool mIgnoreUpdates
Definition: copasiWidget.h:67
const C_FLOAT64 & getInitialValue() const
void slotTypeChanged(int type)
iterator end()
void slotInitialTypeChanged(bool useInitialAssignment)
std::string getVolumeUnitsDisplayString() const
Definition: CModel.cpp:4547
void loadMetaboliteTable()
ListViews * mpListView
Definition: copasiWidget.h:62
bool mExpressionValid
Definition: CQCompartment.h:47
virtual const std::string & getKey() const
virtual void setStatus(const CModelEntity::Status &status)
static StandardButton confirmDelete(QWidget *parent, const QString &objectType, const QString &objects, const std::set< const CCopasiObject * > &deletedObjects)
CCompartment * mpCompartment
Definition: CQCompartment.h:45
std::string getInitialExpression() const
std::string getDuplicateKey(const std::string &sourceKey) const
find the key of the duplicated object from the source object (if it exists)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
bool mInitialExpressionValid
Definition: CQCompartment.h:48
std::vector< int > mItemToType
Definition: CQCompartment.h:44
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
Header file of class CCopasiContainer.
void fillDependencies(const CModel *pModel)
CCopasiDataModel * mpDataModel
Definition: copasiWidget.h:65
bool addObject(const CCopasiObject *x)
const C_FLOAT64 & getValue() const
void slotMetaboliteTableCurrentChanged(int row, int col)
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
virtual void setInitialValue(const C_FLOAT64 &initialValue)
Definition: CModel.h:50
const CModelEntity::Status & getStatus() const
CQCompartment(QWidget *parent=0, const char *name=0)
bool setExpression(const std::string &expression)
bool isMulticompartment() const
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
std::string mKey
Definition: copasiWidget.h:63
void slotExpressionValid(bool valid)