File: ovStoreFile.C

package info (click to toggle)
canu 1.7.1+dfsg-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 7,680 kB
  • sloc: cpp: 66,708; perl: 13,682; ansic: 4,020; makefile: 627; sh: 472; python: 39
file content (419 lines) | stat: -rw-r--r-- 10,464 bytes parent folder | download
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419

/******************************************************************************
 *
 *  This file is part of canu, a software program that assembles whole-genome
 *  sequencing reads into contigs.
 *
 *  This software is based on:
 *    'Celera Assembler' (http://wgs-assembler.sourceforge.net)
 *    the 'kmer package' (http://kmer.sourceforge.net)
 *  both originally distributed by Applera Corporation under the GNU General
 *  Public License, version 2.
 *
 *  Canu branched from Celera Assembler at its revision 4587.
 *  Canu branched from the kmer project at its revision 1994.
 *
 *  This file is derived from:
 *
 *    src/AS_OVS/AS_OVS_overlapFile.C
 *    src/AS_OVS/AS_OVS_overlapFile.c
 *
 *  Modifications by:
 *
 *    Brian P. Walenz from 2007-FEB-28 to 2013-SEP-22
 *      are Copyright 2007-2009,2011-2013 J. Craig Venter Institute, and
 *      are subject to the GNU General Public License version 2
 *
 *    Sergey Koren on 2011-MAR-31
 *      are Copyright 2011 Battelle National Biodefense Institute, and
 *      are subject to the BSD 3-Clause License
 *
 *    Gregory Sims on 2012-FEB-01
 *      are Copyright 2012 J. Craig Venter Institute, and
 *      are subject to the GNU General Public License version 2
 *
 *    Brian P. Walenz from 2014-DEC-09 to 2015-JUN-16
 *      are Copyright 2014-2015 Battelle National Biodefense Institute, and
 *      are subject to the BSD 3-Clause License
 *
 *    Brian P. Walenz beginning on 2016-JAN-11
 *      are a 'United States Government Work', and
 *      are released in the public domain
 *
 *  File 'README.licenses' in the root directory of this distribution contains
 *  full conditions and disclaimers for each license.
 */

#include "ovStore.H"

#ifdef SNAPPY
#include "snappy.h"
#endif

//  The histogram associated with this is written to files with any suffices stripped off.

ovFile::ovFile(gkStore     *gkp,
               const char  *name,
               ovFileType   type,
               uint32       bufferSize) {

  _gkp       = gkp;
  _histogram = new ovStoreHistogram(_gkp, type);

  //  We write two sizes of overlaps.  The 'normal' format doesn't contain the a_iid, while the
  //  'full' format does.  The buffer size must hold an integer number of overlaps, otherwise the
  //  reader will read partial overlaps and fail.  Choose a buffer size that can handle both.

  uint32  lcm = ((sizeof(uint32) * 1 + sizeof(ovOverlapDAT)) *
                 (sizeof(uint32) * 2 + sizeof(ovOverlapDAT)));

  if (bufferSize < 16 * 1024)
    bufferSize = 16 * 1024;

  _bufferLen  = 0;
  _bufferPos  = (bufferSize / (lcm * sizeof(uint32))) * lcm;  //  Forces reload on next read
  _bufferMax  = (bufferSize / (lcm * sizeof(uint32))) * lcm;
  _buffer     = new uint32 [_bufferMax];

#ifdef SNAPPY
  _snappyLen    = 0;
  _snappyBuffer = NULL;
#endif

  assert(_bufferMax % ((sizeof(uint32) * 1) + (sizeof(ovOverlapDAT))) == 0);
  assert(_bufferMax % ((sizeof(uint32) * 2) + (sizeof(ovOverlapDAT))) == 0);

  //  Create the input/output buffers and files.

  _isOutput   = false;
  _isSeekable = false;
  _isNormal   = (type == ovFileNormal) || (type == ovFileNormalWrite);
#ifdef SNAPPY
  _useSnappy  = false;
#endif

  _reader     = NULL;
  _writer     = NULL;

  //  Open store files for reading.  These generally cannot be compressed, but we pretend they can be.
  if (type == ovFileNormal) {
    _reader      = new compressedFileReader(name);
    _file        = _reader->file();
    _isSeekable  = (_reader->isCompressed() == false);
  }

  //  Open dump files for reading.  These certainly can be compressed.
  else if (type == ovFileFull) {
    _reader      = new compressedFileReader(name);
    _file        = _reader->file();
    _isSeekable  = (_reader->isCompressed() == false);
#ifdef SNAPPY
    _useSnappy   = true;
#endif
  }

  //  Open a store file for writing?
  else if (type == ovFileNormalWrite) {
    _writer      = new compressedFileWriter(name);
    _file        = _writer->file();
    _isOutput    = true;
  }

  //  Else, open a dump file for writing.  This catches two cases, one with counts and one without counts.
  else {
    _writer      = new compressedFileWriter(name);
    _file        = _writer->file();
    _isOutput    = true;
#ifdef SNAPPY
    _useSnappy   = true;
#endif
  }

  AS_UTL_findBaseFileName(_prefix, name);
}



ovFile::~ovFile() {

  writeBuffer(true);

  delete    _reader;
  delete    _writer;
  delete [] _buffer;

#ifdef SNAPPY
  delete [] _snappyBuffer;
#endif

  _histogram->saveData(_prefix);

  delete _histogram;
}



void
ovFile::writeBuffer(bool force) {

  if (_isOutput == false)  //  Needed because it's called in the destructor.
    return;

  if ((force == false) && (_bufferLen < _bufferMax))
    return;
  if (_bufferLen == 0)
    return;

  //  If compressing, compress the block then write compressed length and the block.

#ifdef SNAPPY
  if (_useSnappy == true) {
    size_t   bl = snappy::MaxCompressedLength(_bufferLen * sizeof(uint32));

    if (_snappyLen < bl) {
      delete [] _snappyBuffer;
      _snappyLen    = bl;
      _snappyBuffer = new char [_snappyLen];
    }

    snappy::RawCompress((const char *)_buffer, _bufferLen * sizeof(uint32), _snappyBuffer, &bl);

    AS_UTL_safeWrite(_file, &bl,           "ovFile::writeBuffer::bl", sizeof(size_t), 1);
    AS_UTL_safeWrite(_file, _snappyBuffer, "ovFile::writeBuffer::sb", sizeof(char),   bl);
  }

  //  Otherwise, just dump the block

  else
#endif
    AS_UTL_safeWrite(_file, _buffer, "ovFile::writeBuffer", sizeof(uint32), _bufferLen);

  //  Buffer written.  Clear it.
  _bufferLen = 0;
}



void
ovFile::writeOverlap(ovOverlap *overlap) {

  assert(_isOutput == true);

  writeBuffer();

  _histogram->addOverlap(overlap);

  if (_isNormal == false)
    _buffer[_bufferLen++] = overlap->a_iid;

  _buffer[_bufferLen++] = overlap->b_iid;

#if (ovOverlapWORDSZ == 32)
  for (uint32 ii=0; ii<ovOverlapNWORDS; ii++)
    _buffer[_bufferLen++] = overlap->dat.dat[ii];
#endif

#if (ovOverlapWORDSZ == 64)
  for (uint32 ii=0; ii<ovOverlapNWORDS; ii++) {
    _buffer[_bufferLen++] = (overlap->dat.dat[ii] >> 32) & 0xffffffff;
    _buffer[_bufferLen++] = (overlap->dat.dat[ii])       & 0xffffffff;
  }
#endif

  assert(_bufferLen <= _bufferMax);
}



void
ovFile::writeOverlaps(ovOverlap *overlaps, uint64 overlapsLen) {
  uint64  nWritten = 0;

  assert(_isOutput == true);

  //  Add all overlaps to the buffer.

  while (nWritten < overlapsLen) {
    writeBuffer();

    _histogram->addOverlap(overlaps + nWritten);

    if (_isNormal == false)
      _buffer[_bufferLen++] = overlaps[nWritten].a_iid;

    _buffer[_bufferLen++] = overlaps[nWritten].b_iid;

#if (ovOverlapWORDSZ == 32)
    for (uint32 ii=0; ii<ovOverlapNWORDS; ii++)
      _buffer[_bufferLen++] = overlaps[nWritten].dat.dat[ii];
#endif

#if (ovOverlapWORDSZ == 64)
    for (uint32 ii=0; ii<ovOverlapNWORDS; ii++) {
      _buffer[_bufferLen++] = (overlaps[nWritten].dat.dat[ii] >> 32) & 0xffffffff;
      _buffer[_bufferLen++] = (overlaps[nWritten].dat.dat[ii])       & 0xffffffff;
    }
#endif

    nWritten++;
  }

  assert(_bufferLen <= _bufferMax);
}



void
ovFile::readBuffer(void) {

  if (_bufferPos < _bufferLen)
    return;

  //  Need to load a new buffer.  Everyone resets bufferPos to the start.

  _bufferPos = 0;

  //  If compressed, we need to decode the block.

#ifdef SNAPPY
  if (_useSnappy == true) {
    size_t  cl  = 0;
    size_t  clc = AS_UTL_safeRead(_file, &cl, "ovFile::readBuffer::cl", sizeof(size_t), 1);

    if (_snappyLen < cl) {
      delete [] _snappyBuffer;
      _snappyLen    = cl;
      _snappyBuffer = new char [cl];
    }

    size_t  sbc = AS_UTL_safeRead(_file, _snappyBuffer, "ovFile::readBuffer::sb", sizeof(char), cl);

    if (sbc != cl)
      fprintf(stderr, "ERROR: short read on file '%s': read " F_SIZE_T " bytes, expected " F_SIZE_T ".\n",
              _prefix, sbc, cl), exit(1);

    size_t  ol = 0;

    snappy::GetUncompressedLength(_snappyBuffer, cl, &ol);
    snappy::RawUncompress(_snappyBuffer, cl, (char *)_buffer);

    _bufferLen = ol / sizeof(uint32);
  }

  //  But if loading from 'normal' files, just load.  Easy peasy.

  else
#endif
    _bufferLen = AS_UTL_safeRead(_file, _buffer, "ovFile::readBuffer", sizeof(uint32), _bufferMax);
}



bool
ovFile::readOverlap(ovOverlap *overlap) {

  assert(_isOutput == false);

  readBuffer();

  if (_bufferLen == 0)
    return(false);

  assert(_bufferPos < _bufferLen);

  if (_isNormal == FALSE)
    overlap->a_iid      = _buffer[_bufferPos++];

  overlap->b_iid      = _buffer[_bufferPos++];

#if (ovOverlapWORDSZ == 32)
  for (uint32 ii=0; ii<ovOverlapNWORDS; ii++)
    overlap->dat.dat[ii] = _buffer[_bufferPos++];
#endif

#if (ovOverlapWORDSZ == 64)
  for (uint32 ii=0; ii<ovOverlapNWORDS; ii++) {
    overlap->dat.dat[ii]   = _buffer[_bufferPos++];
    overlap->dat.dat[ii] <<= 32;
    overlap->dat.dat[ii]  |= _buffer[_bufferPos++];
  }
#endif

  assert(_bufferPos <= _bufferLen);

  return(true);
}



uint64
ovFile::readOverlaps(ovOverlap *overlaps, uint64 overlapsLen) {
  uint64  nLoaded = 0;

  assert(_isOutput == false);

  while (nLoaded < overlapsLen) {
    readBuffer();

    if (_bufferLen == 0)
      return(nLoaded);

    assert(_bufferPos < _bufferLen);

    if (_isNormal == FALSE)
      overlaps[nLoaded].a_iid      = _buffer[_bufferPos++];

    overlaps[nLoaded].b_iid      = _buffer[_bufferPos++];

#if (ovOverlapWORDSZ == 32)
  for (uint32 ii=0; ii<ovOverlapNWORDS; ii++)
    overlaps[nLoaded].dat.dat[ii] = _buffer[_bufferPos++];
#endif

#if (ovOverlapWORDSZ == 64)
  for (uint32 ii=0; ii<ovOverlapNWORDS; ii++) {
    overlaps[nLoaded].dat.dat[ii]   = _buffer[_bufferPos++];
    overlaps[nLoaded].dat.dat[ii] <<= 32;
    overlaps[nLoaded].dat.dat[ii]  |= _buffer[_bufferPos++];
  }
#endif

    nLoaded++;

    assert(_bufferPos <= _bufferLen);
  }

  return(nLoaded);
}



//  Move to the correct spot, and force a load on the next readOverlap by setting the position to
//  the end of the buffer.
void
ovFile::seekOverlap(off_t overlap) {

  if (_isSeekable == false)
    fprintf(stderr, "ovFile::seekOverlap()-- can't seek.\n"), exit(1);

  AS_UTL_fseek(_file, overlap * recordSize(), SEEK_SET);

  _bufferPos = _bufferLen;  //  We probably need to reload the buffer.
}




void
ovFile::transferHistogram(ovStoreHistogram *copy) {

  if (copy == NULL)
    return;

  copy->add(_histogram);

  delete _histogram;

  _histogram = new ovStoreHistogram;
}