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.
 
 
 

182 lines
5.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.core.bgzf.outputstream;
  21. import bio.core.bgzf.constants;
  22. import bio.core.bgzf.compress;
  23. import bio.core.utils.roundbuf;
  24. import std.stream;
  25. import std.exception;
  26. import std.parallelism;
  27. import std.array;
  28. import std.algorithm : max;
  29. /// Class for BGZF compression
  30. class BgzfOutputStream : Stream {
  31. private {
  32. Stream _stream = void;
  33. TaskPool _task_pool = void;
  34. ubyte[] _buffer = void;
  35. size_t _current_size;
  36. int _compression_level = void;
  37. alias Task!(bgzfCompress, ubyte[], int) CompressionTask;
  38. RoundBuf!(CompressionTask*) _compression_tasks = void;
  39. }
  40. /// Create new BGZF output stream which will use
  41. /// provided $(D task_pool) to do multithreaded compression.
  42. this(Stream output_stream,
  43. int compression_level=-1,
  44. TaskPool task_pool=taskPool,
  45. size_t max_block_size=BGZF_BLOCK_SIZE)
  46. {
  47. enforce(-1 <= compression_level && compression_level <= 9,
  48. "Compression level must be a number in interval [-1, 9]");
  49. _stream = output_stream;
  50. _task_pool = task_pool;
  51. _compression_level = compression_level;
  52. _buffer = uninitializedArray!(ubyte[])(max_block_size);
  53. _compression_tasks = RoundBuf!(CompressionTask*)(max(task_pool.size, 1));
  54. readable = false;
  55. writeable = true;
  56. seekable = false;
  57. }
  58. override size_t readBlock(void* buffer, size_t size) {
  59. throw new ReadException("Stream is not readable");
  60. }
  61. override ulong seek(long offset, SeekPos whence) {
  62. throw new SeekException("Stream is not seekable");
  63. }
  64. override size_t writeBlock(const void* buf, size_t size) {
  65. if (size + _current_size >= _buffer.length) {
  66. size_t room;
  67. ubyte[] data = (cast(ubyte*)buf)[0 .. size];
  68. while (data.length + _current_size >= _buffer.length) {
  69. room = _buffer.length - _current_size;
  70. _buffer[$ - room .. $] = data[0 .. room];
  71. data = data[room .. $];
  72. _current_size = _buffer.length;
  73. flushCurrentBlock();
  74. }
  75. _buffer[0 .. data.length] = data[];
  76. _current_size = data.length;
  77. } else {
  78. _buffer[_current_size .. _current_size + size] = (cast(ubyte*)buf)[0 .. size];
  79. _current_size += size;
  80. }
  81. return size;
  82. }
  83. /// Force flushing current block, even if it is not yet filled.
  84. /// Should be used when it's not desired to have records crossing block borders.
  85. void flushCurrentBlock() {
  86. if (_current_size == 0)
  87. return;
  88. if (_compression_tasks.full) {
  89. auto block = _compression_tasks.front.yieldForce();
  90. _stream.writeExact(block.ptr, block.length);
  91. _compression_tasks.popFront();
  92. }
  93. auto compression_task = task!bgzfCompress(_buffer[0 .. _current_size],
  94. _compression_level);
  95. _compression_tasks.put(compression_task);
  96. _task_pool.put(compression_task);
  97. _buffer = uninitializedArray!(ubyte[])(_buffer.length);
  98. _current_size = 0;
  99. }
  100. /// Flush all remaining BGZF blocks and underlying stream.
  101. override void flush() {
  102. flushCurrentBlock();
  103. foreach (task; _compression_tasks) {
  104. auto block = task.yieldForce();
  105. _stream.writeExact(block.ptr, block.length);
  106. }
  107. _stream.flush();
  108. _current_size = 0;
  109. }
  110. /// Flush all remaining BGZF blocks and close source stream.
  111. /// Automatically adds empty block at the end, serving as
  112. /// indicator of end of stream.
  113. override void close() {
  114. flush();
  115. addEofBlock();
  116. _stream.close();
  117. writeable = false;
  118. }
  119. /// Adds EOF block. This function is called in close() method.
  120. void addEofBlock() {
  121. _stream.writeExact(BGZF_EOF.ptr, BGZF_EOF.length);
  122. }
  123. }
  124. unittest {
  125. import bio.core.bgzf.inputstream;
  126. import bio.core.bgzf.blockrange;
  127. import std.array, std.range, std.stdio;
  128. char[] data = "my very l" ~ array(repeat('o', 1000000)) ~ "ng string";
  129. auto output_stream = new MemoryStream();
  130. auto bgzf_output_stream = new BgzfOutputStream(output_stream);
  131. bgzf_output_stream.writeExact(data.ptr, data.length);
  132. bgzf_output_stream.close();
  133. auto input_stream = new MemoryStream(output_stream.data);
  134. input_stream.seekSet(0);
  135. auto bgzf_input_stream = new BgzfInputStream(input_stream);
  136. char[] uncompressed_data = new char[data.length];
  137. bgzf_input_stream.readExact(uncompressed_data.ptr, data.length);
  138. assert(uncompressed_data == data);
  139. }