forked from MichaelStromberg-KTH/Forage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKey.cpp
More file actions
172 lines (141 loc) · 3.13 KB
/
Key.cpp
File metadata and controls
172 lines (141 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "Key.h"
CKey::CKey()
: m_a(0)
, m_c(0)
, m_g(0)
, m_t(0)
, m_Variation(0)
, m_Sum(0)
, m_Hash(0)
{}
CKey::CKey(double s, unsigned char v)
: m_a(0)
, m_c(0)
, m_g(0)
, m_t(0)
, m_Variation(v)
, m_Sum(s)
, m_Hash(0)
{}
// TODO: make sure that in the Bayesian algorithm, that we never encounter a null variation
CKey::CKey(unsigned int a, unsigned int c, unsigned int g, unsigned int t)
: m_a(a)
, m_c(c)
, m_g(g)
, m_t(t)
, m_Variation(0)
, m_Sum(0)
, m_Hash(0)
{}
CKey::~CKey(void) {}
// creates a new key by incrementing the given allele
CKey CKey::MakeNewKey(unsigned char base) {
CKey tempKey(m_a,m_c,m_g,m_t);
switch(base) {
case 0:
tempKey.m_a++;
break;
case 1:
tempKey.m_c++;
break;
case 2:
tempKey.m_g++;
break;
case 3:
tempKey.m_t++;
break;
default:
printf("ERROR: invalid base found: %d\n",base);
exit(1);
}
unsigned char position = 0;
// m_Variation is reset in the constructor
if(tempKey.m_a > 0) tempKey.m_Variation += 1;
if(tempKey.m_c > 0) tempKey.m_Variation += 2;
if(tempKey.m_g > 0) tempKey.m_Variation += 4;
if(tempKey.m_t > 0) tempKey.m_Variation += 8;
return tempKey;
}
// calculates a new posterior sum
void CKey::CalculateSum(unsigned char base, unsigned int depth, double priorVar, double newPriorVar, double oldSum, double Perror) {
unsigned int count;
switch(base) {
case 0:
count = m_a;
break;
case 1:
count = m_c;
break;
case 2:
count = m_g;
break;
case 3:
count = m_t;
break;
default:
printf("ERROR: invalid base found: %d\n",base);
exit(1);
}
// calculate prior modification factor
// - take into account change in variation
// - take into account base count change
depth++;
m_Sum += oldSum * Perror * newPriorVar * count / (priorVar * depth);
}
// get the hashcode for the key
unsigned int CKey::HashCode(void) {
unsigned int a = 0x9e3779b9 + m_a;
unsigned int b = 0x9e3779b9 + m_c;
unsigned int c = m_g;
mix(a,b,c);
a += m_t;
b += m_a;
c += m_c;
mix(a,b,c);
//printf("hash: %11u a: %3d c: %3d g: %3d t: %3d\n",c,m_a,m_c,m_g,m_t);
return c;
}
// get the hashcode for the key
unsigned int CKey::HashCode(unsigned int depth_a, unsigned int depth_c, unsigned int depth_g, unsigned int depth_t) {
unsigned int a = 0x9e3779b9 + depth_a;
unsigned int b = 0x9e3779b9 + depth_c;
unsigned int c = depth_g;
mix(a,b,c);
a += depth_t;
b += depth_a;
c += depth_c;
mix(a,b,c);
return c;
}
// compares this key to another key k
/*
bool CKey::IsEqual(CKey* k) {
if(m_Sum != k->m_Sum) return false;
if(m_a != k->m_a) return false;
if(m_c != k->m_c) return false;
if(m_g != k->m_g) return false;
if(m_t != k->m_t) return false;
return true;
}
*/
bool CKey::operator<(const CKey& key) const {
return m_Sum > key.m_Sum;
}
/*
void CKey::operator=(const CKey& key) {
m_a = key.m_a;
m_c = key.m_c;
m_g = key.m_g;
m_t = key.m_t;
m_Sum = key.m_Sum;
m_Variation = key.m_Variation;
}
*/
void CKey::operator=(const CKey* key) {
m_a = key->m_a;
m_c = key->m_c;
m_g = key->m_g;
m_t = key->m_t;
m_Sum = key->m_Sum;
m_Variation = key->m_Variation;
}