💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.4 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "codon.hpp"
  20. namespace genetics {
  21. namespace codon {
  22. /**
  23. * Returns the index of a nucleobase for use with a translation table.
  24. *
  25. * @param base IUPAC code of nucleobase, either `U`, `T`, `C`, `A`, or `G`.
  26. * @return Index of the nucleobase, or a negative value if a non-standard nucleobase was supplied.
  27. */
  28. static inline int base_index(char base)
  29. {
  30. switch (base)
  31. {
  32. case 'U':
  33. case 'T':
  34. return 0;
  35. case 'C':
  36. return 1;
  37. case 'A':
  38. return 2;
  39. case 'G':
  40. return 3;
  41. }
  42. return ~3;
  43. }
  44. /**
  45. * Returns the index of a codon for use with a translation table.
  46. *
  47. * @param base1 IUPAC code of first nucleobase, either `U`, `T`, `C`, `A`, or `G`.
  48. * @param base2 IUPAC code of second nucleobase, either `U`, `T`, `C`, `A`, or `G`.
  49. * @param base3 IUPAC code of third nucleobase, either `U`, `T`, `C`, `A`, or `G`.
  50. * @return Index of codon, or a negative value if a non-standard nucleobase was supplied.
  51. */
  52. static inline int codon_index(char base1, char base2, char base3)
  53. {
  54. int i = base_index(base1);
  55. int j = base_index(base2);
  56. int k = base_index(base3);
  57. return (i << 4) | (j << 2) | k;
  58. }
  59. inline char translate(char base1, char base2, char base3, const char* aas)
  60. {
  61. int index = codon_index(base1, base2, base3);
  62. if (index < 0)
  63. return '-';
  64. return aas[index];
  65. }
  66. bool is_start(char base1, char base2, char base3, const char* starts)
  67. {
  68. char aa = translate(base1, base2, base3, starts);
  69. return ((aa != '-') && (aa != '*'));
  70. }
  71. bool is_stop(char base1, char base2, char base3, const char* aas)
  72. {
  73. char aa = translate(base1, base2, base3, aas);
  74. return (aa == '*');
  75. }
  76. } // namspace codon
  77. } // namespace genetics