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.bai.bin;
25 
26 import bio.core.bgzf.chunk;
27 import bio.std.hts.bam.constants;
28 
29 /// Distinct bin
30 struct Bin {
31 
32     /// Construct a bin with an id
33     this(uint id) nothrow {
34         this.id = id;
35     }
36 
37     uint id; /// bin number
38     Chunk[] chunks; 
39 
40     /// How deep the bin is in the tree
41     int level() @property const nothrow {
42         if (id == 0) return 0;
43         if (id < 9) return 1;
44         if (id < 73) return 2;
45         if (id < 585) return 3;
46         if (id < 4681) return 4;
47         return 5;
48     }
49 
50     /// Returns whether the bin is a leaf in the B-tree
51     bool is_leaf() @property const nothrow {
52         return id > BAI_MAX_NONLEAF_BIN_ID;
53     }
54 
55     /// Check if bin can overlap with a region
56     bool canOverlapWith(int begin, int end) const nothrow {
57         if (id == 0) return true;
58         if (id > BAI_MAX_BIN_ID) return false;
59 
60         /// The following code is based on reg2bins() function
61         if (begin < 0) begin = 0;
62         auto magic_number = 4681;
63         auto b = begin >> 14;
64         auto e = end   >> 14;
65 
66         while (true) {
67             auto delta = id - magic_number;
68             if (b <= delta && delta <= e) return true;
69 
70             magic_number >>= 3;
71 
72             if (magic_number == 0) return false;
73 
74             b >>= 3;
75             e >>= 3;
76         }
77     } 
78 }
79 
80 /// Returns bin number for [beg, end) interval (zero-based).
81 /// Taken from SAM/BAM specification.
82 ushort reg2bin(int beg, int end) {
83     if (end == beg) end = beg + 1; // edge case
84 
85     --end;
86     if (beg>>14 == end>>14) return cast(ushort)(((1<<15)-1)/7 + (beg>>14));
87     if (beg>>17 == end>>17) return cast(ushort)(((1<<12)-1)/7 + (beg>>17));
88     if (beg>>20 == end>>20) return cast(ushort)(((1<<9)-1)/7  + (beg>>20));
89     if (beg>>23 == end>>23) return cast(ushort)(((1<<6)-1)/7  + (beg>>23));
90     if (beg>>26 == end>>26) return cast(ushort)(((1<<3)-1)/7  + (beg>>26));
91     return 0;
92 }