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.genotype;
25 
26 import bio.core.base;
27 import bio.core.tinymap;
28 
29 /// Holds ordered pair of two alleles
30 struct DiploidGenotype(B) {
31 
32     mixin TinyMapInterface!(B.ValueSetSize ^^ 2);
33 
34     private static ubyte _getCode(B b1, B b2) {
35         auto c1 = b1.internal_code;
36         auto c2 = b2.internal_code;
37         return cast(ubyte)(c1 * B.ValueSetSize + c2);
38     }
39 
40     /// Construct a genotype from two bases
41     /// Every ambiguous base gets converted to 'N' internally.
42     this(B b1, B b2) {
43         _code = _getCode(b1, b2);
44     }
45 
46     /// Construct homozygous genotype
47     this(B b) {
48         _code = _getCode(b, b);
49     }
50 
51     /// First allele
52     B base1() @property const {
53         return B.fromInternalCode(_code / B.ValueSetSize);
54     }
55 
56     /// Second allele
57     B base2() @property const {
58         return B.fromInternalCode(_code % B.ValueSetSize);
59     }
60 
61     ///
62     bool is_heterozygous() @property const {
63         return base1 != base2;
64     }
65 
66     ///
67     bool is_homozygous() @property const {
68         return base1 == base2;
69     }
70 
71     /// String representation B1|B2 (TODO: add phasing in future?)
72     string toString() const {
73         return base1 ~ "|" ~ base2;
74     }
75 }
76 
77 /// Create an instance of DiploidGenotype
78 auto diploidGenotype(B...)(B bases) {
79     return DiploidGenotype!(B[0])(bases);
80 }
81 
82 unittest {
83     auto g1 = diploidGenotype(Base('C'), Base('W'));
84     assert(g1.base1 == 'C');
85     assert(g1.base2 == 'W');
86 
87     // By default, Base5 is used
88     auto g2 = diploidGenotype(Base5('C'));
89     assert(g2.base1 == g2.base2);
90 
91     // Both bases must be of the same type
92     static assert(!__traits(compiles, diploidGenotype(Base5('T'), Base16('D'))));
93 }