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.
 
 
 

269 lines
8.0 KiB

  1. /*
  2. This file is part of BioD.
  3. Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a
  5. copy of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. DEALINGS IN THE SOFTWARE.
  19. */
  20. module bio.core.base;
  21. import bio.core.tinymap;
  22. import std.traits;
  23. /// Code common to both Base5 and Base16
  24. mixin template CommonBaseOperations() {
  25. /// Convert to char
  26. char asCharacter() @property const { return _code2char[_code]; }
  27. ///
  28. alias asCharacter this;
  29. }
  30. /// Base representation supporting full set of IUPAC codes
  31. struct Base {
  32. mixin TinyMapInterface!16;
  33. private enum ubyte[256] _char2code = [
  34. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  35. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  36. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  37. 1, 2, 4, 8, 15,15,15,15, 15,15,15,15, 15, 0,15,15,
  38. 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
  39. 15,15, 5, 6, 8,15, 7, 9, 15,10,15,15, 15,15,15,15,
  40. 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
  41. 15,15, 5, 6, 8,15, 7, 9, 15,10,15,15, 15,15,15,15,
  42. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  43. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  44. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  45. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  46. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  47. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  48. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  49. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15
  50. ];
  51. // = 0000
  52. //
  53. // A 0001
  54. // C 0010
  55. // G 0100
  56. // T 1000
  57. //
  58. // W 1001 (A T) Weak
  59. // S 0110 (C G) Strong
  60. //
  61. // M 0011 (A C) aMino
  62. // K 1100 (G T) Keto
  63. // R 0101 (A G) puRine
  64. // Y 1010 (A G) pYrimidine
  65. //
  66. // B 1110 (not A)
  67. // D 1101 (not C)
  68. // H 1011 (not G)
  69. // V 0111 (not T)
  70. //
  71. // N 1111 (aNy base)
  72. private enum _code2char = "=ACMGRSVTWYHKDBN";
  73. private enum ubyte[16] _complement_table = [0x0, 0x8, 0x4, 0xC,
  74. 0x2, 0xA, 0x6, 0xE,
  75. 0x1, 0x9, 0x5, 0xD,
  76. 0x3, 0xB, 0x7, 0xF];
  77. /// Complementary base
  78. Base complement() @property const {
  79. // take the code, reverse the bits, and return the base
  80. return Base.fromInternalCode(_complement_table[_code]);
  81. }
  82. unittest {
  83. import std.ascii;
  84. foreach (i, c; _code2char) {
  85. assert(_char2code[c] == i);
  86. }
  87. foreach (c; 0 .. 256) {
  88. auto c2 = _code2char[_char2code[c]];
  89. if (c2 != 'N') {
  90. if ('0' <= c && c <= '9') {
  91. assert(c2 == "ACGT"[c - '0']);
  92. } else {
  93. assert(c2 == toUpper(c));
  94. }
  95. }
  96. }
  97. }
  98. mixin CommonBaseOperations;
  99. /// Construct from IUPAC code
  100. this(char c) {
  101. _code = _char2code[cast(ubyte)c];
  102. }
  103. /// ditto
  104. this(dchar c) {
  105. _code = _char2code[cast(ubyte)c];
  106. }
  107. private enum ubyte[5] nt5_to_nt16 = [1, 2, 4, 8, 15];
  108. private static Base fromBase5(Base5 base) {
  109. Base b = void;
  110. b._code = nt5_to_nt16[base.internal_code];
  111. return b;
  112. }
  113. /// Conversion to Base5
  114. Base5 opCast(T)() const
  115. if (is(T == Base5))
  116. {
  117. return Base5.fromBase16(this);
  118. }
  119. T opCast(T)() const
  120. if (is(Unqual!T == char) || is(Unqual!T == dchar))
  121. {
  122. return asCharacter;
  123. }
  124. }
  125. unittest {
  126. Base b = 'W';
  127. assert(b == 'W');
  128. b = Base.fromInternalCode(0);
  129. assert(b == '=');
  130. }
  131. alias Base Base16;
  132. /// Base representation supporting only 'A', 'C', 'G', 'T', and 'N'
  133. /// (internal codes are 0, 1, 2, 3, and 4 correspondingly)
  134. struct Base5 {
  135. mixin TinyMapInterface!5;
  136. private enum ubyte[256] _char2code = [
  137. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  138. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  139. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  140. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  141. 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
  142. 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  143. 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
  144. 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  145. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  146. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  147. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  148. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  149. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  150. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  151. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  152. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  153. ];
  154. private enum _code2char = "ACGTN";
  155. private enum ubyte[16] nt16_to_nt5 = [4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4];
  156. mixin CommonBaseOperations;
  157. /// Complementary base
  158. Base5 complement() @property const {
  159. return Base5.fromInternalCode(cast(ubyte)(_code == 4 ? 4 : (3 - _code)));
  160. }
  161. /// Construct base from one of "acgtACGT" symbols.
  162. /// Every other character is converted to 'N'
  163. this(char c) {
  164. _code = _char2code[cast(ubyte)c];
  165. }
  166. /// ditto
  167. this(dchar c) {
  168. _code = _char2code[cast(ubyte)c];
  169. }
  170. private static Base5 fromBase16(Base16 base) {
  171. Base5 b = void;
  172. b._code = nt16_to_nt5[base.internal_code];
  173. return b;
  174. }
  175. /// Conversion to Base16
  176. Base16 opCast(T)() const
  177. if(is(T == Base16))
  178. {
  179. return Base16.fromBase5(this);
  180. }
  181. T opCast(T)() const
  182. if (is(Unqual!T == char) || is(Unqual!T == dchar))
  183. {
  184. return asCharacter;
  185. }
  186. }
  187. unittest {
  188. auto b5 = Base5('C');
  189. assert(b5.internal_code == 1);
  190. b5 = Base5.fromInternalCode(3);
  191. assert(b5 == 'T');
  192. // doesn't work with std.conv.to
  193. //
  194. //import std.conv;
  195. //assert(to!Base16(b5).internal_code == 8);
  196. assert((cast(Base16)b5).internal_code == 8);
  197. }
  198. /// Complement base, which might be Base5, Base16, char, or dchar.
  199. B complementBase(B)(B base) {
  200. static if(is(Unqual!B == dchar) || is(Unqual!B == char))
  201. {
  202. return cast(B)(Base16(base).complement);
  203. }
  204. else
  205. return base.complement;
  206. }
  207. /// Convert character to base
  208. template charToBase(B=Base16)
  209. {
  210. B charToBase(C)(C c)
  211. if(is(Unqual!C == char) || is(Unqual!C == dchar))
  212. {
  213. return B(c);
  214. }
  215. }
  216. unittest {
  217. assert(complementBase('T') == 'A');
  218. assert(complementBase('G') == 'C');
  219. assert(complementBase(Base5('A')) == Base5('T'));
  220. assert(complementBase(Base16('C')) == Base16('G'));
  221. assert(charToBase!Base16('A').complement == Base16('T'));
  222. }