utils.h
Go to the documentation of this file.
1 /* *****************************************************************************
2 
3  trimAl v2.0: a tool for automated alignment trimming in large-scale
4  phylogenetics analyses.
5 
6  readAl v2.0: a tool for automated alignment conversion among different
7  formats.
8 
9  2009-2019
10  Fernandez-Rodriguez V. (victor.fernandez@bsc.es)
11  Capella-Gutierrez S. (salvador.capella@bsc.es)
12  Gabaldon, T. (tgabaldon@crg.es)
13 
14  This file is part of trimAl/readAl.
15 
16  trimAl/readAl are free software: you can redistribute it and/or modify
17  it under the terms of the GNU General Public License as published by
18  the Free Software Foundation, the last available version.
19 
20  trimAl/readAl are distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with trimAl/readAl. If not, see <http://www.gnu.org/licenses/>.
27 
28 ***************************************************************************** */
29 
30 #ifndef UTILS_H
31 #define UTILS_H
32 
33 #include <iostream>
34 #include <cstring>
35 #include <cstdlib>
36 #include <fstream>
37 #include <sstream>
38 #include <iomanip>
39 #include <vector>
40 #include <cstdio>
41 #include <string>
42 #include <cmath>
43 #include <map>
44 #include <sys/stat.h>
45 
46 /**
47  \brief Utilities class.
48  This class contains shared methods to be used in multiple parts of the code.
49  */
50 namespace utils {
51 
52 // public:
53  /**
54  \brief Vector initialization.
55  \param [out] vector The vector that will be initialized.
56  \param tam The size of the vector.
57  \param valor The initialization value that will of all positions of the vector.
58 
59  This method is used to initialize
60  all positions of a vector with a given value.
61  */
62  void initlVect(int *vector, int tam, int valor);
63 
64  /**
65  \brief Vector initialization.
66  \param [out] vector The vector that will be initialized.
67  \param tam The size of the vector.
68  \param valor The initialization value of all positions of the vector.
69 
70  This method is used to initialize
71  all positions of a vector with a given value.
72  */
73  void initlVect(float *vector, int tam, float valor);
74 
75  /**
76  \brief Integer vector copying.
77  \param vect1 Vector that we want to copy.
78  \param [out] vect2 Destination vector of the copy.
79  \param tam Vectors size.
80 
81  This method copies integer vector 1 to integer vector 2.
82  */
83  void copyVect(int *vect1, int *vect2, int tam);
84 
85  /**
86  \brief Float vector copying.
87  \param vect1 Vector that we want to copy.
88  \param [out] vect2 Destination vector of the copy.
89  \param tam Vectors size.
90 
91  This method copies float vector 1 to float vector 2.
92  */
93  void copyVect(float *vect1, float *vect2, int tam);
94 
95  /**
96  \brief Round double to inferior integer method.
97  \param number The number that will be rounded.
98  \return the rounded number.
99 
100  This method rounds a double number to the inferior integer.
101  */
102  int roundToInf(double number);
103 
104  /**
105  \brief Round double to integer method.
106  \param number The number that will be rounded.
107  \return the rounded number.
108 
109  This method rounds a double number to a integer.
110  */
111  int roundInt(double number);
112 
113  /**
114  \brief Round double to greater integer method.
115  \param number The number that will be rounded.
116  \return the rounded number.
117 
118  This method rounds a double number to the greater integer.
119  */
120  int roundToSup(double number);
121 
122  /**
123  \brief Maximum of two numbers method.
124  \param x The first number.
125  \param y The second number.
126  \return The maximum between the two given numbers.
127 
128  This method returns the maximum between the two numbers given as parameters.
129  */
130  int max(int x, int y);
131 
132  /**
133  \brief Maximum of two numbers method.
134  \param x The first number.
135  \param y The second number.
136  \return The maximum between the two given numbers.
137 
138  This method returns the maximum between the two numbers given as parameters.
139  */
140  float max(float x, float y);
141 
142  /**
143  \brief Maximum of two numbers method.
144  \param x The first number.
145  \param y The second number.
146  \return The maximum between the two given numbers.
147 
148  This method returns the maximum between the two numbers given as parameters.
149  */
150  double max(double x, double y);
151 
152  /**
153  \brief Minimum of two numbers method.
154  \param x The first number.
155  \param y The second number.
156  \return The minumum between the two given numbers.
157 
158  This method returns the minimum between the two numbers given as parameters.
159  */
160  int min(int x, int y);
161 
162  /**
163  \brief Minimum of two numbers method.
164  \param x The first number.
165  \param y The second number.
166  \return The minumum between the two given numbers.
167 
168  This method returns the minimum between the two numbers given as parameters.
169  */
170  float min(float x, float y);
171 
172  /**
173  \brief Minimum of two numbers method.
174  \param x The first number.
175  \param y The second number.
176  \return The minumum between the two given numbers.
177 
178  This method returns the minimum between the two numbers given as parameters.
179  */
180  double min(double x, double y);
181 
182  /**
183  \brief String-is-number checking.
184  \param num The string we want to check.
185  \return \b true if the string is a number, \b false if not.
186 
187  This method checks if the given string is a number
188  (taking in mind the possibility of floating numbers and scientific notation)
189  */
190  bool isNumber(char *num);
191 
192  /**
193  \brief String comparing method.
194  \param a The first string that will be compared.
195  \param b The second string that will be compared.
196  \return \b true if the two strings are the same, \b false if not.
197 
198  This method compares the two strings given,
199  and returns \b true if the two strings are equal.
200  */
201  bool compare(char *a, char *b);
202 
203  /**
204  \brief Removing spaces method.
205  \param in The string that we want to clean.
206  \param[out] out The destination of the clean string.
207 
208  This method removes spaces in the input string
209  and put the result in the output string.
210  */
211  void removeSpaces(char *in, char *out);
212 
213  /**
214  \brief Quicksort sorting method.
215  \param list The vector that we want to sort.
216  \param ini The first element of the vector.
217  \param fin The last element of the vector.
218 
219  This method sorts the vector using the quicksort method.
220  */
221  void quicksort(float *list, int ini, int fin);
222 
223  /**
224  \brief Swapping elements method
225  \param a One element to swap.
226  \param b Other element to swap.
227 
228  This method swaps the values in a and b.
229  */
230  void swap(float *a, float *b);
231 
232  /**
233  \brief Quicksort sorting method.
234  \param list The vector that we want to sort.
235  \param ini The first element of the vector.
236  \param fin The last element of the vector.
237 
238  This method sorts the vector using the quicksort method.
239  */
240  void quicksort(int *list, int ini, int fin);
241 
242  /**
243  \brief Swapping elements method
244  \param a One element to swap.
245  \param b Other element to swap.
246 
247  This method swaps the values in a and b.
248  */
249  void swap(int *a, int *b);
250 
251  /**
252  \brief Check if a given file exists and its size is greater than 0.
253  \param file ifstream to check
254  */
255  bool checkFile(std::ifstream &file);
256 
257  /**
258  \brief Read a new line from current input stream.\n
259  This function is better than standard one
260  since cares of operative system compatibility.\n
261  It is useful as well because removes tabs and blank spaces
262  at lines at beginning/ending.\n
263  \param file ifstream to read line from.
264  \return \n
265  Line that has been read or
266  nullptr if there is nothing to read.\n
267  */
268  char *readLine(std::ifstream &file);
269 
270  /**
271  \brief Read a new line from current input stream.\n
272  This function is better than standard one
273  since cares of operative system compatibility.\n
274  It is useful as well because removes tabs
275  and blank spaces at lines at beginning/ending.\n
276  \param file ifstream to read line from.
277  \return \n
278  nullptr if there is nothing to read.\n
279  Line that has been read.
280  */
281  char *readLine(std::istream &file);
282 
283  /**
284  \brief Remove all content surrounded by ("") or ([]).\n
285  It warns as well when a mismatch for these flags is found. \n
286  \param nline Line to be trimmed.
287  \return
288  Line trimmed of comments or
289  nullptr if there has been a mismatch\n
290  */
291  char *trimLine(std::string nline);
292 
293  /**
294  \brief Reverses a string
295  \param toReverse String to get a reversed copy.
296  \return Reversed string of toReverse.
297  */
298  std::string getReverse(const std::string &toReverse);
299 
300  /**
301  \brief Removes a determined char from the string
302  \param c Character to remove from line
303  \param line String to remove c from.
304  \return New string without c character
305  */
306  std::string removeCharacter(char c, std::string line);
307 
308  /**
309  \brief Checks an alignment type
310  \param seqNumber Number of sequences to check it's type.
311  \param residNumber Number of residues of the alignment.
312  \param sequences Sequences pointer
313  \return Integer that represents the alignment type.
314  */
315  int checkAlignmentType(int seqNumber,
316  int residNumber,
317  std::string *sequences);
318 
319  /**
320  \brief Reads a line and converts it to an array of number
321  \param line Line to convert to array of ints
322  \return Pointer to an array of numbers that contains line
323  */
324  int *readNumbers(const std::string &line);
325 
326  /**
327  \brief Quicksort sorting method.
328  \param vect The vector that we want to sort.
329  \param ini The first element of the vector.
330  \param fin The last element of the vector.
331  */
332  void quicksort(int **vect, int ini, int fin);
333 
334  /**
335  \brief Swaps double pointers.
336  \param a Double pointer A
337  \param b Double pointer B
338  */
339  void swap(int **a, int **b);
340 
341  /**
342  \brief Checks the color that has to be used on the output report
343  \param res Resiude to check its color
344  \param column Column to which this residue belongs.
345  \return Char that represents the color to be used.
346  */
347  char determineColor(char res, const std::string& column);
348 
349  /**
350  \brief Method to check for a pattern in a string.\n
351  The method will check, character by character of the first string if
352  there is some equality for each character in the pattern.\n
353  When done, it will calculate the fraction of characters
354  present in the pattern and compare to the threshold argument.
355  \param data string that will be compared against a pattern
356  \param pattern string that contains the pattern.
357  \param threshold minimum ratio of hits to consider the pattern valid
358  */
359  bool lookForPattern(const std::string& data,
360  const std::string& pattern,
361  const float threshold);
362 
363  /**
364  \brief Function that replaces a substring
365  with another substring in a string.
366  It does not make a copy of the original string, but modifies it.
367  \param [in,out] subject String to be modified
368  \param search Substring to search and change
369  \param replace Substring to put in place of search
370  */
371  void ReplaceStringInPlace(std::string &subject,
372  const std::string &search,
373  const std::string &replace);
374 
375  /**
376  \brief Function that replaces a substring
377  with another substring in a string.
378  It makes a copy of the original string.
379  \param [in] subject String to be modified
380  \param search Substring to search and change
381  \param replace Substring to put in place of search
382  */
383  std::string ReplaceString(std::string subject,
384  const std::string &search,
385  const std::string &replace);
386 
387  /**
388  \brief Function that gives the gap classification of a column of values.
389  \param gapValue Number of gaps present in the column.
390  \param sequenNumber Number of sequences.
391  \return Int representing the classification of this gap value.
392  */
393  int GetGapStep(int *gapValue, int sequenNumber);
394 
395  /**
396  \brief Function that gives the gap classification of a column of values.\n
397  This function should work faster than it's sister
398  utils::GetGapStep(int * gapValue, int sequenNumber),
399  as it uses a precomputed (by the user)
400  inverseSequenNumber (1F / Alignment::sequenNumber),
401  instead of calculating it over again each time the function
402  is called (which is equal to number of residues). \n
403  This comes with a precision cost that shouldn't be a problem.
404  \param gapValue Number of gaps present in the column.
405  \param inverseSequenNumber Inverse of number of sequences. (1F / sequenNumber)
406  \return Int representing the classification of this gap value.
407  */
408  int GetGapStep(int *gapValue, float inverseSequenNumber);
409 
410  /**
411  \brief Function that gives the
412  similarity classification of a column of values.
413  \param simValue Similarity value.
414  \return Int representing the classification of this gap value.
415  */
416  int GetSimStep(float *simValue);
417 
418  /**
419  \brief Function that gives the
420  consistency classification of a column of values.
421  \param consValue Consistency value.
422  \return Int representing the classification of this gap value.
423  */
424  int GetConsStep(float *consValue);
425 
426  /**
427  * \brief Method to check the existance of a file
428  * @param path Path to the file to check
429  * @return Wheter the file exists or not.
430  */
431  bool fileExists(std::string & path);
432 
433  /**
434  * \brief Method to check the existance of a file.
435  * Works exactly as fileExists(std::string & path),
436  * but accepts r-value reference.
437  * @param path Path to the file to check
438  * @return Wheter the file exists or not.
439  */
440  bool fileExists(std::string && path);
441 
442  /***
443  * Method to transform a char to its upper version
444  * Will return the same char if its already and uppercase char
445  * Works using bit shifts, to avoid using locale.
446  *
447  * If char is not a alpha-character, it will return the same char.
448  * @param c Original character
449  * @return upperCase version of the character
450  */
451  char toUpper(char c);
452 
453  namespace TerminalColors {
459 
460  extern std::map<terminalColor, const std::string> colors;
461  }
462 };
463 
464 
465 
466 #endif
int GetSimStep(float *simValue)
Function that gives the similarity classification of a column of values.
Definition: utils.cpp:893
void initlVect(float *vector, int tam, float valor)
Vector initialization.
Definition: utils.cpp:46
std::string ReplaceString(std::string subject, const std::string &search, const std::string &replace)
Function that replaces a substring with another substring in a string. It makes a copy of the origina...
Definition: utils.cpp:820
void removeSpaces(char *in, char *out)
Removing spaces method.
Definition: utils.cpp:144
bool checkFile(std::ifstream &file)
Check if a given file exists and its size is greater than 0.
Definition: utils.cpp:284
int min(int x, int y)
Minimum of two numbers method.
Definition: utils.cpp:98
int GetGapStep(int *gapValue, int sequenNumber)
Function that gives the gap classification of a column of values.
Definition: utils.cpp:832
char determineColor(char res, const std::string &column)
Checks the color that has to be used on the output report.
Definition: utils.cpp:675
void initlVect(int *vector, int tam, int valor)
Vector initialization.
Definition: utils.cpp:40
float min(float x, float y)
Minimum of two numbers method.
Definition: utils.cpp:104
float max(float x, float y)
Maximum of two numbers method.
Definition: utils.cpp:86
bool compare(char *a, char *b)
String comparing method.
Definition: utils.cpp:139
char toUpper(char c)
Definition: utils.cpp:958
void swap(float *a, float *b)
Swapping elements method.
Definition: utils.cpp:193
double max(double x, double y)
Maximum of two numbers method.
Definition: utils.cpp:92
int roundInt(double number)
Round double to integer method.
Definition: utils.cpp:68
int checkAlignmentType(int seqNumber, int residNumber, std::string *sequences)
Checks an alignment type.
Definition: utils.cpp:499
bool fileExists(std::string &&path)
Method to check the existance of a file. Works exactly as fileExists(std::string & path)...
Definition: utils.cpp:953
void quicksort(int *list, int ini, int fin)
Quicksort sorting method.
Definition: utils.cpp:204
void copyVect(int *vect1, int *vect2, int tam)
Integer vector copying.
Definition: utils.cpp:51
int GetGapStep(int *gapValue, float inverseSequenNumber)
Function that gives the gap classification of a column of values. This function should work faster th...
Definition: utils.cpp:863
void swap(int **a, int **b)
Swaps double pointers.
Definition: utils.cpp:275
char * readLine(std::ifstream &file)
Read a new line from current input stream. This function is better than standard one since cares of o...
Definition: utils.cpp:304
void copyVect(float *vect1, float *vect2, int tam)
Float vector copying.
Definition: utils.cpp:57
bool lookForPattern(const std::string &data, const std::string &pattern, const float threshold)
Method to check for a pattern in a string. The method will check, character by character of the firs...
Definition: utils.cpp:793
int GetConsStep(float *consValue)
Function that gives the consistency classification of a column of values.
Definition: utils.cpp:920
void quicksort(float *list, int ini, int fin)
Quicksort sorting method.
Definition: utils.cpp:159
int roundToSup(double number)
Round double to greater integer method.
Definition: utils.cpp:74
int max(int x, int y)
Maximum of two numbers method.
Definition: utils.cpp:80
std::map< terminalColor, const std::string > colors
Definition: utils.cpp:965
double min(double x, double y)
Minimum of two numbers method.
Definition: utils.cpp:110
void quicksort(int **vect, int ini, int fin)
Quicksort sorting method.
Definition: utils.cpp:246
std::string getReverse(const std::string &toReverse)
Reverses a string.
Definition: utils.cpp:475
int * readNumbers(const std::string &line)
Reads a line and converts it to an array of number.
Definition: utils.cpp:624
char * readLine(std::istream &file)
Read a new line from current input stream. This function is better than standard one since cares of o...
Definition: utils.cpp:348
char * trimLine(std::string nline)
Remove all content surrounded by ("") or ([]). It warns as well when a mismatch for these flags is f...
Definition: utils.cpp:392
Utilities class. This class contains shared methods to be used in multiple parts of the code...
Definition: utils.h:50
std::string removeCharacter(char c, std::string line)
Removes a determined char from the string.
Definition: utils.cpp:485
int roundToInf(double number)
Round double to inferior integer method.
Definition: utils.cpp:62
void ReplaceStringInPlace(std::string &subject, const std::string &search, const std::string &replace)
Function that replaces a substring with another substring in a string. It does not make a copy of the...
Definition: utils.cpp:810
void swap(int *a, int *b)
Swapping elements method.
Definition: utils.cpp:237
bool fileExists(std::string &path)
Method to check the existance of a file.
Definition: utils.cpp:948
bool isNumber(char *num)
String-is-number checking.
Definition: utils.cpp:116