forked from MichaelStromberg-KTH/Forage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDualCodebook.cpp
More file actions
278 lines (210 loc) · 6.95 KB
/
DualCodebook.cpp
File metadata and controls
278 lines (210 loc) · 6.95 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "DualCodebook.h"
// declare the static variables
unsigned char CDualCodebook::m_numDimensions = 4;
const char* CDualCodebook::m_classDescriptors = "ACGT";
double** CDualCodebook::m_obd_points = NULL;
double** CDualCodebook::m_lvq3_points = NULL;
unsigned char* CDualCodebook::m_obd_classifications = NULL;
unsigned char* CDualCodebook::m_lvq3_classifications = NULL;
unsigned int CDualCodebook::m_obd_numEntries = 0;
unsigned int CDualCodebook::m_lvq3_numEntries = 0;
char** CDualCodebook::m_obd_names = NULL;
char** CDualCodebook::m_lvq3_names = NULL;
void CDualCodebook::LoadCodebooks(void) {
// load the OBD codebook from file
LoadObdCodebook("OBD_CODEBOOK.DAT");
// load the LVQ3 codebook from file
LoadLvq3Codebook("LVQ3_CODEBOOK.DAT");
}
void CDualCodebook::Dispose(void) {
// clean up the names and the points (2D)
for(unsigned int i=0;i<m_obd_numEntries;i++) {
delete [] m_obd_names[i];
delete [] m_obd_points[i];
}
for(unsigned int i=0;i<m_lvq3_numEntries;i++) {
delete [] m_lvq3_names[i];
delete [] m_lvq3_points[i];
}
// clean up the names and the points (1D)
delete [] m_obd_names;
delete [] m_obd_points;
delete [] m_lvq3_names;
delete [] m_lvq3_points;
// clean up the classifications
delete [] m_obd_classifications;
delete [] m_lvq3_classifications;
}
// loads the OBD codebook from file
void CDualCodebook::LoadObdCodebook(const char* filename) {
ifstream in(filename, ios::in | ios::binary);
// if our file is missing, bomb
if(in.fail()) {
printf("ERROR: Could not open the codebook.\n");
exit(1);
}
// retrieve the number of vector entries
char bigEndianUnsignedInt[4];
in.read(bigEndianUnsignedInt,sizeof(unsigned int));
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
unsigned int entries;
memcpy(&entries,bigEndianUnsignedInt,sizeof(unsigned int));
#else
unsigned int entries = GetUnsignedInt(bigEndianUnsignedInt);
#endif
m_obd_numEntries = entries;
// retrieve the number of vector dimensions
unsigned char dim;
in.read((char*)&dim,sizeof(dim));
// initialize our data points
m_obd_points = new double*[entries];
for(unsigned int i=0;i<entries;i++) m_obd_points[i] = new double[dim];
// initialize the classifications
m_obd_classifications = new unsigned char[entries];
// initialize the names
m_obd_names = new char*[entries];
// read in the data points
char bigEndianDouble[8];
for(unsigned int i=0;i<entries;i++) {
// read in the data points
for(int j=0;j<dim;j++) {
in.read(bigEndianDouble,sizeof(double));
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
memcpy(&m_obd_points[i][j],bigEndianDouble,sizeof(double));
#else
m_obd_points[i][j] = GetDouble(bigEndianDouble);
#endif
}
// read in the classification
in.read((char*)&m_obd_classifications[i],sizeof(char));
// read in name length
char nameLength;
in.read((char*)&nameLength,sizeof(nameLength));
// read in the string
char* utfString = new char[nameLength*2];
m_obd_names[i] = new char[nameLength+1];
in.read(utfString,nameLength*2);
GetAnsiString(utfString,m_obd_names[i],nameLength);
// clean up temporary variables
delete [] utfString;
}
in.close();
}
// loads the LVQ3 codebook from file
void CDualCodebook::LoadLvq3Codebook(const char* filename) {
ifstream in(filename, ios::in | ios::binary);
// if our file is missing, bomb
if(in.fail()) {
printf("ERROR: Could not open the codebook.\n");
exit(1);
}
// retrieve the number of vector entries
char bigEndianUnsignedInt[4];
in.read(bigEndianUnsignedInt,sizeof(unsigned int));
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
unsigned int entries;
memcpy(&entries,bigEndianUnsignedInt,sizeof(unsigned int));
#else
unsigned int entries = GetUnsignedInt(bigEndianUnsignedInt);
#endif
m_lvq3_numEntries = entries;
// retrieve the number of vector dimensions
unsigned char dim;
in.read((char*)&dim,sizeof(dim));
// initialize our data points
m_lvq3_points = new double*[entries];
for(unsigned int i=0;i<entries;i++) m_lvq3_points[i] = new double[dim];
// initialize the classifications
m_lvq3_classifications = new unsigned char[entries];
// initialize the names
m_lvq3_names = new char*[entries];
// read in the data points
char bigEndianDouble[8];
for(unsigned int i=0;i<entries;i++) {
// read in the data points
for(int j=0;j<dim;j++) {
in.read(bigEndianDouble,sizeof(double));
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
memcpy(&m_lvq3_points[i][j],bigEndianDouble,sizeof(double));
#else
m_lvq3_points[i][j] = GetDouble(bigEndianDouble);
#endif
}
// read in the classification
in.read((char*)&m_lvq3_classifications[i],sizeof(char));
// read in name length
char nameLength;
in.read((char*)&nameLength,sizeof(nameLength));
// read in the string
char* utfString = new char[nameLength*2];
m_lvq3_names[i] = new char[nameLength+1];
in.read(utfString,nameLength*2);
GetAnsiString(utfString,m_lvq3_names[i],nameLength);
// clean up temporary variables
delete [] utfString;
}
in.close();
}
// converts a character array to a little-endian double
double CDualCodebook::GetDouble(char* buffer) {
char c[8];
c[0] = buffer[7];
c[1] = buffer[6];
c[2] = buffer[5];
c[3] = buffer[4];
c[4] = buffer[3];
c[5] = buffer[2];
c[6] = buffer[1];
c[7] = buffer[0];
return *(double*)c;
}
// converts a character array to a little-endian unsigned integer
unsigned int CDualCodebook::GetUnsignedInt(char* buffer) {
char c[4];
c[0] = buffer[3];
c[1] = buffer[2];
c[2] = buffer[1];
c[3] = buffer[0];
return *(unsigned int*)c;
}
// converts a UTF string to an ANSI string
void CDualCodebook::GetAnsiString(char* buffer, char* c, int len) {
for(int i=0;i<len;i++) c[i] = buffer[2*i+1];
c[len] = 0x0;
}
// checks the classification of the specified point and returns true if quorum is reached
bool CDualCodebook::IsSNP(double* points) {
unsigned char obdWinnerClassification = UCHAR_MAX;
double obdWinnerDifference = DBL_MAX;
// Go through all OBD code vectors
for(unsigned int i=0;i<m_obd_numEntries;i++) {
double difference = 0.0;
double tempDiff = 0.0;
for(unsigned int j=0;j<m_numDimensions;j++) {
tempDiff = m_obd_points[i][j] - points[j];
difference += tempDiff * tempDiff;
}
if(difference < obdWinnerDifference) {
obdWinnerClassification = m_obd_classifications[i];
obdWinnerDifference = difference;
}
}
unsigned char lvq3WinnerClassification = UCHAR_MAX;
double lvq3WinnerDifference = DBL_MAX;
// Go through all LVQ3 code vectors
for(unsigned int i=0;i<m_lvq3_numEntries;i++) {
double difference = 0.0;
double tempDiff = 0.0;
for(unsigned int j=0;j<m_numDimensions;j++) {
tempDiff = m_lvq3_points[i][j] - points[j];
difference += tempDiff * tempDiff;
}
if(difference < lvq3WinnerDifference) {
lvq3WinnerClassification = m_lvq3_classifications[i];
lvq3WinnerDifference = difference;
}
}
// if both networks classify this as a SNP, return true
if((lvq3WinnerClassification == 1) && (obdWinnerClassification == 1)) return true;
return false;
}