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.

234 lines
5.0 KiB

9 years ago
9 years ago
  1. #line 1 "region.rl"
  2. /*
  3. This file is part of BioD.
  4. Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
  5. Permission is hereby granted, free of charge, to any person obtaining a
  6. copy of this software and associated documentation files (the "Software"),
  7. to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. and/or sell copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. DEALINGS IN THE SOFTWARE.
  20. */
  21. module bio.core.region;
  22. #line 26 "region.d"
  23. static const int region_parser_start = 1;
  24. static const int region_parser_first_final = 3;
  25. static const int region_parser_error = 0;
  26. static const int region_parser_en_region = 1;
  27. #line 40 "region.rl"
  28. import std.conv;
  29. struct Region {
  30. string reference;
  31. uint beg;
  32. uint end;
  33. }
  34. Region parseRegion(string str) {
  35. char* p = cast(char*)str.ptr;
  36. char* pe = p + str.length;
  37. char* eof = pe;
  38. int cs;
  39. long uint_value;
  40. Region region;
  41. region.beg = 0;
  42. region.end = uint.max;
  43. #line 57 "region.d"
  44. {
  45. cs = region_parser_start;
  46. }
  47. #line 62 "region.rl"
  48. #line 64 "region.d"
  49. {
  50. if ( p == pe )
  51. goto _test_eof;
  52. switch ( cs )
  53. {
  54. goto case; case 1:
  55. if ( (*p) < 43u ) {
  56. if ( 33u <= (*p) && (*p) <= 41u )
  57. goto st3;
  58. } else if ( (*p) > 60u ) {
  59. if ( 62u <= (*p) && (*p) <= 126u )
  60. goto st3;
  61. } else
  62. goto st3;
  63. goto st0;
  64. st0:
  65. cs = 0;
  66. goto _out;
  67. st3:
  68. if ( ++p == pe )
  69. goto _test_eof3;
  70. goto case; case 3:
  71. if ( (*p) == 58u )
  72. goto tr3;
  73. if ( 33u <= (*p) && (*p) <= 126u )
  74. goto st3;
  75. goto st0;
  76. tr3:
  77. #line 29 "region.rl"
  78. { region.reference = str[0 .. p - str.ptr]; }
  79. goto st4;
  80. st4:
  81. if ( ++p == pe )
  82. goto _test_eof4;
  83. goto case; case 4:
  84. #line 100 "region.d"
  85. if ( (*p) == 44u )
  86. goto tr5;
  87. if ( (*p) < 48u ) {
  88. if ( 33u <= (*p) && (*p) <= 47u )
  89. goto st5;
  90. } else if ( (*p) > 57u ) {
  91. if ( 58u <= (*p) && (*p) <= 126u )
  92. goto st5;
  93. } else
  94. goto tr5;
  95. goto st0;
  96. st5:
  97. if ( ++p == pe )
  98. goto _test_eof5;
  99. goto case; case 5:
  100. if ( 33u <= (*p) && (*p) <= 126u )
  101. goto st5;
  102. goto st0;
  103. tr5:
  104. #line 25 "region.rl"
  105. { uint_value = 0; }
  106. #line 26 "region.rl"
  107. { if ((*p) != ',') uint_value *= 10, uint_value += (*p) - '0'; }
  108. goto st6;
  109. tr6:
  110. #line 26 "region.rl"
  111. { if ((*p) != ',') uint_value *= 10, uint_value += (*p) - '0'; }
  112. goto st6;
  113. st6:
  114. if ( ++p == pe )
  115. goto _test_eof6;
  116. goto case; case 6:
  117. #line 133 "region.d"
  118. switch( (*p) ) {
  119. case 44u: goto tr6;
  120. case 45u: goto tr7;
  121. default: break;
  122. }
  123. if ( 48u <= (*p) && (*p) <= 57u )
  124. goto tr6;
  125. goto st0;
  126. tr7:
  127. #line 30 "region.rl"
  128. { region.beg = to!uint(uint_value - 1); }
  129. goto st2;
  130. st2:
  131. if ( ++p == pe )
  132. goto _test_eof2;
  133. goto case; case 2:
  134. #line 150 "region.d"
  135. if ( (*p) == 44u )
  136. goto tr2;
  137. if ( 48u <= (*p) && (*p) <= 57u )
  138. goto tr2;
  139. goto st0;
  140. tr2:
  141. #line 25 "region.rl"
  142. { uint_value = 0; }
  143. #line 26 "region.rl"
  144. { if ((*p) != ',') uint_value *= 10, uint_value += (*p) - '0'; }
  145. goto st7;
  146. tr8:
  147. #line 26 "region.rl"
  148. { if ((*p) != ',') uint_value *= 10, uint_value += (*p) - '0'; }
  149. goto st7;
  150. st7:
  151. if ( ++p == pe )
  152. goto _test_eof7;
  153. goto case; case 7:
  154. #line 170 "region.d"
  155. if ( (*p) == 44u )
  156. goto tr8;
  157. if ( 48u <= (*p) && (*p) <= 57u )
  158. goto tr8;
  159. goto st0;
  160. default: break;
  161. }
  162. _test_eof3: cs = 3; goto _test_eof;
  163. _test_eof4: cs = 4; goto _test_eof;
  164. _test_eof5: cs = 5; goto _test_eof;
  165. _test_eof6: cs = 6; goto _test_eof;
  166. _test_eof2: cs = 2; goto _test_eof;
  167. _test_eof7: cs = 7; goto _test_eof;
  168. _test_eof: {}
  169. if ( p == eof )
  170. {
  171. switch ( cs ) {
  172. case 3:
  173. case 4:
  174. case 5:
  175. #line 29 "region.rl"
  176. { region.reference = str[0 .. p - str.ptr]; }
  177. break;
  178. case 6:
  179. #line 30 "region.rl"
  180. { region.beg = to!uint(uint_value - 1); }
  181. break;
  182. case 7:
  183. #line 31 "region.rl"
  184. { region.end = to!uint(uint_value); }
  185. break;
  186. #line 203 "region.d"
  187. default: break;
  188. }
  189. }
  190. _out: {}
  191. }
  192. #line 63 "region.rl"
  193. return region;
  194. }
  195. unittest {
  196. auto region1 = parseRegion("chr1:1,000-2000");
  197. assert(region1.reference == "chr1");
  198. assert(region1.beg == 999);
  199. assert(region1.end == 2000);
  200. auto region2 = parseRegion("chr2");
  201. assert(region2.reference == "chr2");
  202. assert(region2.beg == 0);
  203. assert(region2.end == uint.max);
  204. auto region3 = parseRegion("chr3:1,000,000");
  205. assert(region3.reference == "chr3");
  206. assert(region3.beg == 999_999);
  207. assert(region3.end == uint.max);
  208. }