utils.cpp
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 #include <utils.h>
31 
33 #include "reportsystem.h"
34 #include "defines.h"
35 #include "values.h"
36 #include "utils.h"
37 
38 namespace utils {
39 
40  void initlVect(int *vector, int tam, int valor) {
41 
42  for (int i = 0; i < tam; i++) vector[i] = valor;
43 
44  }
45 
46  void initlVect(float *vector, int tam, float valor) {
47 
48  for (int i = 0; i < tam; i++) vector[i] = valor;
49  }
50 
51  void copyVect(int *vect1, int *vect2, int tam) {
52 
53  for (int i = 0; i < tam; i++) vect2[i] = vect1[i];
54 
55  }
56 
57  void copyVect(float *vect1, float *vect2, int tam) {
58 
59  for (int i = 0; i < tam; i++) vect2[i] = vect1[i];
60  }
61 
62  int roundToInf(double number) {
63 
64  // return std::floor(number);
65  return ((int) number);
66  }
67 
68  int roundInt(double number) {
69 
70  // return std::round(number);
71  return ((int) ((double) number + 0.5));
72  }
73 
74  int roundToSup(double number) {
75 
76  // return std::ceil(number);
77  return ((int) ((double) number + 1.0));
78  }
79 
80  int max(int x, int y) {
81 
82  if (x > y) return x;
83  else return y;
84  }
85 
86  float max(float x, float y) {
87 
88  if (x > y) return x;
89  else return y;
90  }
91 
92  double max(double x, double y) {
93 
94  if (x > y) return x;
95  else return y;
96  }
97 
98  int min(int x, int y) {
99 
100  if (x < y) return x;
101  else return y;
102  }
103 
104  float min(float x, float y) {
105 
106  if (x < y) return x;
107  else return y;
108  }
109 
110  double min(double x, double y) {
111 
112  if (x < y) return x;
113  else return y;
114  }
115 
116  bool isNumber(char *num) {
117 
118  int tam = strlen(num);
119  if (tam == 0) return false;
120  int i, flt = 1, expn = 1, sgn = 1;
121 
122  for (i = 0; i < tam; i++) {
123  if (num[i] == '.' && flt)
124  flt = 0;
125 
126  else if (((num[i] == 'e') || (num[i] == 'E')) && expn)
127  expn = 0;
128 
129  else if (((num[i] == '+') || (num[i] == '-')) && sgn) {
130  if (!expn) sgn = 0;
131  } else if (num[i] > '9' || num[i] < '0')
132  return false;
133  }
134 
135  return true;
136 
137  }
138 
139  bool compare(char *a, char *b) {
140 
141  return (!strcmp(a, b));
142  }
143 
144  void removeSpaces(char *in, char *out) {
145 
146  unsigned int i, j = 0;
147 
148  for (i = 0; i < strlen(in); i++) {
149 
150  if (in[i] != ' ' && in[i] != '\t') {
151  out[j] = in[i];
152  j++;
153  }
154  }
155  out[j] = '\0';
156  }
157 
158 
159  void quicksort(float *vect, int ini, int fin) {
160  // NOTE Introspective sort seems better - JMaria
161  float elem_div;
162  int i, j;
163 
164  if ((ini >= fin) || (fin < 0))
165  return;
166 
167  elem_div = vect[fin];
168  i = ini - 1;
169  j = fin;
170 
171  while (true) {
172 
173  while (vect[++i] < elem_div)
174  if (i == fin)
175  break;
176 
177  while (vect[--j] > elem_div)
178  if (j == 0)
179  break;
180 
181  if (i < j)
182  swap(&vect[i], &vect[j]);
183  else
184  break;
185  }
186 
187  swap(&vect[i], &vect[fin]);
188 
189  quicksort(vect, ini, i - 1);
190  quicksort(vect, i + 1, fin);
191  }
192 
193  void swap(float *a, float *b) {
194 
195  float temp;
196 
197  temp = *a;
198  *a = *b;
199  *b = temp;
200 
201  }
202 
203 
204  void quicksort(int *vect, int ini, int fin) {
205  // NOTE Introspective sort seems better - JMaria
206  int i, j, elem_div;
207 
208  if ((ini >= fin) || (fin < 0))
209  return;
210 
211  elem_div = vect[fin];
212  i = ini - 1;
213  j = fin;
214 
215  while (true) {
216 
217  while (vect[++i] < elem_div)
218  if (i == fin)
219  break;
220 
221  while (vect[--j] > elem_div)
222  if (j == 0)
223  break;
224 
225  if (i < j)
226  swap(&vect[i], &vect[j]);
227  else
228  break;
229  }
230 
231  swap(&vect[i], &vect[fin]);
232 
233  quicksort(vect, ini, i - 1);
234  quicksort(vect, i + 1, fin);
235  }
236 
237  void swap(int *a, int *b) {
238  int temp;
239 
240  temp = *a;
241  *a = *b;
242  *b = temp;
243 
244  }
245 
246  void quicksort(int **vect, int ini, int fin) {
247  // NOTE Introspective sort seems better - JMaria
248  float elem_div;
249  int i, j;
250 
251  if ((ini >= fin) || (fin < 0))
252  return;
253 
254  elem_div = vect[fin][0];
255  i = ini - 1;
256  j = fin;
257 
258  while (true) {
259 
260  while (vect[++i][0] < elem_div) if (i == fin) break;
261  while (vect[--j][0] > elem_div) if (j == 0) break;
262 
263  if (i < j) swap(&vect[i], &vect[j]);
264  else break;
265 
266  }
267 
268  swap(&vect[i], &vect[fin]);
269 
270  quicksort(vect, ini, i - 1);
271  quicksort(vect, i + 1, fin);
272 
273  }
274 
275  void swap(int **a, int **b) {
276 
277  int *temp;
278 
279  temp = *a;
280  *a = *b;
281  *b = temp;
282  }
283 
284  bool checkFile(std::ifstream &file) {
285  // Check if a given file exists and its size is greater than 0
286  long begin, end;
287 
288  // Check whether input file exists or not
289  if (!file)
290  return false;
291 
292  // Check input file sizes. A valid file should have a size grater than 0
293  begin = file.tellg();
294  file.seekg(0, std::ios::end);
295  end = file.tellg();
296  file.seekg(0, std::ios::beg);
297  // Compare difference between file start and end.
298  // Depending on result, return True or False
299  if (!(end - begin))
300  return false;
301  return true;
302  }
303 
304  char *readLine(std::ifstream &file) {
305  StartTiming("char* readLine(std::ifstream &file) ");
306  // Read a new line from current input stream. This function is better than
307  // standard one since cares of operative system compability. It is useful
308  // as well because remove tabs and blank spaces at lines beginning/ending
309 
310  int state;
311  std::string nline;
312  char *line = nullptr;
313 
314  // Check it the end of the file has been reached or not
315  if (file.eof())
316  return nullptr;
317 
318  /* Store first line found. For -Windows & MacOS compatibility- carriage return
319  * is considered as well as a new line character */
320  // for( ; (c != '\n') && (c != '\r') && ((!file.eof())); file.read(&c, 1))
321  // nline.resize(nline.size() + 1, c);
322  getline(file, nline);
323 
324  // Remove blank spaces & tabs from the beginning of the line
325  state = nline.find(' ', 0);
326  while (state != (int) std::string::npos && state == 0) {
327  nline.erase(state, 1);
328  state = nline.find(' ', state);
329  }
330 
331  state = nline.find('\t', 0);
332  while (state != (int) std::string::npos && state == 0) {
333  nline.erase(state, 1);
334  state = nline.find('\t', state);
335  }
336 
337  /* If there is nothing to return, give back a nullptr pointer ... */
338  if (nline.empty())
339  return nullptr;
340 
341  // Otherwise, initialize the appropiate data structure,
342  // dump the data and return it
343  line = new char[nline.size() + 1];
344  strcpy(line, nline.c_str());
345  return line;
346  }
347 
348  char *readLine(std::istream &file) {
349  // Read a new line from current input stream. This function is better than
350  // standard one since cares of operative system compability. It is useful
351  // as well because remove tabs and blank spaces at lines beginning/ending
352 
353  int state;
354  std::string nline;
355  char *line = nullptr;
356 
357  // Check it the end of the file has been reached or not
358  if (file.eof())
359  return nullptr;
360 
361 
362  /* Store first line found. For -Windows & MacOS compatibility- carriage return
363  * is considered as well as a new line character */
364  // for( ; (c != '\n') && (c != '\r') && ((!file.eof())); file.read(&c, 1))
365  // nline.resize(nline.size() + 1, c);
366  getline(file, nline);
367  /* Remove blank spaces & tabs from the beginning of the line */
368 
369  state = nline.find(' ', 0);
370  while (state != (int) std::string::npos && state == 0) {
371  nline.erase(state, 1);
372  state = nline.find(' ', state);
373  }
374 
375  state = nline.find('\t', 0);
376  while (state != (int) std::string::npos && state == 0) {
377  nline.erase(state, 1);
378  state = nline.find('\t', state);
379  }
380 
381  // If there is nothing to return, give back a nullptr pointer ...
382  if (nline.empty())
383  return nullptr;
384 
385  // Otherwise, initialize the appropiate data structure,
386  // dump the data and return it
387  line = new char[nline.size() + 1];
388  strcpy(line, &nline[0]);
389  return line;
390  }
391 
392  char *trimLine(std::string nline) {
393  // This function is used to remove comments in between a biological sequence.
394  // Removes all content surrounded by ("") or ([]).
395  // It warns as well when a mismatch for these flags is found
396 
397  int pos, next;
398  char *line;
399 
400  // Set-up lower and upper limit to look for comments inside of input std::string
401  pos = -1;
402 
403  // Identify comments inside of input sequence and remove it
404  while (true) {
405  pos = nline.find('\"', (pos + 1));
406 
407  // When there is not any more a comment inside of sequence,
408  // go out from this loop
409  if (pos == (int) std::string::npos)
410  break;
411 
412  // Look for closing flag
413  next = nline.rfind('\"', nline.size());
414 
415  // If a pair of comments flags '"' is found, remove everything inbetween
416  if ((int) nline.find('\"', (pos + 1)) == next) {
417  nline.erase(pos, (next - pos + 1));
418  pos = -1;
419  }
420 
421  // If there is only one flag '"' for comments inside of sequence,
422  // user should be warned about that
423  if (pos == next) {
425  return nullptr;
426  }
427  }
428 
429  // Look for other kind of comments, in this case those with []
430  while (true) {
431  pos = -1;
432  next = -1;
433 
434  // Search for last opened bracket. It is supposed to be the first one for
435  // being close
436  while ((pos = nline.find('[', (pos + 1))) != (int) std::string::npos)
437  next = pos;
438 
439  // If no opening bracket has been found.
440  // Check if there is any closing one
441  if (next == -1) {
442  // There are not any bracket in input std::string
443  if ((int) nline.find(']', 0) == (int) std::string::npos)
444  break;
445  // Otherwise, warn about the error
447  return nullptr;
448  }
449 
450  // Look for closest closing bracket to the opening one found
451  pos = nline.find(']', (next + 1));
452 
453  //If no closing bracket has been found. Warn about the mismatch
454  if (pos == (int) std::string::npos) {
456  return nullptr;
457  }
458 
459  // When both brackets have been found, remove comments inbetween them
460  nline.erase(next, (pos - next + 1));
461  }
462 
463  // Check if after removing all comments from input std::string there is still part
464  // of sequences or not
465  if (nline.empty())
466  return nullptr;
467 
468  // Initialize and store resulting sequence into an appropiate structure
469  line = new char[nline.size() + 1];
470  strcpy(line, &nline[0]);
471 
472  return line;
473  }
474 
475  std::string getReverse(const std::string &toReverse) {
476 
477  std::string line(toReverse.size(), ' ');
478  long i, x;
479 
480  for (i = toReverse.size() - 1, x = 0; i >= 0; i--, x++)
481  line[x] = toReverse[i];
482  return line;
483  }
484 
485  std::string removeCharacter(char c, std::string line) {
486 
487  size_t pos;
488 
489  pos = line.find(c, 0);
490  while (pos != (int) std::string::npos) {
491  line.erase(pos, 1);
492  pos = line.find(c, pos);
493  }
494 
495  return line;
496  }
497 
498 
499  int checkAlignmentType(int seqNumber, int residNumber, std::string *sequences) {
500  // All codes from RNA - DNA - AA and DEG for both versions
501 
502  static const auto check =
503  [](const std::string &pattern,
504  int &counter,
505  const char &character) -> void {
506  size_t pos(0);
507  while ((pos = pattern.find(character, pos)) != std::string::npos) {
508  pos++;
509  counter++;
510  }
511  };
512 
513  // Inosinic Acid is a nucleotide, but it's not part of DNA or RNA
514  static const std::string COMMON = "ACG";
515  static const std::string RNA = "U"; // Removed ACG as is in common
516  static const std::string DNA = "T"; // Removed ACG as is in common
517 
518  static const std::string DEG_NN = "RYSWKMBDHV"; // Indetermination N not added
519 
520  static const std::string AA = "PVLIMFYWHKRQEDSTUO"; // Indetermination N not added // Removed ACG as is in common
521  static const std::string DEG_AA = "BZ"; // Indetermination X not added
522 
523  int rna = 0, dna = 0, deg_nn = 0, aa = 0, deg_aa = 0;
524 
525  for (int s = 0; s < seqNumber; s++) {
526  for (char c : sequences[s]) {
527  if (c == '-' || c == '?' || c == '.') continue;
528  c = utils::toUpper(c);
529  check(RNA, rna, c);
530  check(DNA, dna, c);
531  check(DEG_NN, deg_nn, c);
532  check(AA, aa, c);
533  check(DEG_AA, deg_aa, c);
534 
535  size_t pos(0);
536  while ((pos = COMMON.find(c, pos)) != std::string::npos) {
537  pos++;
538  aa++;
539  rna++;
540  dna++;
541  }
542  }
543  }
544 
545  if (aa > dna && aa > rna) {
547  }
548  if (dna > rna) {
550  }
552 
553 
554  int i, j, k, l, hitDNA, hitRNA, degenerate, gDNA, gRNA, extDNA, extRNA;
555  float ratioDNA, ratioRNA;
556  // Standard tables
557  static char listRNA[11] = "AGCUNagcun";
558  static char listDNA[11] = "AGCTNagctn";
559 
560  // Degenerate Nucleotides codes
561  static char degeneratedCodes[21] = "MmRrWwSsYyKkVvHhDdBb";
562 
563  // For each sequences, this method locks at the 100 letters (excluding gaps).
564  // The method is able to distinguish between pure DNA/RNA nucleotides or those
565  // containing degenerate Nucleotide letters
566  for (i = 0, gDNA = 0, gRNA = 0, extDNA = 0, extRNA = 0; i < seqNumber; i++) {
567 
568  // Looks at the 100 letters (excluding gaps) while doesn's get the sequence's end
569  // When there are less than a 100 characters, break the loop before reaching that limit
570 
571  residNumber = (int) sequences[i].size();
572  for (j = 0, k = 0, hitDNA = 0, hitRNA = 0, degenerate = 0; j < residNumber; j++)
573  if (sequences[i][j] != '-' && sequences[i][j] != '.' && sequences[i][j] != '?') {
574  k++;
575 
576  /* Recognizes between DNA and RNA. */
577  for (l = 0; l < (int) strlen(listDNA); l++)
578  if (listDNA[l] == sequences[i][j])
579  hitDNA++;
580 
581  for (l = 0; l < (int) strlen(listRNA); l++)
582  if (listRNA[l] == sequences[i][j])
583  hitRNA++;
584 
585  for (l = 0; l < (int) strlen(degeneratedCodes); l++)
586  if (degeneratedCodes[l] == sequences[i][j])
587  degenerate++;
588  }
589 
590  // If input sequences have less than 95% of nucleotides, even when residues
591  // are treated with degenerated codes, consider the input file as containing
592  // amino-acidic sequences.
593  ratioDNA = float(degenerate + hitDNA) / k;
594  ratioRNA = float(degenerate + hitRNA) / k;
595 
596  if (ratioDNA < 0.95 && ratioRNA < 0.95)
597  return SequenceTypes::AA;
598 
599  // Identify precisely if nucleotides sequences are DNA/RNA strict or
600  // any degenerate code has been used in the sequence
601  else if (hitRNA > hitDNA && degenerate == 0)
602  gRNA++;
603  else if (hitRNA > hitDNA && degenerate != 0)
604  extRNA++;
605  else if (hitRNA < hitDNA && degenerate == 0)
606  gDNA++;
607  else if (hitRNA < hitDNA && degenerate != 0)
608  extDNA++;
609  }
610  // Return the datatype with greater values, considering always degenerate
611  // codes
612  if (extDNA != 0 && extDNA > extRNA)
614  else if (extRNA != 0 && extDNA < extRNA)
616  else if (gRNA > gDNA)
617  return SequenceTypes::RNA;
618  else if (gDNA >= gRNA)
619  return SequenceTypes::DNA;
620  else
622  }
623 
624  int *readNumbers(const std::string &line) {
625 
626  int i, nElems = 0;
627  static int *numbers;
628 
629  size_t comma, separ, init;
630 
631  comma = size_t(-1);
632  while ((comma = line.find(',', comma + 1)) != (int) std::string::npos)
633  nElems += 2;
634  nElems += 2;
635 
636  numbers = new int[nElems + 1];
637  numbers[0] = nElems;
638 
639  init = 0;
640  i = 1;
641 
642  do {
643  comma = line.find(',', init);
644  separ = line.find('-', init);
645 
646  if (((separ < comma) || (comma == (int) std::string::npos)) && (separ != (int) std::string::npos)) {
647  numbers[i++] = atoi(line.substr(init, separ - init).c_str());
648  numbers[i++] = atoi(line.substr(separ + 1, comma - separ - 1).c_str());
649  init = comma + 1;
650  } else if ((separ > comma) || (separ == (int) std::string::npos)) {
651  numbers[i++] = atoi(line.substr(init, comma - init).c_str());
652  numbers[i++] = atoi(line.substr(init, comma - init).c_str());
653  init = comma + 1;
654  }
655 
656  if (numbers[i - 2] < 0)
657  {
658  delete [] numbers;
659  return nullptr;
660  }
661  if (numbers[i - 1] < numbers[i - 2])
662  {
663  delete [] numbers;
664  return nullptr;
665  }
666  if (comma == (int) std::string::npos)
667  break;
668 
669  } while (true);
670 
671  return numbers;
672  }
673 
674 
675  char determineColor(char res, const std::string &column) {
676 
677  if (toupper(res) == 'G')
678  return 'o';
679 
680  else if (toupper(res) == 'P')
681  return 'y';
682 
683  else if (res != '-') {
684  switch (toupper(res)) {
685 
686  // (W, L, V, I, M, F): {50%, p}{60%, wlvimafcyhp}
687  case 87:
688  case 76:
689  case 86:
690  case 73:
691  case 77:
692  case 70:
693  if (lookForPattern(column, "p", 0.5)) return 'b';
694  else if (lookForPattern(column, "wlvimafcyhp", 0.6)) return 'b';
695  else return 'w';
696 
697 
698 
699  // (A): {50%, p}{60%, wlvimafcyhp}{85% t,s,g}
700  case 65:
701  if (lookForPattern(column, "p", 0.5)) return 'b';
702  else if (lookForPattern(column, "wlvimafcyhp", 0.6)) return 'b';
703  else if (lookForPattern(column, "t", 0.85)) return 'b';
704  else if (lookForPattern(column, "s", 0.85)) return 'b';
705  else if (lookForPattern(column, "g", 0.85)) return 'b';
706  else return 'w';
707 
708 
709 
710  // BLUE: (C): {50%, p}{60%, wlvimafcyhp}{85% s}
711  // PINK: (C): {85%, c}
712  case 67:
713  if (lookForPattern(column, "p", 0.5)) return 'b';
714  else if (lookForPattern(column, "wlvimafcyhp", 0.6)) return 'b';
715  else if (lookForPattern(column, "s", 0.85)) return 'b';
716  else if (lookForPattern(column, "c", 0.85)) return 'p';
717  else return 'w';
718 
719 
720 
721  // (K, R): {60%, kr}{85%, q}
722  case 75:
723  case 82:
724  if (lookForPattern(column, "kr", 0.6)) return 'r';
725  else if (lookForPattern(column, "q", 0.85)) return 'r';
726  else return 'w';
727 
728 
729 
730  // (T): {50%, ts}{60%, wlvimafcyhp }
731  case 84:
732  if (lookForPattern(column, "ts", 0.5)) return 'g';
733  else if (lookForPattern(column, "wlvimafcyhp", 0.6)) return 'g';
734  else return 'w';
735 
736 
737 
738  // (S): {50%, ts}{80%, wlvimafcyhp }
739  case 83:
740  if (lookForPattern(column, "ts", 0.5)) return 'g';
741  else if (lookForPattern(column, "wlvimafcyhp", 0.8)) return 'g';
742  else return 'w';
743 
744 
745 
746  // (N): {50%, n}{85%, d }
747  case 78:
748  if (lookForPattern(column, "n", 0.5)) return 'g';
749  else if (lookForPattern(column, "d", 0.85)) return 'g';
750  else return 'w';
751 
752 
753 
754  // (Q): {50%, qe}{60%, kr}
755  case 81:
756  if (lookForPattern(column, "qe", 0.5)) return 'g';
757  else if (lookForPattern(column, "kr", 0.6)) return 'g';
758  else return 'w';
759 
760 
761 
762  // (D): {50%, de, n}
763  case 68:
764  if (lookForPattern(column, "de", 0.5)) return 'm';
765  else if (lookForPattern(column, "n", 0.5)) return 'm';
766  else return 'w';
767 
768 
769 
770  // (E): {50%, de,qe}
771  case 69:
772  if (lookForPattern(column, "de", 0.5)) return 'm';
773  else if (lookForPattern(column, "qe", 0.5)) return 'm';
774  else return 'w';
775 
776 
777 
778  // (H,Y): {50%, p}{60%, wlvimafcyhp}
779  case 72:
780  case 89:
781  if (lookForPattern(column, "p", 0.5)) return 'c';
782  else if (lookForPattern(column, "wlvimafcyhp", 0.5)) return 'c';
783  else return 'w';
784 
785  default:
786  return 'w';
787  }
788  }
789  return 'w';
790  }
791 
792 
793  bool lookForPattern(const std::string &data, const std::string &pattern, const float threshold) {
794 
795  float count = 0;
796  int i, j;
797 
798  for (i = 0; i < (int) data.size(); i++) {
799  for (j = 0; j < (int) pattern.size(); j++) {
800  if (utils::toUpper(data[i]) == utils::toUpper(pattern[j])) {
801  count++;
802  break;
803  }
804  }
805  }
806 
807  return (count / data.size()) >= threshold;
808  }
809 
810  void ReplaceStringInPlace(std::string &subject,
811  const std::string &search,
812  const std::string &replace) {
813  size_t pos = 0;
814  while ((pos = subject.find(search, pos)) != std::string::npos) {
815  subject.replace(pos, search.length(), replace);
816  pos += replace.length();
817  }
818  }
819 
820  std::string ReplaceString(std::string subject,
821  const std::string &search,
822  const std::string &replace) {
823  size_t pos = 0;
824  while ((pos = subject.find(search, pos)) != std::string::npos) {
825  subject.replace(pos, search.length(), replace);
826  pos += replace.length();
827  }
828  return subject;
829  }
830 
831 
832  int GetGapStep(int *gapValue, int sequenNumber) {
833  // Special cases. Upper and Lower limits.
834  if (*gapValue == 0)
835  return 11;
836 
837  if (*gapValue == sequenNumber)
838  return 0;
839 
840  float relativeGap = 1.F - float(*gapValue) / sequenNumber;
841 
842  if (relativeGap >= .750)
843  return 10;
844  if (relativeGap >= .500)
845  return 9;
846  if (relativeGap >= .350)
847  return 8;
848  if (relativeGap >= .250)
849  return 7;
850  if (relativeGap >= .200)
851  return 6;
852  if (relativeGap >= .150)
853  return 5;
854  if (relativeGap >= .100)
855  return 4;
856  if (relativeGap >= .050)
857  return 3;
858  if (relativeGap >= .001)
859  return 2;
860  return 1;
861  }
862 
863  int GetGapStep(int *gapValue, float inverseSequenNumber) {
864  // Special cases. Upper and Lower limits.
865  if (*gapValue == 0)
866  return 11;
867 
868  float relativeGap = 1.F - float(*gapValue) * inverseSequenNumber;
869 
870  if (relativeGap == 0.F)
871  return 0;
872  if (relativeGap >= .750F)
873  return 10;
874  if (relativeGap >= .500F)
875  return 9;
876  if (relativeGap >= .350F)
877  return 8;
878  if (relativeGap >= .250F)
879  return 7;
880  if (relativeGap >= .200F)
881  return 6;
882  if (relativeGap >= .150F)
883  return 5;
884  if (relativeGap >= .100F)
885  return 4;
886  if (relativeGap >= .050F)
887  return 3;
888  if (relativeGap >= .001F)
889  return 2;
890  return 1;
891  }
892 
893  int GetSimStep(float *simValue) {
894 
895  if (*simValue == 0.F)
896  return 11;
897  if (*simValue == 1.F)
898  return 0;
899  if (*simValue >= .750F)
900  return 10;
901  if (*simValue >= .500F)
902  return 9;
903  if (*simValue >= .250F)
904  return 8;
905  if (*simValue >= .100F)
906  return 7;
907  if (*simValue >= .010F)
908  return 6;
909  if (*simValue >= .001F)
910  return 5;
911  if (*simValue >= 1e-4F)
912  return 4;
913  if (*simValue >= 1e-5F)
914  return 3;
915  if (*simValue >= 1e-6F)
916  return 2;
917  return 1;
918  }
919 
920  int GetConsStep(float *consValue) {
921  // Special cases. Upper and Lower limits.
922  if (*consValue == 1.F)
923  return 11;
924  if (*consValue == 0.F)
925  return 0;
926 
927  if (*consValue >= .750)
928  return 10;
929  if (*consValue >= .500)
930  return 9;
931  if (*consValue >= .350)
932  return 8;
933  if (*consValue >= .250)
934  return 7;
935  if (*consValue >= .200)
936  return 6;
937  if (*consValue >= .150)
938  return 5;
939  if (*consValue >= .100)
940  return 4;
941  if (*consValue >= .050)
942  return 3;
943  if (*consValue >= .001)
944  return 2;
945  return 1;
946  }
947 
948  bool fileExists(std::string &path) {
949  struct stat buffer;
950  return (stat(path.c_str(), &buffer) == 0);
951  }
952 
953  bool fileExists(std::string &&path) {
954  struct stat buffer;
955  return (stat(path.c_str(), &buffer) == 0);
956  }
957 
958  char toUpper(char c) {
959  if (c >= 'a' and c <= 'z')
960  return (char) (c & (~(1 << 5)));
961  return c;
962  }
963 
964  namespace TerminalColors {
965  std::map<terminalColor, const std::string> colors = {
966  {TerminalColors::RESET, "\033[0m"},
967  {TerminalColors::BLACK, "\033[30m"}, /* Black */
968  {TerminalColors::RED, "\033[31m"}, /* Red */
969  {TerminalColors::GREEN, "\033[32m"}, /* Green */
970  {TerminalColors::YELLOW, "\033[33m"}, /* Yellow */
971  {TerminalColors::BLUE, "\033[34m"}, /* Blue */
972  {TerminalColors::MAGENTA, "\033[35m"}, /* Magenta */
973  {TerminalColors::CYAN, "\033[36m"}, /* Cyan */
974  {TerminalColors::WHITE, "\033[37m"}, /* White */
975  {TerminalColors::BOLD, "\033[1m"}, /* Bold Black */
976  {TerminalColors::UNDERLINE, "\033[4m"} /* Underline */
977 
978  };
979  }
980 
981 }
1 << 1 = 2
Definition: defines.h:80
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
1 << 2 = 4
Definition: defines.h:84
SequenceTypes
Definition: defines.h:72
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
#define StartTiming(name)
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
ErrorCode
Definition: reportsystem.h:56
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
= 0
Definition: defines.h:76
int checkAlignmentType(int seqNumber, int residNumber, std::string *sequences)
Checks an alignment type.
Definition: utils.cpp:499
reporting::reportManager debug
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
void report(ErrorCode message, std::string *vars=nullptr)
Method to report an Error. It will be displayed if Level is equal or higher to VerboseLevel::ERROR...
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
1 << 3 = 8
Definition: defines.h:88
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
1 << 4 = 16
Definition: defines.h:92
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