00001 #ifndef INTERACTIVE_CONSOLE_H 00002 #define INTERACTIVE_CONSOLE_H 00003 00004 //-------------------------------------------------------------------- 00005 // 00006 // This file is part of PEACE. 00007 // 00008 // PEACE is free software: you can redistribute it and/or modify it 00009 // under the terms of the GNU General Public License as published by 00010 // the Free Software Foundation, either version 3 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // PEACE is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with PEACE. If not, see <http://www.gnu.org/licenses/>. 00020 // 00021 // Miami University makes no representations or warranties about the 00022 // suitability of the software, either express or implied, including 00023 // but not limited to the implied warranties of merchantability, 00024 // fitness for a particular purpose, or non-infringement. Miami 00025 // University shall not be liable for any damages suffered by licensee 00026 // as a result of using, result of using, modifying or distributing 00027 // this software or its derivatives. 00028 // 00029 // By using or copying this Software, Licensee agrees to abide by the 00030 // intellectual property laws, and all other applicable laws of the 00031 // U.S., and the terms of GNU General Public License (version 3). 00032 // 00033 // Authors: Dhananjai M. Rao raodm@muohio.edu 00034 // 00035 //--------------------------------------------------------------------- 00036 00037 #ifndef _WINDOWS 00038 #include "config.h" 00039 #endif 00040 00041 #include "Utilities.h" 00042 #include <vector> 00043 #include <string> 00044 00045 // Make compiler happy and fast 00046 class ESTAnalyzer; 00047 00048 /** An interactive console for manually studying ESTs. 00049 00050 This class provides an text-based interactive console for studying 00051 and comparing ESTs loaded from a given FASTA file. This class 00052 provides a prompt for users to enter various commands. This class 00053 processes the commands provided by the user and displays the 00054 results on the display. 00055 */ 00056 class InteractiveConsole { 00057 public: 00058 /** The constructor. 00059 00060 The constructor is pretty straightforward and merely 00061 initializes all the instance variables to their default 00062 initial values. 00063 */ 00064 InteractiveConsole(ESTAnalyzer* analyzer); 00065 00066 /** The destructor. 00067 00068 The destructor frees up memory allocated to instance 00069 variables. 00070 */ 00071 ~InteractiveConsole(); 00072 00073 /** The main method of the console. 00074 00075 This method is invoked from the main() function after the 00076 InteractiveConsole has been created. This method performs the 00077 following tasks: 00078 00079 <ol> 00080 00081 <li>It first initializes the analyzer which causes the ESTs to 00082 be loaded from the supplied FASTA file. If errors occur 00083 during initialization then this method reports an error and 00084 exits.</li> 00085 00086 <li>It reads a command from the user and if the command is 00087 "exit" it exits from this method.</li> 00088 00089 <li>Otherwise it tokenizes the input command line into 00090 multiple tokens (as a vector of strings) and then uses a 00091 dispatch table to invoke various methods to do the actual 00092 processing for a given command.</li> 00093 00094 <li> 00095 00096 </ol> 00097 */ 00098 void processCommands(); 00099 00100 protected: 00101 /** Helper method to tokenize a given string. 00102 00103 This is a helper method that is used tokenize a given string 00104 based on a given set of delimiters. This method uses a 00105 standard set of methods in std::string class to extract 00106 sub-strings from str and returns a std::vector containing the 00107 set of tokens. 00108 00109 \param[in] str The string that must be broken to multiple 00110 tokens. 00111 00112 \param[in] delims The set of characters to be used as 00113 delimiters. The default set of delimiters contains just the 00114 standard white space characters (namely: \c " \n\t\r"). 00115 00116 \return This method returns a std::vector containing the 00117 tokens in \c str broken using the set of \c delims specified. 00118 */ 00119 static 00120 std::vector<std::string> tokenize(const std::string& str, 00121 const std::string& delims = " \n\t\r"); 00122 00123 /** A helper method to print statistics about ESTs. 00124 00125 This method is invoked from the \c processCommands method 00126 (indirectly through a dispatch table). This method computes 00127 and prints statistics about the list of ESTs loaded for 00128 analysis. 00129 00130 \param[in] cmdWords The set of words/tokens from the command 00131 entered by the user. 00132 */ 00133 void printStats(const std::vector<std::string>& UNREFERENCED_PARAMETER(cmdWords)); 00134 00135 /** Mehtod to windup the interactive console. 00136 00137 This method is invoked from the \c processCommands method 00138 (indirectly through a dispatch table). This method merely 00139 prints an exit message. 00140 00141 \param[in] cmdWords The set of words/tokens from the command 00142 entered by the user. Currently, this list is ignored by this 00143 method. 00144 */ 00145 void exit(const std::vector<std::string>& UNREFERENCED_PARAMETER(cmdWords)); 00146 00147 /** List the ESTs curently loaded. 00148 00149 This method is invoked from the \c processCommands method 00150 (indirectly through a dispatch table). This method prints the 00151 list of ESTs that are currently available for D2 analysis. 00152 00153 \param[in] cmdWords The set of words/tokens from the command 00154 entered by the user. Currently, this list is ignored by this 00155 method. 00156 */ 00157 void list(const std::vector<std::string>& UNREFERENCED_PARAMETER(cmdWords)); 00158 00159 /** Analyze a given pair of ESTs. 00160 00161 This method is invoked from the \c processCommands method 00162 (indirectly through a dispatch table). This method assumes 00163 that the user has specified the index of thw two ESTs to be 00164 analyzed and prints the result of analyzing the two ESTs. 00165 00166 \param[in] cmdWords The set of words/tokens from the command 00167 entered by the user. This vector must have exactly 3 words, 00168 the first word being "analyze" and the other two words being 00169 either the index of ESTs or the fasta header for the ESTs. 00170 */ 00171 void analyze(const std::vector<std::string>& cmdWords); 00172 00173 /** Display a brief help regarding supported commands. 00174 00175 This method is invoked whenever the user types the command 00176 "help" at the prompt. This method is invoked (from the \c 00177 processCommands method) via its method pointer stored in the 00178 \c cmdHandlerList. This method simply displays a static help 00179 text with the list of commands and some example usage. 00180 00181 \param[in] cmdWords The set of words/tokens from the command 00182 entered by the user. Currently, this list is ignored by this 00183 method. 00184 */ 00185 void help(const std::vector<std::string>& UNREFERENCED_PARAMETER(cmdWords)); 00186 00187 /** Display detailed information about a given EST 00188 00189 This method is invoked whenever the user types the command 00190 "print" at the prompt. This method is invoked (from the \c 00191 processCommands method) via its method pointer stored in the 00192 \c cmdHandlerList. This method simply displays a static help 00193 text with the list of commands and some example usage. 00194 00195 \param[in] cmdWords The set of words/tokens from the command 00196 entered by the user. This method expects exactly one parameter 00197 and process it assuming it is an EST index or an FASTA 00198 identifier corresponding to the EST whose information is to be 00199 displayed. 00200 */ 00201 void print(const std::vector<std::string>& cmdWords); 00202 00203 #ifndef HAVE_LIBREADLINE 00204 /** A helper method used only under Windows. 00205 00206 This is a replacement for the wonderful and interactive 00207 readline functionality that is available under Linux. This 00208 method displays the prompt and reads a line from the standard 00209 input from the user. 00210 00211 \param[in] prompt The prompt string to be displayed to the user. 00212 00213 \return A std::string containing the line of input entered 00214 by the user. Note that the returned pointer must be free'd 00215 by the caller. 00216 */ 00217 static char* readline(const char *prompt); 00218 #endif 00219 00220 private: 00221 /** Helper method to convert index or FASTA identifier to index. 00222 00223 This method is a helper method that is invoked from the \c 00224 analyze method to convert either an index value or a FASTA 00225 header to a index value. 00226 00227 \param[in] id The identifier (either an number) or the FASTA 00228 identifier to be converted to an index value. 00229 00230 \return The index value of the EST. If the index is invalid or 00231 the FASTA identifier was not found, this method prints a 00232 suitable message and returns -1. 00233 */ 00234 int getESTIndex(const std::string& id) const; 00235 00236 /** Helper method to perform initialization. 00237 00238 This is a refactored helper method that was introduced to keep 00239 the code clutter in the processCommands method to a minimum. 00240 This method is invoked only once, right when the 00241 processCommands method is invoked from the \c main function. 00242 This method displays a standard startup message and then 00243 initializes the analyzer which causes the ESTs to be loaded 00244 from the supplied FASTA file. This method also tracks and 00245 reports the time taken for loading the ESTs. 00246 00247 \return This method returns \c true if the initialization was 00248 successful. On errors it returns \c false. 00249 */ 00250 bool initialize(); 00251 00252 /** The analyzer to be used for analysis. 00253 00254 The analyzer to be used for performing the analysis to compute 00255 similarity or distance metrics when the user requests it. This 00256 pointer is set in the constructor and is never changed during 00257 the life time of this object. 00258 */ 00259 ESTAnalyzer* const analyzer; 00260 00261 /** \struct CmdEntry 00262 00263 \brief A structure to ease delegating processing of commands 00264 to various methods in this class. 00265 */ 00266 typedef struct Entry { 00267 /// The command with which this handler is associated. 00268 const char* cmd; 00269 /// The method to be invoked for processing a command. 00270 void (InteractiveConsole::*handler)(const std::vector<std::string>&); 00271 } CmdEntry; 00272 00273 /** The list of command handlers associated in this class. 00274 00275 This array contains the list of command handlers associated 00276 with this class. This list is a static list of handlers 00277 associated with various methods in this class. This list 00278 enables invoking various methods from the \c processCommands 00279 method. 00280 */ 00281 static CmdEntry cmdHandlerList[]; 00282 00283 /** A dummy operator= 00284 00285 The operator=() is supressed for this class as it has constant 00286 members whose value is set when the object is created. These 00287 values cannot be changed during the lifetime of this object. 00288 00289 \param[in] src The source object from where data is to be 00290 copied. Currently this value is ignored. 00291 00292 \return Reference to this. 00293 */ 00294 InteractiveConsole& operator=(const InteractiveConsole& src); 00295 }; 00296 00297 00298 #endif