openModeller  Version 1.5.0
om_model.cpp
Go to the documentation of this file.
1 /*************************************************************************************
2  * A simple command line app to create an openModeller model
3  * -------------------
4  * begin : November 2005
5  * copyright : (C) 2005 by T.Sutton, Kevin Ruland, Renato De Giovanni
6  * email : tim@linfiniti.com
7  *************************************************************************************/
8 
9 /***************************************************************************
10  *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include <openmodeller/om.hh>
18 #include <openmodeller/Log.hh>
20 
21 #include "getopts/getopts.h"
22 
23 #include "om_cmd_utils.hh"
24 
25 #include <fstream> // file I/O for XML
26 #include <sstream> // ostringstream datatype
27 #include <stdio.h> // file I/O for log
28 #include <time.h> // used to limit the number of times that the progress is written to a file
29 #include <string> // string library
30 #include <stdexcept> // try/catch
31 
32 using namespace std;
33 
35 int main( int argc, char **argv ) {
36 
37  Options opts;
38  int option;
39 
40  // command-line parameters (short name, long name, description, take args)
41  opts.addOption( "v", "version" , "Display version info" , false );
42  opts.addOption( "r", "xml-req" , "Model creation request file in XML" , true );
43  opts.addOption( "m", "model-file" , "File to store the generated model" , true );
44  opts.addOption( "" , "log-level" , "Set the log level (debug, warn, info, error)", true );
45  opts.addOption( "" , "log-file" , "Log file" , true );
46  opts.addOption( "" , "prog-file" , "File to store model creation progress" , true );
47  opts.addOption( "c", "config-file" , "Configuration file for openModeller" , true );
48 
49  std::string log_level("info");
50  std::string request_file;
51  std::string model_file;
52  std::string log_file;
53  std::string progress_file;
54  std::string config_file;
55 
56  if ( ! opts.parse( argc, argv ) ) {
57 
58  opts.showHelp( argv[0] );
59  exit(0);
60  }
61 
62  // Set up any related external resources
64 
65  OpenModeller om;
66 
67  while ( ( option = opts.cycle() ) >= 0 ) {
68 
69  switch ( option ) {
70 
71  case 0:
72  printf( "om_model %s\n", om.getVersion().c_str() );
73  printf("This is free software; see the source for copying conditions. There is NO\n");
74  printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
75  exit(0);
76  break;
77  case 1:
78  request_file = opts.getArgs( option );
79  break;
80  case 2:
81  model_file = opts.getArgs( option );
82  break;
83  case 3:
84  log_level = opts.getArgs( option );
85  break;
86  case 4:
87  log_file = opts.getArgs( option );
88  break;
89  case 5:
90  progress_file = opts.getArgs( option );
91  break;
92  case 6:
93  config_file = opts.getArgs( option );
94  break;
95  default:
96  break;
97  }
98  }
99 
100  // Log stuff
101 
102  Log::Level level_code = getLogLevel( log_level );
103 
104  if ( ! log_file.empty() ) {
105 
106  Log::instance()->set( level_code, log_file, "" );
107  }
108  else {
109 
110  // Just set the level - things will go to stderr
111  Log::instance()->setLevel( level_code );
112  }
113 
114  // om configuration
115  if ( ! config_file.empty() ) {
116 
117  Settings::loadConfig( config_file );
118  }
119 
120  // Check parameters
121 
122  if ( request_file.empty() ) {
123 
124  printf( "Please specify a model creation request file in XML\n");
125  exit(-1);
126  }
127 
128  // Initialize progress data if user wants to track progress
129  progress_data prog_data;
130 
131  if ( ! progress_file.empty() ) {
132 
133  prog_data.file_name = progress_file;
134 
135  time( &prog_data.timestamp );
136 
137  prog_data.progress = -1.0; // queued
138 
139  // Always create initial file with progress 0
140  progressFileCallback( 0.0, &prog_data );
141  }
142 
143  // Real work
144 
145  try {
146 
147  // Load algorithms and instantiate controller class
149 
150  // If user wants to track progress
151  if ( ! progress_file.empty() ) {
152 
153  // Set callback to write to a file
154  om.setModelCallback( progressFileCallback, &prog_data );
155  }
156  else if ( ! model_file.empty() ) {
157 
158  // Default callback will display progress on screen when a model file was specified
159  // (which means the model won't be sent to stdout)
161  }
162 
163  ConfigurationPtr input = Configuration::readXml( request_file.c_str() );
164  om.setModelConfiguration( input );
165 
166  om.createModel();
167 
168  om.calculateModelStatistics( input );
169 
171 
172  std::ostringstream model_output;
173 
174  Configuration::writeXml( output, model_output );
175 
176  std::cerr << flush;
177 
178  // Write model output to file, if requested
179  if ( ! model_file.empty() ) {
180 
181  ofstream file( model_file.c_str() );
182  file << model_output.str();
183  file.close();
184  }
185  else {
186 
187  // Otherwise send it to stdout
188  std::cout << model_output.str().c_str() << endl << flush;
189  }
190 
191  // If user wants to track progress
192  if ( ! progress_file.empty() ) {
193 
194  // Check if job was completed
195  if ( prog_data.progress != 1 ) {
196 
197  // -2 means aborted
198  progressFileCallback( -2.0, &prog_data );
199  }
200  }
201  }
202  catch ( runtime_error e ) {
203 
204  // If user is tracking progress
205  if ( ! progress_file.empty() ) {
206 
207  // -2 means aborted
208  progressFileCallback( -2.0, &prog_data );
209  }
210 
211  printf( "om_model aborted: %s\n", e.what() );
212  }
213 }
static void loadConfig(const std::string configFile)
Definition: Settings.cpp:100
static ConfigurationPtr readXml(char const *filename)
void setModelCallback(ModelCreationCallback func, void *param=0)
void calculateModelStatistics(const ConstConfigurationPtr &)
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
std::string file_name
Definition: om_cmd_utils.hh:42
ConfigurationPtr getModelConfiguration() const
Log::Level getLogLevel(std::string level)
Level
Definition: Log.hh:54
void setLevel(Level level)
Definition: Log.hh:107
int main(int argc, char **argv)
Main code.
Definition: om_model.cpp:35
void setupExternalResources()
Definition: os_specific.cpp:95
void set(Level level, std::string fileName, char const *pref="")
Definition: Log.cpp:196
void progressDisplayCallback(float progress, void *extraParam)
static void writeXml(const ConstConfigurationPtr &config, char const *fileaname)
static int searchDefaultDirs()
std::string getVersion()
time_t timestamp
Definition: om_cmd_utils.hh:43
void setModelConfiguration(const ConstConfigurationPtr &)
void progressFileCallback(float progress, void *progressData)