💿🐜 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.

91 lines
2.3 KiB

  1. /*
  2. * Copyright (C) 2020 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 "nucleobase.hpp"
  20. #include <cstdint>
  21. namespace dna {
  22. namespace base {
  23. /**
  24. * Decodes an IUPAC degenerate base symbol into a bit mask representing the possible bases represented by the symbol.
  25. *
  26. * @param symbol IUPAC degenerate base symbol.
  27. * @return Bit mask representing the possible bases represented by the symbol.
  28. */
  29. static std::uint8_t decode(char symbol)
  30. {
  31. static constexpr std::uint8_t bases[26] =
  32. {
  33. 0b0001, // A
  34. 0b1110, // B
  35. 0b0010, // C
  36. 0b1101, // D
  37. 0, // E
  38. 0, // F
  39. 0b0100, // G
  40. 0b1011, // H
  41. 0, // I
  42. 0, // J
  43. 0b1100, // K
  44. 0, // L
  45. 0b0011, // M
  46. 0b1111, // N
  47. 0, // O
  48. 0, // P
  49. 0, // Q
  50. 0b0101, // R
  51. 0b0110, // S
  52. 0b1000, // T
  53. 0b1000, // U
  54. 0b0111, // V
  55. 0b1001, // W
  56. 0, // X
  57. 0b1010, // Y
  58. 0, // Z
  59. };
  60. return (symbol < 'A' || symbol > 'Z') ? 0 : bases[symbol - 'A'];
  61. }
  62. char complement_rna(char symbol)
  63. {
  64. static constexpr char* complements = "TVGHZZCDZZMZKNZZZYSAABWZRZ";
  65. return (symbol < 'A' || symbol > 'Z') ? 'Z' : complements[symbol - 'A'];
  66. }
  67. char complement_dna(char symbol)
  68. {
  69. static constexpr char* complements = "UVGHZZCDZZMZKNZZZYSAABWZRZ";
  70. return (symbol < 'A' || symbol > 'Z') ? 'Z' : complements[symbol - 'A'];
  71. }
  72. char transcribe(char symbol)
  73. {
  74. return (symbol == 'T') ? 'U' : (symbol == 'U') ? 'T' : symbol;
  75. }
  76. int compare(char a, char b)
  77. {
  78. std::uint8_t bases = decode(a) & decode(b);
  79. return (bases & 1) + (bases >> 1 & 1) + (bases >> 2 & 1) + (bases >> 3 & 1);
  80. }
  81. } // namespace base
  82. } // namespace dna