/
hdf5Sequence.cpp
361 lines (317 loc) · 11.6 KB
/
hdf5Sequence.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright (C) 2012 by Glenn Hickey (hickey@soe.ucsc.edu)
*
* Released under the MIT license, see LICENSE.txt
*/
#include <string>
#include <cstring>
#include <sstream>
#include <set>
#include <iostream>
#include "hdf5Sequence.h"
#include "hdf5DNAIterator.h"
#include "defaultTopSegmentIterator.h"
#include "defaultBottomSegmentIterator.h"
#include "defaultColumnIterator.h"
#include "defaultRearrangement.h"
#include "defaultGappedTopSegmentIterator.h"
#include "defaultGappedBottomSegmentIterator.h"
using namespace std;
using namespace H5;
using namespace hal;
const size_t HDF5Sequence::startOffset = 0;
const size_t HDF5Sequence::topSegmentArrayIndexOffset = sizeof(hal_size_t);
const size_t HDF5Sequence::bottomSegmentArrayIndexOffset = topSegmentArrayIndexOffset + sizeof(hal_size_t);
const size_t HDF5Sequence::totalSize = bottomSegmentArrayIndexOffset + sizeof(hal_size_t);
HDF5Sequence::HDF5Sequence(HDF5Genome* genome,
HDF5ExternalArray* idxArray,
HDF5ExternalArray* nameArray,
hal_index_t index) :
_idxArray(idxArray),
_nameArray(nameArray),
_index(index),
_genome(genome)
{
}
HDF5Sequence::~HDF5Sequence()
{
}
H5::CompType HDF5Sequence::idxDataType()
{
// the in-memory representations and hdf5 representations
// don't necessarily have to be the same, but it simplifies
// testing for now.
assert(PredType::NATIVE_HSIZE.getSize() == sizeof(hal_size_t));
CompType dataType(totalSize);
dataType.insertMember("start", startOffset, PredType::NATIVE_HSIZE);
dataType.insertMember("topSegmentArrayIndexOffset",
topSegmentArrayIndexOffset,
PredType::NATIVE_HSIZE);
dataType.insertMember("bottomSegmentArrayIndexOffset",
bottomSegmentArrayIndexOffset,
PredType::NATIVE_HSIZE);
return dataType;
}
H5::StrType HDF5Sequence::nameDataType(hal_size_t maxNameLength)
{
// the in-memory representations and hdf5 representations
// don't necessarily have to be the same, but it simplifies
// testing for now.
assert(PredType::NATIVE_HSIZE.getSize() == sizeof(hal_size_t));
assert(PredType::NATIVE_CHAR.getSize() == sizeof(char));
StrType strType(PredType::NATIVE_CHAR, (maxNameLength + 1) * sizeof(char));
// CompType dataType(nameOffset + strType.getSize());
// dataType.insertMember("name", nameOffset, strType);
return strType;
// return dataType;
}
// SEQUENCE INTERFACE
string HDF5Sequence::getName() const
{
return _nameArray->get(_index);
}
string HDF5Sequence::getFullName() const
{
assert(_genome != NULL);
return _genome->getName() + '.' + getName();
}
const Genome* HDF5Sequence::getGenome() const
{
return _genome;
}
Genome* HDF5Sequence::getGenome()
{
return _genome;
}
hal_index_t HDF5Sequence::getStartPosition() const
{
return _idxArray->getValue<hal_size_t>(_index, startOffset);
}
hal_index_t HDF5Sequence::getEndPosition() const
{
return _idxArray->getValue<hal_size_t>(_index + 1 , startOffset) - 1;
}
hal_index_t HDF5Sequence::getArrayIndex() const
{
return _index;
}
hal_index_t HDF5Sequence::getTopSegmentArrayIndex() const
{
return (hal_index_t)
_idxArray->getValue<hal_size_t>(_index, topSegmentArrayIndexOffset);
}
hal_index_t HDF5Sequence::getBottomSegmentArrayIndex() const
{
return (hal_index_t)
_idxArray->getValue<hal_size_t>(_index, bottomSegmentArrayIndexOffset);
}
// SEGMENTED SEQUENCE INTERFACE
hal_size_t HDF5Sequence::getSequenceLength() const
{
hal_size_t len = _idxArray->getValue<hal_size_t>(_index, startOffset);
hal_size_t nlen = _idxArray->getValue<hal_size_t>(_index + 1, startOffset);
assert(nlen >= len);
return (nlen - len);
}
hal_size_t HDF5Sequence::getNumTopSegments() const
{
hal_index_t idx =
_idxArray->getValue<hal_size_t>(_index, topSegmentArrayIndexOffset);
hal_index_t nextIdx =
_idxArray->getValue<hal_size_t>(_index + 1, topSegmentArrayIndexOffset);
assert(nextIdx >= idx);
return nextIdx - idx;
}
hal_size_t HDF5Sequence::getNumBottomSegments() const
{
hal_index_t idx =
_idxArray->getValue<hal_size_t>(_index, bottomSegmentArrayIndexOffset);
hal_index_t nextIdx =
_idxArray->getValue<hal_size_t>(_index + 1, bottomSegmentArrayIndexOffset);
assert(nextIdx >= idx);
return nextIdx - idx;
}
TopSegmentIteratorPtr HDF5Sequence::getTopSegmentIterator(
hal_index_t position)
{
hal_size_t idx = position + getTopSegmentArrayIndex();
return _genome->getTopSegmentIterator(idx);
}
TopSegmentIteratorConstPtr HDF5Sequence::getTopSegmentIterator(
hal_index_t position) const
{
hal_size_t idx = position + getTopSegmentArrayIndex();
return _genome->getTopSegmentIterator(idx);
}
TopSegmentIteratorConstPtr HDF5Sequence::getTopSegmentEndIterator() const
{
return getTopSegmentIterator(getNumTopSegments());
}
BottomSegmentIteratorPtr HDF5Sequence::getBottomSegmentIterator(
hal_index_t position)
{
hal_size_t idx = position + getBottomSegmentArrayIndex();
return _genome->getBottomSegmentIterator(idx);
}
BottomSegmentIteratorConstPtr HDF5Sequence::getBottomSegmentIterator(
hal_index_t position) const
{
hal_size_t idx = position + getBottomSegmentArrayIndex();
return _genome->getBottomSegmentIterator(idx);
}
BottomSegmentIteratorConstPtr HDF5Sequence::getBottomSegmentEndIterator() const
{
return getBottomSegmentIterator(getNumBottomSegments());
}
DNAIteratorPtr HDF5Sequence::getDNAIterator(hal_index_t position)
{
hal_size_t idx = position + getStartPosition();
HDF5DNAIterator* newIt = new HDF5DNAIterator(_genome, idx);
return DNAIteratorPtr(newIt);
}
DNAIteratorConstPtr HDF5Sequence::getDNAIterator(hal_index_t position) const
{
hal_size_t idx = position + getStartPosition();
HDF5Genome* genome = const_cast<HDF5Genome*>(_genome);
const HDF5DNAIterator* newIt = new HDF5DNAIterator(genome, idx);
return DNAIteratorConstPtr(newIt);
}
DNAIteratorConstPtr HDF5Sequence::getDNAEndIterator() const
{
return getDNAIterator(getSequenceLength());
}
ColumnIteratorConstPtr HDF5Sequence::getColumnIterator(
const std::set<const Genome*>* targets, hal_size_t maxInsertLength,
hal_index_t position, hal_index_t lastPosition, bool noDupes,
bool noAncestors, bool reverseStrand) const
{
hal_index_t idx = (hal_index_t)(position + getStartPosition());
hal_index_t lastIdx;
if (lastPosition == NULL_INDEX)
{
lastIdx = (hal_index_t)(getStartPosition() + getSequenceLength() - 1);
}
else
{
lastIdx = (hal_index_t)(lastPosition + getStartPosition());
}
if (position < 0 ||
lastPosition >= (hal_index_t)(getStartPosition() + getSequenceLength()))
{
stringstream ss;
ss << "HDF5Sequence::getColumnIterators: input indices "
<< "(" << position << ", " << lastPosition << ") out of bounds";
throw hal_exception(ss.str());
}
const DefaultColumnIterator* newIt =
new DefaultColumnIterator(getGenome(), targets, idx, lastIdx,
maxInsertLength, noDupes, noAncestors,
reverseStrand);
return ColumnIteratorConstPtr(newIt);
}
void HDF5Sequence::getString(std::string& outString) const
{
getSubString(outString, 0, getSequenceLength());
}
void HDF5Sequence::setString(const std::string& inString)
{
setSubString(inString, 0, getSequenceLength());
}
void HDF5Sequence::getSubString(std::string& outString, hal_size_t start,
hal_size_t length) const
{
hal_size_t idx = start + getStartPosition();
outString.resize(length);
HDF5DNAIterator dnaIt(const_cast<HDF5Genome*>(_genome), idx);
dnaIt.readString(outString, length);
}
void HDF5Sequence::setSubString(const std::string& inString,
hal_size_t start,
hal_size_t length)
{
if (length != inString.length())
{
stringstream ss;
ss << "setString: input string of length " << inString.length()
<< " has length different from target string in sequence " << getName()
<< " which is of length " << length;
throw hal_exception(ss.str());
}
hal_size_t idx = start + getStartPosition();
HDF5DNAIterator dnaIt(_genome, idx);
dnaIt.writeString(inString, length);
}
RearrangementPtr HDF5Sequence::getRearrangement(hal_index_t position,
hal_size_t gapLengthThreshold,
double nThreshold,
bool atomic) const
{
TopSegmentIteratorConstPtr top = getTopSegmentIterator(position);
DefaultRearrangement* rea = new DefaultRearrangement(getGenome(),
gapLengthThreshold,
nThreshold,
atomic);
rea->identifyFromLeftBreakpoint(top);
return RearrangementPtr(rea);
}
GappedTopSegmentIteratorConstPtr HDF5Sequence::getGappedTopSegmentIterator(
hal_index_t i, hal_size_t gapThreshold, bool atomic) const
{
TopSegmentIteratorConstPtr top = getTopSegmentIterator(i);
DefaultGappedTopSegmentIterator* gt =
new DefaultGappedTopSegmentIterator(top, gapThreshold, atomic);
return GappedTopSegmentIteratorConstPtr(gt);
}
GappedBottomSegmentIteratorConstPtr
HDF5Sequence::getGappedBottomSegmentIterator(
hal_index_t i, hal_size_t childIdx, hal_size_t gapThreshold,
bool atomic) const
{
BottomSegmentIteratorConstPtr bot = getBottomSegmentIterator(i);
DefaultGappedBottomSegmentIterator* gb =
new DefaultGappedBottomSegmentIterator(bot, childIdx, gapThreshold,
atomic);
return GappedBottomSegmentIteratorConstPtr(gb);
}
// LOCAL
void HDF5Sequence::set(hal_size_t startPosition,
const Sequence::Info& sequenceInfo,
hal_size_t topSegmentStartIndex,
hal_size_t bottomSegmentStartIndex)
{
_idxArray->setValue(_index, startOffset, startPosition);
_idxArray->setValue(_index + 1, startOffset,
startPosition + sequenceInfo._length);
_idxArray->setValue(_index, topSegmentArrayIndexOffset, topSegmentStartIndex);
_idxArray->setValue(_index, bottomSegmentArrayIndexOffset,
bottomSegmentStartIndex);
_idxArray->setValue(_index + 1, topSegmentArrayIndexOffset,
topSegmentStartIndex + sequenceInfo._numTopSegments);
_idxArray->setValue(_index + 1, bottomSegmentArrayIndexOffset,
bottomSegmentStartIndex + sequenceInfo._numBottomSegments);
char* arrayBuffer = _nameArray->getUpdate(_index);
strcpy(arrayBuffer, sequenceInfo._name.c_str());
assert(getStartPosition() == (hal_index_t)startPosition);
assert(getNumTopSegments() == sequenceInfo._numTopSegments);
assert(getNumBottomSegments() == sequenceInfo._numBottomSegments);
assert(getSequenceLength() == sequenceInfo._length);
}
// These functions look dangerous. Dont think they're used.
void HDF5Sequence::setNumTopSegments(hal_size_t numTopSegments)
{
_idxArray->setValue(_index + 1, topSegmentArrayIndexOffset,
getTopSegmentArrayIndex() + numTopSegments);
}
void HDF5Sequence::setNumBottomSegments(hal_size_t numBottomSegments)
{
_idxArray->setValue(_index + 1, bottomSegmentArrayIndexOffset,
getBottomSegmentArrayIndex() + numBottomSegments);
}
void HDF5Sequence::setTopSegmentArrayIndex(hal_size_t topIndex)
{
_idxArray->setValue(_index, topSegmentArrayIndexOffset, topIndex);
}
void HDF5Sequence::setBottomSegmentArrayIndex(hal_size_t bottomIndex)
{
_idxArray->setValue(_index, bottomSegmentArrayIndexOffset, bottomIndex);
}