1 /*
2     This file is part of BioD.
3     Copyright (C) 2012    Artem Tarasov <lomereiter@gmail.com>
4 
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     
12     The above copyright notice and this permission notice shall be included in
13     all copies or substantial portions of the Software.
14     
15     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21     DEALINGS IN THE SOFTWARE.
22 
23 */
24 module bio.std.hts.bam.splitter;
25 
26 import bio.std.hts.bam.read;
27 
28 import std.array;
29 import std.functional;
30 import std.range;
31 import std.traits;
32 
33 /// Constructs range of chunks where sum { fn(read) | read in chunk }
34 /// does not exceed given number.
35 struct ReadRangeSplitter(R, alias fn) 
36 {
37     this(R range, size_t threshold, bool split_by_ref) {
38         _range = range;
39         _size = threshold;
40         _split_by_ref = split_by_ref;
41         _appender = appender!(ElementType!R[])();
42         getNextChunk();
43     }
44 
45     private {
46         R _range;
47         bool _empty;
48         bool _split_by_ref;
49         size_t _size;
50         Appender!(ElementType!R[]) _appender;
51     }
52 
53     bool empty() @property {
54         return _empty;
55     }
56 
57     ElementType!R[] front() @property {
58         return _appender.data.dup;
59     }
60 
61     void popFront() {
62         _appender.clear();
63         getNextChunk(); 
64     }
65 
66     private void getNextChunk() {
67         if (_range.empty) {
68             _empty = true;
69             return;
70         } 
71 
72         auto first_read = _range.front;
73         _range.popFront();
74 
75         size_t total_size = first_read.size_in_bytes;
76         auto average_size_estimate = unaryFun!fn(first_read);
77 
78         _appender.reserve(_size / average_size_estimate);
79         _appender.put(first_read);
80 
81         while (total_size <= _size && !_range.empty) {
82             auto read = _range.front;
83             if (_split_by_ref && (read.ref_id != first_read.ref_id)) {
84                 break;
85             }
86             total_size += unaryFun!fn(read);
87             _appender.put(read);
88             _range.popFront();
89         }
90     }
91 }
92 
93 /// Split range in chunks where total amount of memory consumed by all reads 
94 /// in the chunk is roughly chunk_size bytes.
95 ///
96 /// Parameter $(D split_by_ref) specifies that each chunk should contain reads
97 /// aligned to the same reference. In most cases, this simplifies post-processing,
98 /// but in some cases this is not required, therefore it is optional.
99 auto chunksConsumingLessThan(R)(R reads, size_t size_in_bytes, bool split_by_ref=true) {
100     return ReadRangeSplitter!(R, "a.size_in_bytes")(reads, size_in_bytes, split_by_ref);
101 }
102 
103 /// Split range in chunks each containing no more than N reads
104 auto chunksOfSize(R)(R reads, size_t N, bool split_by_ref=true) {
105     return ReadRangeSplitter!(R, "1")(reads, N, split_by_ref);
106 }