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