libStatGen Software 1
Loading...
Searching...
No Matches
PedigreeAlleles.h
1/*
2 * Copyright (C) 2010 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __PEDALLELES_H__
19#define __PEDALLELES_H__
20
21#include "LongInt.h"
22
24{
25public:
26 char one;
27 char two;
28
29 Alleles()
30 {
31 one = two = 0;
32 }
33
34 char & operator [](int i)
35 {
36 return (i == 1) ? one : two;
37 }
38
39 // is the genotype fully defined?
40 bool isKnown()
41 {
42 return (one * two) != 0;
43 }
44 bool isHeterozygous()
45 {
46 return isKnown() && (one != two);
47 }
48 bool isHomozygous()
49 {
50 return isKnown() && (one == two);
51 }
52 bool hasAllele(int a)
53 {
54 return (one == a) || (two == a);
55 }
56
57 // in a bi-allelic system (a, NOT a)
58 bool isHeterozygousFor(int a)
59 {
60 return isHeterozygous() && hasAllele(a);
61 }
62 bool isHomozygousFor(int a)
63 {
64 return !(isHeterozygousFor(a));
65 }
66
67 // how may alleles a in this genotype?
68 int countAlleles(int a)
69 {
70 return ((one == a) ? 1 : 0) + ((two == a) ? 1 : 0);
71 }
72
73 // what is the other allele, assuming genotype is (a, X)
74 int otherAllele(int a)
75 {
76 return ((one == a) ? two : one);
77 }
78
79 // are two unordered genotypes identical?
80 int identicalTo(Alleles & al)
81 {
82 return ((al.one == one) && (al.two == two)) ||
83 ((al.two == one) && (al.one == two));
84 }
85
86 // how many alleles are identical by state
87 int countIBS(Alleles & al)
88 {
89 return (one == al.one) ?
90 ((two == al.two) ? 2 : 1) :
91 ((one == al.two) ?
92 ((two == al.one) ? 2 : 1) :
93 (((two == al.one) || (two == al.two)) ? 1 : 0));
94 }
95
96 int operator == (Alleles & rhs)
97{
98 return identicalTo(rhs);
99 }
100 int operator != (Alleles & rhs)
101 {
102 return !identicalTo(rhs);
103 }
104
105 char Hi()
106 {
107 return one > two ? one : two;
108 }
109 char Lo()
110 {
111 return one > two ? two : one;
112 }
113
114 int SequenceCoded()
115 {
116 return isKnown() ? Hi() *(Hi() - 1) / 2 + Lo() : 0;
117 }
118
119 longint BinaryCoded()
120 {
121 if (isKnown())
122 {
123 longint allele1(1);
124 longint allele2(1);
125
126 allele1 <<= one - 1;
127 allele2 <<= two - 1;
128
129 return allele1 | allele2;
130 }
131 else
132 return NOTZERO;
133 }
134
135 void Intersect(Alleles & geno)
136 {
137 char a1 = Lo(), a2 = Hi();
138 char b1 = geno.Lo(), b2 = geno.Hi();
139
140 if (a1 == b1 && a2 == b2)
141 return;
142 if (a1 == b1 || a1 == b2)
143 one = two = a1;
144 else if (a2 == b1 || a2 == b2)
145 one = two = a2;
146 else
147 one = two = 0;
148 }
149
150 void Intersect(char allele)
151 {
152 if (one != allele && two != allele)
153 one = two = 0;
154 else
155 one = two = allele;
156 }
157
158 bool AddAllele(char allele)
159 {
160 if (one == allele || two == allele)
161 return true;
162
163 if (one != 0 && two != 0)
164 return false;
165
166 if (one == 0) one = allele;
167 else two = allele;
168 return true;
169 }
170
171 void Wipe()
172 {
173 one = two = 0;
174 }
175};
176
177#endif
178