forked from wtsi-npg/simtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sim.cpp
205 lines (181 loc) · 5.64 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
// 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 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 "Sim.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <stdint.h>
#include <errno.h>
using namespace std;
Sim::Sim(void)
{
version=0;
filename = "";
}
void Sim::open(char *f)
{
string fname = f;
open(fname);
}
void Sim::open(string fname)
{
char buff[256];
errorMsg = "";
this->filename = fname;
_openFile(fname);
infile->get(buff,4);
if (strcmp(buff,"sim")) {
throw("File " + filename + " has invalid header [" + buff + "]");
}
magic = buff;
this->filename = fname;
infile->read(buff,1); version = *(uint8_t*)buff;
infile->read(buff,2); sampleNameSize = *(uint16_t*)buff;
infile->read(buff,4); numSamples = *(uint32_t*)buff;
infile->read(buff,4); numProbes = *(uint32_t*)buff;
infile->read(buff,1); numChannels = *(uint8_t*)buff;
infile->read(buff,1); numberFormat = *(uint8_t*)buff;
// calculate and store record length
recordLength = numProbes * numChannels;
switch (numberFormat) {
case FLOAT: recordLength *= 4; break;
case INTEGER: recordLength *= 2; break;
case SCALED_INTEGER: recordLength *= 2; break;
default: cerr << "Invalid number format " << numberFormat;
exit(1);
}
recordLength += sampleNameSize;
_closeFile();
}
void Sim::close(void)
{
if (filename != "-") fout.close(); // don't close stdout!
}
void Sim::createFile(string fname)
{
filename = fname;
}
void Sim::writeHeader(uint32_t _numSamples, uint32_t _numProbes, uint8_t _numChannels, uint8_t _numberFormat)
{
version = VERSION;
sampleNameSize = SAMPLE_NAME_SIZE;
numSamples = _numSamples;
numProbes = _numProbes;
numChannels = _numChannels;
numberFormat = _numberFormat;
_openFile(filename,true);
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::__openin(istream &f)
{
infile = &f;
}
void Sim::__openout(ostream &f)
{
outfile = &f;
}
void Sim::_openFile(string filename, bool writing)
{
if (filename == "-") {
if (writing) { __openout(cout); }
else { __openin(cin); }
} else {
if (writing) { fout.open(filename.c_str(),ios::binary | ios::trunc | ios::in | ios::out); __openout(fout); }
else {
fin.open(filename.c_str(),ios::binary | ios::in);
__openin(fin);
}
}
if (!writing && (!infile || !fin)) {
cerr << "Can't open " << filename << " for reading : " << strerror(errno) << endl;
}
if (writing && (!outfile || !fout)) {
cerr << "Can't open " << filename << " for writing : " << strerror(errno) << endl;
}
}
void Sim::_closeFile(void)
{
// file.close();
// file.clear();
}
void Sim::getNextRecord(char *sampleName, vector<uint16_t> *intensity)
{
unsigned int n;
char *p;
char *buff = new char[recordLength];
infile->read(buff,recordLength);
memcpy(sampleName,buff,sampleNameSize);
for (n=0, p=buff+sampleNameSize; n < numProbes*numChannels; n++, p+=2) {
uint16_t v = *(uint16_t*)p;
intensity->push_back(v);
}
delete buff;
}
void Sim::getNextRecord(char *sampleName, vector<float> *intensity)
{
unsigned int n;
char *p;
char *buff = new char[recordLength];
infile->read(buff,recordLength);
memcpy(sampleName,buff,sampleNameSize);
for (n=0, p=buff+sampleNameSize; n < numProbes*numChannels; n++, p+=4) {
float v = *(float*)p;
intensity->push_back(v);
}
delete buff;
}
void Sim::write(void *buffer, int length)
{
outfile->write((const char*)buffer,length);
}