COPASI API  4.16.103
arguments.cpp
Go to the documentation of this file.
1 // Copyright (C) 2012 - 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 #include "arguments.h"
7 #include "string.h"
8 
9 #include <QtCore/QString>
10 #include <QtCore/QFile>
11 #include <QtCore/QFileInfo>
12 
14 
15 #include "optimization/COptTask.h"
17 
22 
23 #include "qlayout/CQLayoutScene.h"
24 #include "layout/CListOfLayouts.h"
25 
28 
29 #include <iostream>
30 
31 Arguments::Arguments(int argc, char* argv[])
32  : mFilename("")
33  , mOutputDir("")
34  , mTask("")
35  , mReportFile("")
36  , mFileType("pdf")
37  , mRunScheduled(false)
38  , mSetSolutionStatistic(false)
39  , mDisableStatistic(false)
40  , mDisablePlots(false)
41  , mDisableRandomizeStartValues(false)
42  , mHideWindow(false)
43  , mQuitAfterTaskExecution(false)
44  , mClearTargets(false)
45  , mSwitchToTask(-1)
46  , mGenerateOutput(-1)
47  , mArgc(argc)
48  , mArgv(argv)
49 {
50  parseArgs(argc, argv);
51 }
52 
54 {
55  CCopasiVectorN<CCopasiTask> &taskList = *(*CCopasiRootContainer::getDatamodelList())[0]->getTaskList();
56 
57  for (size_t i = 0; i < taskList.size(); ++i)
58  {
59  CCopasiTask *current = taskList[i];
60 
61  if (current->isScheduled())
62  return current;
63  }
64 
65  return NULL;
66 }
67 
68 CCopasiTask* Arguments::getTaskForName(const std::string& name) const
69 {
70  CCopasiVectorN<CCopasiTask> &taskList = *(*CCopasiRootContainer::getDatamodelList())[0]->getTaskList();
71 
72  for (size_t i = 0; i < taskList.size(); ++i)
73  {
74  CCopasiTask *current = taskList[i];
75 
76  if (current->getObjectName() == name)
77  return current;
78  }
79 
80  return NULL;
81 }
82 
84 {
85  if (haveTaskName())
86  return getTaskForName(mTask);
87 
88  return getFirstScheduledTask();
89 }
90 
92 {
93  return mGenerateOutput != -1;
94 }
95 
97 {
98  return mGenerateOutput;
99 }
100 
102 {
103  return !mOutputDir.empty();
104 }
105 
106 const std::string& Arguments::getOutputDir() const
107 {
108  return mOutputDir;
109 }
110 
112 {
113  return !mReportFile.empty();
114 }
115 
117 {
118  if (!(mSaveLayout && haveFile() && haveOutputDir()))
119  return false;
120 
121  CCopasiRootContainer::init(0, NULL, false);
123 
124  try
125  {
126  if (!model.importSBML(mFilename, NULL))
127  model.loadModel(mFilename, NULL);
128  }
129  catch (CCopasiException &ex)
130  {
131  std::cerr << ex.getMessage().getAllMessageText() << std::endl;
132  return true;
133  }
134 
135  for (size_t i = 0; i < model.getListOfLayouts()->size(); ++i)
136  {
137  CLayout* layout = (*model.getListOfLayouts())[i];
138  CQLayoutScene scene(layout, &model);
139  scene.recreate();
140  scene.saveToFile(mOutputDir + "/" + QFileInfo(mFilename.c_str()).baseName().toStdString() + "_" + layout->getObjectName() + "." + mFileType, mFileType);
141  }
142 
143  return true;
144 }
145 
147 {
148  return mSaveLayout;
149 }
150 
151 const std::string& Arguments::getReportFile() const
152 {
153  return mReportFile;
154 }
155 
156 void Arguments::parseArgs(int argc, char* argv[])
157 {
158  for (int i = 1; i < argc; ++i)
159  {
160  QString current(argv[i]);
161  QString lower = current.toLower();
162 
163  if (lower == "-r" || lower == "--run-scheduled")
164  {
165  mRunScheduled = true;
166  }
167  else if (lower == "-q" || lower == "--quit-after-task-execution")
168  {
170  }
171  else if (i + 1 < argc && (lower == "--switch-to-task"))
172  {
173  mSwitchToTask = QString(argv[i + 1]).toInt();
174  ++i;
175  }
176  else if (i + 1 < argc && (lower == "--task"))
177  {
178  mTask = argv[i + 1];
179  ++i;
180  }
181  else if (lower == "--set-solution-statistic")
182  {
183  mSetSolutionStatistic = true;
184  }
185  else if (lower == "--disable-calculate-statistic")
186  {
187  mDisableStatistic = true;
188  }
189  else if (lower == "--disable-other-plots")
190  {
191  mDisablePlots = true;
192  }
193  else if (lower == "--hide")
194  {
195  mHideWindow = true;
196  }
197  else if (lower == "--clear-targets")
198  {
199  mClearTargets = true;
200  }
201  else if (lower == "--disable-randomize-startvalues")
202  {
204  }
205  else if (lower == "--save-layout")
206  {
207  mSaveLayout = true;
208  }
209  else if (i + 1 < argc && (lower == "-g" || lower == "--generate-output"))
210  {
211  mGenerateOutput = QString(argv[i + 1]).toInt();
212  ++i;
213  }
214  else if (i + 1 < argc && (lower == "-f" || lower == "--set-report-file"))
215  {
216  mReportFile = argv[i + 1];
217  ++i;
218  }
219  else if (i + 1 < argc && (lower == "-o" || lower == "--set-output-dir"))
220  {
221  mOutputDir = argv[i + 1];
222  ++i;
223  }
224  else if (i + 1 < argc && (lower == "-t" || lower == "--set-file-type"))
225  {
226  mFileType = argv[i + 1];
227  ++i;
228  }
229  else if (QFile(current).exists() && mFilename.empty())
230  {
231  // only if we don't have a file yet, we assume this to be the copasi file
232  mFilename = current.toStdString();
233  }
234  }
235 }
236 
238 {
239  return mSwitchToTask != -1;
240 }
241 
243 {
244  return mSwitchToTask;
245 }
246 
248 {
249  char ** result = (char**)malloc(sizeof(char*) * 2);
250  result[0] = strdup(mArgv[0]);
251  result[1] = strdup((mFilename + ".view.cps").c_str());
252  return result;
253 }
254 
255 const std::string& Arguments::getFilename() const
256 {
257  return mFilename;
258 }
259 
260 const std::string& Arguments::getFileType() const
261 {
262  return mFileType;
263 }
264 
266 {
267  return !mFilename.empty();
268 }
269 
271 {
272  return mRunScheduled;
273 }
274 
276 {
277  return !mTask.empty();
278 }
279 
280 const std::string& Arguments::getTaskName() const
281 {
282  return mTask;
283 }
284 
286 {
287  return mSetSolutionStatistic;
288 }
289 
291 {
292  return mDisableStatistic;
293 }
294 
296 {
298 }
299 
301 {
302  return mDisablePlots;
303 }
304 
306 {
307  return mHideWindow;
308 }
309 
311 {
313 }
314 
315 bool Arguments::isValid() const
316 {
317  if (!haveFile()) return false;
318 
319  if (!QFile(mFilename.c_str()).exists()) return false;
320 
321  return true;
322 }
323 
324 std::string Arguments::prepareModel() const
325 {
326  if (!isValid()) return "";
327 
329  model->loadModel(getFilename(), NULL);
330 
331  if (mDisablePlots)
332  {
333  for (size_t index = 0; index < model->getPlotDefinitionList()->size(); ++index)
334  {
335  (*model->getPlotDefinitionList())[index]->setActive(false);
336  }
337  }
338 
339  if (mClearTargets)
340  {
341  for (size_t index = 0; index < model->getTaskList()->size(); ++index)
342  {
343  (*model->getTaskList())[index]->getReport().setTarget("");
344  }
345  }
346 
347  CCopasiTask *task = getTask();
348 
349  if (task != NULL)
350  {
351  if (isGenerateOutput())
352  {
353  // calls initialize which is private
355 
356  // generate the output
358  }
359 
360  if (haveReportFile())
362 
363  COptTask* optTask = dynamic_cast<COptTask*>(task);
364 
365  if (optTask != NULL)
366  {
367  COptProblem *problem = (COptProblem *)optTask->getProblem();
368 
371 
373  problem->setRandomizeStartValues(false);
374 
375  if (isDisableStatistic())
376  problem->setCalculateStatistics(false);
377  }
378  }
379 
380  model->saveModel(getFilename() + ".view.cps", NULL, true);
381  return getFilename() + ".view.cps";
382 }
bool isSolutionStatistic() const
Definition: arguments.cpp:285
bool haveOutputDir() const
Definition: arguments.cpp:101
void saveToFile(const std::string &fileName, const std::string &fileType="pdf")
std::string mTask
Definition: arguments.h:84
bool isDisablePlots() const
Definition: arguments.cpp:300
bool mClearTargets
Definition: arguments.h:106
CCopasiTask * getTask() const
Definition: arguments.cpp:83
CCopasiProblem * getProblem()
void parseArgs(int arc, char *argv[])
Definition: arguments.cpp:156
bool mDisableStatistic
Definition: arguments.h:96
bool isHideWindow() const
Definition: arguments.cpp:305
const std::string & getObjectName() const
std::string mFileType
Definition: arguments.h:88
char ** mArgv
Definition: arguments.h:114
virtual size_t size() const
bool isGenerateOutput() const
Definition: arguments.cpp:91
int mSwitchToTask
Definition: arguments.h:108
const CCopasiMessage & getMessage() const
static std::string getAllMessageText(const bool &chronological=true)
bool isSaveLayout() const
Definition: arguments.cpp:146
bool isDisableStatistic() const
Definition: arguments.cpp:290
bool importSBML(const std::string &fileName, CProcessReport *pImportHandler=NULL, const bool &deleteOldData=true)
const bool & isScheduled() const
static CCopasiObject * createDefaultOutput(C_INT32 id, CCopasiTask *task, CCopasiDataModel *pDataModel, bool activate=true)
bool mDisablePlots
Definition: arguments.h:98
static CCopasiTask * getFirstScheduledTask()
Definition: arguments.cpp:53
bool handleCommandLine() const
Definition: arguments.cpp:116
std::string mOutputDir
Definition: arguments.h:82
char ** getInitArgs() const
Definition: arguments.cpp:247
bool mDisableRandomizeStartValues
Definition: arguments.h:100
static std::vector< C_INT32 > getListOfDefaultOutputDescriptions(const CCopasiTask *task=NULL)
bool haveReportFile() const
Definition: arguments.cpp:111
bool mSetSolutionStatistic
Definition: arguments.h:94
const std::string & getFilename() const
Definition: arguments.cpp:255
bool mQuitAfterTaskExecution
Definition: arguments.h:104
CCopasiVectorN< CCopasiTask > * getTaskList()
bool isQuitAfterTaskExecution() const
Definition: arguments.cpp:310
bool isSwitchToTask() const
Definition: arguments.cpp:237
std::string mFilename
Definition: arguments.h:80
const std::string & getOutputDir() const
Definition: arguments.cpp:106
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
bool mHideWindow
Definition: arguments.h:102
bool haveFile() const
Definition: arguments.cpp:265
bool loadModel(std::istream &in, const std::string &pwd, CProcessReport *pProcessReport, const bool &deleteOldData=true)
int getTaskToSwitchTo() const
Definition: arguments.cpp:242
CListOfLayouts * getListOfLayouts()
bool saveModel(const std::string &fileName, CProcessReport *pProcessReport, bool overwriteFile=false, const bool &autoSave=false)
Arguments(int argc, char *argv[])
Definition: arguments.cpp:31
virtual bool setMethodType(const int &type)
Definition: COptTask.cpp:156
void setTarget(const std::string &target)
Definition: CReport.cpp:92
static CCopasiDataModel * addDatamodel()
void setRandomizeStartValues(const bool &randomize)
CCopasiTask * getTaskForName(const std::string &name) const
Definition: arguments.cpp:68
const COutputDefinitionVector * getPlotDefinitionList() const
CReport & getReport()
int mGenerateOutput
Definition: arguments.h:110
const std::string & getReportFile() const
Definition: arguments.cpp:151
bool isValid() const
Definition: arguments.cpp:315
static void init(int argc, char *argv[], const bool &withGui=false)
bool isDisableRandomizeStartValues() const
Definition: arguments.cpp:295
std::string prepareModel() const
Definition: arguments.cpp:324
const std::string & getFileType() const
Definition: arguments.cpp:260
bool haveTaskName() const
Definition: arguments.cpp:275
bool isRunScheduled() const
Definition: arguments.cpp:270
std::string mReportFile
Definition: arguments.h:86
void setCalculateStatistics(const bool &calculate)
bool mSaveLayout
Definition: arguments.h:92
int getOutputToGenerate() const
Definition: arguments.cpp:96
bool mRunScheduled
Definition: arguments.h:90
const std::string & getTaskName() const
Definition: arguments.cpp:280