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.
 
 
 

107 lines
3.5 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.splitter;
  21. import bio.bam.read;
  22. import std.array;
  23. import std.functional;
  24. import std.range;
  25. import std.traits;
  26. /// Constructs range of chunks where sum { fn(read) | read in chunk }
  27. /// does not exceed given number.
  28. struct ReadRangeSplitter(R, alias fn)
  29. {
  30. this(R range, size_t threshold, bool split_by_ref) {
  31. _range = range;
  32. _size = threshold;
  33. _split_by_ref = split_by_ref;
  34. _appender = appender!(ElementType!R[])();
  35. getNextChunk();
  36. }
  37. private {
  38. R _range;
  39. bool _empty;
  40. bool _split_by_ref;
  41. size_t _size;
  42. Appender!(ElementType!R[]) _appender;
  43. }
  44. bool empty() @property {
  45. return _empty;
  46. }
  47. ElementType!R[] front() @property {
  48. return _appender.data.dup;
  49. }
  50. void popFront() {
  51. _appender.clear();
  52. getNextChunk();
  53. }
  54. private void getNextChunk() {
  55. if (_range.empty) {
  56. _empty = true;
  57. return;
  58. }
  59. auto first_read = _range.front;
  60. _range.popFront();
  61. size_t total_size = first_read.size_in_bytes;
  62. auto average_size_estimate = unaryFun!fn(first_read);
  63. _appender.reserve(_size / average_size_estimate);
  64. _appender.put(first_read);
  65. while (total_size <= _size && !_range.empty) {
  66. auto read = _range.front;
  67. if (_split_by_ref && (read.ref_id != first_read.ref_id)) {
  68. break;
  69. }
  70. total_size += unaryFun!fn(read);
  71. _appender.put(read);
  72. _range.popFront();
  73. }
  74. }
  75. }
  76. /// Split range in chunks where total amount of memory consumed by all reads
  77. /// in the chunk is roughly chunk_size bytes.
  78. ///
  79. /// Parameter $(D split_by_ref) specifies that each chunk should contain reads
  80. /// aligned to the same reference. In most cases, this simplifies post-processing,
  81. /// but in some cases this is not required, therefore it is optional.
  82. auto chunksConsumingLessThan(R)(R reads, size_t size_in_bytes, bool split_by_ref=true) {
  83. return ReadRangeSplitter!(R, "a.size_in_bytes")(reads, size_in_bytes, split_by_ref);
  84. }
  85. /// Split range in chunks each containing no more than N reads
  86. auto chunksOfSize(R)(R reads, size_t N, bool split_by_ref=true) {
  87. return ReadRangeSplitter!(R, "1")(reads, N, split_by_ref);
  88. }