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.core.call;
25 
26 import bio.core.base;
27 import bio.core.genotype;
28 
29 /// A genotype call
30 struct Call(alias Gt, B) 
31 {
32     alias Gt!B G;
33 
34     private {
35         string _sample = void;
36         string _chr = void;
37         ulong _pos = void;
38         B _refbase = void;
39         G _gt = void;
40         float _qual = void;
41     }
42 
43     /// Constructor
44     this(string sample, string chr, ulong pos,
45          B refbase, G genotype, float quality=float.nan)
46     {
47         _sample = sample;
48         _chr = chr;
49         _pos = pos;
50         _refbase = refbase;
51         _gt = genotype;
52         _qual = quality;
53     }
54 
55     /// Sample name
56     string sample() @property const {
57         return _sample;
58     }
59 
60     /// Chromosome name
61     string chromosome() @property const {
62         return _chr;
63     }
64 
65     /// 0-based position on the reference
66     ulong position() @property const {
67         return _pos;
68     }
69 
70     /// Reference base at the site
71     B reference_base() @property const {
72         return _refbase;
73     }
74 
75     /// Most probable genotype
76     ref const(G) genotype() @property const {
77         return _gt;
78     }
79 
80     ///
81     alias genotype this; 
82 
83     /// Phred-scaled quality score. If unknown, set to NaN.
84     float quality() @property const {
85         return _qual;
86     }
87 
88     /// Returns true if this call is not a reference one.
89     bool is_variant() @property const {
90         return _gt != G(_refbase);
91     }
92 }
93 
94 alias Call!(DiploidGenotype, Base5) DiploidCall5;
95 alias Call!(DiploidGenotype, Base16) DiploidCall;
96 alias DiploidCall DiploidCall16;
97 
98 unittest {
99     auto call = DiploidCall("NA01234", "chr10", 543210,
100                             Base('T'), diploidGenotype(Base('C'), Base('T')),
101                             47.0);
102 
103     assert(call.is_variant);
104     assert(call.is_heterozygous);
105     assert(call.reference_base == 'T');
106 }