-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSim.cpp
262 lines (238 loc) · 8.2 KB
/
Sim.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
// Sim.cpp
//
// Author: Jennifer Liddle (js10)
//
// $Id: Sim.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 Genome Research Ltd nor the names of the
// 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
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL GENOME RESEARCH LTD. BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT 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 "Sim.h"
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <stdint.h>
#include <errno.h>
#include <stdio.h>
using namespace std;
Sim::Sim(void)
{
version=0;
inPath = "";
filename = "";
errorMsg="";
nanCount = 0;
infCount = 0;
}
void Sim::openInput(string fname)
{
inPath = fname;
char *f = new char[fname.length()+1];
strcpy(f, fname.c_str());
openLowLevel(f);
}
void Sim::openLowLevel(char *fname) {
// open C-style file descriptor and read header data
if (strcmp(fname, "-")==0) inFile = stdin;
else inFile = fopen(fname, "r");
char *magicChars = (char *) calloc(4, sizeof(char));
int size_t;
size_t = fread(magicChars, 1, 3, inFile);
magic = string(magicChars);
if (strcmp(magicChars, "sim") || size_t != 3) {
throw("File " + string(fname) + " has invalid header ["
+ string(magic) + "]");
}
size_t = fread(&version, 1, 1, inFile);
if (size_t != 1) throw("Error reading .sim file header version");
size_t = fread(&sampleNameSize, 2, 1, inFile);
if (size_t != 1) throw("Error reading .sim file header name size");
size_t = fread(&numSamples, 4, 1, inFile);
if (size_t != 1) throw("Error reading .sim file header samples");
size_t = fread(&numProbes, 4, 1, inFile);
if (size_t != 1) throw("Error reading .sim file header probes");
size_t = fread(&numChannels, 1, 1, inFile);
if (size_t != 1) throw("Error reading .sim file header channels");
size_t = fread(&numberFormat, 1, 1, inFile);
if (size_t != 1) throw("Error reading .sim file header numeric format");
if (ferror(inFile)!=0) {
throw("Error reading header from .sim file: [" + string(fname) + "]");
}
// calculate and store record length
switch (numberFormat) {
case FLOAT: numericBytes= 4; break;
case INTEGER: numericBytes = 2; break;
case SCALED_INTEGER: numericBytes = 2; break;
default: cerr << "Invalid number format " << numberFormat;
exit(1);
}
sampleIntensityTotal = numProbes * numChannels;
recordLength = numProbes * numChannels;
recordLength *= numericBytes;
recordLength += sampleNameSize;
}
void Sim::close(void) {
// close input and output files (if open, and not equal to stdin or stdout)
if (filename != "" && filename !="-") {
fout.close();
}
if (inPath !="" && inPath!="-") {
if (ferror(inFile)) {
cerr << "Input file is in error state!" << endl;
exit(1);
}
int status;
status = fclose(inFile);
if (status!=0) {
cerr << "Failed to close input file" << endl;
exit(1);
}
}
}
void Sim::openOutput(string fname) {
filename = fname;
_openOut(filename);
}
void Sim::reset(void)
{
// return to starting point of .sim file (immediately after header)
// use for multiple passes through .sim file in QC metrics
if (filename == "-") {
throw "Cannot reset file position in standard input!";
}
fseek(inFile, HEADER_LENGTH, 0);
nanCount = 0;
infCount = 0;
}
void Sim::writeHeader(uint32_t _numSamples, uint32_t _numProbes,
uint8_t _numChannels, uint8_t _numberFormat)
{
if (filename=="") {
cerr << "Output not defined; need to call Sim::openOutput()" << endl;
exit(1);
}
version = VERSION;
sampleNameSize = SAMPLE_NAME_SIZE;
numSamples = _numSamples;
numProbes = _numProbes;
numChannels = _numChannels;
numberFormat = _numberFormat;
outfile->write("sim", 3);
outfile->write((char*)&version, sizeof(version));
outfile->write((char*)&sampleNameSize, sizeof(sampleNameSize));
outfile->write((char*)&numSamples, sizeof(numSamples));
outfile->write((char*)&numProbes, sizeof(numProbes));
outfile->write((char*)&numChannels, sizeof(numChannels));
outfile->write((char*)&numberFormat, sizeof(numberFormat));
outfile->flush();
}
string Sim::dump(void)
{
ostringstream s;
s << "Number of SNPs: " << numSamples << endl
<< "Magic: " << magic << endl
<< "version: " << version << endl
<< "Sample Name Size: " << sampleNameSize << endl
<< "Num Probes: " << numProbes << endl
<< "Num Channels: " << numChannels << endl
<< "Number Format: " << numberFormat << endl
<< endl;
return s.str();
}
void Sim::__openout(ostream &f)
{
outfile = &f;
}
void Sim::_openOut(string filename)
{
// open filestream for output
if (filename == "-") {
__openout(cout);
} else {
fout.open(filename.c_str(),
ios::binary | ios::trunc | ios::in | ios::out);
__openout(fout);
}
if (!outfile || !fout) {
cerr << "Can't open " << filename << " for writing : "
<< strerror(errno) << endl;
}
}
void Sim::getNextRecord(char *sampleName, uint16_t *intensity) {
// read array of intensity intensities
// no need to check for NaN/inf values, as these can't be integers
char *status = fgets(sampleName, sampleNameSize+1, inFile);
if (status==NULL) throw("Error reading sample name from .sim file!");
int items = fread(intensity, numericBytes, sampleIntensityTotal, inFile);
if (items != sampleIntensityTotal || ferror(inFile)) {
throw("Error reading intensities from .sim file!");
}
}
void Sim::getNextRecord(char *sampleName, float *intensity,
bool cleanup) {
// read array of float intensities & check for NaN/infinite values
// if cleanup=true, reset all NaN/infinite values to zero
char *status = fgets(sampleName, sampleNameSize+1, inFile);
if (status==NULL) throw("Error reading sample name from .sim file!");
int items = fread(intensity, numericBytes, sampleIntensityTotal, inFile);
if (items != sampleIntensityTotal || ferror(inFile)) {
throw("Error reading intensities from .sim file!");
}
// check for nan/inf values in input
// looking at sum is faster than calling isnan/isinf on everything
// nan/inf tend to be concentrated in a few samples
float sum = 0.0;
for (int i=0;i<sampleIntensityTotal;i++) sum += intensity[i];
if (isinf(sum) || isnan(sum)) { // one or more INF/NaN values present
for (int i=0;i<sampleIntensityTotal;i++) {
if (isinf(intensity[i])) {
infCount++;
if (cleanup) intensity[i] = 0;
} else if (isnan(intensity[i])) {
nanCount++;
if (cleanup) intensity[i] = 0;
}
}
}
}
void Sim::reportNonNumeric(void) {
// check infinity/nan counts and report to standard output
cout << "Total NaN values found: " << nanCount << endl;
cout << "Total INF values found: " << infCount << endl;
}
void Sim::write(void *buffer, int length)
{
if (filename=="") {
cerr << "Output not defined; need to call Sim::openOutput()" << endl;
exit(1);
}
outfile->write((const char*)buffer,length);
}