1 /*
2     This file is part of BioD.
3     Copyright (C) 2012-2014    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 
25 module bio.std.hts.bam.region;
26 
27 ///
28 struct BamRegion {
29     uint ref_id; /// Reference ID in the BAM file
30     uint start;  /// 0-based leftmost coordinate (included)
31     uint end;    /// 0-based rightmost coordinate (excluded)
32 
33     int opCmp(const ref BamRegion other) const nothrow {
34         if (this.ref_id > other.ref_id) { return  1; }
35         if (this.ref_id < other.ref_id) { return  -1; }
36 	
37         if (this.start > other.start) { return  1; }
38         if (this.start < other.start) { return  -1; }
39 
40 	if (this.end > other.end) { return  1; }
41         if (this.end < other.end) { return  -1; }
42 
43         return 0;
44     }
45 
46     bool overlaps(uint ref_id, uint position) const {
47         return this.ref_id == ref_id && start <= position && position < end;
48     }
49 
50     bool fullyLeftOf(uint ref_id, uint position) {
51         if (this.ref_id < ref_id)
52             return true;
53         if (this.ref_id == ref_id && end <= position)
54             return true;
55         return false;
56     }
57 
58     bool fullyRightOf(uint ref_id, uint position) {
59         if (this.ref_id > ref_id)
60             return true;
61         if (this.ref_id == ref_id && start > position)
62             return true;
63         return false;
64     }
65 }