forked from marbl/canu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmers-writer.C
338 lines (226 loc) · 9.98 KB
/
kmers-writer.C
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
/******************************************************************************
*
* 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.
*
* Modifications by:
*
* Brian P. Walenz beginning on 2018-JUL-21
* 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 "kmers.H"
#include "bits.H"
#include "files.H"
void
kmerCountFileWriter::initialize(uint32 prefixSize, bool isMultiSet) {
if (_initialized == true) // Nothing to do if we're already done.
return;
// If the global mersize isn't set, we're hosed.
if (kmer::merSize() == 0)
fprintf(stderr, "kmerCountFileWriter::initialize()-- asked to initialize, but kmer::merSize() is zero!\n"), exit(1);
// The count operations call initialize() exactly once, but nextMer() calls
// it once per file and so we need some kind of concurrency control here.
#pragma omp critical (kmerCountFileWriterInit)
if (_initialized == false) {
// If the prefixSize is zero, set it to (arbitrary) 1/4 the kmer size.
// This happens in the streaming writer (which is used when meryl does
// any non-count operation). The prefixSize here just controls how
// often we dump blocks to the file.
if (_prefixSize == 0)
_prefixSize = prefixSize;
#warning how to set prefix size for streaming operations?
if (_prefixSize == 0)
_prefixSize = 12; //max((uint32)8, 2 * kmer::merSize() / 3);
_suffixSize = 2 * kmer::merSize() - _prefixSize;
_suffixMask = uint64MASK(_suffixSize);
// Decide how many files to write. We can make up to 2^32 files, but will
// run out of file handles _well_ before that. For now, limit to 2^6 = 64 files.
_numFilesBits = 6; //(_prefixSize < 7) ? _prefixSize : 6;
_numBlocksBits = _prefixSize - _numFilesBits;
_numFiles = (uint64)1 << _numFilesBits;
_numBlocks = (uint64)1 << _numBlocksBits;
_isMultiSet = isMultiSet;
// Now we're initialized!
fprintf(stderr, "kmerCountFileWriter()-- Creating '%s' for %u-mers, with prefixSize %u suffixSize %u numFiles %lu\n",
_outName, (_prefixSize + _suffixSize) / 2, _prefixSize, _suffixSize, _numFiles);
_initialized = true;
}
}
kmerCountFileWriter::kmerCountFileWriter(const char *outputName,
uint32 prefixSize) {
// Note that we're not really initialized yet. We could call initialize() in some cases,
// but the interesting one can't initialized() until the first meryl input file is opened,
// so we don't initialize any of them.
_initialized = false;
// Save the output directory name, and try to make it. If we can't we'll fail quickly.
strncpy(_outName, outputName, FILENAME_MAX);
AS_UTL_mkdir(_outName);
// Parameters on how the suffixes/values are encoded are set once we know
// the kmer size. See initialize().
_prefixSize = prefixSize;
_suffixSize = 0;
_suffixMask = 0;
_numFilesBits = 0;
_numBlocksBits = 0;
_numFiles = 0;
_numBlocks = 0;
_isMultiSet = false;
}
kmerCountFileWriter::~kmerCountFileWriter() {
uint32 flags = (uint32)0x0000;
// Set flags.
if (_isMultiSet)
flags |= (uint32)0x0001;
// Create a master index with the parameters.
stuffedBits *masterIndex = new stuffedBits;
masterIndex->setBinary(64, 0x646e496c7972656dllu); // HEX: ........ ONDISK: merylInd
masterIndex->setBinary(64, 0x33302e765f5f7865llu); // 30.v__xe ex__v.03
masterIndex->setBinary(32, _prefixSize);
masterIndex->setBinary(32, _suffixSize);
masterIndex->setBinary(32, _numFilesBits);
masterIndex->setBinary(32, _numBlocksBits);
masterIndex->setBinary(32, flags);
_stats.dump(masterIndex);
// Store the master index (and stats) to disk.
char N[FILENAME_MAX+1];
FILE *F;
snprintf(N, FILENAME_MAX, "%s/merylIndex", _outName);
F = AS_UTL_openOutputFile(N);
masterIndex->dumpToFile(F);
AS_UTL_closeFile(F);
delete masterIndex;
}
uint32
kmerCountFileWriter::fileNumber(uint64 prefix) {
assert(_initialized);
// Based on the prefix, decide what output file to write to.
// The prefix has _prefixSize bits. We want to save the highest _numFiles bits.
uint64 oi = prefix >> _numBlocksBits;
if (oi >= _numFiles) {
fprintf(stderr, "kmerCountFileWriter()-- Formed invalid file number %lu >= number of files %lu:\n", oi, _numFiles);
fprintf(stderr, "kmerCountFileWriter()-- prefix 0x%016lx\n", prefix);
fprintf(stderr, "kmerCountFileWriter()-- prefixSize %u\n", _prefixSize);
fprintf(stderr, "kmerCountFileWriter()-- suffixSize %u\n", _suffixSize);
fprintf(stderr, "kmerCountFileWriter()-- numFilesBits %u\n", _numFilesBits);
fprintf(stderr, "kmerCountFileWriter()-- numBlocksBits %u\n", _numBlocksBits);
}
assert(oi < _numFiles);
return((uint32)oi);
}
//void
//kmerCountFileWriter::importStatistics(kmerCountStatistics &import) {
//#warning NOT IMPORTING STATISTICS
//}
void
kmerCountFileWriter::writeBlockToFile(FILE *datFile,
kmerCountFileIndex *datFileIndex,
uint64 prefix,
uint64 nKmers,
uint64 *suffixes,
uint32 *values) {
// Figure out the optimal size of the Elias-Fano prefix. It's just log2(N)-1.
uint32 unaryBits = 0;
uint64 unarySum = 1;
while (unarySum < nKmers) {
unaryBits += 1;
unarySum <<= 1;
}
uint32 binaryBits = _suffixSize - unaryBits; // Only _suffixSize is used from the class.
// Dump data.
stuffedBits *dumpData = new stuffedBits;
dumpData->setBinary(64, 0x7461446c7972656dllu); // Magic number, part 1.
dumpData->setBinary(64, 0x0a3030656c694661llu); // Magic number, part 2.
dumpData->setBinary(64, prefix);
dumpData->setBinary(64, nKmers);
dumpData->setBinary(8, 1); // Kmer coding type
dumpData->setBinary(32, unaryBits); // Kmer coding parameters
dumpData->setBinary(32, binaryBits);
dumpData->setBinary(64, 0);
dumpData->setBinary(8, 1); // Value coding type
dumpData->setBinary(64, 0); // Value coding parameters
dumpData->setBinary(64, 0);
// Split the kmer suffix into two pieces, one unary encoded offsets and one binary encoded.
uint64 lastPrefix = 0;
uint64 thisPrefix = 0;
for (uint32 kk=0; kk<nKmers; kk++) {
thisPrefix = suffixes[kk] >> binaryBits;
dumpData->setUnary(thisPrefix - lastPrefix);
dumpData->setBinary(binaryBits, suffixes[kk]);
lastPrefix = thisPrefix;
}
// Save the values, too. Eventually these will be cleverly encoded. Really.
uint64 lastValue = 0;
uint64 thisValue = 0;
for (uint32 kk=0; kk<nKmers; kk++) {
dumpData->setBinary(32, values[kk]);
}
// Save the index entry.
uint64 block = prefix & uint64MASK(_numBlocksBits);
datFileIndex[block].set(prefix, datFile, nKmers);
// Dump data to disk, cleanup, and done!
dumpData->dumpToFile(datFile);
delete dumpData;
}
void
kmerCountFileWriter::writeBlockToFile(FILE *datFile,
kmerCountFileIndex *datFileIndex,
uint64 prefix,
uint64 nKmers,
uint64 *suffixes,
uint64 *values) {
// Figure out the optimal size of the Elias-Fano prefix. It's just log2(N)-1.
uint32 unaryBits = 0;
uint64 unarySum = 1;
while (unarySum < nKmers) {
unaryBits += 1;
unarySum <<= 1;
}
uint32 binaryBits = _suffixSize - unaryBits; // Only _suffixSize is used from the class.
// Dump data.
stuffedBits *dumpData = new stuffedBits;
dumpData->setBinary(64, 0x7461446c7972656dllu); // Magic number, part 1.
dumpData->setBinary(64, 0x0a3030656c694661llu); // Magic number, part 2.
dumpData->setBinary(64, prefix);
dumpData->setBinary(64, nKmers);
dumpData->setBinary(8, 1); // Kmer coding type
dumpData->setBinary(32, unaryBits); // Kmer coding parameters
dumpData->setBinary(32, binaryBits);
dumpData->setBinary(64, 0);
dumpData->setBinary(8, 2); // Value coding type
dumpData->setBinary(64, 0); // Value coding parameters
dumpData->setBinary(64, 0);
// Split the kmer suffix into two pieces, one unary encoded offsets and one binary encoded.
uint64 lastPrefix = 0;
uint64 thisPrefix = 0;
for (uint32 kk=0; kk<nKmers; kk++) {
thisPrefix = suffixes[kk] >> binaryBits;
dumpData->setUnary(thisPrefix - lastPrefix);
dumpData->setBinary(binaryBits, suffixes[kk]);
lastPrefix = thisPrefix;
}
// Save the values, too. Eventually these will be cleverly encoded. Really.
uint64 lastValue = 0;
uint64 thisValue = 0;
for (uint32 kk=0; kk<nKmers; kk++) {
dumpData->setBinary(64, values[kk]);
}
// Save the index entry.
uint64 block = prefix & uint64MASK(_numBlocksBits);
datFileIndex[block].set(prefix, datFile, nKmers);
// Dump data to disk, cleanup, and done!
dumpData->dumpToFile(datFile);
delete dumpData;
}