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.
 
 
 

163 lines
4.8 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.readrange;
  21. import bio.bam.read;
  22. import bio.bam.abstractreader;
  23. import bio.bam.reader;
  24. import bio.core.bgzf.inputstream;
  25. import bio.core.bgzf.virtualoffset;
  26. import bio.core.utils.switchendianness;
  27. import std.stream;
  28. import std.algorithm;
  29. import std.system;
  30. /// Read + its start/end virtual offsets
  31. struct BamReadBlock {
  32. VirtualOffset start_virtual_offset; ///
  33. VirtualOffset end_virtual_offset; ///
  34. BamRead read; ///
  35. alias read this; ///
  36. ///
  37. BamReadBlock dup() @property const {
  38. return BamReadBlock(start_virtual_offset, end_virtual_offset, read.dup);
  39. }
  40. }
  41. ///
  42. mixin template withOffsets() {
  43. /**
  44. Returns: virtual offsets of beginning and end of the current read
  45. plus the current read itself.
  46. */
  47. BamReadBlock front() @property {
  48. return BamReadBlock(_start_voffset,
  49. _stream.virtualTell(),
  50. _current_record);
  51. }
  52. private VirtualOffset _start_voffset;
  53. private void beforeNextBamReadLoad() {
  54. _start_voffset = _stream.virtualTell();
  55. }
  56. }
  57. ///
  58. mixin template withoutOffsets() {
  59. /**
  60. Returns: current read
  61. */
  62. ref BamRead front() @property {
  63. return _current_record;
  64. }
  65. private void beforeNextBamReadLoad() {}
  66. }
  67. /// $(D front) return type is determined by $(I IteratePolicy)
  68. struct BamReadRange(alias IteratePolicy)
  69. {
  70. /// Create new range from IChunkInputStream.
  71. this(ref IChunkInputStream stream, BamReader reader=null) {
  72. _stream = stream;
  73. _reader = reader;
  74. _endian_stream = new EndianStream(_stream, Endian.littleEndian);
  75. readNext();
  76. }
  77. ///
  78. bool empty() @property const {
  79. return _empty;
  80. }
  81. mixin IteratePolicy;
  82. ///
  83. void popFront() {
  84. readNext();
  85. }
  86. private:
  87. IChunkInputStream _stream;
  88. EndianStream _endian_stream;
  89. BamReader _reader;
  90. BamRead _current_record;
  91. bool _empty = false;
  92. /*
  93. Reads next bamRead block from stream.
  94. */
  95. void readNext() {
  96. // In fact, on BAM files containing a special EOF BGZF block
  97. // this condition will be always false!
  98. //
  99. // The reason is that we don't want to unpack next block just
  100. // in order to see if it's an EOF one or not.
  101. if (_stream.eof()) {
  102. _empty = true;
  103. return;
  104. }
  105. // In order to get the right virtual offset, we need to do it here.
  106. beforeNextBamReadLoad();
  107. // Here's where _empty is really set!
  108. int block_size = void;
  109. ubyte* ptr = cast(ubyte*)(&block_size);
  110. auto _read = 0;
  111. while (_read < int.sizeof) {
  112. auto _actually_read = _endian_stream.readBlock(ptr, int.sizeof - _read);
  113. if (_actually_read == 0) {
  114. version(development) {
  115. import std.stdio;
  116. stderr.writeln("[info][bamRead range] empty, read ", _read, " bytes, expected ", int.sizeof);
  117. }
  118. _empty = true;
  119. return;
  120. }
  121. _read += _actually_read;
  122. ptr += _actually_read;
  123. }
  124. if (std.system.endian != Endian.littleEndian) {
  125. switchEndianness(&block_size, int.sizeof);
  126. }
  127. _current_record = BamRead(_stream.readSlice(block_size));
  128. _current_record.associateWithReader(cast(IBamSamReader)_reader);
  129. }
  130. }
  131. /// Returns: lazy range of BamRead/BamReadBlock structs constructed from a given stream.
  132. auto bamReadRange(alias IteratePolicy=withoutOffsets)(ref IChunkInputStream stream, BamReader reader) {
  133. return BamReadRange!IteratePolicy(stream, reader);
  134. }