File: kmc1_db_reader.h

package info (click to toggle)
kmc 2.3%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,412 kB
  • sloc: cpp: 17,316; perl: 178; makefile: 90; sh: 16
file content (379 lines) | stat: -rw-r--r-- 10,572 bytes parent folder | download | duplicates (2)
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
/*
  This file is a part of KMC software distributed under GNU GPL 3 licence.
  The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
  
  Authors: Marek Kokot
  
  Version: 2.3.0
  Date   : 2015-08-21
*/

#ifndef _KMC1_DB_READER_H
#define _KMC1_DB_READER_H
#include "kmer.h"
#include "defs.h"
#include "config.h"
#include "bundle.h"
#include "kmc_header.h"
#include "queues.h"
#include <iostream>
#include <cstring>
#include <thread>

enum class KMCDBOpenMode { sequential, sorted, counters_only };

//************************************************************************************************************
// CKMC1DbReader - reader of KMC1 database
//************************************************************************************************************
template<unsigned SIZE> class CKMC1DbReader : public CInput<SIZE>
{
public:
	CKMC1DbReader(const CKMC_header& header, const CInputDesc& desc, CPercentProgress& percent_progress, KMCDBOpenMode open_mode);

	void NextBundle(CBundle<SIZE>& bundle) override
	{
		bool exists = circular_queue->pop(bundle.Data());

		percent_progress.UpdateItem(progress_id, bundle.Size());
		
		if (exists)
			return;
		
		percent_progress.Complete(progress_id);

		this->finished = true;
		this->sorted_access_thread.join();
		delete this->circular_queue;		
	}

	void IgnoreRest() override
	{
		circular_queue->force_finish();
		this->finished = true;
		this->sorted_access_thread.join();
		delete this->circular_queue;
	}

	~CKMC1DbReader()
	{
		if(prefix_file != nullptr)
			fclose(prefix_file);
		if(sufix_file != nullptr)
			fclose(sufix_file);
		delete[] prefix_buff;
		delete[] sufix_buff;
	}
	
	bool NextKmerSequential(CKmer<SIZE>& kmer, uint32& counter)
	{		
		if (next_kmer_sorted(kmer, counter))
		{
			percent_progress.UpdateItem(progress_id);
			return true;
		}
		percent_progress.Complete(progress_id);
		return false;
	}

	bool NextCounter(uint32& counter);

private:
	static const uint32 PREFIX_BUFF_BYTES = KMC1_DB_READER_PREFIX_BUFF_BYTES; 
	static const uint32 SUFIX_BUFF_BYTES = KMC1_DB_READER_SUFIX_BUFF_BYTES;
	const CKMC_header& header;
	const CInputDesc& desc;

	CPercentProgress& percent_progress;
	KMCDBOpenMode open_mode;

	uint32 progress_id;

	FILE* prefix_file;
	FILE* sufix_file;

	uint32 record_size; //of sufix, in bytes	
	uint32 current_preffix;
	uint32 sufix_bytes;
	uint64* prefix_buff = nullptr;
	uchar* sufix_buff = nullptr;

	uint32 prefix_bytes;
	uint32 kmer_bytes;

	uint64 prefix_buff_size;
	uint64 sufix_buff_size;

	uint64 prefix_buff_pos;
	uint64 sufix_buff_pos;

	uint64 prefix_left_to_read;
	uint64 sufix_left_to_read;

	std::string prefix_file_name;
	std::string sufix_file_name;

	uint64 sufix_number;

	CCircularQueue<SIZE>* circular_queue = nullptr; //for sorted access only
	std::thread sorted_access_thread;

	void reload_pref_buff();

	bool reload_suf_buff();

	bool next_kmer_sorted(CKmer<SIZE>& kmer, uint32& counter);

	void open_files();

	void allocate_buffers()
	{
		sufix_buff = new uchar[sufix_buff_size];
		if (open_mode == KMCDBOpenMode::sequential || open_mode == KMCDBOpenMode::sorted)
			prefix_buff = new uint64[prefix_buff_size];
	}
};

/*****************************************************************************************************************************/
/******************************************************** CONSTRUCTOR ********************************************************/
/*****************************************************************************************************************************/

template<unsigned SIZE> CKMC1DbReader<SIZE>::CKMC1DbReader(const CKMC_header& header, const CInputDesc& desc, CPercentProgress& percent_progress, KMCDBOpenMode open_mode) :
	header(header), desc(desc), percent_progress(percent_progress), open_mode(open_mode)
{
	progress_id = percent_progress.RegisterItem(header.total_kmers);

	prefix_file = sufix_file = nullptr;
	sufix_bytes = (header.kmer_len - header.lut_prefix_len) / 4;
	record_size = sufix_bytes + header.counter_size;
	sufix_buff_size = SUFIX_BUFF_BYTES / record_size * record_size;
	prefix_buff_size = PREFIX_BUFF_BYTES / sizeof(uint64);

	sufix_left_to_read = header.total_kmers * record_size;

	if (sufix_left_to_read < sufix_buff_size)
		sufix_buff_size = sufix_left_to_read;

	prefix_left_to_read = (1 << header.lut_prefix_len * 2) - 1;

	if (prefix_left_to_read < prefix_buff_size)
		prefix_buff_size = prefix_left_to_read;

	prefix_bytes = (header.lut_prefix_len + 3) / 4;

	kmer_bytes = prefix_bytes + sufix_bytes;

	open_files();
	allocate_buffers();
	if(open_mode == KMCDBOpenMode::sequential || open_mode == KMCDBOpenMode::sorted)
		reload_pref_buff();
	reload_suf_buff();

	current_preffix = 0;
	sufix_number = 0;

	if (open_mode == KMCDBOpenMode::sorted)
	{
		circular_queue = new CCircularQueue<SIZE>(DEFAULT_CIRCULAL_QUEUE_CAPACITY);
		sorted_access_thread = std::thread([this]{

			CKmer<SIZE> kmer;
			uint32 counter;
			CBundleData<SIZE> bundle_data;

			while (next_kmer_sorted(kmer, counter))
			{
				bundle_data.Insert(kmer, counter);
				if (bundle_data.Full())
				{
					if (!this->circular_queue->push(bundle_data))
						break;
				}
			}
			if (!bundle_data.Empty())
				this->circular_queue->push(bundle_data);
			this->circular_queue->mark_completed();		
		});
	}
}

/*****************************************************************************************************************************/
/********************************************************** PUBLIC ***********************************************************/
/*****************************************************************************************************************************/

/*****************************************************************************************************************************/
template<unsigned SIZE> bool CKMC1DbReader<SIZE>::NextCounter(uint32& counter)
{
	while (true)
	{
		if (sufix_number >= header.total_kmers)
			return false;

		uchar* record = sufix_buff + sufix_buff_pos + sufix_bytes;

		counter = 0;
		for (int32 i = header.counter_size - 1; i >= 0; --i)
		{
			counter <<= 8;
			counter += record[i];
		}

		++sufix_number;
		sufix_buff_pos += record_size;

		if (sufix_buff_pos >= sufix_buff_size)
			reload_suf_buff();

		if (counter >= desc.cutoff_min && counter <= desc.cutoff_max)
			return true;
	}
}

/*****************************************************************************************************************************/
/********************************************************** PRIVATE **********************************************************/
/*****************************************************************************************************************************/

/*****************************************************************************************************************************/
template<unsigned SIZE> void CKMC1DbReader<SIZE>::reload_pref_buff()
{
	uint64 to_read = MIN(prefix_left_to_read, prefix_buff_size);
	prefix_buff_pos = 0;
	if (to_read == 0)
	{
		prefix_buff[0] = header.total_kmers;//guard		
		return;
	}

	if (fread(prefix_buff, sizeof(uint64), to_read, prefix_file) != to_read)
	{
		std::cout << "Error: some error while reading " << prefix_file_name << "\n";
		exit(1);
	}	
	prefix_left_to_read -= to_read;
	if (to_read < prefix_buff_size)
	{
		prefix_buff[to_read] = header.total_kmers;//guard
	}
}

/*****************************************************************************************************************************/
template<unsigned SIZE> bool CKMC1DbReader<SIZE>::reload_suf_buff()
{
	uint64 to_read = MIN(sufix_left_to_read, sufix_buff_size);
	if (to_read == 0)
		return false;
	uint64 readed = fread(sufix_buff, 1, to_read, sufix_file);
	if (readed != to_read)
	{
		std::cout << "Error: some error while reading " << sufix_file_name << "\n";
		exit(1);
	}
	sufix_buff_pos = 0;
	sufix_left_to_read -= to_read;
	return true;
}

/*****************************************************************************************************************************/
template<unsigned SIZE> void CKMC1DbReader<SIZE>::open_files()
{
	
	sufix_file_name = desc.file_src + ".kmc_suf";
	
	sufix_file = fopen(sufix_file_name.c_str(), "rb");
	setvbuf(sufix_file, NULL, _IONBF, 0);
	
	if (!sufix_file)
	{
		std::cout << "Error: cannot open file: " << sufix_file_name << "\n";
		exit(1);
	}

	char marker[4];
	if (fread(marker, 1, 4, sufix_file) != 4)
	{
		std::cout << "Error: while reading start marker in file: " << sufix_file_name << "\n";
		exit(1);
	}

	if (strncmp(marker, "KMCS", 4) != 0)
	{
		std::cout << "Error: wrong start marker in file: " << sufix_file_name << "\n";
		exit(1);
	}


	my_fseek(sufix_file, -4, SEEK_END);
	if (fread(marker, 1, 4, sufix_file) != 4)
	{
		std::cout << "Error: while reading end marker in file: " << sufix_file_name << "\n";
		exit(1);
	}

	if (strncmp(marker, "KMCS", 4) != 0)
	{
		std::cout << "Error: wrong end marker in file: " << sufix_file_name << "\n";
		exit(1);
	}
	my_fseek(sufix_file, 4, SEEK_SET); //skip KMCS

	if (open_mode == KMCDBOpenMode::sequential || open_mode == KMCDBOpenMode::sorted)
	{
		prefix_file_name = desc.file_src + ".kmc_pre";
		
		prefix_file = fopen(prefix_file_name.c_str(), "rb");
		setvbuf(prefix_file, NULL, _IONBF, 0);
		
		if (!prefix_file)
		{
			std::cout << "Error: cannot open file: " << prefix_file_name << "\n";
			exit(1);
		}
		my_fseek(prefix_file, 4 + sizeof(uint64), SEEK_SET);//skip KMCP and first value as it must be 0
	}
}


/*****************************************************************************************************************************/
template<unsigned SIZE> bool CKMC1DbReader<SIZE>::next_kmer_sorted(CKmer<SIZE>& kmer, uint32& counter)
{
	while (true)
	{
		if (sufix_number >= header.total_kmers)
			return false;

		while (prefix_buff[prefix_buff_pos] <= sufix_number)
		{
			++current_preffix;
			++prefix_buff_pos;
			if (prefix_buff_pos >= prefix_buff_size)
				reload_pref_buff();
		}

		uchar* record = sufix_buff + sufix_buff_pos;
		uint32 pos = kmer_bytes - 1;

		kmer.load(record, sufix_bytes);
		for (int32 i = prefix_bytes - 1; i >= 0; --i)
			kmer.set_byte(pos--, current_preffix >> (i << 3));

		counter = 0;
		for (int32 i = header.counter_size - 1; i >= 0; --i)
		{
			counter <<= 8;
			counter += record[i];
		}

		++sufix_number;
		sufix_buff_pos += record_size;
		
		if (sufix_buff_pos >= sufix_buff_size)
			reload_suf_buff();

		if (counter >= desc.cutoff_min && counter <= desc.cutoff_max)
			return true;
	}
}


#endif

// ***** EOF