forked from wtsi-npg/simtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gtc.cpp
321 lines (283 loc) · 8.79 KB
/
Gtc.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// Gtc.cpp
//
// Author: Jennifer Liddle (js10)
//
// $Id: Gtc.cpp 1354 2010-11-11 16:20:09Z js10 $
//
// Copyright (c) 2009 - 2010 Genome Research Ltd.
//
// Author: Jennifer Liddle <[email protected], [email protected]>
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// 3. Neither the name of the Genome Research Ltd nor the names of its contributors
// may be used to endorse or promote products derived from software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. EVENT SHALL GENOME RESEARCH LTD. BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
//
#include "Gtc.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
XFormClass::XFormClass(void)
{
}
Gtc::Gtc(void)
{
version=0;
filename = "";
}
void Gtc::open(char *f, int control)
{
string s;
s = f;
open(s,control);
}
void Gtc::open(string filename, int control)
{
char buff[256];
char c;
int nEntries;
ifstream file;
errorMsg = "";
// Clear arrays (to help prevent memory leaks)
XForm.clear();
xRawControl.clear();
yRawControl.clear();
xRawIntensity.clear();
yRawIntensity.clear();
genotypes.clear();
baseCalls.clear();
scores.clear();
this->filename = "";
file.open(filename.c_str(),ios::binary);
if (!file) {
errorMsg = "Can't open file " + filename;
return;
}
file.get(buff,4);
if (strcmp(buff,"gtc")) {
errorMsg = "File " + filename + " has invalid header";
return;
}
this->filename = filename;
file.get(c);
version = (int)c;
/*
if (sizeof(int) != 4) {
cout << "Errr...an integer seems to be " << sizeof(int) << " bytes" << endl;
}
if (sizeof(short) != 2) {
cout << "Errr...a short seems to be " << sizeof(short) << " bytes" << endl;
}
if (sizeof(float) != 4) {
cout << "Errr...a float seems to be " << sizeof(float) << " bytes" << endl;
}
*/
file.read((char*)&nEntries,4);
for (int n=0; n<nEntries; n++) {
short id;
unsigned int offset;
file.read((char*)&id,2);
file.read((char*)&offset,4);
switch (id) {
case 1: // number of SNPs
numSnps = offset;
break;
case 10: sampleName = readString(file,offset); break;
case 11: samplePlate = readString(file,offset); break;
case 12: sampleWell = readString(file,offset); break;
case 100: clusterFile = readString(file,offset); break;
case 101: manifest = readString(file,offset); break;
case 200: imagingDate = readString(file,offset); break;
case 201: autocallDate = readString(file,offset); break;
case 300: autocallVersion = readString(file,offset); break;
case 400: if (control & XFORM) { readXForm(file,offset); } break;
case 500: if (control & CONTROL) { readShortArray(file,offset,xRawControl); } break;
case 501: if (control & CONTROL) { readShortArray(file,offset,yRawControl); } break;
case 1000: if (control & INTENSITY) { readShortArray(file,offset,xRawIntensity); } break;
case 1001: if (control & INTENSITY) { readShortArray(file,offset,yRawIntensity); } break;
case 1002: if (control & GENOTYPES) { readByteArray(file,offset,genotypes); } break;
case 1003: if (control & BASECALLS) { readBaseCallArray(file,offset,baseCalls); } break;
case 1004: if (control & SCORES) { readFloatArray(file,offset,scores); } break;
case 1005: // scanner data
ios::pos_type pos = file.tellg();
file.seekg(offset);
scannerName = _readString(file);
file.read((char*)&pmtGreen,4);
file.read((char*)&pmtRed,4);
scannerVersion = _readString(file);
imagingUser = _readString(file);
file.seekg(pos);
break;
}
}
file.close();
}
string Gtc::_readString(ifstream &file)
{
char c;
char buff[256];
memset(buff,0,256);
file.get(c);
int len = (unsigned char)c;
file.read(buff,len);
string *s = new string(buff);
return *s;
}
string Gtc::readString(ifstream &file, int offset)
{
ios::pos_type pos = file.tellg();
file.seekg(offset);
string s = _readString(file);
file.seekg(pos);
return s;
}
string Gtc::dump(void)
{
ostringstream s;
s << "Number of SNPs: " << numSnps << endl
<< "Sample Name: " << sampleName << endl
<< "Sample Plate: " << samplePlate << endl
<< "Sample Well: " << sampleWell << endl
<< "Cluster File: " << clusterFile << endl
<< "Manifest: " << manifest << endl
<< "Imaging Date: " << imagingDate << endl
<< "Autocall Date: " << autocallDate << endl
<< "Autocall Version: " << autocallVersion << endl
<< "Scanner Name: " << scannerName << endl
<< "Pmt Green: " << pmtGreen << endl
<< "Pmt Red: " << pmtRed << endl
<< "Scanner Version: " << scannerVersion << endl
<< "Imaging User: " << imagingUser << endl
<< "XForm contains: " << XForm.size() << " entries" << endl
<< "xRawControl: " << xRawControl.size() << " entries" << endl
<< "yRawControl: " << yRawControl.size() << " entries" << endl
<< "xRawIntensity: " << xRawIntensity.size() << " entries" << endl
<< "yRawIntensity: " << yRawIntensity.size() << " entries" << endl
<< "Genotypes: " << genotypes.size() << " entries" << endl
<< "Scores: " << scores.size() << " entries" << endl
<< "Base Calls: " << baseCalls.size() << " entries" << endl
<< endl;
return s.str();
}
void Gtc::readXForm(ifstream &file, int offset)
{
int arrayLen;
float reserved;
ios::pos_type pos = file.tellg();
file.seekg(offset);
file.read((char*)&arrayLen,4);
for (int n=0; n<arrayLen; n++) {
XFormClass X;
file.read((char*)&X.version, 4);
file.read((char*)&X.xOffset, 4);
file.read((char*)&X.yOffset, 4);
file.read((char*)&X.xScale, 4);
file.read((char*)&X.yScale, 4);
file.read((char*)&X.shear, 4);
file.read((char*)&X.theta, 4);
file.read((char*)&reserved, 4);
file.read((char*)&reserved, 4);
file.read((char*)&reserved, 4);
file.read((char*)&reserved, 4);
file.read((char*)&reserved, 4);
file.read((char*)&reserved, 4);
this->XForm.push_back(X);
}
file.seekg(pos);
}
void Gtc::readShortArray(ifstream &file, int offset, vector<unsigned short> &a)
{
int arrayLen;
unsigned short x;
ios::pos_type pos = file.tellg();
file.seekg(offset);
file.read((char*)&arrayLen, 4);
for (int n=0; n<arrayLen; n++) {
file.read((char*)&x,2);
a.push_back(x);
}
file.seekg(pos);
}
void Gtc::readByteArray(ifstream &file, int offset, vector<char> &a)
{
int arrayLen;
char c;
ios::pos_type pos = file.tellg();
file.seekg(offset);
file.read((char*)&arrayLen, 4);
for (int n=0; n<arrayLen; n++) {
file.get(c);
a.push_back(c);
}
file.seekg(pos);
}
void Gtc::readBaseCallArray(ifstream &file, int offset, vector<BaseCallClass> &a)
{
int arrayLen;
BaseCallClass b;
char c;
ios::pos_type pos = file.tellg();
file.seekg(offset);
file.read((char*)&arrayLen, 4);
for (int n=0; n<arrayLen; n++) {
file.get(c); b.a = c;
file.get(c); b.b = c;
a.push_back(b);
}
file.seekg(pos);
}
void Gtc::readFloatArray(ifstream &file, int offset, vector<float> &a)
{
int arrayLen;
float f;
ios::pos_type pos = file.tellg();
file.seekg(offset);
file.read((char*)&arrayLen, 4);
for (int n=0; n<arrayLen; n++) {
file.read((char*)&f,4);
a.push_back(f);
}
file.seekg(pos);
}
double Gtc::passRate(double cutOff)
{
int pass=0;
for (vector<float>::iterator s = scores.begin(); s != scores.end(); s++) {
if (*s >= cutOff) pass++;
}
if (scores.size() == 0) return 0;
return (double)pass / (double)scores.size() * 100.0;
}
double Gtc::correctedPassRate(double cutOff)
{
int pass=0;
int fail=0;
for (vector<float>::iterator s = scores.begin(); s != scores.end(); s++) {
if (*s > 0.00000001) {
if (*s >= cutOff) pass++;
else fail++;
}
}
if (pass+fail == 0) return 0.0;
return (double)pass / (double)(pass+fail) * 100.0;
}