00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef OPENMS_APPLICATIONS_TOPPBASE_H
00028 #define OPENMS_APPLICATIONS_TOPPBASE_H
00029
00030 #include <OpenMS/DATASTRUCTURES/Param.h>
00031 #include <OpenMS/CONCEPT/Exception.h>
00032 #include <OpenMS/CONCEPT/ProgressLogger.h>
00033 #include <OpenMS/DATASTRUCTURES/String.h>
00034 #include <OpenMS/DATASTRUCTURES/StringList.h>
00035
00036 #include <iostream>
00037 #include <fstream>
00038 class QStringList;
00039
00040 namespace OpenMS
00041 {
00042
00043 namespace Exception
00044 {
00046 class UnregisteredParameter
00047 : public Exception::Base
00048 {
00049 public:
00050 UnregisteredParameter( const char* file, int line, const char* function, const String& parameter )
00051 : Base( file, line, function, "UnregisteredParameter", parameter )
00052 {
00053 globalHandler.setMessage( what_ );
00054 }
00055 };
00057 class WrongParameterType
00058 : public Exception::Base
00059 {
00060 public:
00061 WrongParameterType( const char* file, int line, const char* function, const String& parameter )
00062 : Base( file, line, function, "WrongParameterType", parameter )
00063 {
00064 globalHandler.setMessage( what_ );
00065 }
00066 };
00068 class RequiredParameterNotGiven
00069 : public Exception::Base
00070 {
00071 public:
00072 RequiredParameterNotGiven( const char* file, int line, const char* function, const String& parameter )
00073 : Base( file, line, function, "RequiredParameterNotGiven", parameter )
00074 {
00075 globalHandler.setMessage( what_ );
00076 }
00077 };
00078 }
00079
00095 class TOPPBase
00096 {
00097 public:
00098
00100 enum ExitCodes
00101 {
00102 EXECUTION_OK,
00103 INPUT_FILE_NOT_FOUND,
00104 INPUT_FILE_NOT_READABLE,
00105 INPUT_FILE_CORRUPT,
00106 INPUT_FILE_EMPTY,
00107 CANNOT_WRITE_OUTPUT_FILE,
00108 ILLEGAL_PARAMETERS,
00109 MISSING_PARAMETERS,
00110 UNKNOWN_ERROR,
00111 EXTERNAL_PROGRAM_ERROR,
00112 PARSE_ERROR,
00113 INCOMPATIBLE_INPUT_DATA,
00114 INTERNAL_ERROR
00115 };
00116
00118 TOPPBase( const String& tool_name, const String& tool_description );
00119
00121 virtual ~TOPPBase();
00122
00124 ExitCodes main(int argc, const char** argv);
00125
00127 struct ParameterInformation
00128 {
00130 enum ParameterTypes
00131 {
00132 NONE = 0,
00133 STRING,
00134 INPUT_FILE,
00135 OUTPUT_FILE,
00136 DOUBLE,
00137 INT,
00138 FLAG,
00139 TEXT,
00140 NEWLINE
00141 };
00142
00144 String name;
00146 ParameterTypes type;
00148 String default_value;
00150 String description;
00152 String argument;
00154 bool required;
00156
00157 std::vector<String> valid_strings;
00158 Int min_int;
00159 Int max_int;
00160 DoubleReal min_float;
00161 DoubleReal max_float;
00163
00165 ParameterInformation( const String& n, ParameterTypes t, const String& arg, const String& def, const String& desc, bool req )
00166 : valid_strings(),
00167 min_int(-std::numeric_limits<Int>::max()),
00168 max_int(std::numeric_limits<Int>::max()),
00169 min_float(-std::numeric_limits<DoubleReal>::max()),
00170 max_float(std::numeric_limits<DoubleReal>::max())
00171 {
00172 name = n;
00173 type = t;
00174 default_value = def;
00175 description = desc;
00176 argument = arg;
00177 required = req;
00178 }
00179
00180 ParameterInformation()
00181 : name(),
00182 type( NONE ),
00183 default_value(),
00184 description(),
00185 argument(),
00186 required(true),
00187 valid_strings(),
00188 min_int(-std::numeric_limits<Int>::max()),
00189 max_int(std::numeric_limits<Int>::max()),
00190 min_float(-std::numeric_limits<DoubleReal>::max()),
00191 max_float(std::numeric_limits<DoubleReal>::max())
00192 {
00193 }
00194
00195 ParameterInformation& operator=( const ParameterInformation& rhs )
00196 {
00197 if ( &rhs == this ) return * this;
00198
00199 name = rhs.name;
00200 type = rhs.type;
00201 default_value = rhs.default_value;
00202 description = rhs.description;
00203 argument = rhs.argument;
00204 required = rhs.required;
00205 valid_strings = rhs.valid_strings;
00206 min_int = rhs.min_int;
00207 max_int = rhs.max_int;
00208 min_float = rhs.min_float;
00209 max_float = rhs.max_float;
00210 return *this;
00211 }
00212 };
00213
00214
00215 private:
00216
00218 String const tool_name_;
00219
00221 String const tool_description_;
00222
00224 Int const instance_number_;
00225
00227 String const ini_location_;
00228
00230 TOPPBase();
00231
00233 TOPPBase( const TOPPBase& );
00234
00236 Int debug_level_;
00237
00239 Param param_;
00240
00242 Param param_inifile_;
00243
00245 Param param_cmdline_;
00246
00248 Param param_instance_;
00249
00251 Param param_common_tool_;
00252
00254 Param param_common_;
00255
00257 mutable std::ofstream log_;
00258
00266 void enableLogging_() const;
00267
00269 std::vector<ParameterInformation> parameters_;
00270
00280 virtual Param getSubsectionDefaults_( const String& section ) const;
00281
00283 std::map<String, String> subsections_;
00284
00294 String getParamAsString_( const String& key, const String& default_value = "" ) const;
00295
00301 Int getParamAsInt_( const String& key, Int default_value = 0 ) const;
00302
00308 double getParamAsDouble_( const String& key, double default_value = 0 ) const;
00309
00319 bool getParamAsBool_( const String& key, bool default_value = false ) const;
00320
00332 DataValue const& getParam_( const String& key ) const;
00334
00335 protected:
00336
00344 String const& getIniLocation_() const
00345 {
00346 return ini_location_;
00347 }
00348
00369 virtual void registerOptionsAndFlags_() = 0;
00370
00380 void registerStringOption_(const String& name, const String& argument, const String& default_value, const String& description, bool required = true);
00381
00388 void setValidStrings_(const String& name, const std::vector<String>& strings) throw (Exception::ElementNotFound<String>,Exception::InvalidParameter);
00389
00402 void registerInputFile_( const String& name, const String& argument, const String& default_value, const String& description, bool required = true );
00403
00416 void registerOutputFile_( const String& name, const String& argument, const String& default_value, const String& description, bool required = true );
00417
00430 void setValidFormats_(const String& name, const std::vector<String>& formats) throw (Exception::ElementNotFound<String>,Exception::InvalidParameter);
00431
00432
00442 void registerDoubleOption_( const String& name, const String& argument, double default_value, const String& description, bool required = true );
00443
00449 void setMinInt_(const String& name, Int min) throw (Exception::ElementNotFound<String>);
00455 void setMaxInt_(const String& name, Int max) throw (Exception::ElementNotFound<String>);
00461 void setMinFloat_(const String& name, DoubleReal min) throw (Exception::ElementNotFound<String>);
00467 void setMaxFloat_(const String& name, DoubleReal max) throw (Exception::ElementNotFound<String>);
00468
00478 void registerIntOption_( const String& name, const String& argument, Int default_value, const String& description, bool required = true );
00479
00481 void registerFlag_( const String& name, const String& description );
00482
00490 void registerSubsection_( const String& name, const String& description );
00491
00493 void addEmptyLine_();
00494
00496 void addText_( const String& text );
00497
00499 String getStringOption_( const String& name ) const throw ( Exception::UnregisteredParameter, Exception::RequiredParameterNotGiven, Exception::WrongParameterType, Exception::InvalidParameter, Exception::FileNotFound, Exception::FileNotReadable, Exception::FileEmpty, Exception::UnableToCreateFile);
00500
00506 double getDoubleOption_( const String& name ) const throw ( Exception::UnregisteredParameter, Exception::RequiredParameterNotGiven, Exception::WrongParameterType, Exception::InvalidParameter );
00507
00513 Int getIntOption_( const String& name ) const throw ( Exception::UnregisteredParameter, Exception::RequiredParameterNotGiven, Exception::WrongParameterType, Exception::InvalidParameter );
00514
00516 bool getFlag_( const String& name ) const throw ( Exception::UnregisteredParameter, Exception::WrongParameterType );
00517
00519 bool setByUser_( const String& name ) const throw ( Exception::UnregisteredParameter );
00520
00522 const ParameterInformation& findEntry_( const String& name ) const throw ( Exception::UnregisteredParameter );
00523
00529 Param const& getParam_() const;
00530
00544 void checkParam_( const Param& param, const String& filename, const String& location ) const;
00546
00548 void printUsage_() const;
00549
00551 virtual ExitCodes main_(int argc , const char** argv) = 0;
00552
00554
00555
00556 void writeLog_( const String& text ) const;
00557
00559 void writeDebug_( const String& text, UInt min_level ) const;
00560
00562 void writeDebug_( const String& text, const Param& param, UInt min_level ) const;
00564
00565
00578
00579 void inputFileReadable_( const String& filename ) const throw ( Exception::FileNotFound, Exception::FileNotReadable, Exception::FileEmpty );
00580
00582 void outputFileWritable_( const String& filename ) const throw ( Exception::UnableToCreateFile );
00584
00586 void parseRange_( const String& text, double& low, double& high ) const;
00587
00589 ProgressLogger::LogType log_type_;
00590 };
00591
00592
00593
00594 }
00595
00596 #endif //OPENMS_APPLICATIONS_TOPPBASE_H