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.
 
 
 

166 lines
5.9 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.bam.baifile;
  21. public import bio.bam.bai.chunk;
  22. public import bio.bam.bai.bin;
  23. import bio.core.bgzf.virtualoffset;
  24. import bio.bam.constants;
  25. import std.stream;
  26. import std.system;
  27. import std.exception;
  28. import std.algorithm;
  29. import std.conv;
  30. import std.range;
  31. import std.file;
  32. import std.path;
  33. /// Represents index for a single reference
  34. struct Index {
  35. /// Information about bins
  36. Bin[] bins;
  37. /// Virtual file offsets of first alignments overlapping 16384-byte windows
  38. /// on the reference sequence. This linear index is used to reduce amount
  39. /// of file seeks for region queries, since with its help one can reduce the
  40. /// number of chunks to be investigated based on their end position.
  41. ///
  42. ///
  43. /// Suppose you have a region [beg, end) and want to do a query.
  44. ///
  45. /// Here's the reference:
  46. /// [....................!..............!.................................]
  47. /// beg end
  48. ///
  49. /// Here's the same reference with 16384-byte long windows:
  50. /// [%...........%.......!....%.........!..%...........%...........%......]
  51. /// beg end
  52. /// [ 1st window][ 2nd window][...
  53. ///
  54. /// With linear index, we can take the second window, find out what is
  55. /// the minimum virtual offset among alignments overlapping this window,
  56. /// and skip all chunks which end position is less or equal to this offset:
  57. ///
  58. /// [........@...........!..............!.................................]
  59. /// . ..min. offset beg end
  60. /// [ ). . <- this chunk is skipped
  61. /// [ ) <- this one is not
  62. ///
  63. VirtualOffset[] ioffsets;
  64. /// Get (approximate) virtual offset of the first alignment overlapping $(D position)
  65. ///
  66. /// Returned virtual offset is less or equal to real offset.
  67. VirtualOffset getMinimumOffset(int position) {
  68. int pos = max(0, position);
  69. int _i = min(pos / BAI_LINEAR_INDEX_WINDOW_SIZE, cast(int)ioffsets.length - 1);
  70. auto min_offset = (_i == -1) ? VirtualOffset(0) : ioffsets[_i];
  71. return min_offset;
  72. }
  73. }
  74. struct BaiFile {
  75. Index[] indices;
  76. /// Initialize from stream which contains BAI data
  77. this(ref Stream stream) {
  78. _stream = stream;
  79. parse();
  80. }
  81. /// Open BAI file given either filename of BAM file or that of BAI file.
  82. this(string filename) {
  83. Stream fstream;
  84. if (!endsWith(filename, ".bai")) {
  85. /// Unfortunately, std.path.addExt is going to be deprecated
  86. auto first_filename = filename ~ ".bai";
  87. auto second_filename = to!string(retro(find(retro(filename), '.'))) ~ "bai";
  88. if (std.file.exists(first_filename)) {
  89. fstream = new BufferedFile(absolutePath(first_filename));
  90. } else {
  91. if (std.file.exists(second_filename)) {
  92. fstream = new BufferedFile(absolutePath(second_filename));
  93. } else {
  94. throw new Exception("searched for " ~ first_filename ~ " or " ~
  95. second_filename ~ ", found neither");
  96. }
  97. }
  98. } else {
  99. fstream = new BufferedFile(filename);
  100. }
  101. Stream estream = new EndianStream(fstream, Endian.littleEndian);
  102. this(estream);
  103. }
  104. private:
  105. Stream _stream;
  106. /// according to section 4.2 of SAM/BAM specification
  107. void parse() {
  108. auto magic = _stream.readString(4);
  109. enforce(magic == "BAI\1", "Invalid file format: expected BAI\\1");
  110. int n_ref;
  111. _stream.read(n_ref);
  112. indices.length = n_ref;
  113. foreach (i; 0 .. n_ref) {
  114. int n_bin;
  115. _stream.read(n_bin);
  116. indices[i].bins.length = n_bin;
  117. foreach (j; 0 .. n_bin) {
  118. _stream.read(indices[i].bins[j].id);
  119. int n_chunk;
  120. _stream.read(n_chunk);
  121. indices[i].bins[j].chunks.length = n_chunk;
  122. foreach (k; 0 .. n_chunk) {
  123. ulong tmp;
  124. _stream.read(tmp);
  125. indices[i].bins[j].chunks[k].beg = VirtualOffset(tmp);
  126. _stream.read(tmp);
  127. indices[i].bins[j].chunks[k].end = VirtualOffset(tmp);
  128. }
  129. }
  130. int n_intv;
  131. _stream.read(n_intv);
  132. indices[i].ioffsets.length = n_intv;
  133. foreach (j; 0 .. n_intv) {
  134. ulong tmp;
  135. _stream.read(tmp);
  136. indices[i].ioffsets[j] = VirtualOffset(tmp);
  137. }
  138. }
  139. }
  140. }