4 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
6 * @author Mark Larkin, Conway Institute, UCD. mark.larkin@ucd.ie
10 * 2007-12-03, Andreas Wilm (UCD): replaced gets with fgets, and
11 * got rid of some compiler warning
20 #include "general/userparams.h"
34 * Removes trailing blanks from a string
36 void Utility::rTrim(string *str)
38 string::reverse_iterator rit = str->rbegin();
40 while(rit != str->rend() && isspace(*rit))
42 str->erase((++rit).base());
47 * Removes trailing blanks from a string
48 * @param str String to remove trailing blanks from.
49 * @return Pointer to the processed string
51 char * Utility::rTrim(char *str)
57 while (isspace(str[p]))
68 * Replace blanks in a string with underscores. Also replaces , ; : ( or ) with _.
69 * @param str String to process.
70 * @return Pointer to the processed string
72 char * Utility::blankToUnderscore(char *str)
78 for (i = 0; i <= p; i++)
79 if ((str[i] == ' ') || (str[i] == ';') || (str[i] == ',') || (str[i] ==
80 '(') || (str[i] == ')') || (str[i] == ':'))
89 * Replace blanks in a string with underscores. Also replaces , ; : ( or ) with _.
90 * @param str String to process.
91 * @return Pointer to the processed string
93 string Utility::blankToUnderscore(string str)
99 for (i = 0; i <= p; i++)
100 if ((str[i] == ' ') || (str[i] == ';') || (str[i] == ',') || (str[i] ==
101 '(') || (str[i] == ')') || (str[i] == ':'))
114 char Utility::getChoice(string instr)
116 cout << instr << ": ";
120 // We only want one character, so we ignore the rest.
123 cin.ignore(150, '\n');
126 if(isalpha(choice) || isNumeric(choice))
130 else if(choice == '\n')
140 bool Utility::isNumeric(char ch)
142 if(ch >= '0' && ch <= '9')
155 void Utility::getStr(string instr, string& outstr)
157 cout << instr << ": ";
160 getline(cin, temp, '\n');
173 double Utility::getReal(const char *instr, double minx, double maxx, double def)
181 fprintf(stdout, "%s (%.1f-%.1f) [%.1f]: ", instr, minx, maxx, def);
183 fgets(line, MAXLINE, stdin);
184 status = sscanf(line, "%f", &ret);
191 fprintf(stderr, "ERROR: Max. value=%.1f\n\n", maxx);
196 fprintf(stderr, "ERROR: Min. value=%.1f\n\n", minx);
212 int Utility::getInt(const char *instr, int minx, int maxx, int def)
219 fprintf(stdout, "%s (%d..%d) [%d]: ", instr, minx, maxx, def);
221 fgets(line, MAXLINE, stdin);
222 status = sscanf(line, "%d", &ret);
229 fprintf(stderr, "ERROR: Max. value=%d\n\n", maxx);
234 fprintf(stderr, "ERROR: Min. value=%d\n\n", minx);
248 bool Utility::lineType(char *line, const char *code)
250 // AW: introduced sanity check
252 if (strlen(line)<strlen(code))
257 return (strncmp(line, code, strlen(code)) == 0);
265 bool Utility::blankLine(char *line)
269 for (i = 0; line[i] != '\n' && line[i] != EOS; i++)
271 if (isdigit(line[i]) || isspace(line[i]) || (line[i] == '*') ||
272 (line[i] == ':') || (line[i] == '.'))
287 void Utility::getPath(string str, string *path)
293 for (i = _temp.length() - 1; i > - 1; --i)
295 if (str[i] == DIRDELIM)
312 _temp = _temp.substr(0, i + 1);
325 char Utility::promptForYesNo(char *title, const char *prompt)
327 cout << "\n" << title << "\n";
328 string promptMessage = string(prompt) + "(y/n) ? [y]";
331 getStr(promptMessage, answer);
335 if ((answer[0] != 'n') && (answer[0] != 'N'))
350 char Utility::promptForYesNo(const char *title, const char *prompt)
352 cout << "\n" << title << "\n";
353 string promptMessage = string(prompt) + "(y/n) ? [y]";
356 getStr(promptMessage, answer);
360 if ((answer[0] != 'n') && (answer[0] != 'N'))
372 void Utility::error( char *msg,...)
377 fprintf(stderr, "\n\nERROR: ");
378 vfprintf(stderr, msg, ap);
379 fprintf(stderr, "\n\n");
387 void Utility::warning( char *msg,...)
392 fprintf(stderr, "\n\nWARNING: ");
393 vfprintf(stderr, msg, ap);
394 fprintf(stderr, "\n\n");
402 void Utility::error( const char *msg,...)
407 fprintf(stderr, "\n\nERROR: ");
408 vfprintf(stderr, msg, ap);
409 fprintf(stderr, "\n\n");
417 void Utility::warning( const char *msg,...)
422 fprintf(stderr, "\n\nWARNING: ");
423 vfprintf(stderr, msg, ap);
424 fprintf(stderr, "\n\n");
432 void Utility::info( char *msg,...)
439 fprintf(stdout, "\n");
440 vfprintf(stdout, msg, ap);
449 void Utility::info(const char *msg,...)
455 fprintf(stdout, "\n");
456 vfprintf(stdout, msg, ap);
466 void Utility::myname( char *myname)
468 strcpy(myname, "clustalw\0");
475 * Mark 25-1-2007. I made this change to get around the problem of having to keep track
476 * of output indexes in the alignment stage. This function returns the next unique id.
478 unsigned long Utility::getUniqueSequenceIdentifier()
480 static unsigned long nextSequenceIdentifier = MinIdentifier;
481 return nextSequenceIdentifier++;
484 void Utility::setInfoLabelPtr(QLabel* ptrToLabelObj)
486 // Dont do anything. This is here to allow the function to be added to QTUtility.
487 // Polymorphism wont work with different functions in the classes.
491 double Utility::average(std::vector<double>& v)
494 std::vector<double>::iterator i;
499 for (i=v.begin(); i != v.end(); i++)
501 return (tmp / v.size());
504 double Utility::stdDev(std::vector<double>& v)
506 std::vector<double>::iterator i;
508 double avg = average(v);
513 for(i=v.begin(); i != v.end(); ++i)
514 tmp += (*i - avg) * (*i - avg);
515 return sqrt(tmp / v.size());
518 double Utility::median(std::vector<double> v)
520 // From Moo & Koenig, "Accelerated C++:
521 typedef vector<double>::size_type vec_sz;
522 vec_sz size = v.size();
527 std::sort(v.begin(), v.end());
529 return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid];