forked from MichaelStromberg-KTH/Forage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBayesianAlgorithm.cpp
More file actions
213 lines (155 loc) · 5.67 KB
/
BayesianAlgorithm.cpp
File metadata and controls
213 lines (155 loc) · 5.67 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "BayesianAlgorithm.h"
CBayesianAlgorithm::CBayesianAlgorithm() {}
CBayesianAlgorithm::~CBayesianAlgorithm() {}
// Returns the Bayesian probability that an alignment position is polymorphic
// Performance data (iterations / s) with 5 sequences
// ==================================================
// Perl reference 171
// Java reference 3738 (22x)
// Microsoft C++ 4364
// Intel C++ 5826 (34x)
// Intel C++ Opt2 6085 (36x)
// Microsoft C++ char 5976
// Intel C++ char 9086 (53x) (2.4x Java)
double CBayesianAlgorithm::Analyze(unsigned char* bases, unsigned char* qualities, unsigned int numSeqs) {
// If we are using more sequences than has been tested with our hash function
// exit the program.
if(numSeqs > 1023) {
printf("Number of sequences: %u\n",numSeqs);
printf("The hash function in the Key class has only been tested with 1023 sequences.\n");
exit(1);
}
unsigned int depth = numSeqs;
// reset our main variables
m_Psnp = 0.0;
m_Pvar = 0.0;
m_Variation = 0;
//
// calculate base error probability
//
double** Perrors = new double*[depth];
for(unsigned int i=0;i<depth;i++) {
Perrors[i] = new double[4];
double error = pow(10.0,(double)qualities[i]/-10.0);
double diverror = error / 3.0;
for(unsigned char j=0;j<4;j++)
if(bases[i] == j) Perrors[i][j] = 1.0 - error;
else Perrors[i][j] = diverror;
}
// assign variation-specific absolute priors
CProbHash priorVariation = CBayesianUtils::GetPriorVariations(depth);
// processed variation probabilities
CProbHash probVariation;
CKeyTable keys, newKeys;
CKey seedKey(1.0,0);
keys.Put(&seedKey);
CKey tempKey, newKey;
unsigned int maxterms = 50;
double priorVar, newPriorVar;
unsigned int hashCode;
double sum;
CKey* keyHashes = NULL;
// consider each position in the column
for(unsigned int depthIteration=0;depthIteration<depth;depthIteration++) {
//printf("Starting depth: %u, keys: %u.\n",depthIteration,keys.m_Size);
unsigned int numKeyHashes = keys.m_Size;
// clear previous key hashes
if(keyHashes) delete [] keyHashes;
keyHashes = new CKey[numKeyHashes];
keys.GetKeys(keyHashes);
// consider each old variation
for(unsigned int i=0;i<numKeyHashes;i++) {
tempKey = keyHashes[i];
//printf("* Using key %u:%u:%u:%u sum: %f variation: %u.\n",tempKey.m_a,tempKey.m_c,tempKey.m_g,tempKey.m_t,tempKey.m_Sum,tempKey.m_Variation);
priorVar = priorVariation.Get(tempKey.m_Variation);
sum = tempKey.m_Sum;
// consider contribution of each nucleotide at the current level
for(unsigned char base=0;base<4;base++) {
// create new variation
newKey = tempKey.MakeNewKey(base);
// find out if there is a duplicate
hashCode = newKey.HashCode();
if(newKeys.Contains(hashCode)) newKey = newKeys.Get(hashCode);
// get the prior variation data
newPriorVar = priorVariation.Get(newKey.m_Variation);
// calculate the sum of probabilities
newKey.CalculateSum(base,depthIteration,priorVar,newPriorVar,sum,Perrors[depthIteration][base]);
//printf("Adding key %u:%u:%u:%u sum: %f variation: %u to newKeys.\n",newKey.m_a,newKey.m_c,newKey.m_g,newKey.m_t,newKey.m_Sum,newKey.m_Variation);
// add the new key to the key set
newKeys.Put(&newKey);
}
}
// Reduce number of terms by keeping the top 50 probabilities
keys.SortAndAdd(&newKeys,depthIteration);
}
// do some cleanup
for(unsigned int i=0;i<depth;i++) delete [] Perrors[i];
delete [] Perrors;
delete [] keyHashes;
// calculate the sum of the probabilities so that we can normalize them
// and update total posterior probability for each term
double normSum = 0;
double tmpDouble;
unsigned char variation;
unsigned int numKeys = keys.m_Size;
CKey* keyArray = new CKey[numKeys];
keys.GetKeys(keyArray);
for(unsigned int i=0;i<numKeys;i++) {
variation = keyArray[i].m_Variation;
sum = keyArray[i].m_Sum;
// weight the variation probability with prior variation
tmpDouble = probVariation.Get(variation) + sum;
probVariation.Add(variation,tmpDouble);
// aggregate variation probability weighted by prior variation
normSum += sum;
}
// clear previous key array
delete [] keyArray;
// Normalize probabilities
double probMultiplicity[5];
memset(probMultiplicity,0,sizeof(double)*5);
double maxProbVariation = 0;
char maxVariation = 0;
//double[] hashProbabilities = probVariation.
// probVariation.hash;
//String[] hashVariations = probVariation.hashLabels;
// public String[] hashLabels = { "","a","c","g","t","ac","ag","at","cg","ct","gt","acg","act","agt","cgt","acgt" };
// normalize the monomorphic variations
for(unsigned int i=1;i<5;i++) {
tmpDouble = probVariation.m_hash[i] / normSum;
probMultiplicity[1] += tmpDouble;
if(tmpDouble > maxProbVariation) {
maxProbVariation = tmpDouble;
maxVariation = i;
}
}
// normalize the bi-allelic variations
for(unsigned int i=5;i<11;i++) {
tmpDouble = probVariation.m_hash[i] / normSum;
probMultiplicity[2] += tmpDouble;
if(tmpDouble > maxProbVariation) {
maxProbVariation = tmpDouble;
maxVariation = i;
}
}
// normalize the tri-allelic variations
for(unsigned int i=11;i<15;i++) {
tmpDouble = probVariation.m_hash[i] / normSum;
probMultiplicity[3] += tmpDouble;
if(tmpDouble > maxProbVariation) {
maxProbVariation = tmpDouble;
maxVariation = i;
}
}
// normalize the tetra-allelic variation
tmpDouble = probVariation.m_hash[15] / normSum;
probMultiplicity[4] = tmpDouble;
if(tmpDouble > maxProbVariation) {
maxProbVariation = tmpDouble;
maxVariation = 15;
}
m_Psnp = 1 - probMultiplicity[1];
m_Pvar = maxProbVariation;
m_Variation = maxVariation;
return m_Psnp;
}