COPASI API  4.16.103
SBWTranslator.cpp
Go to the documentation of this file.
1 // Copyright (C) 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 // Main
7 //
8 // (C) Stefan Hoops 2002
9 //
10 
11 #include <stdlib.h>
12 #include <sstream>
13 #include <string>
14 #include <iostream>
15 
16 #include <SBWTranslator.h>
17 
18 #include <SBW/SBW.h>
19 
20 #define COPASI_MAIN
21 
22 #include <copasi/copasi.h>
28 
29 using namespace SystemsBiologyWorkbench;
30 using namespace std;
31 
34 
36 {
37 }
39 {
40 }
41 
42 void Copasi2SBMLTranslator::registerMethods(MethodTable<Copasi2SBMLTranslator> &table)
43 {
44  table.addMethod(&Copasi2SBMLTranslator::getName, "string getName()", false, "Return the name of this importer");
45  table.addMethod(&Copasi2SBMLTranslator::getVersion, "string getVersion()", false, "Return the version of this importer");
46  table.addMethod(&Copasi2SBMLTranslator::getAuthor, "string getAuthor()", false, "Return the corresponding author for this importer");
47  table.addMethod(&Copasi2SBMLTranslator::getDescription, "string getDescription()", false, "Return a description for this importer");
48  table.addMethod(&Copasi2SBMLTranslator::getName, "string getDisplayName()", false, "Return the display name for this importer.");
49  table.addMethod(&Copasi2SBMLTranslator::getCopyright, "string getCopyright()", false, "Return the copyright for this importer");
50  table.addMethod(&Copasi2SBMLTranslator::getURL, "string getURL()", false, "Return a URL for this reporter.");
51  table.addMethod(&Copasi2SBMLTranslator::getFileExtension, "string getFileExtension()", false, "Returns the file extension as a string");
52  table.addMethod(&Copasi2SBMLTranslator::getFileType, "string getFileType()", false, "Returns a string describing the type of file");
53  table.addMethod(&Copasi2SBMLTranslator::translate, "string translate(string)", false, "Translate SBML string to COPASI.");
54  table.addMethod(&Copasi2SBMLTranslator::translateToSBML, "string translateToSBML(string)", false, "Translate COPASI string to SBML.");
55  table.addMethod(&Copasi2SBMLTranslator::translateFileToSBML, "string translateFileToSBML(string)", false, "Translate COPASI string to SBML.");
56 }
57 
58 std::string Copasi2SBMLTranslator::translateFileToSBML(const std::string &fileName)
59 {
62  pDataModel->loadModel(fileName, NULL);
63  string sbml = pDataModel->exportSBMLToString(NULL, mLevel, mVersion);
64  return sbml;
65 }
66 
67 std::string Copasi2SBMLTranslator::translate(const std::string &sbmlString)
68 {
71  pDataModel->importSBMLFromString(sbmlString);
72 
73  string tempDir;
74  COptions::getValue("Tmp", tempDir);
75  string tempFile = "tmp.cps";
76  CDirEntry::makePathAbsolute(tempFile, tempDir);
77  tempFile = CDirEntry::fileName(tempFile);
78  pDataModel->saveModel(tempFile, NULL, true);
79  std::ifstream ifs(tempFile.c_str());
80  std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
81  ifs.close();
82  CDirEntry::remove(tempFile);
83  return str;
84 }
85 
86 std::string Copasi2SBMLTranslator::translateToSBML(const std::string &copasiString)
87 {
90 
91  istringstream str(copasiString);
92 
93  pDataModel->loadModel(str, COptions::getPWD(), NULL);
94  string sbml = pDataModel->exportSBMLToString(NULL, mLevel, mVersion);
95  return sbml;
96 }
97 
98 DataBlockWriter Copasi2SBMLTranslator::getName(Module from, DataBlockReader reader)
99 {
100  static string name("Copasi2SBML Translator");
101  return DataBlockWriter();
102 }
103 
104 DataBlockWriter Copasi2SBMLTranslator::getVersion(Module from, DataBlockReader reader)
105 {
106  static string version(CVersion::VERSION.getVersion());
107  return DataBlockWriter() << version;
108 }
109 
110 DataBlockWriter Copasi2SBMLTranslator::getAuthor(Module from, DataBlockReader reader)
111 {
112  static string author("Frank T. Bergmann");
113  return DataBlockWriter() << author;
114 }
115 DataBlockWriter Copasi2SBMLTranslator::getDescription(Module from, DataBlockReader reader)
116 {
117  static string description("This converter converts a COPASI model to an SBML file");
118  return DataBlockWriter();
119 }
120 DataBlockWriter Copasi2SBMLTranslator::getCopyright(Module from, DataBlockReader reader)
121 {
122  static string copy("(c) 2012 COPASI Team");
123  return DataBlockWriter() << copy;
124 }
125 DataBlockWriter Copasi2SBMLTranslator::getURL(Module from, DataBlockReader reader)
126 {
127  static string url("http://copasi.org");
128  return DataBlockWriter() << url;
129 }
130 DataBlockWriter Copasi2SBMLTranslator::getFileExtension(Module from, DataBlockReader reader)
131 {
132  static string extension(".cps");
133  return DataBlockWriter() << extension;
134 }
135 DataBlockWriter Copasi2SBMLTranslator::getFileType(Module from, DataBlockReader reader)
136 {
137  static string fileType("COPASI file");
138  return DataBlockWriter() << fileType;
139 }
140 DataBlockWriter Copasi2SBMLTranslator::translateFileToSBML(Module from, DataBlockReader reader)
141 {
142  std::string fileName; reader >> fileName;
143  return DataBlockWriter() << translateFileToSBML(fileName);
144 }
145 DataBlockWriter Copasi2SBMLTranslator::translate(Module from, DataBlockReader reader)
146 {
147  std::string model; reader >> model;
148  return DataBlockWriter() << translate(model);
149 }
150 DataBlockWriter Copasi2SBMLTranslator::translateToSBML(Module from, DataBlockReader reader)
151 {
152  std::string model; reader >> model;
153  return DataBlockWriter() << translateToSBML(model);
154 }
155 
156 int main(int argc, char *argv[])
157 {
158 
159  try
160  {
161  setlocale(LC_ALL, "C");
162  bool doTranslate = false;
163  string fileName;
164 
165  for (int i = 0; i < argc; i++)
166  {
167  string current(argv[i]);
168 
169  if (current == "-f" && i + 1 < argc)
170  {
171  doTranslate = true;
172  fileName = argv[i + 1];
173  i++;
174  }
175  }
176 
177  if (doTranslate)
178  {
179  cout << Copasi2SBMLTranslator::translateFileToSBML(fileName) << endl;
180  exit(0);
181  }
182 
183  ModuleImpl modImpl(
184  "org.copasi.copasi2sbml", // module identification
185  "Copasi to SBML Translator", // humanly readable name
186  UniqueModule); // management scheme
187 
188  modImpl.addServiceObject(
189  "translator", // service identification
190  "Copasi2SBML Translator", // humanly readable name
191  "plugin/sbmlTranslator/bidirectional", // category
192  new Copasi2SBMLTranslator()); // service implementation
193 
194  modImpl.run(argc, argv);
195  }
196  catch (SBWException *e)
197  {
198  fprintf(stderr, "Copasi2SBMLTranslator exception: %s\n", e->getMessage().c_str());
199  return -1;
200  }
201 
202  return 0;
203 }
static bool remove(const std::string &path)
Definition: CDirEntry.cpp:275
std::string exportSBMLToString(CProcessReport *pExportHandler, int sbmlLevel, int sbmlVersion)
static std::string fileName(const std::string &path)
Definition: CDirEntry.cpp:119
static std::string getPWD(void)
Definition: COptions.cpp:338
int main(int argc, char *argv[])
SystemsBiologyWorkbench::DataBlockWriter getAuthor(SystemsBiologyWorkbench::Module from, SystemsBiologyWorkbench::DataBlockReader reader)
static std::string translateFileToSBML(const std::string &fileName)
static std::string translateToSBML(const std::string &copasiString)
SystemsBiologyWorkbench::DataBlockWriter getVersion(SystemsBiologyWorkbench::Module from, SystemsBiologyWorkbench::DataBlockReader reader)
SystemsBiologyWorkbench::DataBlockWriter getDescription(SystemsBiologyWorkbench::Module from, SystemsBiologyWorkbench::DataBlockReader reader)
bool loadModel(std::istream &in, const std::string &pwd, CProcessReport *pProcessReport, const bool &deleteOldData=true)
SystemsBiologyWorkbench::DataBlockWriter getURL(SystemsBiologyWorkbench::Module from, SystemsBiologyWorkbench::DataBlockReader reader)
bool saveModel(const std::string &fileName, CProcessReport *pProcessReport, bool overwriteFile=false, const bool &autoSave=false)
static void registerMethods(SystemsBiologyWorkbench::MethodTable< Copasi2SBMLTranslator > &table)
static void getValue(const std::string &name, CType &value)
Definition: COptions.h:124
static CCopasiDataModel * addDatamodel()
static std::string translate(const std::string &sbmlString)
SystemsBiologyWorkbench::DataBlockWriter getFileType(SystemsBiologyWorkbench::Module from, SystemsBiologyWorkbench::DataBlockReader reader)
bool importSBMLFromString(const std::string &sbmlDocumentText, CProcessReport *pImportHandler=NULL, const bool &deleteOldData=true)
SystemsBiologyWorkbench::DataBlockWriter getName(SystemsBiologyWorkbench::Module from, SystemsBiologyWorkbench::DataBlockReader reader)
static void init(int argc, char *argv[], const bool &withGui=false)
static bool makePathAbsolute(std::string &relativePath, const std::string &absoluteTo)
Definition: CDirEntry.cpp:481
SystemsBiologyWorkbench::DataBlockWriter getCopyright(SystemsBiologyWorkbench::Module from, SystemsBiologyWorkbench::DataBlockReader reader)
SystemsBiologyWorkbench::DataBlockWriter getFileExtension(SystemsBiologyWorkbench::Module from, SystemsBiologyWorkbench::DataBlockReader reader)
virtual ~Copasi2SBMLTranslator()
static const CVersion VERSION
Definition: CVersion.h:164